找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7939|回复: 20
收起左侧

基于STM32F103平台CANOPEN工业协议源码

  [复制链接]
ID:96552 发表于 2018-7-26 20:22 | 显示全部楼层 |阅读模式
基于STM32F103平台CANOPEN工业协议,性价比非常的方案,工业产品必备

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f10x.h"
  3. #include "delay.h"
  4. #include "can.h"
  5. #include "mco.h"
  6. #include "lcd.h"
  7. #include "bsp.h"
  8. #include "i2c.h"
  9. #include "24cxx.h"
  10. #include "stdio.h"

  11. typedef struct
  12. {
  13.   u8 BaudRate_Data;
  14.   u8 ODNodeID_Data;
  15. }e2prom;
  16. e2prom Image;
  17. #define SIZE sizeof(Image)

  18. void RCC_Configuration(void);
  19. void GPIO_Configuration(void);
  20. void NVIC_Configuration(void);
  21. void SYS_Tick_Configuration(void);
  22. void Set_Task(void);

  23. u32 *pDataIn,*pDataIna;
  24. u32  Datain,Dataina;
  25. u8   Key0_Flag=RESET;
  26. u8   Set_Flag=0;
  27. u8   KeyUp_Flag=RESET;
  28. u8   Baud_Data=0x01;
  29. u16 setData=0x45;
  30. extern OD_TABLE OD[MAX_OD_SIZE];
  31. extern WORD volatile gTimCnt;
  32. extern BYTE ODNodeID;

  33. int main(void)
  34. {
  35.   RCC_Configuration();
  36.   GPIO_Configuration();
  37.   delay_init();
  38.   LCD_Init();
  39.   LCD_Fill(0,0,239,319,BRED);
  40.   POINT_COLOR=RED;       //点的颜色
  41.   LCD_ShowString(30,50,200,16,16,"zhanjian STM32 ^_^");
  42.   LCD_ShowString(30,70,200,16,16,"CAN TEST");
  43.   LCD_ShowString(30,90,200,16,16,"wang xiao xin zhi zuo");
  44.   LCD_ShowString(30,110,200,16,16,"2014/03/24");
  45.   LCD_ShowString(60,130,200,16,16,"Datain:");  //8*7=56+8=64
  46.   LCD_ShowString(60,150,200,16,16,"Datain:");  //8*7=56+8=64
  47.   LCD_ShowString(60,170,200,16,16,"BaudRate:");//8*9=72+8=80
  48.   LCD_ShowString(60,190,200,16,16,"ODNodeID:");
  49.   /**读出e2prom值,在Set_Task()中判断是否要从新设置波特率和ID**/
  50.   while(AT24CXX_Check());//核查波特率和ID在不在范围内
  51.   AT24CXX_Read(0,(u8*)&Image,SIZE);//读取e2prom
  52.   ODNodeID=Image.ODNodeID_Data;
  53.   Baud_Data=Image.BaudRate_Data;
  54.   SYS_Tick_Configuration();
  55.   NVIC_Configuration();
  56.   CAN_Configration(Baud_Data);
  57.   MCOUSER_ResetCommunication();
  58.   OD[83].Val = setData;
  59.   pDataIn = &OD[MCO_Search_ODTABLE(0x6001,0x02)].Val;
  60.   while (1)
  61.   {
  62.     pDataIna=&OD[MCO_Search_ODTABLE(0x2002,0x03)].Val;
  63.     if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==SET)
  64.     {
  65.       if(KeyUp_Flag==RESET)
  66.       {
  67.         KeyUp_Flag=SET;
  68.         Set_Task();
  69.       }
  70.     }
  71.     else{ KeyUp_Flag=RESET;}
  72.     Ctx();
  73.     Datain = *pDataIn;
  74.     Dataina = *pDataIna;
  75.     LCD_ShowxNum(124,130,Datain,4,16,0);           //显示Datain的值
  76.     LCD_ShowxNum(124,150,Dataina,4,16,0);           //显示Datain的值
  77.     LCD_ShowxNum(140,170,Baud_Data,4,16,0);        //显示BaudRate的值
  78.     LCD_ShowxNum(140,190,ODNodeID,4,16,0);         //显示ODNodeID的值
  79.   }
  80. }

  81. /****************************************************************************
  82. * 名称:RCC_Configuration()
  83. * 功能:设置系统时钟,使能使用的时钟
  84. * 入口参数:无
  85. * 出口参数:无
  86. ****************************************************************************/
  87. void RCC_Configuration(void)
  88. {
  89.   SystemInit();
  90.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  91.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  92.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
  93.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
  94.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  95. }

  96. /*******************************************************************************
  97. *函数名称:void SYS_Tick_Configuration(void)
  98. *函数功能:系统滴答时钟的配置
  99. *入口参数:无
  100. *出口参数:无
  101. *******************************************************************************/
  102. void SYS_Tick_Configuration(void)
  103. {
  104.   SysTick->CTRL &= (u32)0xFFFFFFFE;   //失能系统滴答计数器        
  105.   SysTick->CTRL &= (u32)0xFFFFFFFD;   //失能中断
  106.   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);//设置时钟为9M
  107.   SysTick->LOAD = 9000;               //设置重新装载值
  108.   SysTick->CTRL |= (u32)0x00000001;   //使能系统滴答时钟
  109.   SysTick->CTRL |= (u32)0x00000002;   //使能中断
  110. }
  111. /****************************************************************************
  112. * 名称:GPIO_Configuration(void)
  113. * 功能:配置引脚
  114. * 入口参数:无
  115. * 出口参数:无
  116. ****************************************************************************/
  117. void GPIO_Configuration(void)
  118. {
  119.   GPIO_InitTypeDef GPIO_InitStructure;
  120.   //CAN  I/O初始化
  121.   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;//RX
  122.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  123.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  124.   GPIO_Init(GPIOA,&GPIO_InitStructure);
  125.   
  126.   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;//TX
  127.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  128.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
  129.   GPIO_Init(GPIOA,&GPIO_InitStructure);
  130.   //LED I/O初始化
  131.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  132.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  133.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  134.   GPIO_Init(GPIOB,&GPIO_InitStructure);
  135.   GPIO_SetBits(GPIOB,GPIO_Pin_5);
  136.    
  137.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  138.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  139.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  140.   GPIO_Init(GPIOE,&GPIO_InitStructure);
  141.   GPIO_SetBits(GPIOE,GPIO_Pin_5);
  142.   //KEY I/O初始化
  143.   GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
  144.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  145.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  146.   
  147.   GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
  148.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  149.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  150.   //IIC I/O初始化
  151.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
  152.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;   //推挽输出
  153.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  154.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  155.   GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11);         //PB10,PB11 输出高
  156. }


  157. /****************************************************************************
  158. * 名称:NVIC_Configuration(void)
  159. * 功能:设置中断优先级
  160. * 入口参数:无
  161. * 出口参数:无
  162. ****************************************************************************/
  163. void NVIC_Configuration(void)
  164. {
  165.   NVIC_InitTypeDef NVIC_InitStructure;
  166.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  167.   NVIC_SetPriority (SysTick_IRQn, 0);
  168.   
  169.   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  170.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  171.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  172.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  173.   NVIC_Init(&NVIC_InitStructure);
  174. }

  175. /****************************************************************************
  176. * 名称:void SysTickHandler(void)
  177. * 功能:滴答定时器服务程序
  178. * 入口参数:无
  179. * 出口参数:无
  180. ****************************************************************************/
  181. void SysTick_Handler(void)
  182. {
  183.   gTimCnt++;
  184. }

  185. /****************************************************************************
  186. * 名称:void Set_Task(void)
  187. * 功能:设置波特率和ID号
  188. * 入口参数:无
  189. * 出口参数:无
  190. ****************************************************************************/
  191. void Set_Task(void)
  192. {
  193.   while(1)
  194.     {
  195.       if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==RESET)//key0按下
  196.       {
  197.         if(Key0_Flag==RESET)
  198.         {
  199.             Key0_Flag=SET;
  200.             Set_Flag++;
  201.             //if(Set_Flag==4) Set_Flag=0;
  202.         }
  203.       }
  204.       else Key0_Flag=RESET;
  205.       if(Set_Flag==1)//设置ODNodeID
  206.       {
  207.         Key_ReceiveODNodeID();
  208.       }
  209.       if(Set_Flag==2)//设置BaudRate
  210.       {
  211.         //CAN_Configration(Key_ReceiveBaudRate());
  212.         Baud_Data = Key_ReceiveBaudRate();
  213.       }
  214.       if(Set_Flag==3)//确定设置
  215.       {
  216.         Set_Flag=0;
  217.         Image.ODNodeID_Data = ODNodeID;
  218.         Image.BaudRate_Data = Baud_Data;
  219.         AT24CXX_Write(0,(u8*)&Image,SIZE);//写入e2prom
  220.         SYS_Tick_Configuration();
  221.         NVIC_Configuration();
  222.         CAN_Configration(Baud_Data);
  223.         MCOUSER_ResetCommunication();
  224.         pDataIn = &OD[MCO_Search_ODTABLE(0x6001,0x02)].Val;
  225.         break;
  226.       }
  227.     LCD_ShowxNum(140,150,Baud_Data,4,16,0); //显示BaudRate的值
  228.     LCD_ShowxNum(140,170,ODNodeID,4,16,0);         //显示ODNodeID的值
  229.     }
  230. }
