#include "ht32.h"
#include "ht32_board.h"
#include "ht32_board_config.h"
uint8_t DiapBuf[4]={0,0,0,0};
const u8 NumberChar[]={
/* a: Seg4(0x10), b: Seg7(0x80), c: Seg2(0x04), d: Seg5(0x20),
e: Seg3(0x08), f: Seg0(0x01), g: Seg6(0x40), dp: Seg1(0x02) */
0xBD, /* '0' = a+b+c+d+e+f = 0x10 + 0x80 + 0x04 + 0x20 + 0x08 + 0x01 = 0xBD */
0x84, /* '1' = b+c+ = 0x80 + 0x04 = 0x84 */
0xF8, /* '2' = a+b +d+e +g = 0x10 + 0x80 + 0x20 + 0x08 + + 0x40 = 0xF8 */
0xF4, /* '3' = a+b+c+d +g = 0x10 + 0x80 + 0x04 + 0x20 + + 0x40 = 0xF4 */
0xC5, /* '4' = b+c +f+g = 0x80 + 0x04 + 0x01 + 0x40 = 0xC5 */
0x75, /* '5' = a +c+d +f+g = 0x10 + 0x04 + 0x20 + 0x01 + 0x40 = 0x75 */
0x7D, /* '6' = a +c+d+e+f+g = 0x10 + 0x04 + 0x20 + 0x08 + 0x01 + 0x40 = 0x7D */
0x95, /* '7' = a+b+c +f = 0x10 + 0x80 + 0x04 + 0x01 = 0x95 */
0xFD, /* '8' = a+b+c+d+e+f+g = 0x10 + 0x80 + 0x04 + 0x20 + 0x08 + 0x01 + 0x40 = 0xFD */
0xF5, /* '9' = a+b+c+d +f+g = 0x10 + 0x80 + 0x04 + 0x20 + 0x01 + 0x40 = 0xF5 */
0x02 /* '.' = dp =
0x02 = 0x02 */
};
void LEDC_Configuration(void);
void GPIO_Configuration(void);
extern bool gIsUpdateChar;
void UxART_Configuration(void);
void UxART_RxTest(void);
uc8 *gURTx_Ptr;
vu32 gURTx_Length = 0;
u8 *gURRx_Ptr;
vu32 gURRx_Length = 0;
vu32 gIsTxFinished = FALSE;
u8 gRx_Buffer[10];
void LEDC_Disp(void)
{
LEDC_SetData(HTCFG_LEDC_COM_D1, DiapBuf[0]);
LEDC_SetData(HTCFG_LEDC_COM_D2, DiapBuf[1]);
LEDC_SetData(HTCFG_LEDC_COM_D3, DiapBuf[2]);
LEDC_SetData(HTCFG_LEDC_COM_D4, DiapBuf[3]);
}
int main(void)
{
HT32F_DVB_LEDInit(HT_LED1);
HT32F_DVB_LEDInit(HT_LED2);
UxART_Configuration();
GPIO_Configuration();
LEDC_Configuration();
gURRx_Ptr = gRx_Buffer;
while (1)
{
UxART_RxTest();
if(gIsUpdateChar)
{
LEDC_Disp();
gIsUpdateChar = FALSE;
}
}
}
void UxART_Configuration(void)
{
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.HTCFG_UART_RX_GPIO_CLK = 1;
CKCUClock.Bit.HTCFG_UART_IPN = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
GPIO_PullResistorConfig(HTCFG_UART_RX_GPIO_PORT, HTCFG_UART_RX_GPIO_PIN, GPIO_PR_UP);
AFIO_GPxConfig(HTCFG_UART_TX_GPIO_ID, HTCFG_UART_TX_AFIO_PIN, AFIO_FUN_USART_UART);
AFIO_GPxConfig(HTCFG_UART_RX_GPIO_ID, HTCFG_UART_RX_AFIO_PIN, AFIO_FUN_USART_UART);
{
USART_InitTypeDef USART_InitStructure = {0};
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WORDLENGTH_8B;
USART_InitStructure.USART_StopBits = USART_STOPBITS_1;
USART_InitStructure.USART_Parity = USART_PARITY_NO;
USART_InitStructure.USART_Mode = USART_MODE_NORMAL;
USART_Init(HTCFG_UART_PORT, &USART_InitStructure);
}
NVIC_EnableIRQ(HTCFG_UART_IRQn);
USART_IntConfig(HTCFG_UART_PORT, USART_INT_RXDR, ENABLE);
USART_TxCmd(HTCFG_UART_PORT, ENABLE);
USART_RxCmd(HTCFG_UART_PORT, ENABLE);
}
void LEDC_Configuration(void)
{
LEDC_InitTypeDef LEDC_InitStruct;
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
CKCUClock.Bit.LEDC = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
LEDC_InitStruct.LEDC_ClockSource = LEDC_SRC_LSI;
LEDC_InitStruct.LEDC_ClockPrescaler = HTCFG_LEDC_CLOCK_PRESCAL - 1;
LEDC_InitStruct.LEDC_DutyClockNumber = HTCFG_LEDC_DUTY_CLOCK_NUMBER;
LEDC_InitStruct.LEDC_COMxEN = HTCFG_LEDC_COM1EN | HTCFG_LEDC_COM2EN |\
HTCFG_LEDC_COM3EN | HTCFG_LEDC_COM4EN;
LEDC_InitStruct.LEDC_DeadTime = 0;
LEDC_Init(&LEDC_InitStruct);
LEDC_SetPolarityMode(HTCFG_LEDC_COM1POL | HTCFG_LEDC_COM2POL | HTCFG_LEDC_COM3POL | HTCFG_LEDC_COM4POL,
LEDC_SEG0POL | LEDC_SEG1POL | LEDC_SEG2POL | LEDC_SEG3POL |\
LEDC_SEG4POL | LEDC_SEG5POL | LEDC_SEG6POL | LEDC_SEG7POL,
COMMON_CATHODE);
LEDC_IntConfig(ENABLE);
NVIC_EnableIRQ(LEDC_IRQn);
LEDC_Cmd(ENABLE);
}
void GPIO_Configuration(void)
{
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
CKCUClock.Bit.AFIO = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(HTCFG_LEDC_SEG0_GPIO_ID, HTCFG_LEDC_SEG0_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG1_GPIO_ID, HTCFG_LEDC_SEG1_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG2_GPIO_ID, HTCFG_LEDC_SEG2_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG3_GPIO_ID, HTCFG_LEDC_SEG3_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG4_GPIO_ID, HTCFG_LEDC_SEG4_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG5_GPIO_ID, HTCFG_LEDC_SEG5_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG6_GPIO_ID, HTCFG_LEDC_SEG6_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG7_GPIO_ID, HTCFG_LEDC_SEG7_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_COM0_GPIO_ID, HTCFG_LEDC_COM0_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_COM1_GPIO_ID, HTCFG_LEDC_COM1_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_COM2_GPIO_ID, HTCFG_LEDC_COM2_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_COM3_GPIO_ID, HTCFG_LEDC_COM3_AFIO_PIN, AFIO_FUN_LEDC);
}
void UxART_RxTest(void)
{
if (gURRx_Length >= 6)
{
DiapBuf[0]=NumberChar[(gRx_Buffer[3])&0x0f];
DiapBuf[1]=NumberChar[(gRx_Buffer[4]>>4)&0x0f]|0x02;
DiapBuf[2]=NumberChar[(gRx_Buffer[4])&0x0f];
DiapBuf[3]=0xd1;
gURRx_Length = 0;
}
}