cancel
Showing results for 
Search instead for 
Did you mean: 

Uart application

ezrab
Associate II
Posted on October 06, 2010 at 15:35

hi

i was tring to activate USART3 On Port C PC10,PC11.

 

i used this configuration :

USART3->CR1 = 0x0C; //Receiver enable, Transmitter enable, USART disable

    USART3->CR2 = 0x0;

    USART3->BRR=156;//9600 at 24Mhz

   setsystemclock(); //set system clock to 24Mhz

 

    AFIO->MAPR|=0x10;//port remap as uart //PC10 TX PC11 RX

    GPIOC->CRH=0x4B00; //PC11 in input floating mode for RX. PC10 Alternative Output TX.

    RCC->APB2ENR |=0x40000;

 USART3->CR1 |=2000; //Uart Enable;

  while(1)

  {

   while(TxCounter < 30)

  {

    /* Send one byte from USARTy to USARTz */

      USART3->DR =( TxBuffer[TxCounter] & (uint16_t)0x01FF);

      while(((USART3->SR)&0x40)>0);//wait transmition complete

    TxCounter++;

    /* Loop until USARTy DR register is empty */

  }

  TxCounter=0;

  //GPIOC->ODR=0x3ffff;

  //GPIOC->ODR=0x0;

  }

but for some reason  it does not work??

can you help?

thanks.
15 REPLIES 15
Posted on October 06, 2010 at 19:24

Well you need to enable the clocks for GPIOC, AFIO and USART3 *BEFORE* configuring the perpheral units themselves.

The AFIO (APB2) is important because you are remapping the IO

The GPIOC (APB2) is important because you are doing IO on PORTC

The USART3 (APB1) is important because you are using it.

The BRR for 9600 baud at 24 MHz (assuming APB1 is @ 24MHz) is 0x9C4

Suggest you use the library code rather than banging on register bits.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ezrab
Associate II
Posted on October 07, 2010 at 08:35

hi.

thanks for your replay.

i tried what you said:

int main(void)

{

  //RCC->APB2ENR |=0x14;

    setsystemclock(); //set system clock to 24Mhz

    AFIO->MAPR|=0x10;//port remap as uart //PC10 TX PC11 RX

    GPIOC->CRH=0x4B00; //PC11 in input floating mode for RX. PC10 Alternative Output TX.

    RCC->APB1ENR =0x40000;

    USART3->CR1 = 0x0C; //Receiver enable, Transmitter enable, USART disable

    USART3->CR2 = 0x0;

    USART3->BRR=0x9C4;//9600 at 24Mhz

    USART3->CR1 |=2000; //Uart Enable;

  while(1)

  {

   while(TxCounter < 30)

  {

    /* Send one byte from USARTy to USARTz */

      USART3->DR =( TxBuffer[TxCounter] & (uint16_t)0x01FF);

      while(((USART3->SR)&0x40)==0);//wait transmition complete

      USART3->SR|=0x40;

    TxCounter++;

    /* Loop until USARTy DR register is empty */

  }

  TxCounter=0;

  }

}

but still i get nothing from the PC10 output.

and the program is stuck on   while(((USART3->SR)&0x40)==0);//wait transmition complete

becuse the USART3->SR stayis in 0.

why?

 what am i doing wrong?

i tried using the librerey code but then i need to add so many files to my project for avery simple definion or i dont understand exeactly what liberey you refer to and how.

thanks for your help.

joa
Associate II
Posted on October 07, 2010 at 10:50

Did you enable all clocks clive1 mentioned before you start to configure the usart and the port. It will not work otherwise.

Even if you don't use stm32f10x_usart.[ch], you could at least use the defined constants in stm32f10x.h.

For example, instead of

USART3->CR1 |=2000;

you can write

USART3->CR1 |= USART_CR1_UE;

which is a lot easier to read. (But if you insist of using magic numbers, try 0x2000 instead of 2000)
ezrab
Associate II
Posted on October 07, 2010 at 12:06

thanks for all your help.

i finaly got it working.

int main(void)

{

  //RCC->APB2ENR |=0x14;

    setsystemclock(); //set system clock to 24Mhz

    GPIOC->CRH=GPIO_CRH_MODE10 | GPIO_CRH_CNF10_1 |  GPIO_CRH_CNF11_0 ;//PC11 in input floating mode for RX. PC10 Alternative Output TX.

    AFIO->MAPR=AFIO_MAPR_USART3_REMAP_0 ;//port remap as uart //PC10 TX PC11 RX

    RCC->APB1ENR =RCC_APB1ENR_USART3EN ;

    USART3->CR1 =USART_CR1_RE|USART_CR1_TE; //Receiver enable, Transmitter enable, USART disable

    USART3->BRR=0x9C4;//9600 at 24Mhz

    USART3->CR1 |= USART_CR1_UE;; //Uart Enable;

  while(1)

  {

   while(TxCounter < 30)

  {

      USART3->DR =('5');

      while(((USART3->SR)&USART_SR_TXE )==0);/* Loop until USARTy DR register is empty */

      TxCounter++;

  }

  TxCounter=0;

  }

}

it works.

ezrab
Associate II
Posted on October 07, 2010 at 13:26

sorry i thouth it was working but it does not ,

i dont get any output from PC10 .

why?

thanks for your help.

ezrab
Associate II
Posted on October 07, 2010 at 13:53

for some reason i can see that the GPIOC->CRH registor does not change.

why?

ezrab
Associate II
Posted on October 07, 2010 at 14:01

found the solution the line:

RCC->APB2ENR =RCC_APB2RSTR_IOPCRST | RCC_APB2RSTR_AFIORST;//port C clock and alternative clock enable. page 79

was missing.

thanks any way.

Posted on October 07, 2010 at 16:54

Or'ing it would be better, rather than assuming you knew the initial content

RCC->APB2ENR |=RCC_APB2RSTR_IOPCRST | RCC_APB2RSTR_AFIORST;

Which would be the equivalent to RCC->APB2ENR |= 0x11; /* AND NOT 0x14 */

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ezrab
Associate II
Posted on October 10, 2010 at 08:43

hi

thanks for your help.

but i dont realy understand

if i use it this way RCC->APB2ENR |=RCC_APB2RSTR_IOPCRST | RCC_APB2RSTR_AFIORST;

then i dont realy know the inital value of  RCC->APB2ENR.

and in this way RCC->APB2ENR =RCC_APB2RSTR_IOPCRST | RCC_APB2RSTR_AFIORST;

i determine the value without assuming the inital value.

right?