#include "ht32.h"
#include "usart.h"
#include "Delay.h"
#include "led.h"
#include "key.h"
#include "stdio.h"
#include "lcd.h"
#include "gui.h"
#include "test.h"
#include "Initerface.h"
#include "rtc.h"
#include "time.h"
#include "ht32_board_config.h"
#define BreatheMax 600 //定时器呼吸灯
uint8_t data[7] = {0};
uint16_t count = 0; //计时
uint8_t puff[4] = {0};
u8 sum = 0;
u32 succeed = 0;
u32 fail = 0;
u32 key_sum = 0;
u32 rx_data = 0;
typedef struct
{
u32 OverflowCounter;
u32 StartValue;
u32 CapturePulse;
TM_CHP_Enum ChannelPolarity;
bool DataValid;
bool DataOverwrite;
} sPulseCaptureStructure;
void Capture_Configuration(void);
void Capture_MainRoutine(void);
void Capture_IRQHandler(void);
void Capture_Process(sPulseCaptureStructure* cap, u16 capture_value, bool isCapBeforeUpdate);
void PWM_OUT_Configuration(void);
sPulseCaptureStructure CaptureCHx;
void Timer_Init(void)
{
CKCU_PeripClockConfig_TypeDef CKCUClock= {{0}};
CKCUClock.Bit.BFTM0 = 1; //开启中断时钟
CKCU_PeripClockConfig(CKCUClock, ENABLE);
NVIC_EnableIRQ(BFTM0_IRQn);
BFTM_SetCounter(HT_BFTM0, 0);
//BFTM_SetCompare(HT_BFTM1, SystemCoreClock);//定时1s产生中断
BFTM_SetCompare(HT_BFTM0, SystemCoreClock/1000);//定时1ms产生中断 呼吸灯1us中断1000000
BFTM_IntConfig(HT_BFTM0, ENABLE);//使能中断
BFTM_EnaCmd(HT_BFTM0, ENABLE);//使能BFTM
}
void BFTM0_IRQHandler(void)
{
if(BFTM_GetFlagStatus(HT_BFTM0) != RESET )
{
count++;
if(count == 500) //
{
count = 0;
}
//Capture_MainRoutine();
BFTM_ClearFlag(HT_BFTM0);//清除中断标志
}
}
void Capture_MainRoutine(void)
{
u32 Dval = 0;
u32 data = 9;
if (CaptureCHx.DataValid)
{
Dval = CaptureCHx.CapturePulse / (SystemCoreClock / 1000000ul);
//printf("Capture: %d %8.2f uS\r\n", CaptureCHx.CapturePulse, ((float)CaptureCHx.CapturePulse / (SystemCoreClock / 1000000ul)));
if(Dval>4200 && Dval<4700)
{
data = 3;
}
if(Dval>300 && Dval<800)
{
data = 0;
rx_data <<= 1;
rx_data |= 0;
sum++;
}
if(Dval>1400 && Dval<1800)
{
data = 1;
rx_data <<= 1;
rx_data |= 1;
sum++;
}
if(sum >= 32)
{
rx_data >>= 8;
rx_data &= 0x00FF;
key_sum++;
switch (rx_data)
{
case 0xA2:succeed++;printf("%d:%d + %d = %d%% RX:CH-\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x62:succeed++;printf("%d:%d + %d = %d%% RX:CH\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0xE2:succeed++;printf("%d:%d + %d = %d%% RX:CH+\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x22:succeed++;printf("%d:%d + %d = %d%% RX:|<<\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x02:succeed++;printf("%d:%d + %d = %d%% RX:>>|\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0xC2:succeed++;printf("%d:%d + %d = %d%% RX:>||\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0xE0:succeed++;printf("%d:%d + %d = %d%% RX:-\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0xA8:succeed++;printf("%d:%d + %d = %d%% RX:+\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x90:succeed++;printf("%d:%d + %d = %d%% RX:EQ\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x68:succeed++;printf("%d:%d + %d = %d%% RX:0\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x98:succeed++;printf("%d:%d + %d = %d%% RX:100+\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0xB0:succeed++;printf("%d:%d + %d = %d%% RX:200+\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x30:succeed++;printf("%d:%d + %d = %d%% RX:1\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x18:succeed++;printf("%d:%d + %d = %d%% RX:2\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x7A:succeed++;printf("%d:%d + %d = %d%% RX:3\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x10:succeed++;printf("%d:%d + %d = %d%% RX:4\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x38:succeed++;printf("%d:%d + %d = %d%% RX:5\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x5A:succeed++;printf("%d:%d + %d = %d%% RX:6\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x42:succeed++;printf("%d:%d + %d = %d%% RX:7\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x4A:succeed++;printf("%d:%d + %d = %d%% RX:8\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
case 0x52:succeed++;printf("%d:%d + %d = %d%% RX:9\r\n",key_sum,succeed,fail,(succeed*100)/key_sum);break;
default:fail++;printf("%d:%d + %d = %d%% RX:%X Error! Please reset the reboot!\r\n",key_sum,succeed,fail,(succeed*100)/key_sum,rx_data);break;
}
rx_data = 0;
sum = 0;
}
CaptureCHx.DataValid = FALSE;
}
}
/*********************************************************************************************************//**
* [url=home.php?mod=space&uid=247401]@brief[/url] Configures TM to capture waveform.
* @retval None
***********************************************************************************************************/
void Capture_Configuration(void)
{
{ /* Enable peripheral clock */
CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.HTCFG_CAP_IPN = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
}
/* Configure AFIO mode as TM function */
AFIO_GPxConfig(HTCFG_CAP_GPIO_ID, HTCFG_CAP_AFIO_PIN, AFIO_FUN_MCTM_GPTM);
{ /* Time base configuration */
/* !!! NOTICE !!!
Notice that the local variable (structure) did not have an initial value.
Please confirm that there are no missing members in the parameter settings below in this function.
*/
TM_TimeBaseInitTypeDef TimeBaseInit;
TimeBaseInit.Prescaler = 1 - 1; // Timer clock = CK_AHB / 1
TimeBaseInit.CounterReload = 0xFFFF;
TimeBaseInit.RepetitionCounter = 0;
TimeBaseInit.CounterMode = TM_CNT_MODE_UP;
TimeBaseInit.PSCReloadTime = TM_PSC_RLD_IMMEDIATE;
TM_TimeBaseInit(HTCFG_CAP_PORT, &TimeBaseInit);
/* Clear Update Event Interrupt flag since the "TM_TimeBaseInit()" writes the UEV1G bit */
TM_ClearFlag(HTCFG_CAP_PORT, TM_FLAG_UEV);
}
{ /* Channel n capture configuration */
/* !!! NOTICE !!!
Notice that the local variable (structure) did not have an initial value.
Please confirm that there are no missing members in the parameter settings below in this function.
*/
TM_CaptureInitTypeDef CapInit;
TM_CaptureStructInit(&CapInit);
CapInit.Channel = HTCFG_CAP_CH;
CapInit.Polarity = TM_CHP_NONINVERTED;
CapInit.Selection = TM_CHCCS_DIRECT;
CapInit.Prescaler = TM_CHPSC_OFF;
#if (LIBCFG_TM_652XX_V1)
CapInit.Fsampling = TM_CHFDIV_1;
CapInit.Event = TM_CHFEV_OFF;
#else
CapInit.Filter = 0x0;
#endif
TM_CaptureInit(HTCFG_CAP_PORT, &CapInit);
}
/* Enable TM Channel Capture and Update Event interrupts */
TM_IntConfig(HTCFG_CAP_PORT, HTCFG_CAP_CCR | TM_INT_UEV, ENABLE);
NVIC_EnableIRQ(HTCFG_CAP_IRQn);
TM_Cmd(HTCFG_CAP_PORT, ENABLE);
}
/*********************************************************************************************************//**
* @brief This function handles GPTM interrupt.
* @retval None
***********************************************************************************************************/
void HTCFG_CAP_IRQHandler(void)
{
bool update_flag = FALSE;
/* store and clear all interrupt flags */
u32 status = HTCFG_CAP_PORT->INTSR;
u32 cnt = HTCFG_CAP_PORT->CNTR;
#if 0
if ((status & TM_INT_UEV) != (HTCFG_CAP_PORT->INTSR & TM_INT_UEV))
{
status = HTCFG_CAP_PORT->INTSR;
cnt = HTCFG_CAP_PORT->CNTR;
}
#endif
HTCFG_CAP_PORT->INTSR = ~status;
if (status & TM_INT_UEV)
{
update_flag = TRUE;
/* The OverflowCounter will stop at max value 0xFFFF */
if (CaptureCHx.OverflowCounter != 0xFFFF)
CaptureCHx.OverflowCounter++;
}
if (status & HTCFG_CAP_CCR)
{
u32 cap_value = TM_GetCaptureCompare(HTCFG_CAP_PORT, HTCFG_CAP_CH);
bool isCapBeforeUpdate = (update_flag && (cap_value > cnt))? TRUE : FALSE;
Capture_Process(&CaptureCHx, cap_value, isCapBeforeUpdate);
TM_ChPolarityConfig(HTCFG_CAP_PORT, HTCFG_CAP_CH, CaptureCHx.ChannelPolarity);
}
}
/*********************************************************************************************************//**
* @brief Capture Process function.
* @retval None
************************************************************************************************************/
void Capture_Process(sPulseCaptureStructure* cap, u16 capture_value, bool isCapBeforeUpdate)
{
if (cap->ChannelPolarity == TM_CHP_NONINVERTED)
{
/* Reset OverflowCounter and store capture value when rising edge occurred */
if (isCapBeforeUpdate)
{
cap->OverflowCounter = 1;
}
else
{
cap->OverflowCounter = 0;
}
cap->StartValue = capture_value;
/* Change channel polarity to capture when falling edge occur */
cap->ChannelPolarity = TM_CHP_INVERTED;
}
else
{
/* Compute pulse width in PCLK unit when falling edge occurred */
if (isCapBeforeUpdate)
cap->OverflowCounter--;
cap->CapturePulse = (cap->OverflowCounter << 16) - cap->StartValue + capture_value + 1;
if (cap->DataValid)
cap->DataOverwrite = TRUE;
else
cap->DataValid = TRUE;
/* Change channel polarity to capture when rising edge occur */
cap->ChannelPolarity = TM_CHP_NONINVERTED;
}
}
/*********************************************************************************************************//**
* @brief Configures TM to output PWM waveform.
* @retval None
* [url=home.php?mod=space&uid=1543424]@Details[/url] Configuration as frequency 1 Hz and duty 2500 uS (10/4000).
***********************************************************************************************************/
void PWM_OUT_Configuration(void)
{
{ /* Enable peripheral clock */
CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.HTCFG_PWM_IPN = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
}
/* Configure AFIO mode as TM function */
AFIO_GPxConfig(HTCFG_PWM_GPIO_ID, HTCFG_PWM_AFIO_PIN, HTCFG_PWM_AFIO_FUN);
{ /* Time base configuration */
/* !!! NOTICE !!!
Notice that the local variable (structure) did not have an initial value.
Please confirm that there are no missing members in the parameter settings below in this function.
*/
TM_TimeBaseInitTypeDef TimeBaseInit;
TimeBaseInit.Prescaler = (SystemCoreClock / 4000) - 1; // Timer clock = 4 kHz
TimeBaseInit.CounterReload = 4000 - 1; // PWM frequency = 1 Hz
TimeBaseInit.RepetitionCounter = 0;
TimeBaseInit.CounterMode = TM_CNT_MODE_UP;
TimeBaseInit.PSCReloadTime = TM_PSC_RLD_IMMEDIATE;
TM_TimeBaseInit(HTCFG_PWM_PORT, &TimeBaseInit);
/* Clear Update Event Interrupt flag since the "TM_TimeBaseInit()" writes the UEV1G bit */
#if 0
TM_ClearFlag(HTCFG_PWM_PORT, TM_FLAG_UEV);
#endif
}
{ /* Channel n output configuration */
/* !!! NOTICE !!!
Notice that the local variable (structure) did not have an initial value.
Please confirm that there are no missing members in the parameter settings below in this function.
*/
TM_OutputInitTypeDef OutInit;
OutInit.Channel = HTCFG_PWM_CH;
OutInit.OutputMode = TM_OM_PWM2;
OutInit.Control = TM_CHCTL_ENABLE;
OutInit.ControlN = TM_CHCTL_DISABLE;
OutInit.Polarity = TM_CHP_NONINVERTED;
OutInit.PolarityN = TM_CHP_NONINVERTED;
OutInit.IdleState = MCTM_OIS_LOW;
OutInit.IdleStateN = MCTM_OIS_HIGH;
OutInit.Compare = 4000 - 10; // PWM duty = 10/4000 = 2500 us
OutInit.AsymmetricCompare = 0;
TM_OutputInit(HTCFG_PWM_PORT, &OutInit);
}
TM_Cmd(HTCFG_PWM_PORT, ENABLE);
}