嵌入式STM32学习——串口USART 2.3(串口发送数据控制LED灯)
本实验主要是利用串口发送指令实现LED的点亮,基本代码与之前一致,本次只展示改变代码,即main.c
#include "stm32f10x.h"
#include "main.h"
#include "led.h"
#include "bear.h"
#include "key.h"
#include "shake.h"
#include "usart.h"
#include "stdio.h"
void delay(uint16_t time)
{uint16_t i=0;while(time--){i =12000;while(i--);}}int main()
{my_usart_init();LED_Init();
// My_Usart_Send_Byte( USART1, 'A');
// My_Usart_Send_Byte( USART1, 'B');
// My_Usart_Send_Byte( USART1, 'C');
// My_Usart_Send_Sting( USART1, "\r\n");
// My_Usart_Send_Sting(USART1 , "fei \r\n");printf("Hello,shi \r\n");while(1){}
}void EXTI0_IRQHandler() //°´¼üÖжϺ¯Êý
{if(EXTI_GetITStatus(EXTI_Line0) != RESET)//ÍⲿÖжϻñÈ¡±ê־λ{GPIO_ResetBits(GPIOA,GPIO_Pin_1);EXTI_ClearITPendingBit(EXTI_Line0); //Çå³ýÍⲿÖжϱê־룬ҪÓëEXTI_GetITStatus´îÅäʹÓÃ}
}void USART1_IRQHandler() //串口中断函数
{char receive_data;if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET){receive_data = USART_ReceiveData(USART1); printf("receive data: %c \r\n", receive_data); if(receive_data == '1') //判断是否符合条件开关灯{GPIO_SetBits(GPIOA,GPIO_Pin_1);printf("LED OFF \r\n");}if(receive_data == '0'){GPIO_ResetBits(GPIOA,GPIO_Pin_1);printf("LED ON \r\n");}USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志位}}
实验结果