程序功能就是单片机接收上位机(串口助手)发送的 adbcd ;将接收的数据放入数组 Rec_array[] ;
通过 if(0 == memcmp(Rec_array, "abc", 3)) 语句,翻转 LED ;
正常情况是数组存放了 {a,b,c,d,e}; 前三项 abc对应正确,led翻转
但是接收数据,在 if(0 == memcmp(Rec_array, "abc", 3)) 识别时数组里只存放了 {b,0,0,0,0};
想做到 发送-abcde-五位,led翻转,再发送-aaaaa-不翻转,再发送-abcee-翻转
程序结构很简单:一个usart函数,一个mian函数
usart.c文件
- #include "usart.h"
- #include "string.h"
- #include "led.h"
- #define Code_Max 5//最大值 5
- u8 Rec_array[Code_Max];//存放数组
- unsigned int Code_Cnt = 0;//数组变量
- /*中断初始化*/
- static void USART1_NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- NVIC_InitStructure.NVIC_IRQChannel = USART_IRQ;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
-
- NVIC_Init(&NVIC_InitStructure);
- }
- void USART1_GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /*开启串口GPIO时钟*/
- USART_APBxClkCmd(USART_GPIO_CLK,ENABLE);
- GPIO_InitStructure.GPIO_Pin = USART_TX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(USART_TX_GPIO_PORT,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = USART_RX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(USART_RX_GPIO_PORT,&GPIO_InitStructure);
- }
- /*串口GPIO初始化*/
- void USART1_Configuration(void)
- {
- USART1_NVIC_Configuration();
- USART1_GPIO_Configuration();
- USART_InitTypeDef USART_InitStructure;
- /*开启串口时钟*/
- USART_APBxClkCmd(USART_CLK,ENABLE);
- USART_InitStructure.USART_BaudRate = USART_Baudrate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USARTx,&USART_InitStructure);
-
- USART_ITConfig(USARTx,USART_IT_RXNE,ENABLE);
- USART_ITConfig(USARTx,USART_IT_TXE,DISABLE);
-
- USART_Cmd(USARTx,ENABLE);
- }
- /*串口接收中断*/
- void USART1_IRQHandler(void)
- {
- u8 data;
- if(USART_GetITStatus(USARTx,USART_IT_RXNE) != RESET)
- {
- data = USART_ReceiveData(USARTx);
- Rec_array[Code_Cnt] = data;//存放到数组
- Code_Cnt++;
- if(Code_Cnt >= Code_Max)
- { Code_Cnt = 0;
- }
- USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
- }
- }
- /*串口接收数据函数*/
- void USART1_Recive(void)
- {
- Code_Cnt = 0;
- if(0 == memcmp(Rec_array, "abc", 3))
- {
- LED_BLUE;
- }
- if(0 == memcmp(Rec_array, "bca", 3))
- {
- LED_RED;
- }
- Rec_array[0] = Rec_array[1] = Rec_array[2] = Rec_array[3] = Rec_array[4] = 0;
- }
复制代码 usart.h
- #ifndef __USART_H
- #define __USART_H
- #include "stm32f10x.h"
- #include <stdio.h>
- #define USARTx USART1
- #define USART_CLK RCC_APB2Periph_USART1
- #define USART_Baudrate 115200
- #define USART_GPIO_CLK RCC_APB2Periph_GPIOA
- #define USART_APBxClkCmd RCC_APB2PeriphClockCmd
- #define USART_TX_GPIO_PORT GPIOA
- #define USART_TX_GPIO_PIN GPIO_Pin_9
- #define USART_RX_GPIO_PORT GPIOA
- #define USART_RX_GPIO_PIN GPIO_Pin_10
- #define USART_IRQ USART1_IRQn
- #define USART_IRQHandler USART1_IRQHandler
- void USART1_Configuration(void);
- void USART1_Recive(void);
- #endif
复制代码
main.c
- #include "stm32f10x.h"
- #include "led.h"
- #include "usart.h"
- int main(void)
- {
- LED_GPIO_Config();
- USART1_Configuration();
- while (1)
- {
- USART1_Recive();
- }
- }
复制代码
|