[mw_shl_code=c,true]#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
//ALIENTEK ̽Ë÷ÕßSTM32F407¿ª·¢°å ʵÑé0
//STM32F4¹¤³ÌÄ£°å-¿âº¯Êý°æ±¾
//¼¼ÊõÖ§³Ö£º
www.openedv.com
//ÌÔ±¦µêÆÌ£º
http://eboard.taobao.com
//¹ãÖÝÊÐÐÇÒíµç×ӿƼ¼ÓÐÏÞ¹«Ë¾
//×÷ÕߣºÕýµãÔ­×Ó @ALIENTEK
void my_USART_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //ʹÄÜ´®¿Ú1ʱÖÓ
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ʹÄÜGPIOAʱÖÓ
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //Òý½Å¸´ÓÃÓ³Éä
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//GPIOA³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//´®¿Ú²ÎÊý³õʼ»¯
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART1,USART_IT_TC,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler()
{
extern int flag;
int res,i;
if(USART_GetITStatus(USART1,USART_IT_RXNE))
{
res = USART_ReceiveData(USART1);
USART_SendData(USART1,0xbb);
}
else if(USART_GetITStatus(USART1,USART_IT_TC)==1)
{ flag = 1;
PFout(9)=0; //ÁÁ
delay_ms(500);
USART_ClearFlag(USART1, USART_FLAG_TC);
}
}
int flag;
int main(void)
{
extern int flag;
u8 res;
int result;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
my_USART_Init();
LED_Init();
while(1)
{
flag = 0;
PFout(9)=1; //Ãð
delay_ms(500);
USART_SendData(USART1,0xaa);
}
}
[/mw_shl_code]
代码功能:主函数while循环,灯灭延时500ms,USART1发送数据,进入中断,等亮500ms,跳出中断,继续while循环。
效果应该是LED0闪烁,周期1s。但是现在的效果是LED0一直亮,程序一直在执行中断函数。为什么???
单步调试,在执行while循环中delay_ms(500)时,会进入中断灯亮。这个delay是用来延时灯灭的啊。
跟着原子哥手把手视频初识STM32F4(探索者),浅薄之处望谅解。
加上清除中断标志语句还是灯一直亮效果,而且USART1没有接收到数据,程序不会执行到这里。
例程正常。
2楼说的对,中断中加延时的问题,把中断中的delay_ms(500)删掉,加在USART_SendData(USART1,0xaa); 后面,正常了。但是在外部中断实验中,在中断服务函数里有delay_ms(10)消抖,又有疑问,到底中断中能不能加延时?还是说中断服务函数中延时不能太长?
一周热门 更多>