功能测试正常………可以上硬件………
1/gpioa pin2
2/晶振 8mhz
can ———usart1----rs485----modbus
can———uart4------putchar----printf
250k j1939
/arm uart4 初始化 转
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE); //uart4是挂载在APB1总线上的,用RCC_APB1PeriphClockCmd()函数初始化!
//³õʼ»¯UART4-TX-PC10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// //UART-RX-PC11
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(UART4, &USART_InitStructure);
USART_Cmd(UART4, ENABLE);
USART_ClearFlag(UART4,USART_FLAG_TC);
/can 初始化 转
/*CAN RX0 中断优先级配置 */
void CAN_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* enabling interrupt */
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 GPIO 和时钟配置 */
void CAN_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能外设时钟
/* 复用功能和GPIOB端口时钟使能*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE);
GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
/* Configure CAN pin: RX */ // PB8
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN pin: TX */ // PB9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* CAN1 模块时钟使能 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
}
/* CAN初始化 */
void CAN_INIT(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CanTxMsg TxMessage;
CAN_GPIO_Config();//CAN管脚初始化
CAN_NVIC_Configuration(); //CAN中断初始化
/* CAN register init */
CAN_DeInit(CAN1); //将外设CAN的全部寄存器重设为缺省值
CAN_StructInit(&CAN_InitStructure);//把CAN_InitStruct中的每一个参数按缺省值填入
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE;//没有使能时间触发模式
CAN_InitStructure.CAN_ABOM=ENABLE;//使能自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE;//没有使能自动唤醒模式
CAN_InitStructure.CAN_NART=DISABLE;//没有使能非自动重传模式
CAN_InitStructure.CAN_RFLM=DISABLE;//没有使能接收FIFO锁定模式
CAN_InitStructure.CAN_TXFP=DISABLE;//没有使能发送FIFO优先级
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;//CAN设置为正常模式
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //重新同步跳跃宽度1个时间单位
CAN_InitStructure.CAN_BS1=CAN_BS1_2tq; //时间段1为3个时间单位
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq; //时间段2为2个时间单位
CAN_InitStructure.CAN_Prescaler=12; //时间单位长度为60
CAN_Init(CAN1,&CAN_InitStructure);
//波特率为:72M/2/12(1+3+2)=0.5 即500K
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=1;//指定过滤器为1
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//指定过滤器为标识符屏蔽位模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//过滤器位宽为32位
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;// 过滤器标识符的高16位值
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;// 过滤器标识符的低16位值
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//过滤器屏蔽标识符的高16位值
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;// 过滤器屏蔽标识符的低16位值
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;// 设定了指向过滤器的FIFO为0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;// 使能过滤器
CAN_FilterInit(&CAN_FilterInitStructure);// 按上面的参数初始化过滤器
/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); //使能FIFO0消息挂号中断
}
/can接收中断处理
#if CAN1_RX0_INT_ENABLE
void USB_LP_CAN1_RX0_IRQHandler(const char *id)
{
int i=0;
CanRxMsg RxMessage;
RxMessage.StdId=0x00; //new id
RxMessage.ExtId=0x00;
RxMessage.IDE=0;
RxMessage.RTR=0;
RxMessage.FMI=0;
for(i=0;i<8;i++)
RxMessage.Data[i]=0;
CAN_Receive(CAN1, 0, &RxMessage);
//USART1_DATA(RxMessage.Data,RxMessage.DLC);
printf("IRQ RxMessage.Data = %s\r\n", RxMessage.Data);
}
#endif
F4 先系问pd1 0