程序功能就是單片機接收上位機(串口助手)發(fā)送的 adbcd ;將接收的數(shù)據(jù)放入數(shù)組 Rec_array[] ;
通過 if(0 == memcmp(Rec_array, "abc", 3)) 語句,翻轉(zhuǎn) LED ;
正常情況是數(shù)組存放了 {a,b,c,d,e}; 前三項 abc對應正確,led翻轉(zhuǎn)
但是接收數(shù)據(jù),在 if(0 == memcmp(Rec_array, "abc", 3)) 識別時數(shù)組里只存放了 {b,0,0,0,0};
想做到 發(fā)送-abcde-五位,led翻轉(zhuǎn),再發(fā)送-aaaaa-不翻轉(zhuǎn),再發(fā)送-abcee-翻轉(zhuǎn)
程序結(jié)構(gòu)很簡單:一個usart函數(shù),一個mian函數(shù)
usart.c文件
- #include "usart.h"
- #include "string.h"
- #include "led.h"
- #define Code_Max 5//最大值 5
- u8 Rec_array[Code_Max];//存放數(shù)組
- unsigned int Code_Cnt = 0;//數(shù)組變量
- /*中斷初始化*/
- 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;//存放到數(shù)組
- Code_Cnt++;
- if(Code_Cnt >= Code_Max)
- { Code_Cnt = 0;
- }
- USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
- }
- }
- /*串口接收數(shù)據(jù)函數(shù)*/
- 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();
- }
- }
復制代碼
|