2

I am new to stm8 (stm8s003f3p) I have done following code but could not able to find what is wrong with it. UART transmit is working but couldn't able to receive anything.

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

void InitialiseSystemClock()
{
    CLK_ICKR = 0;                       //  Reset the Internal Clock Register.
    CLK_ICKR_HSIEN = 1;                 //  Enable the HSI.
    CLK_ECKR = 0;                       //  Disable the external clock.
    while (CLK_ICKR_HSIRDY == 0);       //  Wait for the HSI to be ready for use.
    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_SWIMCCR = 0;                    //  Set SWIM to run at clock / 2.
    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 = UART1_SR;
    tmp = UART1_DR;
    UART1_CR1 = 0;
    UART1_CR2 = 0;
    UART1_CR4 = 0;
    UART1_CR3 = 0;
    UART1_CR5 = 0;
    UART1_GTR = 0;
    UART1_PSCR = 0;
    UART1_CR1_M = 0;        //  8 Data bits.
    UART1_CR1_PCEN = 0;     //  Disable parity.
    UART1_CR3_STOP = 0;     //  1 stop bit.
    UART1_BRR2 = 0x02;      //  Set the baud rate registers to 9600 baud
    UART1_BRR1 = 0x68;      //  based upon a 16 MHz system clock.
    UART1_CR2_TEN = 0;      //  Disable transmit.
    UART1_CR2_REN = 0;      //  Disable receive.
    UART1_CR3_CPOL = 1;
    UART1_CR3_CPHA = 1;
    UART1_CR3_LBCL = 1;
    UART1_CR2_TEN = 1;
    UART1_CR2_REN = 1;
    UART1_CR3_CKEN = 1;
}

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

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

int main( void )
{
    unsigned char out;
    char dis[25];
    // PD_DDR = (1<<5)|(1<<2);
    //PD_CR1 |= (1<<2);
    //*out ='l';
    //in=out;
    __disable_interrupt();
    InitialiseSystemClock();
    InitialiseUART();
    __enable_interrupt();
    while (1)
    {
        out = RecUART1();

        //for (long counter = 0; counter < 250000; counter++);
        sprintf(dis,"this is %c\n",out);
        UARTPrintF(dis);
    }
}
Bence Kaulics
  • 6,353
  • 12
  • 33
  • 60
yogesh singh
  • 65
  • 1
  • 8
  • Not working means Is it getting stuck in `while (!(UART1_SR_RXNE)); `? – Swanand Oct 30 '15 at 08:36
  • No it is not getting stuck there. Instead the value on UART DATA register is not getting updated. – yogesh singh Oct 31 '15 at 06:40
  • 1
    Thank you for the question and the answer, that saved my life... I found that in offical reference manual (RM0016), page 81, chapter 9.1.2, there's a note reads: 'At startup the master clock source is automatically selected as HSI RC clock output divided by 8 (fHSI/8).'. Once that any character received or transmitted via UART was 0x00, after I add the codes from yogesh singh, the UART worked fine. The baud are calculated from fMatser (in BBR1 and BBR2), since the fMaster were divided by 8, the baud are incorrect in my case. – stephen chiang Jun 13 '20 at 15:08

1 Answers1

2

Problem get solved by commenting HSI

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

void InitialiseSystemClock()
{
 CLK_ICKR = 0;                       //  Reset the Internal Clock Register.
 //CLK_ICKR_HSIEN = 1;                 //  Enable the HSI.
 CLK_ECKR = 0;                       //  Disable the external clock.
 //while (CLK_ICKR_HSIRDY == 0);       //  Wait for the HSI to be ready for     use.
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_SWIMCCR = 0;                    //  Set SWIM to run at clock / 2.
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.
}

while rest of the program is same.

Could anybody explain me this. It worked but i don't know how and why?

yogesh singh
  • 65
  • 1
  • 8
  • 1
    You disabled the Internal Oscillator? and it worked? what kind of sorcery is this? – ammar.cma Nov 26 '15 at 07:42
  • I don't know maybe by default stm8s work at 16mhz and i didn't disabled it, i just didn't enable it . If its by default meant to run at 16mhz then it must be enable. I don't understand what problem occurred when it specifically enable it in my code. – yogesh singh Dec 11 '15 at 05:11
  • Okay. You commented the Internal Clock line, and disabled the external clock; so it took internal oscillator as default. – ammar.cma Dec 11 '15 at 11:28
  • I don't know for sure as nothing like this is written in datasheet or reference manual. – yogesh singh Dec 12 '15 at 09:08