这篇测试下定时器的使用,使用定时器1动态驱动LED数码管。
一、硬件电路
1.1、数码管的码段使用74HC245驱动
1.2、片选使用74HC138驱动
1.3、开发板使用这些端口,PA-PA7对应码段,PB4-PB6对应片选
二、程序
2.1、定时器1程序
定时器1配置为1ms,定时中断执行数码管驱动程序
#include "gd32f3x0.h"
#include "gd32f310g_start.h"
#include "systick.h"
void init_timer(void)
{
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER1);
timer_deinit(TIMER1);
timer_initpara.prescaler = 71;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER1, &timer_initpara);
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(TIMER1_IRQn,0,1);
timer_interrupt_enable(TIMER1,TIMER_INT_UP);
timer_auto_reload_shadow_enable(TIMER1);
timer_enable(TIMER1);
}
2.2、定时器中断
uint16_t timer_cnt=0;
void TIMER1_IRQHandler(void)
{
timer_flag_clear(TIMER1,TIMER_FLAG_UP);
timer_cnt++;
//if(timer_cnt>1)
{
timer_cnt=0;
//leds_test();
//led1_tog();
leddisp();
}
}
2.3、数码管驱动程序
在定时器1中断执行,动态扫描驱动LED数码管。
#include "gd32f3x0.h"
uint8_t tim_disp_js=0;
uint8_t tim_cnt=0;
uint16_t js=0;
const uint8_t shumag[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
const uint8_t shumagd[] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
const uint8_t dispbit[8]={0x70,0x60,0x50,0x40,0x30,0x20,0x10,0x00};
uint8_t disp[8]={0,0,0,0,0,0,0,0};
void init_ledd(void)
{
uint8_t pt;
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);
pt=gpio_output_port_get(GPIOA);
gpio_port_write(GPIOA,(pt|0x00ff));
pt=gpio_output_port_get(GPIOB);
gpio_port_write(GPIOB,(pt|0x000f));
}
//leddisp bit
void led_write_disbit(uint8_t ds)
{
uint8_t pt;
pt=gpio_output_port_get(GPIOB);
gpio_port_write(GPIOB,(pt&0xFF0f)|dispbit[ds]);
}
//leddisp dat
void led_write_disdat(uint8_t ds)
{
uint8_t pt;
pt=gpio_output_port_get(GPIOA);
gpio_port_write(GPIOA,(pt&0xFF00)|shumag[ds]);
}
//leddisp datp
void led_write_disdatp(uint8_t ds)
{
uint8_t pt;
pt=gpio_output_port_get(GPIOA);
gpio_port_write(GPIOA,(pt&0xFF00)|shumagd[ds]);
}
void leddisp(void)
{
tim_cnt++;
if(tim_cnt>100)
{
tim_cnt=0;
js++;
if(js>65500)
{
js=0;
}
disp[4]=js/10000;
disp[3]=(js%10000)/1000;
disp[2]=((js%10000)%1000)/100;
disp[1]=(((js%10000)%1000)%100)/10;
disp[0]=(((js%10000)%1000)%100)%10;
}
tim_disp_js++;
if(tim_disp_js>7)
{
tim_disp_js=0;
}
led_write_disdat(10);
led_write_disbit(tim_disp_js);
switch (tim_disp_js)
{
case 0:
led_write_disdat(disp[0]);
break;
case 1:
led_write_disdat(disp[1]);
break;
case 2:
led_write_disdat(disp[2]);
break;
case 3:
led_write_disdat(disp[3]);
break;
case 4:
led_write_disdat(disp[4]);
break;
case 5:
led_write_disdat(disp[5]);
break;
case 6:
led_write_disdat(disp[6]);
break;
case 7:
led_write_disdat(disp[7]);
break;
default:
break;
}
}
四、运行