复制代码

这个代码有什么问题吗?求指导,测试了延时器文件,整个工程都工作不了
CANopen test.7z (2.65 MB, 下载次数: 459)
回复

使用道具 举报

ID:102263 发表于 2018-11-9 15:39 | 显示全部楼层
谢谢分享~~~
回复

使用道具 举报

ID:111291 发表于 2019-4-15 17:39 | 显示全部楼层
学习学习,谢谢分享!
回复

使用道具 举报

ID:554945 发表于 2019-6-4 18:35 | 显示全部楼层

学习学习,谢谢分享!
回复

使用道具 举报

ID:285890 发表于 2019-6-8 21:02 | 显示全部楼层
非常感谢分享!!
回复

使用道具 举报

ID:486587 发表于 2019-6-17 02:52 | 显示全部楼层
学习学习,谢谢分享!
回复

使用道具 举报

ID:164385 发表于 2019-6-17 08:08 | 显示全部楼层
谢谢楼主的分享了~~~
回复

使用道具 举报

ID:564716 发表于 2019-6-17 16:11 | 显示全部楼层
学习了,谢谢大佬分享
回复

使用道具 举报

ID:66768 发表于 2019-8-29 10:24 | 显示全部楼层
感谢楼主分享!
回复

使用道具 举报

ID:369933 发表于 2019-9-25 11:14 | 显示全部楼层
感谢分享。。。。
回复

