0
\$\begingroup\$

I am trying to set up UART on a STM8L controller, and I have a code which works only on PC3. But I want to transmit on PA2, but I don't know why is it not working like this. I don't even initialize the PC3 pin. Is there anything I am missing or is there any way to define pins? I am working in IAR.

full code:

#include <iostm8l151k6.h>
#include <intrinsics.h>
#include <stdio.h>

void InitialiseSystemClock()
{
    CLK_ICKCR = 0;                       //  Reset the Internal Clock Register.
    CLK_ICKCR_HSION = 1;                 //  Enable the HSI.
    CLK_ECKR = 0;                       //  Disable the external clock.
    CLK_CKDIVR = 0;                     //  Ensure the clocks are running at full speed.
    CLK_PCKENR1 = 0xff;                 //  Enable all peripheral clocks.
    CLK_PCKENR2 = 0xff;                 //  Ditto.
    CLK_CCOR = 0;                       //  Turn off CCO.
    CLK_HSITRIMR = 0;                   //  Turn off any HSIU trimming.
    CLK_SWR = 0xe1;                     //  Use HSI as the clock source.
    CLK_SWCR = 0;                       //  Reset the clock switch control register.
    CLK_SWCR_SWEN = 1;                  //  Enable switching.
    while (CLK_SWCR_SWBSY != 0);        //  Pause while the clock switch isbusy.
}

void InitialiseUART()
{
    unsigned char tmp = USART1_SR;
    tmp = USART1_DR;
    USART1_CR1 = 0;
    USART1_CR2 = 0;
    USART1_CR4 = 0;
    USART1_CR3 = 0;
    USART1_CR5 = 0;
    USART1_GTR = 0;
    USART1_PSCR = 0;
    USART1_CR1_M = 0;        //  8 Data bits.
    USART1_CR1_PCEN = 0;     //  Disable parity.
    USART1_CR3_STOP0 = 0;     //  1 stop bit.
    USART1_BRR2 = 0x0a;      //  Set the baud rate registers to 115200 baud
    USART1_BRR1 = 0x08;      //  based upon a 16 MHz system clock.
    USART1_CR2_TEN = 0;      //  Disable transmit.
    USART1_CR2_REN = 0;      //  Disable receive.
    USART1_CR3_CPOL = 1;
    USART1_CR3_CPHA = 1;
    USART1_CR3_LBCL = 1;
    USART1_CR2_TEN = 1;
    USART1_CR2_REN = 1;
    USART1_CR3_CLKEN = 1;
}

void UARTPrintF(char *message)
{
    char *ch = message;
    while (*ch)
    {
        USART1_DR = (unsigned char) *ch;     //  Put the next character into the data transmission register.
        while (USART1_SR_TXE == 0);          //  Wait for transmission to complete.
        ch++;                               //  Grab the next character.
    }
}

void delay(long n)
{
    while (n-- > 0);
}

unsigned char RecUART1(void)
{
    while (!(USART1_SR_RXNE));    // USART_SR[5]:RXNE   Read data register not empty
    PA_ODR |= (1<<2);                                      //   0: Data is not received, 1: Received data is ready to be read.
    return USART1_DR;
}

int main( void )
{
    PA_DDR = 00000100;// TX on PA2
    PA_CR1 = 00000100;
    //out ='l';
    __disable_interrupt();
    InitialiseSystemClock();
    InitialiseUART();
    __enable_interrupt();
    while (1)
    {
        //out = RecUART1();
    PB_ODR = 0; //Turn off all pins
    PB_DDR_DDR4 = 1; //PortB, Bit 4 is output (PD4 - Data Direction Register)
    PB_CR1_C14 = 1; //PortB, Control Register 1, Bit 4 (PD4) set to Push-Pull
    PB_CR2_C24 = 1; //PortB, Control Register 2, Bit 4 (PD4) set to Push-Pull
    PB_ODR = 0x10;
    delay(30000);
        //for (long counter = 0; counter < 250000; counter++);
        //sprintf(dis,"this is %c\n",out);
     char sz[] = "Hello from STM8 \n\r";
     char *z = &sz[0];
     UARTPrintF(z);
     PB_ODR_bit.ODR4 = !PB_ODR_bit.ODR4;
     delay(30000);
    }
}

Relevant part of the schematic: enter image description here

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Both pins are possibilities for UART1, you will probably need to set an appropriate alternate function configuration for the pin you want, and perhaps unset a default (or inherited from bootloader?) configuration for the pin you do not want. Read up on the alternate function configuration section / register of the GPIO block. \$\endgroup\$ Dec 4, 2019 at 21:40

1 Answer 1

0
\$\begingroup\$

reference manual

Thanks to @Chris Stratton, if anybody has the same problem here is the answer

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.