We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi I'm having issue getting the data send from the usart1; I initialised properly, but the problem is not the rightdata is display on my PC terminal; I'm using stm32f407VG
#include "stm32f4xx.h" #include <stdio.h> void init_GPIO(void){ GPIO_InitTypeDef GPIO_InitStruct; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12; // we want to configure all LED GPIO pins GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; // we want the pins to be an output GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; // this sets the GPIO modules clock speed GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain) GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; // this sets the pullup / pulldown resistors to be inactive GPIO_Init(GPIOD, &GPIO_InitStruct); // this finally passes all the values to the GPIO_Init function which takes care of setting the corresponding bits. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; // we want to configure PA0 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; // we want it to be an input GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;//this sets the GPIO modules clock speed GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain) GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN; // this enables the pulldown resistor --> we want to detect a high level GPIO_Init(GPIOA, &GPIO_InitStruct); // this passes the configuration to the Init function which takes care of the low level stuff } void initUART (uint32_t baud){ GPIO_InitTypeDef GPIO_InitStructureTx; GPIO_InitTypeDef GPIO_InitStructureRx; USART_InitTypeDef USART_InitStructure; // Enable Clocks // //USART_Cmd(USART1, DISABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // Enable clocks RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); GPIO_InitStructureTx.GPIO_Pin = GPIO_Pin_6; // Configure TX pin GPIO_InitStructureTx.GPIO_Mode = GPIO_Mode_AF; // Push-Pull GPIO_InitStructureTx.GPIO_OType = GPIO_OType_PP; // Push-Pull GPIO_InitStructureTx.GPIO_Speed = GPIO_Speed_2MHz;; // 2MHz GPIO_Init(GPIOB, &GPIO_InitStructureTx); // Initialise GPIO_InitStructureRx.GPIO_Pin = GPIO_Pin_7; // Configure RX pin GPIO_InitStructureRx.GPIO_Mode = GPIO_Mode_IN; // Push-Pull GPIO_InitStructureRx.GPIO_Speed = GPIO_Speed_2MHz; // 2MHz GPIO_Init(GPIOB, &GPIO_InitStructureRx); // Initialise USART_InitStructure.USART_BaudRate = baud; // Set USART1 Baud Rate USART_InitStructure.USART_WordLength = USART_WordLength_8b; // Set USART1 Word Length USART_InitStructure.USART_StopBits = USART_StopBits_1; // Configure USART1 Stop Bits USART_InitStructure.USART_Parity = USART_Parity_No; // Configure USART1 Parity Bits USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // Configure USART1 Flow Control USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // Set USART1 Operation Mode USART_Init(USART1, &USART_InitStructure); // Configure UART USART_Cmd(USART1, ENABLE); // Enable UART } void USART_puts(USART_TypeDef* USARTx, volatile char *s){ while(*s){ // wait until data register is empty while( !(USARTx->SR & 0x00000040) ); USART_SendData(USARTx, *s); *s++; } } void Delay(__IO uint32_t nCount) { while(nCount--) { } } int main() { uint32_t brrrr=650; //uint32_t brrrr=0; uint32_t baud=1600; //printf("4"); init_GPIO(); initUART (1600); //USART_Configuration(); USART_puts(USART1, "Garlir"); while(1) { //GPIOD->BSRRL = 0xF000; // set PD12 thru PD15 //GPIOD->ODR=1<<13; // if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)) // { // // } GPIO_SetBits(GPIOD, GPIO_Pin_13); //USART_puts(USART1, "Garlir"); USART_SendData(USART1, 'P'); Delay(38000); GPIO_ResetBits(GPIOD, GPIO_Pin_13); //USART_puts(USART1, "Ga"); USART_SendData(USART1, 'L'); Delay(38000); //USART1->BRR=52.125; //baud=baud+300; //initUART (baud); brrrr=brrrr+1; USART1->BRR=brrrr; Delay(38000); } }
Hy do you start with the very non-standard baudrate of 1600?
And why do you then increase the baudrate with 1 for every iteration of the loop?
It seems to me like you have decided the "trial and error" method of developing programs. Alas, there are billions and billions of possible ways to introduce errors, so "trial and error" is a very slow approach.
Better to do "right" and then compare what happens with what should happen and deduce where reality deviates from the plan.
Hi; Thanks for the reply; I'm increasing the baud rate successively as to see if I'll get any character that make sense; so I can know that maybe I'm wrong with my clocks configuration. But when the terminal start receiving and displaying characters, I never identify any of the character I sent (in the code) till cutoff.
these are the character I get on the terminal; I google more information on the problem but can find any. " ðððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð ¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼¼<<¼¼<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<88<<88<<8888888888<<88888888888888888888888888 88888888888888888888888888888888888888888888888888xx88xxxxxxxxxxxxxxÎÎðÎÎÎðÎðÎðÎ ðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎ ðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðÎðððððððððððððððððððððððððððððððððððððððððððððððððððððððððð ððððððððððððððððððððððððððððððððððððððððððððððððàðððàðàðàààààààààààààààààààààààà àààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààà àààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààààà àààààààààààààààààààààààààààààààààààààààààààààààààààààààààÀÀààààààààÀÀÀÀÀÀÀÀÀÀÀÀÀ ÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ "
Don't you have access to an oscilloscope? It is way easier to verify the baudrate by just looking at the signal from your transmission.
John;
You do not mention what device that you are attempting to use so I feel free to refer you to the "Hello World" example in the Keil ARM Examples. It it built on the LPC2100 series.
It is very simple and it works.
Bradford
Hi; Finally I got it working, I'm using smt32f407VG; I had to use a max232 chip between then MCU and the PC. And still using the successive baud increment approach, I changed my main function to:
int main() { char valuez[40]={0}; uint32_t baud=1600; init_GPIO(); initUART (baud); while(1) { GPIO_SetBits(GPIOD, GPIO_Pin_13); USART_SendData(USART1, 'P'); Delay(380000); GPIO_ResetBits(GPIOD, GPIO_Pin_13); //USART_puts(USART1, "Ga"); USART_SendData(USART1, 'L'); Delay(380000); sprintf(valuez, "%d\n", baud); USART_puts(USART1,valuez); baud=baud+100; initUART (baud); Delay(38000); } }
And on my terminal, I have:
ààààààøøààààààøøààààààððððððððððÀÀÀÀÀÀÀÀÀÀÀÀÀÀ ðÀðÀàààààààààààüüüüüþþÀÀþÀÀþÀÀþÀ0À0À0À0À0À0À0À0À0À0À8À8À8À8ààààààààààààààààààààààààààààææxææææ8öööö8öööö<òòòò<òò<òò<òò¼òò¼òò¼òò¼òò¼óó¼òòóóóó ÃßÃßÃàÃàÃãÃãÃÜÀÃÜÀÃÜÃÃÜÃÃÜÄÃÜÄÃÜÇÃÜÇÃÜØÃÜØÃÜÛÃÜÛÃÜÜÃÜÜÃÜßÃÜßÃÜðÃÜð@ÿ8ÿ8ÿÃ8ÿÃ@ÿ8ÿ@ÿ8ÿ@ÿ8ÿ@ÿ8ÿ@ÿ8ÿ@ÿ8ÿ@ÿ8ÿ@ÿ8ÿ'ÿ8ÿ'ÿ8ÿ@ÿ8ÿ@ÿ8ÿ'ÿÿ'ÿÿ'ÿÿ'ÿÿ'ÿÿ'ÿÿ'ÿÿ'ÿÿ'ÿÿ'ÿÿ'ÿÿ'ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿ ÿÿPPL18L188200áPL18300 PL18400áPL18500 PL18600áPL18700 PL18800áPL18900 PL19000áPL19100 PL19200 PL19300 PL19400 PL19500áPL19600 PL19700 PL19800 PL19900 PL20000áPL20100áP̲°200áP¬2³0°Ð²°°°¬¬ ¬ ¤ ¤ ¤ ¬¨¤¨¤¨¤¨¤¨¤¨¤¨¤
it's clear that at a baud of 19200, I get the exact data. Thanks good day!