程序功能就是單片機接收上位機(串口助手)發送的 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();
- }
- }
復制代碼
|