i have been trying to run USART on discovery board(STM32F429 DISC1). But all get in the transmission is corrupt data pls help me out. This my code for my project.
int main() { board_init(); led_blinkALT(5); while(GPIO_ReadInputDataBit(GPIOA , GPIO_Pin_0)==0) led_blinkRed(2); usart_Tx_str("Hello!"); led_blinkGreen(2); while(GPIO_ReadInputDataBit(GPIOA , GPIO_Pin_0)==0) led_blinkRed(2); led_blinkGreen(2); while(1) TIMER_Capture(); usart_Rx(); }
void TIMER_Capture(void) { if (cn==0) { ICvalue1=TIM_GetCapture1(TIM2); cn=1; } else if (cn ==1) { ICvalue2=TIM_GetCapture1(TIM2); if(ICvalue2 > ICvalue1) { capture = (ICvalue2 - ICvalue1); } else if (ICvalue2 < ICvalue1) { capture = ((0xFFFF - ICvalue1) + ICvalue2); } else { capture = 0; } freq = (uint32_t) SystemCoreClock / capture; cn = 0; usart_Tx(freq); } return; }
int usart_Tx(int ch) { while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USART1, ch); //led_blinkGreen(1); return(ch); } void usart_Tx_str(char *str) { int i = 0; while(str[i] != 0) { usart_Tx(str[i++]); } } int usart_Rx() { while ((USART1->SR & USART_SR_RXNE) == 0) { led_blinkRed(1); } led_blinkGreen(1); return (USART1->DR & 0x1FF); } void board_init(void) { GPIO_init(); USART_init(); TIMER_init(); } void GPIO_init(void) { GPIO_InitTypeDef GPIO_InitDef;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE); GPIO_InitDef.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14; GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitDef.GPIO_OType = GPIO_OType_PP; GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOG, &GPIO_InitDef);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitDef.GPIO_Pin = GPIO_Pin_0; GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN; GPIO_InitDef.GPIO_OType = GPIO_OType_PP; GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitDef);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_TIM2); GPIO_InitDef.GPIO_Pin = GPIO_Pin_15; GPIO_InitDef.GPIO_Mode = GPIO_Mode_AF; GPIO_InitDef.GPIO_OType = GPIO_OType_PP; GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitDef);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4); GPIO_InitDef.GPIO_Pin = GPIO_Pin_12; GPIO_InitDef.GPIO_OType = GPIO_OType_PP; GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitDef.GPIO_Mode = GPIO_Mode_AF; GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitDef);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); GPIO_InitDef.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7; GPIO_InitDef.GPIO_Mode = GPIO_Mode_AF; GPIO_InitDef.GPIO_OType = GPIO_OType_PP; GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitDef);
/* GPIO_PinAFConfig(GPIOB, GPIO_PinSource2, GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_TIM2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_TIM2); GPIO_InitDef.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitDef.GPIO_OType = GPIO_OType_PP; GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitDef.GPIO_Mode = GPIO_Mode_AF; GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitDef);*/
} void USART_init(void) { USART_InitTypeDef USART_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); USART_InitStruct.USART_BaudRate = 9600; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART1, &USART_InitStruct); USART_Cmd(USART1, ENABLE);
}
void TIMER_init(void) { TIM_TimeBaseInitTypeDef TIM_BaseStruct; TIM_ICInitTypeDef TIM_ICDef; TIM_OCInitTypeDef TIM_OCStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_BaseStruct.TIM_Prescaler = 0; TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_BaseStruct.TIM_Period = 83999; /* 1kHz PWM */ TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_BaseStruct.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM4, &TIM_BaseStruct); TIM_Cmd(TIM4, ENABLE);
TIM_OCStruct.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCStruct.TIM_Pulse = 41999; //50% duty cycle TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM4, &TIM_OCStruct); TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_BaseStruct.TIM_Prescaler = 84-1; TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_BaseStruct.TIM_Period = 0xFFFF; TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_BaseStruct.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_BaseStruct);
TIM_ICDef.TIM_Channel = TIM_Channel_1; TIM_ICDef.TIM_ICFilter = 0; TIM_ICDef.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICDef.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICDef.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInit(TIM2, &TIM_ICDef); TIM_Cmd(TIM2, ENABLE);
void led_blinkALT(int x) { int i; for(i=0;i<x;i++) { led_greenON(); delay(5000000); led_redON(); led_greenOFF(); delay(5000000); led_redOFF(); } } void led_blinkGreen(int x) { int i; for(i=0;i<x;i++) { led_greenON(); delay(5000000); led_greenOFF(); delay(5000000); } }
void led_blinkRed(int x) { int i; for(i=0;i<x;i++) { led_redON(); delay(5000000); led_redOFF(); delay(5000000); } } void led_redON(void) { GPIO_SetBits(GPIOG, led_red); } void led_redOFF(void) { GPIO_ResetBits(GPIOG, led_red); } void led_greenON(void) { GPIO_SetBits(GPIOG, led_green); } void led_greenOFF(void) { GPIO_ResetBits(GPIOG, led_green); } void delay(int a) { int i,j; for(i=0;i<a;i++) { j++; } }
Please read the posting instructions before pasting in hundreds of lines of code.
Does the USART work if you simplify this down to just outputting strings in a loop?
Define the nature of the corruption. ie what you expect to see on the terminal vs what you actually see.