Hello there. I have a homework to do stopwatch, but I'm having a few problems. In code there are 4 button interrupt(B0-B3), 1 timer1 interrupt for 1 ms and USART2 interrupt. Buttons and timer interrupts work properly but USART2 interrupt work just one time or too late or never. When I send 'b' data to start stopwatch, it starts but after that when I send any data , it do that too late or don't.
#include "stm32f10x.h" // Device header #include "KS0108.h" #include "graphic.h" #include <stdio.h> int started = 0; int initizaled = 0; int dd = 0; int ss = 0; int mm = 0; int b_c = 0; char kronometre[20]; char tur[20]; static void USART_SendString(USART_TypeDef* USARTx, char* s); void USART2_IRQHandler(void){ if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET ) { if((char)USART_ReceiveData(USART2) == 'b') { started = 1; USART_SendString(USART2, "Started"); USART_ClearITPendingBit(USART2, USART_IT_RXNE); } else if((char)USART_ReceiveData(USART2) == 'd') { started = 0; USART_SendString(USART2, "Stopped"); USART_ClearITPendingBit(USART2, USART_IT_RXNE); } else if((char)USART_ReceiveData(USART2) == 't') { USART_SendString(USART2, "Tur bilgisi alindi."); sprintf(tur, " %d:%d:%d ",dd,ss,mm); GLCD_GoTo(0,3); GLCD_WriteString(" TUR 1 "); GLCD_GoTo(0,4); GLCD_WriteString(tur); USART_ClearITPendingBit(USART2, USART_IT_RXNE); } else if((char)USART_ReceiveData(USART2) == 'r') { dd = 0; ss = 0; mm = 0; sprintf(kronometre, " %d:%d:%d ",dd,ss,mm); GLCD_GoTo(0,2); GLCD_WriteString(" 0:0:000 "); USART_ClearITPendingBit(USART2, USART_IT_RXNE); } else if((char)USART_ReceiveData(USART2) == 'g') { USART_SendString(USART2, kronometre); USART_ClearITPendingBit(USART2, USART_IT_RXNE); } else if((char)USART_ReceiveData(USART2) == 'i') { USART_SendString(USART2, tur); USART_ClearITPendingBit(USART2, USART_IT_RXNE); } } } // B0 void EXTI0_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line0)) { // Clear interrupt flag EXTI_ClearITPendingBit(EXTI_Line0); if (started == 0) { started = 1; } else if (started == 1) { started = 0; } } } // B1 void EXTI1_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line1)) { // Clear interrupt flag EXTI_ClearITPendingBit(EXTI_Line1); sprintf(tur, " %d:%d:%d ",dd,ss,mm); GLCD_GoTo(0,3); GLCD_WriteString(" TUR 1 "); GLCD_GoTo(0,4); GLCD_WriteString(tur); } } // B2 void EXTI2_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line2)) { EXTI_ClearITPendingBit(EXTI_Line2); b_c = b_c + 1; if (b_c == 1) { started = 0; dd = 0; ss = 0; mm = 0; sprintf(kronometre, " %d:%d:%d ",dd,ss,mm); GLCD_GoTo(0,2); GLCD_WriteString(" 0:0:000 "); } else if (b_c == 2) { GLCD_ClearScreen(); GLCD_GoTo(0,2); GLCD_WriteString(" 0:0:000 "); b_c = 0; } } } // B3 void EXTI9_5_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line3)) { EXTI_ClearITPendingBit(EXTI_Line3); USART_SendString(USART2, tur); } } void TIM1_UP_IRQHandler(){ if(TIM_GetITStatus(TIM1, TIM_IT_Update)==SET){ // Timer 1 kesme bayragini temizleme TIM_ClearITPendingBit(TIM1, TIM_IT_Update); if (started == 1) { mm = mm + 1; if (mm==1000) { ss = ss + 1; mm=0; if (ss==60) { dd = dd + 1; ss=0; } } sprintf(kronometre, " %d:%d:%d ",dd,ss,mm); GLCD_GoTo(0,2); GLCD_WriteString(kronometre); } } } void UART_Initialize(void) { GPIO_InitTypeDef GPIO_InitStructurea; USART_InitTypeDef USART_InitStrutcurea; NVIC_InitTypeDef NVIC_InitStructurea; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Configure USART2 Rx as input floating */ GPIO_InitStructurea.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructurea.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructurea); /* Configure USART2 Tx as alternate function push-pull */ GPIO_InitStructurea.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructurea.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructurea.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructurea); // uart2'yi pd5 ve pd6 pinleri uzerinden kullanmak icin // remap etmek gerekiyor bknz=datasheet GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); USART_InitStrutcurea.USART_BaudRate = 9600; USART_InitStrutcurea.USART_WordLength = USART_WordLength_8b; USART_InitStrutcurea.USART_Parity = USART_Parity_No; USART_InitStrutcurea.USART_StopBits = USART_StopBits_1; USART_InitStrutcurea.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStrutcurea.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART2, &USART_InitStrutcurea); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_Cmd(USART2, ENABLE); NVIC_InitStructurea.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructurea.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructurea.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStructurea.NVIC_IRQChannelSubPriority = 0x01; NVIC_Init(&NVIC_InitStructurea); } void EXT_INT_GPIO_Init(void){ GPIO_InitTypeDef GPIO_InitStruct; EXTI_InitTypeDef EXTI_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* Set pin as input */ GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_2 | GPIO_Pin_1 | GPIO_Pin_0; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStruct); // Step 2: Initialize EXTI RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource2); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource3); EXTI_InitStruct.EXTI_Line = EXTI_Line3 | EXTI_Line2 | EXTI_Line1 | EXTI_Line0; EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStruct.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStruct); // Step 3: Initialize NVIC for EXTI0_IRQn channel NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); // Step 3: Initialize NVIC for EXTI1_IRQn channel NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); // Step 3: Initialize NVIC for EXTI2_IRQn channel NVIC_InitStruct.NVIC_IRQChannel = EXTI2_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); // Step 3: Initialize NVIC for EXTI9_5_IRQn channel NVIC_InitStruct.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); } void TM_TIMER1_Init(void) { TIM_TimeBaseInitTypeDef TIM_BaseStruct; NVIC_InitTypeDef NVIC_InitStructure; /* Enable clock for TIM1 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); // Timer Struct Ilklendirme TIM_BaseStruct.TIM_Prescaler = (uint16_t) (SystemCoreClock / 10000) - 1;; /* Count up */ TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_BaseStruct.TIM_Period = 1 - 1; TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_BaseStruct.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_BaseStruct); // Timer 1 aktive etme TIM_Cmd(TIM1, ENABLE); // Timer 1 kesmesi aktive etme TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); // Time 1 için kesme yapisini ayarlama ve ilklendirme NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } static void USART_SendString(USART_TypeDef* USARTx, char* s) { while(*s) { while(!USART_GetFlagStatus(USARTx, USART_FLAG_TC)); USART_SendData(USARTx, *s); s++; } } int main(void) { TM_TIMER1_Init(); EXT_INT_GPIO_Init(); UART_Initialize(); GLCD_Initialize(); GLCD_ClearScreen(); while(1) { } }
You need to understand what the USART_ReceiveData(USART2) function does.