AnsweredAssumed Answered

USART Help

Question asked by beltran.victor_manue on Apr 6, 2014
Latest reply on Apr 7, 2014 by beltran.victor_manue
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
//---------------------------------------------------------------------------------------------------------------------------------//


#include <stm32f30x.h>
#include <stdio.h>
#include <math.h>
#include "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));

Outcomes