2021-07-15 02:42 AM
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "stdio.h"
/**
* @addtogroup UART1_HyperTerminal_Interrupt
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#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
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
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[]);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
static char *cmd = "ASDF-TC:" ;
static uint8_t cmd_cmpr = 0;
void main(void)
{
/* Clock configuration -----------------------------------------*/
CLK_Config();
GPIO_Init(Relay_1_Port, (GPIO_Pin_TypeDef)Relay_1_Pin, GPIO_MODE_OUT_PP_LOW_FAST);
Delay(100000);
GPIO_Init(LED_1_OFF_Port, (GPIO_Pin_TypeDef)LED_1_OFF_Pin, GPIO_MODE_OUT_PP_LOW_FAST);
Delay(100000);
/* UART1 configuration -----------------------------------------*/
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_WriteHigh(Relay_1_Port, (GPIO_Pin_TypeDef)Relay_1_Pin);
GPIO_WriteLow(LED_1_OFF_Port, (GPIO_Pin_TypeDef)LED_1_OFF_Pin); // this is not working
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 working
}
}
}
}
// Delay(100000); // If put this Delay then UART 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;
}
/**
* @brief Configure system clock to run at 16Mhz
* @param None
* @retval None
*/
static void CLK_Config(void)
{
/* Initialization of the clock */
/* Clock divider to HSI/1 */
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
}
/**
* @brief Configure LEDs available on the evaluation board
* @param None
* @retval None
*/
/**
* @brief Configure UART1 for the communication with HyperTerminal
* @param None
* @retval None
*/
static void UART1_Config(void)
{
/* EVAL COM (UART) configuration -----------------------------------------*/
/* USART configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- Odd parity
- Receive and transmit enabled
- UART Clock disabled
*/
UART1_Init((uint32_t)115200, UART1_WORDLENGTH_8D,UART1_STOPBITS_1, UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
/* Enable the UART Receive interrupt: this interrupt is generated when the UART
receive data register is not empty */
//UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
/* Enable the UART Transmit complete interrupt: this interrupt is generated
when the UART transmit Shift Register is empty */
//UART1_ITConfig(UART1_IT_TXE, ENABLE);
/* Enable UART */
UART1_Cmd(ENABLE);
/* Enable general interrupts */
// enableInterrupts();
}
/**
* @brief Delay.
* @param nCount
* @retval None
*/
void Delay(uint32_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
=================================================================
Command ASDF-TC:111 and ASDF-TC:110