这节任务是用定时器中断实现数码管数值的递减。
借用网上的原理图,模块是共阳极,74HC595是一个8位串行输入、并行输出的位移缓存器:并行输出为三态输出。在SCK 的上升沿,串行数据由SDL输入到内部的8位位移缓存器,并由Q7'输出,而并行输出则是在LCK的上升沿将在8位位移缓存器的数据存入到8位并行输出缓存器。当串行数据输入端OE的控制信号为低使能时,并行输出端的输出值等于并行输出缓存器所存储的值。8位数码管模块一共五个接口,还是使用PA26、PA13、PA27接口,具体接线如下所示。
3V3 --VCC
PA26--SCLK
PA13--RCLK
PA27--DIO
GND--GND
按照连接图连接开发板和数码管模块
然后通过syscfg工具配置定时器。
ranhou
然后再配置GPIO口,和点灯类似,不再重复。
主代码如下:
#include "ti_msp_dl_config.h"
#include <math.h>
#define NUM_MAXLENGTH 8
#define delay_us 32
typedef struct{
uint32_t Num_start;
uint8_t Num_polarity;
uint8_t polarity_flag;
uint8_t func_mode;
uint8_t stopwatch_status;
}LED_Segment_Mode;
LED_Segment_Mode LS_Mode;
uint8_t Num_List[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0xFF,0xbf};
int32_t Num_Decrease=1;
void HC595_CLKRisingEdge_Config(GPIO_Regs* PORTx,uint32_t PINx,uint32_t t1,uint32_t t2){
DL_GPIO_clearPins(PORTx, PINx);
delay_cycles(delay_us*t1);
DL_GPIO_setPins(PORTx, PINx);
delay_cycles(delay_us*t2);
}
void LED_Segment_WriteByte(uint8_t byte){
for(uint8_t i=1;i<=8;i++){
if(byte&0x80){
DL_GPIO_setPins(PORT_HC595_PORT,PORT_HC595_PIN_HC595_DIO_PIN);
}
else
{
DL_GPIO_clearPins(PORT_HC595_PORT,PORT_HC595_PIN_HC595_DIO_PIN);
}
HC595_CLKRisingEdge_Config(PORT_HC595_PORT,PORT_HC595_PIN_HC595_SCLK_PIN,2,2);
byte<<=1;
}
}
void LED_Segment_Display_Num_One(uint8_t Num_one,uint8_t index){
// switch(Num_one){
// case 0:
// break;
// }
LED_Segment_WriteByte(Num_List[Num_one]);
LED_Segment_WriteByte(1<<index);
HC595_CLKRisingEdge_Config(PORT_HC595_PORT,PORT_HC595_PIN_HC595_RCLK_PIN,2,2);
}
void LED_Segment_Display_point(uint8_t Num_one,uint8_t index){
// switch(Num_one){
// case 0:
// break;
// }
LED_Segment_WriteByte(Num_one);
LED_Segment_WriteByte(1<<index);
HC595_CLKRisingEdge_Config(PORT_HC595_PORT,PORT_HC595_PIN_HC595_RCLK_PIN,2,2);
}
void Num_LED_Segment_Process(uint32_t Num){
uint8_t num_lsb=0;
uint8_t i;
for(i=0;i<NUM_MAXLENGTH;i++)
{
num_lsb=Num%10;
Num/=10;
LED_Segment_Display_Num_One(num_lsb,i);
if(Num==0)break;
}
if(LS_Mode.Num_polarity==0){
LED_Segment_WriteByte(0xbf);
LED_Segment_WriteByte(0x01<<(i+1));
HC595_CLKRisingEdge_Config(PORT_HC595_PORT,PORT_HC595_PIN_HC595_RCLK_PIN,2,2);
}
}
int main(void)
{
SYSCFG_DL_init();
NVIC_SetPriority(TIMER_0_INST_INT_IRQN, 2);
NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);
DL_TimerG_startCounter(TIMER_0_INST);
//
NVIC_SetPriority(TIMER_1_INST_INT_IRQN, 1);
NVIC_EnableIRQ(TIMER_1_INST_INT_IRQN);
DL_TimerG_startCounter(TIMER_1_INST);
NVIC_SetPriority(GPIO_MULTIPLE_GPIOA_INT_IRQN, 0);
NVIC_EnableIRQ(GPIO_MULTIPLE_GPIOA_INT_IRQN);
LS_Mode.Num_start=10;
LS_Mode.polarity_flag=1;
LS_Mode.Num_polarity=1;//默认显示正数
LS_Mode.func_mode=0;
LS_Mode.stopwatch_status=0;
while (1) {
delay_cycles(10);
// LED_Segment_Display_Num_One(0x7f,0x01<<5);
// LED_Segment_Display_point(0x7f,5);
// Num_LED_Segment_Process_Float(0.356,2);
}
}
void TIMER_0_INST_IRQHandler(void)
{
switch (DL_TimerG_getPendingInterrupt(TIMER_0_INST))
{
case DL_TIMER_IIDX_ZERO:
Num_LED_Segment_Process(Num_Decrease);
break;
default:
break;
}
}
void TIMER_1_INST_IRQHandler(void)
{
switch (DL_TimerG_getPendingInterrupt(TIMER_1_INST)) {
case DL_TIMER_IIDX_ZERO:
// DL_GPIO_togglePins(GPIO_LEDS_PORT,GPIO_LEDS_USER_LED_1_PIN | GPIO_LEDS_USER_TEST_PIN);
if(LS_Mode.func_mode==0){
if(LS_Mode.polarity_flag==1){
Num_Decrease--;
if(Num_Decrease<0)
Num_Decrease=LS_Mode.Num_start;
}
// else
// {
//
// if(LS_Mode.Num_polarity==1){
// Num_Decrease--;
// if(Num_Decrease<0)
// LS_Mode.Num_polarity=0;
// }
// else{
// Num_Decrease++;
// if(Num_Decrease==LS_Mode.Num_start)
// LS_Mode.Num_polarity=1;
// }
// }
}
else
{
if(LS_Mode.stopwatch_status==0)
Num_Decrease=0;
else if(LS_Mode.stopwatch_status==1)
Num_Decrease++;
else
{
}
}
break;
default:
break;
}
}
void GROUP1_IRQHandler(void)
{
switch (DL_GPIO_getPendingInterrupt(PORT_Key2_PORT)) {
case PORT_Key2_PIN_Key2_IIDX:
switch(LS_Mode.stopwatch_status){
case 0:
LS_Mode.stopwatch_status++;
break;
case 1:
LS_Mode.stopwatch_status++;
break;
case 2:
LS_Mode.stopwatch_status=0;
break;
default:
LS_Mode.stopwatch_status=0;
break;
}
break;
case PORT_Key1_PIN_Key1_IIDX:
DL_GPIO_togglePins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
if(LS_Mode.func_mode==0)
{
Num_Decrease=0;
LS_Mode.stopwatch_status=0;
LS_Mode.func_mode++;
}
else if(LS_Mode.func_mode==1)
{
LS_Mode.func_mode--;
Num_Decrease=LS_Mode.Num_start;
}
else
{
LS_Mode.func_mode=0;
Num_Decrease=LS_Mode.Num_start;
}
break;
default:
// DL_GPIO_togglePins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
break;
}
}
编译下载运行,结果如下: