ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

51单片机电力载波应用开发(六)数据链

51单片机电力载波应用开发(六)数据链 /******************** 鑫盛电子工作室 ******************** * 文件名 main.c * 描述 通过串口调试软件向板子发送数据板子接收到数据后立即回传给电脑。 * 实验平台MINI STM32开发板 基于STM32F103C8T6 * 库版本 ST3.0.0 * 淘宝店http://shop66177872.taobao.com *********************************************************/ #include stm32f10x.h #include usart1.h #include led.h #include Time_test.h #include sys_lamp.h #include can.h #include uart_str.h SYS_DATA_TYPE sys_data; void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); /* 2 bit for pre-emption priority, 2 bits for subpriority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Enable the Ethernet global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority 0; NVIC_InitStructure.NVIC_IRQChannelCmd ENABLE; NVIC_Init(NVIC_InitStructure); } volatile u32 time; // ms 计时变量 ////////////////////////////////////////////////////////////// //485 modbus //Modbus 通讯数据报文 // 下行 01 03 00 00 00 01 84 0A//查询动作次数 // 上行 01 03 02 00 01 79 84 //上报数 // 下行 01 06 00 00 00 00 89 CA//清零计数 // 下行 01 06 00 01 00 01 19 CA//设置运行方式 ////////////////////////////////////////////////////////////// ////////////////////////////////crc 校验//////////////////////////////////// #define U16 unsigned int #define U8 unsigned char static U16 Get_CRC16(U8 *value, U8 length) { U16 crc16 0xFFFF;/* 设置十六位的寄存器初值为0xFFFF */ U8 i; U8 j; for(j 0; j length; j)/* 数组中的每个字节依次循环进入 */ { crc16 crc16 ^ value[j];/* 寄存器的值与数组值异或 */ for(i 0; i 8; i)/* 8次移位操作 */ { if(crc16 1)/* CRC寄存器的值最低位判断 */ { crc16 1;/* CRC寄存器的值右移一位 */ crc16 ^ 0xA001;/* CRC寄存器的值与预存值异或 */ } else { crc16 1;/* CRC寄存器的值右移一位 */ } } } crc16(crc16/256)((crc16%256)8); return crc16;/* 返回CRC校验值 */ } unsigned char buff_send[20]{0xff,0xff}; unsigned char buff_get[20]{0xff,0xff}; unsigned char cnt_get0; void buff_send_init(void) { unsigned char i; unsigned int crc_result; // for(i0;i16;i) // { // buff_send[i]0x55; // } buff_send[0]0x01; buff_send[1]0x03; buff_send[2]0x02; buff_send[3]0x00; buff_send[4]0x01; crc_result Get_CRC16(buff_send, 5); buff_send[5]crc_result/256; buff_send[6]crc_result%256; } void buff_send_init_100(void) { unsigned char i; unsigned int crc_result; // for(i0;i16;i) // { // buff_send[i]0x55; // } for(i0;i6;i) { sys_data.lamp_sw[i]0x20; } buff_send[0]0x01; buff_send[1]0x03; buff_send[2]0x0C; buff_send[3]0x00; buff_send[4]0x01; buff_send[5]0x00; buff_send[6]0x01; buff_send[7]0x00; buff_send[8]0x01; buff_send[9]0x00; buff_send[10]0x01; buff_send[11]0x00; buff_send[12]0x01; buff_send[13]0x00; buff_send[14]0x01; for(i0;i6;i) { buff_send[4i*2]sys_data.lamp_sw[i]; } crc_result Get_CRC16(buff_send, 15); buff_send[15]crc_result/256; buff_send[16]crc_result%256; } void buff_send_init_200(void) { unsigned char i; unsigned int crc_result; // for(i0;i16;i) // { // buff_send[i]0x55; // } // for(i0;i6;i) // { // sys_data.lamp_sw[i]0x20; // } buff_send[0]0x01; buff_send[1]0x03; buff_send[2]0x0C; buff_send[3]0x00; buff_send[4]0x01; buff_send[5]0x00; buff_send[6]0x01; buff_send[7]0x00; buff_send[8]0x01; buff_send[9]0x00; buff_send[10]0x01; buff_send[11]0x00; buff_send[12]0x01; buff_send[13]0x00; buff_send[14]0x01; for(i0;i6;i) { buff_send[4i*2]sys_data.lamp_sw[i]; } crc_result Get_CRC16(buff_send, 15); buff_send[15]crc_result/256; buff_send[16]crc_result%256; } void buff_get_clear(void) { int i; for(i0;i16;i) { buff_get[i]0xff; } } #define TRUE 1 #define FALSE 0 unsigned char flag_frameFALSE; unsigned int cnt_frame0; void modbus_echo(void) { if((flag_frameTRUE) (buff_get[0]0x01)(buff_get[1]0x03)) { //buff_get_clear(); flag_frameFALSE; time0; while(time10); RS485_Send_Data(buff_send,17); //将接收到的数据发送出去 cnt_get0; buff_get_clear(); } } extern void main_can(void); extern void GetKey(void); //检测是否有按键按下 void LED_test(void) { LED1(ON);LED2(ON); __nop(); LED1(OFF);LED2(OFF); __nop(); } ///////////////////////////////控制io初始化///////////////////////////////////////// //#define LAMP_MODE void lamp_id_io_init(void) { //PB1 PB5 //PA4 PA5 PA6 PA7 // #ifdef LAMP_MODE //port b GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); // 使能PB端口时钟 GPIO_InitStructure.GPIO_Pin GPIO_Pin_1| GPIO_Pin_5; #ifdef LAMP_MODE GPIO_InitStructure.GPIO_Mode GPIO_Mode_Out_PP; #else GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; #endif GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; GPIO_Init(GPIOB, GPIO_InitStructure); //初始化PB端口 GPIO_ResetBits(GPIOB, GPIO_Pin_1 | GPIO_Pin_5); // 关闭所有LED __nop(); GPIO_SetBits(GPIOB, GPIO_Pin_1 | GPIO_Pin_5); // 关闭所有LED __nop(); //port a // GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能外设时钟 //RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); // 使能PB端口时钟 GPIO_InitStructure.GPIO_Pin GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7; #ifdef LAMP_MODE GPIO_InitStructure.GPIO_Mode GPIO_Mode_Out_PP; #else GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; #endif GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; GPIO_Init(GPIOA, GPIO_InitStructure); //初始化PA端口 GPIO_ResetBits(GPIOA, GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7); // 关闭所有LED __nop(); GPIO_SetBits(GPIOA, GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7); // 关闭所有LED __nop(); // #else // #endif } //////////////////////////////////////////////////////////////////////////////////// void sensor_io_init(void) { GPIO_InitTypeDef GPIO_InitStructure; //PA0 init //port a // GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能外设时钟 //RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); // 使能PB端口时钟 GPIO_InitStructure.GPIO_Pin GPIO_Pin_0;// GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; // #ifdef LAMP_MODE // GPIO_InitStructure.GPIO_Mode GPIO_Mode_Out_PP; // #else // GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; // #endif GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; GPIO_Init(GPIOA, GPIO_InitStructure); //初始化PA端口 // GPIO_ResetBits(GPIOA, GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7); // 关闭所有LED // __nop(); // GPIO_SetBits(GPIOA, GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7); // 关闭所有LED // __nop(); } extern unsigned char flag_20ms;//FALSE; void sensor_read_again(void) { static int cnt0; cnt; if(cnt5) {cnt0; if((Bit_RESET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))) { //state2; //can send data //void can_tx3(u8 Data1,u8 Data2,u8 Data3) //can_tx3(0x0f,0x0f,0x40); can_tx3(sys_data.group_id,sys_data.sensor_id,0xff); //cnt; //if(cnt) } if(Bit_SET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) { //cnt0; can_tx3(sys_data.group_id,sys_data.sensor_id,0x00); } } } void sensor_state_read(void) { // if(Bit_SET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) // { // //SYS_ID|(10); // sys_data.sensor_state1; // //can_tx3(sys_data.group_id,sys_data.sensor_id,0x20); // __nop(); // } // // else // { // sys_data.sensor_state0; // //can_tx3(sys_data.group_id,sys_data.sensor_id,0x20); // __nop(); // } static char state0; int i0; if(flag_20msTRUE) { flag_20msFALSE; switch(state) { case 0://wait falling edge if(Bit_RESET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) { state1; } break; case 1://if valid if(Bit_RESET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) { state2; //can send data //void can_tx3(u8 Data1,u8 Data2,u8 Data3) //can_tx3(0x0f,0x0f,0x40); can_tx3(sys_data.group_id,sys_data.sensor_id,0xff); for(i0;i5000;i); can_tx3(sys_data.group_id,sys_data.sensor_id,0xff); for(i0;i5000;i); can_tx3(sys_data.group_id,sys_data.sensor_id,0xff); for(i0;i5000;i); } break; case 2://wait up edge if(Bit_SET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) { state0; //can send data //void can_tx3(u8 Data1,u8 Data2,u8 Data3) //can_tx3(0x0f,0x0f,0x00); can_tx3(sys_data.group_id,sys_data.sensor_id,0x00); for(i0;i5000;i); can_tx3(sys_data.group_id,sys_data.sensor_id,0x00); for(i0;i5000;i); can_tx3(sys_data.group_id,sys_data.sensor_id,0x00); for(i0;i5000;i); } break; defalut: state0;break; } } } void f_50Hz_io_init(void) { //IO PA11 GPIO_InitTypeDef GPIO_InitStructure; //PA0 init //port a // GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能外设时钟 //RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); // 使能PB端口时钟 GPIO_InitStructure.GPIO_Pin GPIO_Pin_11;// GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7; // GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Mode GPIO_Mode_Out_PP; // #ifdef LAMP_MODE // GPIO_InitStructure.GPIO_Mode GPIO_Mode_Out_PP; // #else // GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; // #endif GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; GPIO_Init(GPIOA, GPIO_InitStructure); //初始化PA端口 GPIO_ResetBits(GPIOA, GPIO_Pin_11); // 关闭所有LED __nop(); GPIO_SetBits(GPIOA, GPIO_Pin_11); // 关闭所有LED __nop(); } ///////////////////////////////////////////////// void group_io_init(void) { //PB6 PB7 PA12 PC13 GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能外设时钟 GPIO_InitStructure.GPIO_Pin GPIO_Pin_6| GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; GPIO_Init(GPIOB, GPIO_InitStructure); //初始化PA端口 //GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能外设时钟 GPIO_InitStructure.GPIO_Pin GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; GPIO_Init(GPIOA, GPIO_InitStructure); //初始化PA端口 //GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能外设时钟 GPIO_InitStructure.GPIO_Pin GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; GPIO_Init(GPIOA, GPIO_InitStructure); //初始化PA端口 } void group_io_read(void) { //PB6 PB7 PA12 PC13 // sys_data.group_id0; unsigned char temp0; if(Bit_RESET GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(11); __nop(); } if(Bit_RESET GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(10); __nop(); } if(Bit_RESET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(12); __nop(); } if(Bit_RESET GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(13); __nop(); } if(Bit_RESET GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(14); __nop(); } if(Bit_RESET GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(15); __nop(); } sys_data.group_idtemp; __nop(); } // } ///////////////////////////////////////////////// void sensor_id_io_init(void) { //PB1 PB5 //PA4 PA5 PA6 PA7 //port b GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); // 使能PB端口时钟 GPIO_InitStructure.GPIO_Pin GPIO_Pin_1| GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; GPIO_Init(GPIOB, GPIO_InitStructure); //初始化PB端口 // GPIO_ResetBits(GPIOB, GPIO_Pin_1 | GPIO_Pin_5); // 关闭所有LED // __nop(); // GPIO_SetBits(GPIOB, GPIO_Pin_1 | GPIO_Pin_5); // 关闭所有LED // __nop(); // //port a RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能外设时钟 GPIO_InitStructure.GPIO_Pin GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed GPIO_Speed_50MHz; // GPIO_Init(GPIOA, GPIO_InitStructure); //初始化PA端口 // GPIO_ResetBits(GPIOA, GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7); // 关闭所有LED // __nop(); // GPIO_SetBits(GPIOA, GPIO_Pin_4| GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7); // 关闭所有LED // __nop(); } void sensor_id_read(void) { //unsigned char sensor_id; //PB1 PA4 PA5 PA7 PA6 PB5 unsigned char temp0; //bit0 if(Bit_RESET GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(10); __nop(); } //bit1 if(Bit_RESET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(11); __nop(); } //bit2 if(Bit_RESET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(12); __nop(); } //bit3 if(Bit_RESET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(13); __nop(); } //bit4 if(Bit_RESET GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(14); __nop(); } //bit5 if(Bit_RESET GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5)) { //SYS_ID|(10); //sys_data.sensor_state1; temp|(15); __nop(); } // if(temp47) // {temp47;} sys_data.sensor_idtemp; __nop(); } ///////////////////////////////////////////////// extern unsigned int cnt_1ms;//0; extern unsigned char flag_20s;//FALSE; extern void can_heart(u8 Data1,u8 Data2,u8 Data3); extern void can_boot(u8 Data1,u8 Data2,u8 Data3); int cnt_id0; int main(void) { int i; SystemInit(); //配置系统时钟为 72M NVIC_Configuration(); //中断优先级设置 USART_Configuration(); //串口配置 RS_485_RX_EN; //RS485接收使能 LED_GPIO_Config();/* led 端口配置 */ TIM2_NVIC_Configuration(); /* TIM2 定时配置 */ TIM2_Configuration(); START_TIME; /* TIM2 开始计时 */ //buff_send_init(); buff_send_init_100(); main_can(); //LED_test(); lamp_id_io_init(); sensor_io_init(); f_50Hz_io_init(); group_io_init(); sensor_id_io_init(); for(i0;i3;i) { group_io_read(); sensor_id_read(); can_boot(sys_data.group_id,sys_data.sensor_id,0xff); } //uart_class_test(); UartRx_Create(uart1); /* 相当于“构造对象” */ while (1) { // if(flag_20sTRUE) // { // flag_20sFALSE; // main_can(); // } buff_send_init_200(); if(RS485_REC_Flag 1)//判断是否接收到了数据 { RS485_REC_Flag 0;//清除接收标志 RS485_Send_Data(RS485_buff,RS485_rec_counter); //将接收到的数据发送出去 RS485_rec_counter 0; //接收计数器清零 RS485_Send_Data(buff_send,7); //将接收到的数据发送出去 GPIO_WriteBit(GPIOB, GPIO_Pin_15, (BitAction)((1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_15)))); /* LED1 取反 */ } modbus_echo(); // GetKey(); sensor_state_read(); group_io_read(); sensor_id_read(); if (uart1.complete TRUE) { // printf(收到一帧: %s\n, uart1.getString(uart1)); uart1.reset(uart1); } // if(cnt_1ms100) // { // cnt_1ms0; // can_tx3(sys_data.group_id,sys_data.sensor_id,0); // } // if ( time 500 ) /* 500ms 时间到 */ { time 0; // sensor_read_again(); //can_heart(sys_data.group_id,sys_data.sensor_id,0xff); GPIO_WriteBit(GPIOB, GPIO_Pin_15, (BitAction)((1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_15)))); /* LED1 取反 */ RS485_Send_Data(buff_send,7); //将接收到的数据发送出去 cnt_id; if(cnt_id10) { cnt_id0; can_tx3(sys_data.group_id,sys_data.sensor_id,0); } } } }
返回列表