canopen通訊程序
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "delay.h"
- #include "can.h"
- #include "mco.h"
- #include "lcd.h"
- #include "bsp.h"
- #include "i2c.h"
- #include "24cxx.h"
- #include "stdio.h"
- typedef struct
- {
- u8 BaudRate_Data;
- u8 ODNodeID_Data;
- }e2prom;
- e2prom Image;
- #define SIZE sizeof(Image)
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- void SYS_Tick_Configuration(void);
- void Set_Task(void);
- u32 *pDataIn,*pDataIna;
- u32 Datain,Dataina;
- u8 Key0_Flag=RESET;
- u8 Set_Flag=0;
- u8 KeyUp_Flag=RESET;
- u8 Baud_Data=0x01;
- u16 setData=0x45;
- extern OD_TABLE OD[MAX_OD_SIZE];
- extern WORD volatile gTimCnt;
- extern BYTE ODNodeID;
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- delay_init();
- LCD_Init();
- LCD_Fill(0,0,239,319,BRED);
- POINT_COLOR=RED; //點的顏色
- LCD_ShowString(30,50,200,16,16,"zhanjian STM32 ^_^");
- LCD_ShowString(30,70,200,16,16,"CAN TEST");
- LCD_ShowString(30,90,200,16,16,"wang xiao xin zhi zuo");
- LCD_ShowString(30,110,200,16,16,"2014/03/24");
- LCD_ShowString(60,130,200,16,16,"Datain:"); //8*7=56+8=64
- LCD_ShowString(60,150,200,16,16,"Datain:"); //8*7=56+8=64
- LCD_ShowString(60,170,200,16,16,"BaudRate:");//8*9=72+8=80
- LCD_ShowString(60,190,200,16,16,"ODNodeID:");
- /**讀出e2prom值,在Set_Task()中判斷是否要從新設置波特率和ID**/
- while(AT24CXX_Check());//核查波特率和ID在不在范圍內
- AT24CXX_Read(0,(u8*)&Image,SIZE);//讀取e2prom
- ODNodeID=Image.ODNodeID_Data;
- Baud_Data=Image.BaudRate_Data;
- SYS_Tick_Configuration();
- NVIC_Configuration();
- CAN_Configration(Baud_Data);
- MCOUSER_ResetCommunication();
- OD[83].Val = setData;
- pDataIn = &OD[MCO_Search_ODTABLE(0x6001,0x02)].Val;
- while (1)
- {
- pDataIna=&OD[MCO_Search_ODTABLE(0x2002,0x03)].Val;
- if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==SET)
- {
- if(KeyUp_Flag==RESET)
- {
- KeyUp_Flag=SET;
- Set_Task();
- }
- }
- else{ KeyUp_Flag=RESET;}
- Ctx();
- Datain = *pDataIn;
- Dataina = *pDataIna;
- LCD_ShowxNum(124,130,Datain,4,16,0); //顯示Datain的值
- LCD_ShowxNum(124,150,Dataina,4,16,0); //顯示Datain的值
- LCD_ShowxNum(140,170,Baud_Data,4,16,0); //顯示BaudRate的值
- LCD_ShowxNum(140,190,ODNodeID,4,16,0); //顯示ODNodeID的值
- }
- }
- /****************************************************************************
- * 名稱:RCC_Configuration()
- * 功能:設置系統時鐘,使能使用的時鐘
- * 入口參數:無
- * 出口參數:無
- ****************************************************************************/
- void RCC_Configuration(void)
- {
- SystemInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
- }
- /*******************************************************************************
- *函數名稱:void SYS_Tick_Configuration(void)
- *函數功能:系統滴答時鐘的配置
- *入口參數:無
- *出口參數:無
- *******************************************************************************/
- void SYS_Tick_Configuration(void)
- {
- SysTick->CTRL &= (u32)0xFFFFFFFE; //失能系統滴答計數器
- SysTick->CTRL &= (u32)0xFFFFFFFD; //失能中斷
- SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);//設置時鐘為9M
- SysTick->LOAD = 9000; //設置重新裝載值
- SysTick->CTRL |= (u32)0x00000001; //使能系統滴答時鐘
- SysTick->CTRL |= (u32)0x00000002; //使能中斷
- }
- /****************************************************************************
- * 名稱:GPIO_Configuration(void)
- * 功能:配置引腳
- * 入口參數:無
- * 出口參數:無
- ****************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- //CAN I/O初始化
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;//RX
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;//TX
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- //LED I/O初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_5);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOE,&GPIO_InitStructure);
- GPIO_SetBits(GPIOE,GPIO_Pin_5);
- //KEY I/O初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //IIC I/O初始化
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11); //PB10,PB11 輸出高
- }
- /****************************************************************************
- * 名稱:NVIC_Configuration(void)
- * 功能:設置中斷優先級
- * 入口參數:無
- * 出口參數:無
- ****************************************************************************/
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_SetPriority (SysTick_IRQn, 0);
-
- NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- /****************************************************************************
- * 名稱:void SysTickHandler(void)
- * 功能:滴答定時器服務程序
- * 入口參數:無
- * 出口參數:無
- ****************************************************************************/
- void SysTick_Handler(void)
- {
- gTimCnt++;
- }
- /****************************************************************************
- * 名稱:void Set_Task(void)
- * 功能:設置波特率和ID號
- * 入口參數:無
- * 出口參數:無
- ****************************************************************************/
- void Set_Task(void)
- {
- while(1)
- {
- if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==RESET)//key0按下
- {
- if(Key0_Flag==RESET)
- {
- Key0_Flag=SET;
- Set_Flag++;
- //if(Set_Flag==4) Set_Flag=0;
- }
- }
- else Key0_Flag=RESET;
- if(Set_Flag==1)//設置ODNodeID
- {
- Key_ReceiveODNodeID();
- }
- if(Set_Flag==2)//設置BaudRate
- {
- //CAN_Configration(Key_ReceiveBaudRate());
- Baud_Data = Key_ReceiveBaudRate();
- }
- if(Set_Flag==3)//確定設置
- {
- Set_Flag=0;
- Image.ODNodeID_Data = ODNodeID;
- Image.BaudRate_Data = Baud_Data;
- AT24CXX_Write(0,(u8*)&Image,SIZE);//寫入e2prom
- SYS_Tick_Configuration();
- NVIC_Configuration();
- CAN_Configration(Baud_Data);
- MCOUSER_ResetCommunication();
- pDataIn = &OD[MCO_Search_ODTABLE(0x6001,0x02)].Val;
- break;
- }
- LCD_ShowxNum(140,150,Baud_Data,4,16,0); //顯示BaudRate的值
- LCD_ShowxNum(140,170,ODNodeID,4,16,0); //顯示ODNodeID的值
- }
- }
復制代碼
單片機源程序如下(注意這個不是Keil代碼是iar的):
IAR工程文件CANopen test.7z
(2.78 MB, 下載次數: 62)
2022-4-12 03:50 上傳
點擊文件名下載附件
|