实现数码管数字递减
要注意
DAT
CLK
RCK
与硬件的接线
代码如下
#include "ti_msp_dl_config.h"
/* This results in approximately 0.5s of delay assuming 32MHz CPU_CLK */
#define DELAY (16000000)
uint8_t Disp_Dx[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x8c,0xbf,0xc6,0xa1,0x86,0xff};
uint8_t Disp_Px[8]={1,2,3,4,5,6,7,8};
uint16_t tempval =456;
uint8_t TimerCnt = 0 ;
#define HC595_DAT(x) ((x)?(DL_GPIO_setPins(GPIO_LEDS_PORT,GPIO_LEDS_DAT_PIN)):(DL_GPIO_clearPins(GPIO_LEDS_PORT,GPIO_LEDS_DAT_PIN)))
#define HC595_CLK(x) ((x)?(DL_GPIO_setPins(GPIO_LEDS_PORT,GPIO_LEDS_CLK_PIN)):(DL_GPIO_clearPins(GPIO_LEDS_PORT,GPIO_LEDS_CLK_PIN)))
#define HC595_RCK(x) ((x)?(DL_GPIO_setPins(GPIO_LEDS_PORT,GPIO_LEDS_RCK_PIN)):(DL_GPIO_clearPins(GPIO_LEDS_PORT,GPIO_LEDS_RCK_PIN)))
void display_out()
{
HC595_RCK(0);
delay_cycles(32);
HC595_RCK(1);
delay_cycles(32);
}
void hc595_writedata(uint8_t data)
{
uint8_t i;
for(i =0 ;i < 8; i++)
{
if(data&0x80)
{
HC595_DAT(1);
}
else
{
HC595_DAT(0);
}
data <<=1;
HC595_CLK(0);
delay_cycles(32);
HC595_CLK(1);
delay_cycles(32);
}
}
void hc_send_data(uint8_t dis_num,uint8_t dis_bit)
{
hc595_writedata(dis_num);
hc595_writedata(1<<dis_bit);
display_out();
}
void disp_data(uint16_t datal)
{
uint16_t templ;
uint8_t num_q,num_b,num_s,num_g;
templ = datal;
num_q = templ/1000;
num_b = templ/100%10;
num_s = templ/10%10;
num_g = templ%10;
hc_send_data(Disp_Dx[num_q],3);
hc_send_data(Disp_Dx[num_b],2);
hc_send_data(Disp_Dx[num_s],1);
hc_send_data(Disp_Dx[num_g],0);
}
int main(void)
{
/* Power on GPIO, initialize pins as digital outputs */
SYSCFG_DL_init();
NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);
while (1) {
disp_data(tempval);
}
}
void TIMER_0_INST_IRQHandler(void)
{
switch(DL_TimerG_getPendingInterrupt(TIMER_0_INST))
{
case DL_TIMER_IIDX_ZERO:
TimerCnt++;
if(TimerCnt >=100)
{
TimerCnt=0;
tempval--;
if(tempval>500)tempval=0;
}
break;
default:
break;
}
}
演示视频
https://www.bilibili.com/video/BV1PwhkehEeG/?vd_source=4f8f0092a0967d5a44cc4790809b35be