設(shè)想:采用定時器,中斷觸發(fā)后通過串口發(fā)送一串hex出去。
代碼如下:
timer.h
#ifndef?TIMER_H #define?TIMER_H #include?"debug.h" //?Timer?configuration?structure typedef?struct?{ ????uint16_t?period;??????//?Auto-reload?value ????uint16_t?prescaler;???//?Clock?prescaler ????uint8_t?priority;?????//?Interrupt?priority ????uint8_t?sub_priority;?//?Interrupt?sub-priority }?Timer_Config_t; //?Initialize?Timer2?with?configuration void?Timer2_Init(Timer_Config_t?*config); //?Register?callback?for?Timer2?interrupt void?Timer2_RegisterCallback(void?(*callback)(void)); //?Start/Stop?Timer2 void?Timer2_Start(void); void?Timer2_Stop(void); #endif
timer.c
//?timer.c #include?"timer.h" static?void?(*timer2_callback)(void)?=?NULL; void?TIM2_IRQHandler(void)?__attribute__((interrupt("WCH-Interrupt-fast"))); void?Timer2_Init(Timer_Config_t?*config) { ????TIM_TimeBaseInitTypeDef?TIM_TimeBaseStructure?=?{0}; ????NVIC_InitTypeDef?NVIC_InitStructure?=?{0}; ????//?Enable?Timer2?clock ????RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,?ENABLE); ????//?Configure?Timer ????TIM_TimeBaseStructure.TIM_Period?=?config->period?-?1; ????TIM_TimeBaseStructure.TIM_Prescaler?=?config->prescaler?-?1; ????TIM_TimeBaseStructure.TIM_ClockDivision?=?TIM_CKD_DIV1; ????TIM_TimeBaseStructure.TIM_CounterMode?=?TIM_CounterMode_Up; ????TIM_TimeBaseInit(TIM2,?&TIM_TimeBaseStructure); ????//?Enable?Timer2?interrupt ????TIM_ITConfig(TIM2,?TIM_IT_Update,?ENABLE); ????//?Configure?NVIC ????NVIC_InitStructure.NVIC_IRQChannel?=?TIM2_IRQn; ????NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority?=?config->priority; ????NVIC_InitStructure.NVIC_IRQChannelSubPriority?=?config->sub_priority; ????NVIC_InitStructure.NVIC_IRQChannelCmd?=?ENABLE; ????NVIC_Init(&NVIC_InitStructure); } void?Timer2_RegisterCallback(void?(*callback)(void)) { ????timer2_callback?=?callback; } void?Timer2_Start(void) { ????TIM_Cmd(TIM2,?ENABLE); } void?Timer2_Stop(void) { ????TIM_Cmd(TIM2,?DISABLE); } void?TIM2_IRQHandler(void) { ????if?(TIM_GetITStatus(TIM2,?TIM_IT_Update)?!=?RESET) ????{ ????????TIM_ClearITPendingBit(TIM2,?TIM_IT_Update); ????????//?Execute?callback?if?registered ????????if?(timer2_callback?!=?NULL) ????????{ ????????????timer2_callback(); ????????} ????} }
main.c片段
//發(fā)送hex void?KeepAlive(void) { //?構(gòu)建返回數(shù)據(jù)結(jié)構(gòu) uint8_t?response[10]; uint8_t?address=0; response[0]?=?FRAME_HEAD; if?(rx_buffer[1]==0x00)?{ address?=?0x11; }?else?{ address?=?rx_buffer[1]; } response[1]?=?((address?&?0x70)?>>?4)?|?((address?&?0x07)?<<?4); response[2]?=?g_tm1650_data[0]; response[3]?=?g_tm1650_data[1]; response[4]?=?0x13; //當(dāng)前為固定值?g_tm1650_data[2]; response[5]?=?0x14; //當(dāng)前為固定值?g_tm1650_data[3]; response[6]?=?g_tm1804_data; response[7]?=?0x17; //當(dāng)前為固定值 //?計算校驗碼 uint8_t?resp_checksum?=?0; for?(int?i?=?1;?i?<=?7;?i++) { resp_checksum?^=?response[i]; } response[8]?=?resp_checksum; response[9]?=?FRAME_TAIL; //?發(fā)送返回數(shù)據(jù) for?(int?i?=?0;?i?<?10;?i++) { while(USART_GetFlagStatus(USART1,?USART_FLAG_TC)?==?RESET); USART_SendData(USART1,?response[i]); } } int?main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); SystemCoreClockUpdate(); Delay_Init(); //?配置1秒定時器 ????Timer_Config_t?timer_config?=?{ ????????.period?=?1000,??????????//?1ms周期 ????????.prescaler?=?48,?????????//?48分頻 ????????.priority?=?1,???????????//?優(yōu)先級1 ????????.sub_priority?=?0????????//?子優(yōu)先級0 ????}; ????//?初始化定時器 ????Timer2_Init(&timer_config); ????//?注冊回調(diào)函數(shù) ????Timer2_RegisterCallback(KeepAlive); ????//?啟動定時器 ????Timer2_Start(); while(1) { } }
當(dāng)前碰到的問題是:無論如何修改.period = 1000值,感覺觸發(fā)中斷的時間都沒有變化。
熱門產(chǎn)品 :
CH390:以太網(wǎng)控制器芯片