通过数码管显示数字变化,首先需要将跳线帽拔掉
进入syscfg进行初始化GPIO管脚和定时器,分别配置
PA27==>HC595_DAT,
PA26==>HC595_SLK,
PA13==>HC595_RLK,
在代码中编写HC595驱动函数:
uint16_t tempval =456; uint8_t TimerCnt = 0 ; unsigned char Disp_DX[16] ={ 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x8C, 0xBF, 0xC6, 0xA1, 0x86, 0xFF }; unsigned char Disp_PX[8] = {1,2,3,4,5,6,7,8}; #define HC595_DAT(x) ((x)?(DL_GPIO_setPins(GPIOA_HC595_PORT,GPIOA_HC595_HC595_DAT_PIN)) : (DL_GPIO_clearPins(GPIOA_HC595_PORT,GPIOA_HC595_HC595_DAT_PIN))) #define HC595_CLK(x) ((x)?(DL_GPIO_setPins(GPIOA_HC595_PORT,GPIOA_HC595_HC595_SLK_PIN)) : (DL_GPIO_clearPins(GPIOA_HC595_PORT,GPIOA_HC595_HC595_SLK_PIN))) #define HC595_RCK(x) ((x)?(DL_GPIO_setPins(GPIOA_HC595_PORT,GPIOA_HC595_HC595_RLK_PIN)) : (DL_GPIO_clearPins(GPIOA_HC595_PORT,GPIOA_HC595_HC595_RLK_PIN))) void Display_0ut() { HC595_RCK(0); delay_cycles(32); HC595_RCK(1); delay_cycles(32); } void HC595_WriteData(unsigned char data) { unsigned char 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 HC595_Send_Data(uint8_t dis_num,uint8_t dis_bit) { HC595_WriteData(dis_num); HC595_WriteData(1<<dis_bit); Display_0ut(); } void Disp_Data(uint16_t u16DataH) { uint16_t templ,tempH; uint8_t num_q,num_b,num_s,num_g; tempH = u16DataH; num_q = tempH/1000; num_b = tempH/100%10; num_s = tempH/10%10; num_g = tempH%10; HC595_Send_Data(Disp_DX[num_q],7); HC595_Send_Data(Disp_DX[num_b],6); HC595_Send_Data(Disp_DX[num_s],5); HC595_Send_Data(Disp_DX[num_g],4); } 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; } }
在主函数中调用: