本帖最后由 tlled 于 2020-4-21 08:59 编辑
使用MSP432P401R驱动TM1638来点亮LED数码管。
一、硬件电路
1.1、LED驱动板电路图
1.2、MCU部分电路图
使用P2.5、P2.6、P2.7来驱动TM1638模块
二、驱动程序
2.1、main.c
#include "config.h"
/* Initializes Clock System */
void CS_init()
{
/* Set the core voltage level to VCORE1 */
MAP_PCM_setCoreVoltageLevel(PCM_VCORE1);
/* Set 2 flash wait states for Flash bank 0 and 1*/
MAP_FlashCtl_setWaitState(FLASH_BANK0, 2);
MAP_FlashCtl_setWaitState(FLASH_BANK1, 2);
/* Initializes Clock System */
MAP_CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_48);
MAP_CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
MAP_CS_initClockSignal(CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
MAP_CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
MAP_CS_initClockSignal(CS_ACLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1);
}
int main(void)
{
uint32_t i;
uint8_t dispdat[8];
uint32_t js=0;
// Stop watchdog timer
//WDT_A_hold(WDT_A_BASE);
MAP_WDT_A_holdTimer();
MAP_Interrupt_disableMaster();
CS_init();
//__delay_cycles(1000000);
LED_Init();
UART0_Init();
TM1638_Init();
//__delay_cycles(1000000);
for(i=0;i<8;i++)
{
dispdat[i]=0;
}
while(1)
{
UART0_transmitString( "www.21ic.com -- msp432p401r\r\n" );
for(i=100000; i>0; i--);
if(js>9999)
{
js=0;
}
js++;
dispdat[4]=js/1000;
dispdat[5]=(js%1000)/100;
dispdat[6]=((js%1000)%100)/10;
dispdat[7]=((js%1000)%100)%10;
disp_led(dispdat);
}
}
2.2、tm1638.c
#include "config.h"
uint8_t const tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
//writebyte
void tm1638_WriteDat(uint8_t dat)
{
uint8_t i;
for(i=0;i<8;i++)
{
clk_l();
if(dat&0X01)
{
dio_h();
}
else
{
dio_l();
}
dat>>=1;
clk_h();
}
}
void tm1638_Write_cmd(uint8_t cmd)
{
stb_l();
tm1638_WriteDat(cmd);
stb_h();
}
void tm1638_Write_Addr(uint8_t addr,uint8_t dat)
{
tm1638_Write_cmd(0x44);
stb_l();
tm1638_WriteDat(0xc0|addr);
tm1638_WriteDat(dat);
stb_h();
}
void tm1638_Write_LED(uint8_t LED_flag)
{
uint8_t i;
for(i=0;i<8;i++)
{
if(LED_flag&(1<<i))
{
tm1638_Write_Addr(2*i+1,1);
}
else
tm1638_Write_Addr(2*i+1,0);
}
}
void TM1638_GpioInit(void)
{
GPIO_setAsOutputPin( GPIO_PORT_P2, GPIO_PIN5 ); //P2.5
GPIO_setAsOutputPin( GPIO_PORT_P2, GPIO_PIN6 ); //P2.6
GPIO_setAsOutputPin( GPIO_PORT_P2, GPIO_PIN7 ); //P2.7
}
//TM1638 INIT
void TM1638_Init(void)
{
uint8_t i;
TM1638_GpioInit();
tm1638_Write_cmd(0x8b);
tm1638_Write_cmd(0x40);
stb_l();
tm1638_WriteDat(0xc0);
for(i=0;i<16;i++)
{
tm1638_WriteDat(0x00);
}
stb_h();
for(i=0;i<8;i++)
{
tm1638_Write_Addr(i<<1,tab[0]); //³õʼ»¯¼Ä´æÆ÷
}
}
void disp_led(uint8_t *dat)
{
uint8_t i;
for(i=0;i<8;i++)
{
tm1638_Write_Addr(i<<1,tab[dat[i]]);
}
}
1.3、tm1638.h
#ifndef TM1638_H_
#define TM1638_H_
#define dio_l() GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN5)
#define dio_h() GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN5)
#define clk_l() GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN6)
#define clk_h() GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN6)
#define stb_l() GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN7)
#define stb_h() GPIO_setOutputHighOnPin(GPIO_PORT_P2, GPIO_PIN7)
void TM1638_Init(void);
void disp_led(uint8_t *dat);
#endif
三、执行结果
数码管正常显示。
|