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的值
- }
- }
- 单片机源程序如下:
所有资料51hei提供下载:
CANOPEN.zip
|