cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103RB receiving data from USART

toniospritz
Associate III
Posted on May 06, 2012 at 17:18

Hi i stay try to send a character from Hyperterminal on the usart without any control on flow of usart(HW control or Xon/Xoff control)

this is my code

#define RCC_APB2ENR *(long*) 0x40021018

#define GPIOA_ODR *(long*) 0x4001080C

#define GPIOB_ODR *(long*) 0x40010C0C

#define GPIOA_CRH *(long*) 0x40010804

#define GPIOB_CRH *(long*) 0x40010C04

// Define for USART_RS232

#define USART1_SR *(long*) 0x40013800

#define USART1_BRR *(long*) 0x40013808

#define USART1_DR *(long*) 0x40013804

#define USART1_CR1 *(long*) 0x4001380C

#define USART1_CR2 *(long*) 0x40013810

#define USART1_CR3 *(long*) 0x40013814

#define USART1_GTPR *(long*) 0x40013818

int main (void)

{

RCC_APB2ENR=0x400C; //Set USART1 GPIOB GPIOA

GPIOA_CRH=0x4B0; //PA9 Altenate OUTPUT   PA10 Floating INPUT

GPIOB_CRH=0x22222222; //Out led at 2 MHz

USART1_SR=0x00000000;

USART1_BRR=0x0340; //Baud Rate a 9600

USART1_CR1=0x0000200C; // UE, TE, RE

USART1_CR2=0x00000801; // Clock Enabled e indirizzo 0001

USART1_CR3=0x00;

USART1_GTPR=0x00;

 

while(1)

{

while((USART1_SR&&0x00000020)==0x00000020) //Control RNXE flag

{USART1_DR=0x43; //character control

if(USART1_DR==0x41) 

{GPIOB_ODR=0x005500; //If receive A ODR on portB is 5500

}

if(USART1_DR==0x42) //If receive B ODR  on portB is AA00

{GPIOB_ODR=0x00AA00;

}

}

}

}

...i not Know how read correctly from USART_DR or i not set properly for take data from the shift register
7 REPLIES 7
Posted on May 06, 2012 at 18:33

while((USART1_SR&&0x00000020)==0x00000020) //Control RNXE flag

 

Too many '&' there for a bitwise AND.

while((USART1_SR & 0x20)==0x20) //Control RXNE flag

The DR is 16-bit wide, you should load it into a variable and then compare the variable.

i = USART1_DR & 0xFF;

if ((i == 'X') || (x == 'x')) puts(''Pressed X'')

else if ((i == 'Y') || (i == 'y')) puts(''Pressed Y'');

else if ((i == 'Z') || (i == 'z')) puts(''Pressed Z'');

Bit of advice, use the library it'll save a lot of wasted dev/debug time.

Mask new data on to peripheral registers, don't blind write over previous configurations.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
toniospritz
Associate III
Posted on May 06, 2012 at 22:07

Hi clive1!

Thank you for your help!

I've change the while and write only one '&', set i=USART1_DR&0xFF and the if, and not work :-(.

I've try also to give in output on Usart the C character and i receive in loop the C, also if i keep pressing for some time the character A or B the status of led change ( I suppose that saturated the shift register).

I can't use the library because the prof have tell us to not use them.

Thanks for your help!
Posted on May 07, 2012 at 05:04

Well your teacher seems stuck in 1980, I apologize.

Try this to output a stream of 'C's, I didn't check the addresses, but you do want them to be volatile.

#define RCC_APB2ENR *((volatile long*) 0x40021018)
#define GPIOA_ODR *((volatile short*) 0x4001080C)
#define GPIOB_ODR *((volatile short*) 0x40010C0C)
#define GPIOA_CRH *((volatile long*) 0x40010804)
#define GPIOB_CRH *((volatile long*) 0x40010C04)
// Define for USART_RS232
#define USART1_SR *((volatile long*) 0x40013800)
#define USART1_BRR *((volatile long*) 0x40013808)
#define USART1_DR *((volatile long*) 0x40013804)
#define USART1_CR1 *((volatile long*) 0x4001380C)
#define USART1_CR2 *((volatile long*) 0x40013810)
#define USART1_CR3 *((volatile long*) 0x40013814)
#define USART1_GTPR *((volatile long*) 0x40013818)
main()
{
RCC_APB2ENR |= 0x400C; // Clock Enable USART1 GPIOB GPIOA
GPIOA_CRH |= (GPIOA_CRH & 0xFFFFF00F)|0x4B0; //PA9 Alternate OUTPUT PA10 Floating INPUT
GPIOB_CRH = 0x22222222; //Out led at 2 MHz
USART1_BRR =0x0341; //Baud Rate a 9600 @ 8 MHz
USART1_CR1 =0x0000000C; // TE, RE
USART1_CR2 =0x00000000;
USART1_CR1|=0x00002000; //USART ENABLE 
while(1)
{
while((USART1_SR & 0x40)==0);
USART1_DR='C';
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 07, 2012 at 16:25

''I can't use the library because the prof have tell us to not use them.''

I trust that's just because he wants you to understand what it's like to work at the register level? 

As general advice, as clive1 says, it is rather poor and outdated.

Anyhow, the source code of the library is provided - so you can look at the library examples, and see how they do it...

 
toniospritz
Associate III
Posted on May 08, 2012 at 15:43

Hii neil.andrew and clive1,

 

Thanks for your helps!!!

yesteday i'm goed to university and the prof have correct me. I've not impremented well the RS232 protocoll and now work!

#define RCC_APB2ENR *(long*) 0x40021018

#define GPIOA_ODR *(long*) 0x4001080C

#define GPIOB_ODR *(long*) 0x40010C0C

#define GPIOA_CRH *(long*) 0x40010804

#define GPIOB_CRH *(long*) 0x40010C04

// Define per USART_RS232

#define     USART1_SR         *(long*) 0x40013800

#define     USART1_BRR *(long*) 0x40013808

#define USART1_DR    *(long*) 0x40013804

#define USART1_CR1         *(long*) 0x4001380C

#define USART1_CR2         *(long*) 0x40013810

#define USART1_CR3         *(long*) 0x40013814

#define    USART1_GTPR     *(long*) 0x40013818

int main (void)

{ int IN=0;

RCC_APB2ENR=0x400C; //Enable USART1 GPIOB GPIOA

GPIOA_CRH=0x8B0; //PA9 Altenate OUTPUT   PA10 Floating INPUT

GPIOB_CRH=0x22222222; //Out led at 2 MHz

USART1_SR=0x00000000;

USART1_BRR=0x0340; //Baud Rate  9600 @8Mhz

USART1_CR1=0x0000200C; // Enable UE, TE, RE

while(1)  

{

   while(!(USART1_SR & 0x00000020)); 

IN=USART1_DR;

if(IN==0x41) 

GPIOB_ODR=0x005500;

 

if(IN==0x42) 

GPIOB_ODR=0x00AA00;  

USART1_DR=USART1_DR+2; //Give in Output Rx+2

}    

}

 

Posted on May 08, 2012 at 16:26

I would generally avoid reading the DR multiple time, as it has side effects.

USART1_DR=IN+2; //Give in Output Rx+2

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
toniospritz
Associate III
Posted on May 08, 2012 at 16:44

Ok Clive1. I change the code and read the value from IN variable :)

Thank you very much for your help.

My next step is the adc1 setting and reading!

i'm happy to know that there are people as you on the ST communities.