题目:基于STM32F103ZET6库函数串口中断点亮LED灯
问题:在编写基于STM32F103ZET6库函数串口中断点亮LED灯时,我的板子仅能接收数据处理数据,但无法向我的电脑串口助手发送数据,我在编写字符点亮LED灯的代码就可以成功发送,所以我的串口助手是好的;
已知我的LED1在PB5,LED2在PE5,USART的Tx\Rx分别在PA9/10,发送字节“AB000000”,其中“AB”为协议头,第一个“01”点亮LED1,第二个“01”点亮LED2,最后的“00”为保留字符。以下是我的代码
代码:
```c
#include "stm32f10x.h"
u8 index;
u8 Flag;
char String[10];
char Down_String[] = "AB000000\n";
char Head[] = "AB";
void delay_ms(int32_t ms)
{
int32_t i;
while(ms--)
{
i = 7500;
while(i--);
}
}
void USART1_configure(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructrue;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO,ENABLE);
//setting PA9 as USART1_Tx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//setting PA10 as USART1_Rx
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
USART_InitStructrue.USART_BaudRate = 9600;
USART_InitStructrue.USART_WordLength = USART_WordLength_8b;
USART_InitStructrue.USART_StopBits = USART_StopBits_1;
USART_InitStructrue.USART_Parity = USART_Parity_No;
USART_InitStructrue.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructrue.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructrue);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel= USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitStructure);
}
void LED1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
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_ResetBits(GPIOB,GPIO_Pin_5);
}
void LED1_on(void)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
void LED1_off(void)
{
GPIO_SetBits(GPIOB,GPIO_Pin_5);
}
void LED2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
void LED2_on(void)
{
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
}
void LED2_off(void)
{
GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
void Send_Byte(uint16_t Byte)
{
USART_SendData(USART1, Byte);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void Serial_SendString(char * String)
{
uint8_t i;
for(i=0; String[i] != '\0'; i++)
{
Send_Byte(String[i]);
}
}
char Get_LEDInfo(void)
{
// LED1
if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5)==1)
{
Down_String[3] = '0';
}
else{
Down_String[3] = '1';
}
// LED2
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_5)==1)
{
Down_String[5] = '0';
}
else{
Down_String[5] = '1';
}
return Down_String[7];
}
uint8_t ReadRx(char * String)
{
uint8_t j;
for(j=0; j<2; ++j)
{
if(String[j] != Head[j])
{
return 0;
}
}
if(String[2] =='0' && String[3] == '1')
{
LED1_on();
}
else if(String[2] =='0' && String[3] == '0')
{
LED1_off();
}
else
{
return 0;
}
if(String[4] =='0' && String[5] == '1')
{
LED2_on();
}
else if(String[4] =='0' && String[5] == '0')
{
LED2_off();
}
else
{
return 0;
}
return 1;
}
void Print(void)
{
if(Flag == 0)
{
Serial_SendString("Failed!\n");
Serial_SendString(Down_String);
}
else
{
Serial_SendString( "Successful!\n");
Serial_SendString(Down_String);
}
Flag = 0;
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
uint8_t Serial_RxData;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
Serial_RxData = USART_ReceiveData(USART1);
String[index++] = (char)Serial_RxData;
}
}
int main(void)
{
USART1_configure();
LED1_Init();
LED2_Init();
while (1)
{
if(index >= 8)
{
Flag = ReadRx((char *)String);
Print();
index = 0;
}
}
}
```