Hi all, I'm newbie with stm32f103c8, and I have a problem with Interrupt in c ++, basically the same code works in c but not in c ++, here is my code
#include "stm32f10x_rcc.h" #include "stm32f10x_gpio.h" #include "stm32f10x_usart.h" #include "stm32f10x.h" // Device header #include <stdio.h> #include <stdlib.h> #include <string.h> char j=0; char D[3]; void putttc(char c); void puttts(char *s);
void delay(unsigned int nCount);
void USART1_IRQHandler(void){ if( USART_GetITStatus(USART1, USART_IT_RXNE) != RESET ) { D[0]= USART_ReceiveData(USART1); //USART_SendData(USART1,D[0]); putttc(D[0]); } }
int main(){ //USART1_Init(); GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStruct; //RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
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(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_SetPriority(SysTick_IRQn, 0); NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct);
while (1) { GPIO_ResetBits(GPIOA, GPIO_Pin_0); puttts("hello\n"); delay(1000); GPIO_SetBits(GPIOA, GPIO_Pin_0); delay(1000); } }
void delay(unsigned int nCount){ unsigned int i, j; for (i = 0; i < nCount; i++)
for (j = 0; j < 0x2AFF; j++);
}
void putttc(char c) { while (!USART_GetFlagStatus(USART1, USART_FLAG_TXE)); USART_SendData(USART1, c); }
void puttts(char *s) { // Send a string while (*s) { putttc(*s++); } }
BTW: this code works fine before an interrupt, when an interrupt occurs the program stop working, thanks
it works! thanks very much :D