usart stm32f0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-02-06 4:31 AM
hello,
i'm trying to connect my stm32f0 discovery with the pc and set up a uart connection. I have found the following code (in this community) but i can't get it to work. Does anyone have a suggestion about what could be wrong. ( i have allready used a level shifter..)int
main(
void
)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
/* Configure USART2 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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);
while
(1)
{
while
(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
USART_SendData(USART2,
'X'
);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-02-06 5:20 AM
while
(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
; // << SEMI COLON AT THE END THERE
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-02-06 5:31 AM
He Clive,
First off, thanks for the really quick response. Oke, i have put the semicollum there but there is still nothing happening. The pc terminal is set to :8 bits1 stopbit9600 baudno parityno hardware control. just as i specify in my hardware. the tx of the level shifter is connected to PA3 and the rx to PA2. so i have no clue what to do next. because the pc terminal is not showing any sign of life. (not receiving anything from the usart. My processor is running at 8mhz (internal clock) could that have something to do with it?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-02-06 7:11 AM
At this point you might want to attach a scope and evaluate the signals and voltages you see.
Perhaps you could try some loop back testing and see if you can receive data.Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-02-07 2:11 AM
Dear clive,
The problem was not the software afterall. I made a mistake in soldering my level shifter. So one of the ports was shortcircuited. Thanks for your help :)- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-02-07 11:57 PM
Oke this is the next problem I run into. I need to convert a int to a string to send it by usart. So i do the following line and add the headers needed for this call:
char str[20];sprintf(str, ''%d'', timer);UART_Puts(str); // a function that eventually sends the string (tested and it works with '' hello world'' string.in which timer is the int that needs to be converted. This gives me the following error, and i have a hunch that my compiler doesn't support sprintf?undefined reference to `_sbrk'what to do?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2013-02-08 4:33 AM
GNU based?
I think you need to find the right library to link in. If it's just a matter of decimal, perhaps write a simple routine to decode or something like itoa()char *dec32(uint32_t i)
{
static char string[16]; // enough for 10 digits + NUL
char *s = string + sizeof(string);
*--s = 0;
do
{
*--s = '0' + (char)(i % 10);
i = i / 10;
} while(i);
return(s);
}
Up vote any posts that you find helpful, it shows what's working..
