2011-11-27 10:47 AM
Hi all.
First of all excuse my english
I am trying to implement the EXTI library but I am doing something wrong.
The program starts and send 0 by the uart (at this point all ok), then whait for a external interrupt with PA0 falling edgue (checked with the oscilloscope and its ok)
I dont know if the flag is reset or not, after applying the external interrupt the program not answered.This is the code:
#include ''stm32l1xx.h''
#include <stdio.h>GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;uint8_t CONT=0x00;
int main (void){
///UART CONFIGURATION////////////////////////////////////
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);GPIO_PinAFConfig(GPIOA, 2, GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOA, &GPIO_InitStructure);USART_InitStructure.USART_BaudRate = 115200;
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(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE);///EXTI CONFIGURATION//////////////////////////////
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure);EXTI_ClearITPendingBit(EXTI_Line0);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);USART_SendData(USART2, 0x30);
while (1)
{if (EXTI_GetITStatus(EXTI_Line0)== SET){
EXTI_ClearITPendingBit(EXTI_Line0); EXTI_ClearFlag(EXTI_Line0); USART_SendData(USART2, CONT); CONT++; } }}
I hope you can help me.
Thanks in advance and best regards
2011-11-27 01:20 PM
If you are generating interrupts, you'd better service them in their interrupt handler, and not in a loop in main().
Failing to clear the interrupt flag within the interrupt service will result in the M3 being stuck endlessly in interrupt context, and no foreground code will execute.2011-12-01 01:25 AM
Hi clive, thanks a lot for the advice, I used the irqhandler and it is running.
I did use loop to simplify but finally was an error Regards