# Easy problems on usart STM32

Status
Not open for further replies.

#### draaknar

##### Newbie level 3
Hello guys!

I have two functions where i have a RX-INTERRUPT and i send data via TX.
But i cannot understand a simple problems ive come accross.

1. Why cant i write printf("%d",index_rx) in my void USART1_IRQHandler(void) function?;---> is it because "uint8_t" and "int" are not the same?? ,I know that we have to use "%d" when its an "int" declaration.
2. Why doesnt it work when i write int index_rx=0;, and compile, whats the difference between static and int ?

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
void USART1_IRQHandler(void){
static uint8_t index_rx=0;
if(USART_GetITStatus(USART1, USART_IT_RXNE)!=RESET);
{
}
void send_data(USART_TypeDef* USARTx, volatile char *str){
while (*str)
{
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, *(str++));
}
}

Last edited by a moderator:

#### DTFT

##### Newbie level 6

* Try doing a printf() call in the handler function, but without referencing any variable. If that works but it fails with the uint_8 type, you know your issue is with the variable.
* Cast index_rx as an int when you call printf:
Code:
printf("%d", (int)index_rx);(

*The difference between statically declared variables and locally declared variables is that static variables are basically stored 'permanently' in memory, i.e. they persist even after your function has returned to its caller. Non-static variables are called local variables, and exist only on the stack while that particular instance of the function is running. When the function returns, that local variable is thrown away. So if you declare static int x and let x=5, and then return, next time your function is called, x will still be 5. Local variables don't do that.

It would seem weird to me that the compiler isn't letting you declare a local variable. There can be issues with stacks overwriting data, etc., but you should still be able to create local variables and use them, even in an interrupt handler. Can you confirm that the code will compile and run with the variable declared as static, but not without the static keyword?

Status
Not open for further replies.