#include "stm32f10x.h"
#include "stdio.h"
void USART_Configuration(void);
void CAN_Configuration(void);
void CAN_SetMsg(void);
u8 flag=1;
int fputc(int ch,FILE *fp)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
return(ch);
}
int main(void)
{
USART_Configuration();
CAN_Configuration();
CAN_SetMsg();
while(flag==1)
{
}
}
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void CAN_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
// CanTxMsg TxMessage;
// uint8_t TransmitMailbox=0;
// u32 i;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=5;
CAN_Init(CAN1,&CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=((0x1314<<3)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow=((0x1314<<3)|0x000000004|0x00000000)&0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xffff;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0xffff;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FilterFIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
// TxMessage.ExtId=0x1314;
// TxMessage.IDE=CAN_Id_Extended;
// TxMessage.RTR=CAN_RTR_Data;
// TxMessage.DLC=2;
// TxMessage.Data[0]=0xAB;
// TxMessage.Data[1]=0xCD;
// CAN_Transmit(CAN1,&TxMessage);
// TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
//
// i=0;
// while((CAN_TransmitStatus(CAN1,TransmitMailbox)!=CANTXOK)&&(i!=0xff))
// {
// i++;
// }
// i=0;
// while((CAN_MessagePending(CAN1,CAN_FIFO0)<1)&&(i!=0xff))
// {
// i++;
// }
}
void CAN_SetMsg(void)
{
uint8_t TransmitMailbox=0;
u32 i;
CanTxMsg TxMessage;
TxMessage.ExtId=0x1314;
TxMessage.IDE=CAN_Id_Extended;
TxMessage.RTR=CAN_RTR_Data;
TxMessage.DLC=2;
TxMessage.Data[0]=0xAB;
TxMessage.Data[1]=0xCD;
CAN_Transmit(CAN1,&TxMessage);
TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
i=0;
while((CAN_TransmitStatus(CAN1,TransmitMailbox)!=CANTXOK)&&(i!=0xff))
{
i++;
}
i=0;
while((CAN_MessagePending(CAN1,CAN_FIFO0)<1)&&(i!=0xff))
{
i++;
}
}
中断程序代码
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
RxMessage.StdId=0;
RxMessage.IDE=CAN_Id_Extended;
RxMessage.DLC=0;
RxMessage.Data[0]=0x00;
RxMessage.Data[1]=0x00;
CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);
if((CAN_Id_Extended==0x1314)&&(RxMessage.IDE!=CAN_Id_Extended)&&(RxMessage.DLC!=2)&&((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xDCBA))
{
flag=0;
}
else
{
flag=1;
}
}
|