STM32三串口通信加网络通信代码
霍尔传感器接下位机版纵撕代码单片机源程序如下:
- #include <stm32f10x.h>
- #include <string.h>
- #include "includes.h"
- #include "timer.h"
- #include <stdio.h>
- u8 volatile rcv_usart1_str_flag = 0; //串口数据接收启动标记
- u8 volatile rcv_usart1_end_flag = 0; //串口数据接收结束标记
- u8 volatile rcv_usart1_over_count = 0; //数据接收时间溢出标记
- u8 inRx1Buf[50]; //接收缓存
- u16 inRx1Length = 0; //接收字节长度
- u16 PD_Length=0;
- extern u8 kt_flag;
- u8 SD_flag=0;
- u8 zero_data[4]={0x87,0xff,0xff,0x0a};
- unsigned char txt_buf[8]= {0xA5,0x5A,0x05,0x82,0x00,0x02,0x00,0x00};
- unsigned char on_off_data[3]={0x88,0x00,0x0a};
- unsigned char ceshi_data[3]={0x00,0x00,0x00};
- //========
- char test[UART1BUF_LEN];
- //-=======
- u8 uart1RxBuf[UART1BUF_LEN];
- u8 uart1RxStart = 0;
- u8 uart1RxEnd = 0;
- u8 uart2RxBuf[UART2BUF_LEN];
- u8 uart2RxStart = 0;
- u8 uart2RxEnd = 0;
- u8 uart3RxBuf[UART1BUF_LEN];
- u8 uart3RxStart = 0;
- u8 uart3RxEnd = 0;
- void LED_ON(void);
- void LED_OFF(void);
- void InitLedIO(void);
- void USART1_Init(void);
- int WriteString(u8 *text, FIL *file);
- extern u8 bootup_adjust;
- extern u8 bootup_font_update;
- #if ICO_TO_SD==0
- extern u8 bootup_ico_update;
- #endif
- u8 wlSendBuf[32];
- int res = -1;
- u8 rt_flag=0;
- UINT byte_read = 0;
- int volatile isFileOpen = -1;
- //unsigned char *txt_buf0;
- int readok=0;
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB, GPIO_Pin_1);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB, GPIO_Pin_6);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB, GPIO_Pin_7);
-
-
- //GPIOA.0 中断线以及中断初始化配置 上升沿触发 PA0 WK_UP
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
- EXTI_InitStructure.EXTI_Line=EXTI_Line0;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //使能按键WK_UP所在的外部中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级2,
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- 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_ResetBits(GPIOB, GPIO_Pin_5);
- }
- void App_Init(void)
- {
- delay_init(72);
- NVIC_Configuration();
- USART_Configuration();
- delay_ms(1500);
- Timer_Configuration();
- InitLedIO();
- }
- void reflus()
- {
- int i;
- for(i=0;i<60;i++)
- {
- test[i]=0;
- uart1RxEnd=0;
- }
- }
- int main(void)
- {
- int i;
- u8 s[20];
- SystemInit();
- App_Init();
- delay_ms(10);
- PD_Length=0;
- USART1_Put_Data(ceshi_data,3);
- USART3_Put_Data(ceshi_data,3);
- txt_buf[6]=PD_Length>>8;
- txt_buf[7]=PD_Length&0x00ff;
- //USART1_Put_Data(txt_buf,8);
- // GPIO_SetBits(GPIOB, GPIO_Pin_6);
- // GPIO_SetBits(GPIOB, GPIO_Pin_7);
- USART1_Put_Data(zero_data,4);
- USART3_Put_Data(zero_data,4);
- delay_ms(1000);
- if(kt_flag==1)
- {
- SD_flag=0;
- on_off_data[1]=0x06;
- GPIO_ResetBits(GPIOB, GPIO_Pin_1);
- USART1_Put_Data(on_off_data,3);
- USART3_Put_Data(on_off_data,3);
- }
- else
- {
- SD_flag=1;
- on_off_data[1]=0x05;
- GPIO_SetBits(GPIOB, GPIO_Pin_1);
- USART1_Put_Data(on_off_data,3);
- USART3_Put_Data(on_off_data,3);
- }
- while(1)
- {
- if(kt_flag==1&&SD_flag==1)
- {
- delay_ms(1500);
- if(kt_flag==1)
- {
- SD_flag=0;
- on_off_data[1]=0x06;
- GPIO_ResetBits(GPIOB, GPIO_Pin_1);
- USART1_Put_Data(on_off_data,3);
- USART3_Put_Data(on_off_data,3);
- }
- }
- uart1RxEnd=0;
- uart3RxEnd=0;
-
- }
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
霍尔传感器接下位机版纵撕.7z
(841.73 KB, 下载次数: 18)
|