cancel
Showing results for 
Search instead for 
Did you mean: 

STM8S103: UART Receive String Problem With Delay() in while(1)

Jaydip Patel
Associate
 
 
#include "stm8s.h"
#include "stdio.h"
 
 
 
#define Relay_1_Port  (GPIOB)
#define Relay_1_Pin  (GPIO_PIN_5)
#define Relay_1 '1'
 
#define LED_1_OFF_Port (GPIOC)
#define LED_1_OFF_Pin (GPIO_PIN_7)
 
 
#define Read_fun '0'
#define Write_fun '1'
 
#define HIGH '1'
#define LOW '0'
 
 
#define DEBUG_SERIAL_ECHO_BACK // fOR DEBUGGING
#define DEBUG_RECEIVED_FUNCTION_FROM_SERIAL
 
 
static void CLK_Config(void);
static void UART1_Config(void);
void Delay(uint32_t nCount);
char Serial_read_char(void);
void Serial_print_char (char value);
bool Serial_available();
void Serial_print_string (char string[]);
 
 
 
static char *cmd = "AURA-TC:" ;
static uint8_t cmd_cmpr = 0;
void main(void)
{
 
  CLK_Config();
 
  GPIO_Init(Relay_1_Port, (GPIO_Pin_TypeDef)Relay_1_Pin, GPIO_MODE_OUT_PP_LOW_FAST);
 
  GPIO_Init(LED_1_OFF_Port, (GPIO_Pin_TypeDef)LED_1_OFF_Pin, GPIO_MODE_OUT_PP_LOW_FAST);
 
 
 
  UART1_Config();
  Delay(100000);
  while (1)
  {
    
    if (Serial_available() > 0)
    {
      cmd_cmpr = 0; // RESET
      char data[20] = {0};
      for (int i = 0; i < 8; i++)
      {
        data[i] = Serial_read_char();
 
#ifdef DEBUG_SERIAL_ECHO_BACK
        Serial_print_char(data[i]);
#endif
 
        if (data[i] == *(cmd + i))
        {
          cmd_cmpr = 1;
        }
        else
        {
          cmd_cmpr = 0;
          break;
        }
 
      }
 
      char Read_write_fun  = Serial_read_char();
      char Relay_num       = Serial_read_char();
      char Write_High_Low = Serial_read_char();
 
#ifdef DEBUG_RECEIVED_FUNCTION_FROM_SERIAL
      Serial_print_string("\n----- Received Command code --------\n");
      Serial_print_char(Read_write_fun);
      Serial_print_char(Relay_num);
      Serial_print_char(Write_High_Low);
#endif
 
 
 
      if (cmd_cmpr)
      {
 
 
        if (Read_write_fun == Write_fun)
        {
          if (Relay_num == Relay_1)
          {
            if (Write_High_Low == HIGH)
            {
              GPIO_WriteLow(LED_1_OFF_Port, (GPIO_Pin_TypeDef)LED_1_OFF_Pin);
              GPIO_WriteHigh(Relay_1_Port, (GPIO_Pin_TypeDef)Relay_1_Pin);
            }
 
            if (Write_High_Low == LOW)
            {
              GPIO_WriteLow(Relay_1_Port, (GPIO_Pin_TypeDef)Relay_1_Pin);
              GPIO_WriteHigh(LED_1_OFF_Port, (GPIO_Pin_TypeDef)LED_1_OFF_Pin);
            }
 
          }
 
        }
 
 
      }
 
 
 
 
 
    }
 
   // ================= This is Problem ====================
   // Delay(10000); if putting this the serial receiving not working properly
   
  }
}
 
 
 
 
void Serial_print_string (char string[])
{
  while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET); //wait for sending
  char i = 0;
 
  while (string[i] != 0x00)
  {
    UART1_SendData8(string[i]);
    while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
    i++;
  }
}
 
 
 
 
char Serial_read_char(void)
{
 
  int read_count_var = 0;
  while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == 0)
  {
    read_count_var++;
    Delay(1);
    if (read_count_var >= 100) {
      break;
    }
 
 
  }
  while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
  UART1_ClearFlag(UART1_FLAG_RXNE);
 
  return (UART1_ReceiveData8());
 
}
 
void Serial_print_char (char value)
{
  UART1_SendData8(value);
  while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET); //wait for sending
}
 
 
bool Serial_available()
{
  if (UART1_GetFlagStatus(UART1_FLAG_RXNE) == 1)
    return TRUE;
  else
    return FALSE;
}
 
 
 
static void CLK_Config(void)
{
 
  CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
}
 
 
 
 
static void UART1_Config(void)
{
 
  UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
             UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
 
 
 
 
 
 
 
 
  UART1_Cmd(ENABLE);
 
 
}
 
 
void Delay(uint32_t nCount)
{
 
  while (nCount != 0)
  {
    nCount--;
  }
}
 
#ifdef USE_FULL_ASSERT
 
 
void assert_failed(uint8_t* file, uint32_t line)
{
 
  while (1)
  {
  }
}
#endif

0 REPLIES 0