2014-03-05 02:37 PM
Hi all,
I'm hoping this is an easy one and just something I am missing. A google didn't return anything obviously relating to my problem. I have some messy code that sets up my USART peripheral correctly. I have used GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; ...cast as global structures at the top of the script. Main then uses these to setup the usart and begin transmitting. This works correctly. However, as my code grows I now have the need to put GPIO init in to it's own function. I noticed when doing this that having the USART_InitTypeDef USART_InitStructure; structure declared locally to the function the usart output did 'funny' things (not expected output). GPIO Init structure location seems to have no affect. Please advise! Thanks as always guys, Ian2014-03-05 02:52 PM
Perhaps also worthy of note, I get some very weird working to non-working behavior between flashes with the structure declared locally.
(essentially the problem is fixed, I just want to know why!)2014-03-05 03:13 PM
auto/local variables allocated from the stack will contain random junk, they must be completely initialized, global statics are normally zeroed.
2014-03-05 03:16 PM
2014-03-05 09:28 PM
Im having one doubt, i think it would helps you.
If you are using array to send and receive usart data.What is the array length you are giving and check whether array overflow was occurring.It is the most thing causes random data to occur.2014-03-05 10:15 PM
You could do
USART_InitTypeDef USART_InitStructure = { 0 }; or USART_StructInit(&USART_InitStructure); or simply fully qualifying the fields of the structure when initializing it.2014-03-06 12:11 AM
Apologies for the 'multi reply'. Mobile didn't seem to work so well!
How come the example code generally gets away with this? (from most of the examples I find of other peoples code online)2014-03-06 01:28 AM
Unfortunately, there are known issues when submitting from tablets or mobiles.
I removed repeated answers. -Mayla-To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2014-03-06 05:32 AM
How come the example code generally gets away with this? (from most of the examples I find of other peoples code online)
Well usually by initializing enough of the fields that it works properly. Hard to say why in your experience didn't work, but I haven't seen the code.2014-03-06 06:47 AM
>> How come the example code generally gets away with this? (from most of the examples I find of other peoples code online)
There's a lot of hidden work done by the linker and runtime startup. Variables are put in a section of memory by the compiler/linker that is zeroed when the runtime is initialized (the code that runs before you get to main() routine). Jack Peacock