can.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "include.h"
  2. u8 CAN_DATA6[8];
  3. u8 CAN_DATA_CNT;
  4. u32 Init_Flash_Data = 0x12345678;
  5. void CAN_GPIO_Config(u32 RCC_APBnPeriphn, GPIO_TypeDef* GPIOx, u16 GPIO_Pin, u16 GPIO_PinSource, u8 GPIO_AF)
  6. {
  7. RCC_AHB1PeriphClockCmd(RCC_APBnPeriphn, ENABLE);
  8. //初始化GPIO
  9. //引脚复用映射配置
  10. GPIO_PinAFConfig(GPIOx,GPIO_PinSource,GPIO_AF);//PA11复用为CAN1
  11. GPIO_InitTypeDef GPIO_InitStructure;
  12. GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
  13. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
  14. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
  15. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  16. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
  17. GPIO_Init(GPIOx,&GPIO_InitStructure); //初始化PD0,PD1
  18. }
  19. void CAN_NVIC_Config(u8 NVIC_IRQChannel, u8 pre, u8 sub)
  20. {
  21. NVIC_InitTypeDef NVIC_InitStructure;
  22. NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel;
  23. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre; // 主优先级为1
  24. NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub; // 次优先级为0
  25. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  26. NVIC_Init(&NVIC_InitStructure);
  27. }
  28. void CAN_Mod_Config(u32 RCC_APB1Periph, CAN_TypeDef* CANx, u32 EXTID, u32 MaskID)
  29. {
  30. //u32 EXTID1 = 0X014E550A;
  31. //u32 MaskId_ID = 0XFFFF0000;
  32. RCC_APB1PeriphClockCmd(RCC_APB1Periph, ENABLE);
  33. CAN_InitTypeDef CAN_InitStructure;
  34. CAN_InitStructure.CAN_TTCM = DISABLE; //非时间触发通信模式
  35. CAN_InitStructure.CAN_ABOM = DISABLE; //软件自动离线管理
  36. CAN_InitStructure.CAN_AWUM = DISABLE; //睡眠模式通过软件唤醒
  37. CAN_InitStructure.CAN_NART = ENABLE; //禁止报文自动传送
  38. CAN_InitStructure.CAN_RFLM = DISABLE; //报文不锁定,新的覆盖旧的
  39. CAN_InitStructure.CAN_TXFP = DISABLE; //优先级由报文标识符决定
  40. CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; //模式设置 CAN_Mode_Normal
  41. CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; //重新同步跳跃宽度为个时间单位
  42. CAN_InitStructure.CAN_BS1 = CAN_BS1_8tq; //时间段1占用8个时间单位
  43. CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq; //时间段2占用5个时间单位
  44. CAN_InitStructure.CAN_Prescaler = 6; //分频系数(Fdiv)6 500K
  45. CAN_Init(CANx, &CAN_InitStructure); //初始化CAN1
  46. /*
  47. CAN_FilterInitTypeDef CAN_FilterInitStructure;
  48. CAN_FilterInitStructure.CAN_FilterNumber = 0;//过滤器0
  49. CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;// CAN_FilterMode_IdMask
  50. CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;//32位
  51. CAN_FilterInitStructure.CAN_FilterIdHigh = ((EXTID<<3)>>16) & 0xffff;//32位ID
  52. CAN_FilterInitStructure.CAN_FilterIdLow = ((EXTID<<3)& 0xffff) | CAN_ID_EXT;
  53. CAN_FilterInitStructure.CAN_FilterMaskIdHigh = ((MaskID<<3)>>16) & 0xffff;//32位ID
  54. CAN_FilterInitStructure.CAN_FilterMaskIdLow = ((MaskID<<3)& 0xffff) | CAN_ID_EXT;
  55. CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;//FIFO0
  56. CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;//激活过滤器0
  57. CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
  58. */
  59. CAN_FilterInitTypeDef CAN_FilterInitStructure;
  60. CAN_FilterInitStructure.CAN_FilterNumber = 0;//过滤器0
  61. CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;// CAN_FilterMode_IdMask
  62. CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;//32位
  63. CAN_FilterInitStructure.CAN_FilterIdHigh = ((EXTID<<3)>>16) & 0xffff;//32位ID
  64. CAN_FilterInitStructure.CAN_FilterIdLow = ((EXTID<<3)& 0xffff) | CAN_ID_EXT;
  65. CAN_FilterInitStructure.CAN_FilterMaskIdHigh = ((MaskID<<3)>>16) & 0xffff;//32位ID
  66. CAN_FilterInitStructure.CAN_FilterMaskIdLow = ((MaskID<<3)& 0xffff) | CAN_ID_EXT;
  67. CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;//FIFO0
  68. CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;//激活过滤器0
  69. CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
  70. CAN_ITConfig(CANx,CAN_IT_FMP0,ENABLE);//FIFO0 允许 消息挂号中断.
  71. }
  72. u8 CAN1_Send_Msg(u8* msg,u32 Can_ID,u8 len)
  73. {
  74. u8 mbox;
  75. u32 i = 0;
  76. CanTxMsg TxMessage;
  77. TxMessage.StdId = 0; // 标准标识符为0
  78. TxMessage.ExtId = Can_ID;//0x1f4e5301 0x1f4e5701 // 设置扩展标示符(29位)
  79. TxMessage.IDE = CAN_ID_EXT; // 使用扩展标识符
  80. TxMessage.RTR = CAN_RTR_DATA; // 消息类型为数据帧,一帧8位
  81. TxMessage.DLC = len; // 发送两帧信息
  82. for(i=0;i<len;i++)
  83. {
  84. TxMessage.Data[i] = msg[i]; // 第一帧信息
  85. }
  86. mbox = CAN_Transmit(CAN1,&TxMessage);
  87. i=0;
  88. while((CAN_TransmitStatus(CAN1,mbox)==CAN_TxStatus_Failed)&&(i<0XFFFF))
  89. {
  90. i++;//等待发送结束
  91. }
  92. if(i>=0XFFFF)return 1;
  93. return 0;
  94. }
  95. u8 CAN1_Receive_Msg(u8 *buf, u32 *id)
  96. {
  97. u32 i;
  98. CanRxMsg RxMessage;
  99. if(CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0; //没有接收到数据,直接退出
  100. CAN_Receive(CAN1, CAN_FIFO0,&RxMessage); //读取数据
  101. *id = RxMessage.ExtId;
  102. for(i=0;i<RxMessage.DLC;i++)
  103. {
  104. buf[i]=RxMessage.Data[i];
  105. }
  106. return RxMessage.DLC;
  107. }
  108. void Send_CAN_DATA6(void)
  109. {
  110. CAN_DATA6[0] = (u8)light_pwm;
  111. CAN_DATA6[1] = (u8)(light_pwm>>8);
  112. CAN_DATA6[2] = (u8)usbl_opendown;
  113. CAN_DATA6[3] = (u8)0;
  114. CAN_DATA6[4] = (u8)(temperature_board*10);
  115. CAN_DATA6[5] = (u8)((u16)(temperature_board*10)>>8);
  116. CAN_DATA6[6] = 15;
  117. CAN_DATA6[7] = 0xC0 + CAN_DATA_CNT++;
  118. if(CAN_DATA_CNT>=31) CAN_DATA_CNT = 0;
  119. CAN1_Send_Msg(CAN_DATA6, CAN_Send_Speed_ID, 8);
  120. }