【CH579M-R1】+ Turn on timer 0 to control LED flashing and refresh LCD time display
[Copy link]
In order to refresh the date and time displayed on the LCD at regular intervals, I started to use the SysTick timer to count, but I didn't know where the interrupt processing function was after searching for a long time. I only found a function in the CH57x_sys.c file to get the current count value of SysTick. I looked through the data sheet but still had no clue. I had to set timer 0, set the interrupt cycle to 100ms, then add 1 to the timing variable in the interrupt processing function, and make judgments in the main loop of the main function. The following figure shows the test process:
The LED on the left of the upper left corner is a breathing light, and the LED on the right flashes at a cycle of 1 second. Here is the animated effect:
Here is the code for the test:
/********************************** (C) COPYRIGHT *******************************
* File Name : Main.c
* Author : WCH
* Version :
* Date : 2020/09/10
* Description : 开发板测试
*******************************************************************************/
#include "CH57x_common.h"
#include "lcd_5110.h"
#include "ds1307.h"
UINT8 TxBuff[]="This is a tx exam\r\n";
UINT8 RxBuff[100];
UINT8 trigB;
UINT8 mode;
uint16_t year; //年
uint8_t week,month,day,hour,minute,second,DS_Buff[8];//月日时分秒
volatile UINT16 time,ms;
int main()
{
UINT8 i,dir,half,flag;
/* 配置串口1:先配置IO口模式,再配置串口 */
GPIOA_SetBits(GPIO_Pin_9);
GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeIN_PU); // RXD-配置上拉输入
GPIOA_ModeCfg(GPIO_Pin_9, GPIO_ModeOut_PP_5mA); // TXD-配置推挽输出,注意先让IO口输出高电平
UART1_DefInit();
// 测试串口发送字符串
UART1_SendString( TxBuff, sizeof(TxBuff) );
// 配置定时器0
TMR0_TimerInit( FREQ_SYS/10 ); // 设置定时时间 100ms
TMR0_ITCfg(ENABLE, TMR0_3_IT_CYC_END); // 开启中断
NVIC_EnableIRQ( TMR0_IRQn );
/* 配置LED和PWM */
GPIOA_ModeCfg(GPIO_Pin_6, GPIO_ModeOut_PP_5mA); // PA6 - PWM4(LED4)
GPIOB_ModeCfg(GPIO_Pin_6, GPIO_ModeOut_PP_5mA); // PB6 - LED3
PWMX_CLKCfg( 4 );
PWMX_CycleCfg( PWMX_Cycle_64 );
GPIOB_ModeCfg(GPIO_Pin_4, GPIO_ModeOut_PP_5mA); // PB4 - LED_G
GPIOB_ModeCfg(GPIO_Pin_7, GPIO_ModeOut_PP_5mA); // PB7 - LED_Y
GPIOB_InverseBits(4);
LCD_init(); //LCD5110初始化
display_main(); //显示主界面
DS1307_Init(); //初始化DS1307模块
DS1307_read_date();
// 中断方式:接收数据后发送出去
UART1_ByteTrigCfg( UART_7BYTE_TRIG );
trigB = 7;
UART1_INTCfg( ENABLE, RB_IER_RECV_RDY|RB_IER_LINE_STAT );
NVIC_EnableIRQ( UART1_IRQn );
while(1)
{
/* //串口测试 查询方式:接收数据后发送出去
len = UART1_RecvString(RxBuff);
if( len )
{
UART1_SendString( RxBuff, len );
}
*/
//PWM测试
if(dir){
i--;
if(i == 0){
dir = 0;
GPIOB_InverseBits(GPIO_Pin_4);
}
}
else{
i++;
if(i == 64){
dir = 1;
GPIOB_InverseBits(GPIO_Pin_7);
}
}
PWMX_ACTOUT( CH_PWM4, i, Low_Level, ENABLE);
// PWMX_ACTOUT( CH_PWM7, 64-i, Low_Level, ENABLE);
mDelaymS(10);
//LED3闪烁测试
time++;
if(ms > 4){ //半秒处理
ms = 0;
GPIOB_InverseBits(GPIO_Pin_6);
half++;
}
if(half>1){ //秒处理
second++;
if(second > 59){
second = 0;
DS1307_read_date();
}
display_time(hour,minute,second);
half = 0;
if(flag) //轮换显示月日或年份
display_year(year);
else
display_date(month,day);
flag++;
if(flag > 1)
flag = 0;
}
}
}
void UART1_IRQHandler(void)
{
UINT8 i;
switch( UART1_GetITFlag() )
{
case UART_II_LINE_STAT: // 线路状态错误
UART1_GetLinSTA();
break;
case UART_II_RECV_RDY: // 数据达到设置触发点
for(i=0; i!=trigB; i++)
{
RxBuff[i] = UART1_RecvByte();
UART1_SendByte(RxBuff[i]);
}
break;
case UART_II_RECV_TOUT: // 接收超时,暂时一帧数据接收完成
i = UART1_RecvString(RxBuff);
UART1_SendString( RxBuff, i );
break;
case UART_II_THR_EMPTY: // 发送缓存区空,可继续发送
break;
case UART_II_MODEM_CHG: // 只支持串口0
break;
default:
break;
}
}
void TMR0_IRQHandler( void ) // TMR0 定时中断
{
if( TMR0_GetITFlag( TMR0_3_IT_CYC_END ) )
{
TMR0_ClearITFlag( TMR0_3_IT_CYC_END ); // 清除中断标志
GPIOB_InverseBits( GPIO_Pin_3 );
}
ms++;
}
|