cancel
Showing results for 
Search instead for 
Did you mean: 

USART Help

definitely
Associate II
Posted on April 06, 2014 at 03:23

I'm using a STM32F3 Discovery and connecting it to labVIEW the problem is that I can't receive more than one byte. Can someone help me modifying my code to allow multiple byte reception. I'm testing it sending something and reading it in labVIEW. A problem occurs when I send more than 1 byte.

//-----------------------------------------------------Register comment format-----------------------------------------------------//

//[ Register bits ] Function name[ Function bits ] -> Value Effect explanation

//---------------------------------------------------------------------------------------------------------------------------------//

&sharpinclude <stm32f30x.h>

&sharpinclude <stdio.h>

&sharpinclude <math.h>

&sharpinclude ''lib/delay.h''

void charTX ( char ch );

void stringTX ( char *string );

uint8_t GetChar ( void );

char letra;

int main ( void ) 

{

char output[4];

delayinit();

//-----------------------------------------------------PC4 Configuration------------------------------------------------------//

RCC->AHBENR |= RCC_AHBENR_GPIOCEN; // [ 19 ] IOPCEN = 1 I/O port A clock enabled.

GPIOC->MODER |= 2 << ( 4*2 ); // [ 9:8 ] MODER4[ 1:0 ] = 10b Alternate function on 

// PIN C4

//GPIOC->OTYPER |= 1 << ( 4*1 ); // [ 4 ] OT4 = 1 Output as open drain

GPIOC->OSPEEDR |= 3 << ( 4*2 ); // [ 9:8 ] OSPEEDR4[ 1:0 ] = 11b 50 MHz High-speed on PIN C4

//GPIOC->PUPDR &= ~( 3 << ( 4*2 ) ); // [ 9:8 ] PUPDR4[ 1:0 ] = 0 No pull-up/down om PIN C4

GPIOC->AFR[ 0 ] |= 7 << ( 4*4 ); // [ 17:16 ] AFRL4[ 3:0 ] = 0111b AF7( USART1_TX ) on PIN C4

// PC5 configuration (RX)

GPIOC->MODER |= 2 << (5*2);  // GPIO_Mode_AF

GPIOC->AFR[0] |= 7 << (5*4);  //  AF7

RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // Enable USART1 clock

USART1->BRR = 72000000/9600;

USART1->CR1 &= ~USART_CR1_OVER8; // Oversampling mode = 16 

USART1->CR1 &= ~USART_CR1_M;  // Word length = 8 bits

USART1->CR1 &= ~USART_CR1_PCE;  // No parity

USART1->CR1 |= USART_CR1_TE;  // Transmitter enable

USART1->CR1 |= USART_CR1_RE;  // Receiver enable

USART1->CR1 |= USART_CR1_UE;  // USART enable

USART1->CR2 &= ~(USART_CR2_STOP_1 | USART_CR2_STOP_0); // one stop bit

for( ;; )

{

letra = GetChar()

sprintf(output,''%4d\n'',letra);

stringTX(output);

delaybyms(10);

}

}

void charTX ( char ch ) 

{

while (!(USART1->ISR & USART_ISR_TXE));

USART1->TDR = (ch & 0xFF);

}

void stringTX ( char *string )

{

    do

    {

        charTX( *string );

        *string++;

    }while(*string);

}

uint8_t GetChar ( void ) 

while (!(USART1->ISR & USART_ISR_RXNE));

return ((uint8_t)(USART1->RDR & 0xFF));

#usart-stm32f3
14 REPLIES 14
definitely
Associate II
Posted on April 06, 2014 at 07:44

Ok. But for manipulating data I have to use 

 USART1->RDR and USART1->TDR ?

Posted on April 06, 2014 at 08:00

And so the problem is what exactly? The routines to perform ''getchar'' and ''putchar'' functionality are pretty basic, requiring that you check that something is available, or empty, respectively. They could be implemented in ways which are non-blocking, use buffers, and interrupts, etc.

Have you done much C, and STDIO, type development before?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
definitely
Associate II
Posted on April 06, 2014 at 08:29

Not so much. What string functions do you recommend for me to study

Posted on April 07, 2014 at 02:15

You should probably familiarize yourself with all the functions, your can always look up the specific syntax when you need to use them.

http://web.cs.swarthmore.edu/~newhall/unixhelp/C_strings.html

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
definitely
Associate II
Posted on April 07, 2014 at 06:58

Hey Clive. Can you explain me this:

I send 4000 and as a response I receive 4000 so it's fine. But if I see at debugger ch variable (which is the receiver) I see that has a value of 51 (always). Why is that? What kind of conversion do I have to do with ''ch''. I don't get it.