使用道具 举报

ID:622848 发表于 2019-10-12 14:02 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

ID:481976 发表于 2019-10-16 17:02 | 显示全部楼层

感谢分享。。。。
回复

使用道具 举报

ID:628323 发表于 2019-10-22 17:05 | 显示全部楼层
感谢分享,使用后再来交流交流
回复

使用道具 举报

ID:660659 发表于 2019-12-10 15:39 | 显示全部楼层
请问这个协议实现是完整的吗?
回复

使用道具 举报

ID:309119 发表于 2020-1-20 10:14 | 显示全部楼层
谢谢楼主分享,正好需要
回复

使用道具 举报

ID:710761 发表于 2020-3-18 13:45 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:570527 发表于 2020-3-28 10:22 | 显示全部楼层
谢谢分享!!!!!!!!
回复

使用道具 举报

ID:385734 发表于 2020-6-24 19:01 | 显示全部楼层
感谢分享!找了好久了
回复

使用道具 举报

ID:416258 发表于 2021-3-5 18:10 | 显示全部楼层
感谢楼主分享,正好需要学习学习
回复

使用道具 举报

ID:381245 发表于 2021-3-7 14:14 | 显示全部楼层
请问这个协议实现是完整的吗?学习学习
回复

使用道具 举报

ID:301191 发表于 2022-4-12 03:57 | 显示全部楼层
顶一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表