/*---------------------------------------------------------------------------------------------------------*/
/* */
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright(c) 2020 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include "ms51_16k.h"
#define STATE_DELAY_TIME 1000
#define RECEIVE_DATA_LENGTH 30
// Global variables
volatile uint16_t systemTick = 0;
uint8_t state = 0;
uint16_t stateFirstTime = 0;
uint16_t stateSecondTime = 0;
volatile uint8_t rxBuffer[RECEIVE_DATA_LENGTH];
volatile uint8_t index = 0;
/************************************************************************************************************/
/* PROTOTYPES */
/************************************************************************************************************/
void timer_1ms_init();
uint16_t millis();
void UART_Init(void);
void analyse_data(char*);
/************************************************************************************************************/
/* ISR FUNCTIONS */
/************************************************************************************************************/
void Timer2_ISR (void) interrupt 5 // Vector @ 0x2B
{
_push_(SFRS);
systemTick++;
clr_T2CON_TF2;
_pop_(SFRS);
}
void SerialPort1_ISR(void) interrupt 15
{
_push_(SFRS);
if (RI_1)
{
uart1_receive_data = SBUF_1;
if (index < RECEIVE_DATA_LENGTH - 1)
{
rxBuffer[index++] = uart1_receive_data;
if (uart1_receive_data == '\n')
{
rxBuffer[index] = '\0';
analyse_data(rxBuffer);
index = 0;
}
}
RI_1 = 0;
}
if (TI_1)
{
TI_1 = 0;
}
_pop_(SFRS);
}
/************************************************************************************************************/
/* FUNCTION_PURPOSE: Main Loop */
/************************************************************************************************************/
void main (void)
{
// Init functions
MODIFY_HIRC(HIRC_24);
Enable_UART0_VCOM_printf_24M_115200();
UART_Init();
timer_1ms_init(); // led blink on electronic board
P12_PUSHPULL_MODE;
P12 = 1;
ENABLE_GLOBAL_INTERRUPT;
while(1)
{
switch(state)
{
case 0:
{
state = 1;
}break;
case 1:
{
stateFirstTime = millis();
// printf("stateFirstTime = %u\r",stateFirstTime);
state = 2;
}
case 2:
{
stateSecondTime = millis();
// printf("stateSecondTime = %u\n",stateSecondTime);
if (stateSecondTime - stateFirstTime > STATE_DELAY_TIME)
{
P12 ^= 1;
state = 0;
}
}
}
}
}
/************************************************************************************************************/
/* TIMER RELATED FUNCTIONS */
/************************************************************************************************************/
void timer_1ms_init()
{
// Timer2_AutoReload_Interrupt_Initial(24,300000);
TIMER2_DIV_128;
TIMER2_Auto_Reload_Delay_Mode;
RCMP2L = TIMER_DIV128_VALUE_1ms_FOSC_24000000;
RCMP2H = TIMER_DIV128_VALUE_1ms_FOSC_24000000 >> 8;
TL2 = 0;
TH2 = 0;
set_ET2; // Set timerinterrupt
set_TR2; // Start timer
}
uint16_t millis()
{
return systemTick;
}
/************************************************************************************************************/
/* UART RELATED FUNCTIONS */
/************************************************************************************************************/
void UART_Init(void)
{
P16_QUASI_MODE;
P02_INPUT_MODE;
UART_Open(24000000, UART1_Timer3 , 9600);
ENABLE_UART1_INTERRUPT;
}
void analyse_data(char* buffer)
{
int i = 0;
while (buffer[i] != '\0')
{
printf("%X", buffer[i]);
i++;
}
printf("\r\n");
index = 0;
}