123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #include "include.h"
- u8 CAN_DATA6[8];
- u8 CAN_DATA_CNT;
- u32 Init_Flash_Data = 0x12345678;
- void CAN_GPIO_Config(u32 RCC_APBnPeriphn, GPIO_TypeDef* GPIOx, u16 GPIO_Pin, u16 GPIO_PinSource, u8 GPIO_AF)
- {
- RCC_AHB1PeriphClockCmd(RCC_APBnPeriphn, ENABLE);
- //初始化GPIO
- //引脚复用映射配置
- GPIO_PinAFConfig(GPIOx,GPIO_PinSource,GPIO_AF);//PA11复用为CAN1
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- GPIO_Init(GPIOx,&GPIO_InitStructure); //初始化PD0,PD1
- }
- void CAN_NVIC_Config(u8 NVIC_IRQChannel, u8 pre, u8 sub)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre; // 主优先级为1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub; // 次优先级为0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void CAN_Mod_Config(u32 RCC_APB1Periph, CAN_TypeDef* CANx, u32 EXTID, u32 MaskID)
- {
- //u32 EXTID1 = 0X014E550A;
- //u32 MaskId_ID = 0XFFFF0000;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph, ENABLE);
-
- CAN_InitTypeDef CAN_InitStructure;
- CAN_InitStructure.CAN_TTCM = DISABLE; //非时间触发通信模式
- CAN_InitStructure.CAN_ABOM = DISABLE; //软件自动离线管理
- CAN_InitStructure.CAN_AWUM = DISABLE; //睡眠模式通过软件唤醒
- CAN_InitStructure.CAN_NART = ENABLE; //禁止报文自动传送
- CAN_InitStructure.CAN_RFLM = DISABLE; //报文不锁定,新的覆盖旧的
- CAN_InitStructure.CAN_TXFP = DISABLE; //优先级由报文标识符决定
- CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; //模式设置 CAN_Mode_Normal
- CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; //重新同步跳跃宽度为个时间单位
- CAN_InitStructure.CAN_BS1 = CAN_BS1_8tq; //时间段1占用8个时间单位
- CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq; //时间段2占用5个时间单位
- CAN_InitStructure.CAN_Prescaler = 6; //分频系数(Fdiv)6 500K
- CAN_Init(CANx, &CAN_InitStructure); //初始化CAN1
- /*
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
- CAN_FilterInitStructure.CAN_FilterNumber = 0;//过滤器0
- CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;// CAN_FilterMode_IdMask
- CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;//32位
- CAN_FilterInitStructure.CAN_FilterIdHigh = ((EXTID<<3)>>16) & 0xffff;//32位ID
- CAN_FilterInitStructure.CAN_FilterIdLow = ((EXTID<<3)& 0xffff) | CAN_ID_EXT;
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh = ((MaskID<<3)>>16) & 0xffff;//32位ID
- CAN_FilterInitStructure.CAN_FilterMaskIdLow = ((MaskID<<3)& 0xffff) | CAN_ID_EXT;
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;//FIFO0
- CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;//激活过滤器0
- CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
- */
- CAN_FilterInitTypeDef CAN_FilterInitStructure;
- CAN_FilterInitStructure.CAN_FilterNumber = 0;//过滤器0
- CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;// CAN_FilterMode_IdMask
- CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;//32位
- CAN_FilterInitStructure.CAN_FilterIdHigh = ((EXTID<<3)>>16) & 0xffff;//32位ID
- CAN_FilterInitStructure.CAN_FilterIdLow = ((EXTID<<3)& 0xffff) | CAN_ID_EXT;
- CAN_FilterInitStructure.CAN_FilterMaskIdHigh = ((MaskID<<3)>>16) & 0xffff;//32位ID
- CAN_FilterInitStructure.CAN_FilterMaskIdLow = ((MaskID<<3)& 0xffff) | CAN_ID_EXT;
- CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;//FIFO0
- CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;//激活过滤器0
- CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
-
- CAN_ITConfig(CANx,CAN_IT_FMP0,ENABLE);//FIFO0 允许 消息挂号中断.
- }
- u8 CAN1_Send_Msg(u8* msg,u32 Can_ID,u8 len)
- {
- u8 mbox;
- u32 i = 0;
- CanTxMsg TxMessage;
-
- TxMessage.StdId = 0; // 标准标识符为0
- TxMessage.ExtId = Can_ID;//0x1f4e5301 0x1f4e5701 // 设置扩展标示符(29位)
-
- TxMessage.IDE = CAN_ID_EXT; // 使用扩展标识符
- TxMessage.RTR = CAN_RTR_DATA; // 消息类型为数据帧,一帧8位
- TxMessage.DLC = len; // 发送两帧信息
-
- for(i=0;i<len;i++)
- {
- TxMessage.Data[i] = msg[i]; // 第一帧信息
- }
- mbox = CAN_Transmit(CAN1,&TxMessage);
-
- i=0;
- while((CAN_TransmitStatus(CAN1,mbox)==CAN_TxStatus_Failed)&&(i<0XFFFF))
- {
- i++;//等待发送结束
- }
-
- if(i>=0XFFFF)return 1;
- return 0;
- }
- u8 CAN1_Receive_Msg(u8 *buf, u32 *id)
- {
- u32 i;
- CanRxMsg RxMessage;
-
- if(CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0; //没有接收到数据,直接退出
-
- CAN_Receive(CAN1, CAN_FIFO0,&RxMessage); //读取数据
-
- *id = RxMessage.ExtId;
-
- for(i=0;i<RxMessage.DLC;i++)
- {
- buf[i]=RxMessage.Data[i];
- }
- return RxMessage.DLC;
- }
- void Send_CAN_DATA6(void)
- {
- CAN_DATA6[0] = (u8)light_pwm;
- CAN_DATA6[1] = (u8)(light_pwm>>8);
- CAN_DATA6[2] = (u8)usbl_opendown;
- CAN_DATA6[3] = (u8)0;
- CAN_DATA6[4] = (u8)(temperature_board*10);
- CAN_DATA6[5] = (u8)((u16)(temperature_board*10)>>8);
- CAN_DATA6[6] = 15;
- CAN_DATA6[7] = 0xC0 + CAN_DATA_CNT++;
- if(CAN_DATA_CNT>=31) CAN_DATA_CNT = 0;
-
- CAN1_Send_Msg(CAN_DATA6, CAN_Send_Speed_ID, 8);
- }
|