#define CS PB13
#define WR PB14
#define DATA PB15
#define SYS_DIS 0x0 //关闭系统振荡器和 LCD偏压发生器
#define SYS_EN 0x2 //打开系统振荡器
#define LCD_OFF 0x4 //关闭 LCD 显示
#define LCD_ON 0x6 //开启 LCD 显示
#define WDT_DIS 0x10 //禁止看门狗
#define SC 0x30 //110000
#define uchar unsigned char
//输出数据
uint32_t TM1622Tab[16]={0x1e0,0x1e0,0x11e0,0x1e0,0x1e0,0x1e0,0x1e0,0x1e0,0x1e0,0x1e0,0x1e0,0x1e0,0x1e0,0x1e0,0x1e0,0x0};
uint32_t TM1622Tab_input[10]={0x23,0x243,0x223,0x222,0x2333,0x552,0x23,0x32,0x232,0x523};
//时延函数,单位us
void delay_t(uint32_t i){
int u32Delay = 6;
TIMER0->CMP = 12*i;
TIMER0->CTL = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE|0 ;
while(TIMER0->CTL & TIMER_CTL_ACTSTS_Msk);
for(; u32Delay > 0UL; u32Delay--)
{
__NOP();
}
while(TIMER0->CTL & TIMER_CTL_ACTSTS_Msk);
}
//时延函数,单位50ns
void delay_ns(uint32_t time){
int u32Delay=time;
for(; u32Delay > 0UL; u32Delay--)
{
__NOP();
}
}
//写数据
void TM1622Wr_Data(uint32_t Data,uint32_t cnt){
for(int i=0;i<cnt;i++){
DATA = (Data&0x100)>>8;
delay_t(1);
WR = 0;
delay_t(4);
WR = 1;
delay_t(3);
Data <<=1;
}
}
//输入具体控制命令函数
void TM1622WrCmd(uint32_t Cmd){
CS = 0;
delay_t(5);
TM1622Wr_Data(0x100,3);//输入命令类型100
TM1622Wr_Data(Cmd,9); //输入具体控制命令
CS = 1;
delay_ns(6);
}
//连续写入函数,Addr为写入地址,p为写入数据首地址
void TM1622WrALLData(uint32_t Addr,uint32_t *p,uint32_t cnt){
CS = 0;
delay_t(3);
TM1622Wr_Data(0x140,3);//输入命令类型101
TM1622Wr_Data(Addr<<3,6);
for(int i=0;i<cnt;i++){
TM1622Wr_Data(*p,4);
p++;
}
CS = 1;
delay_ns(6);
}
void TM1622WrOneData(uint32_t Addr,uint32_t Data)
{
CS=0;
TM1622Wr_Data(0x140,3); //写入数据标志101 ,10100000
TM1622Wr_Data(Addr<<3,6); //写入地址数据
TM1622Wr_Data(Data<<1,4); //写入数据
CS=1;
delay_ns(6);
}
void delay_ms(uint32_t t){
int k=1,j;
for(int i=0;i<t*10;i++) {
for(int j=0;j<65;j++) {
k--;
k++;
}
}
}
int main(void)
{
/* Init System, IP clock and multi-function I/O */
SYS_Init();
/* Init UART to 115200-8n1 for print message */
UART_Open(UART0, 115200);
GPIO_SetMode(PB, BIT13, GPIO_MODE_OUTPUT);//1,CS
GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT);//2,WR
GPIO_SetMode(PB, BIT15, GPIO_MODE_OUTPUT);//3,DATA
GPIO_SetMode(PB, BIT5, GPIO_MODE_OUTPUT);//4 BLK,3.3v电压输入,高电平亮
CS = 1;
WR = 1;
DATA = 1;
PB5 = 1;
delay_ms(100);
TM1622WrCmd(SC);
TM1622WrCmd(SYS_EN);
TM1622WrCmd(LCD_ON);
// TM1622WrCmd(0x80);//打开声音输出
//TM1622WrCmd(0x4);//关闭LCD显示器
TM1622WrCmd(0x146);//时基本输出频率:8Hz WDT 使能后输出延时:1/2 s
TM1622WrCmd(0xc);//使能时基输入
TM1622WrCmd(0x1e);//清除WDT状态
TM1622WrCmd(0xa);//WDT失效
TM1622WrCmd(0x1a);//时基发生器清零
TM1622WrCmd(0x1c6);//正常模式
delay_ms(10);
for(int i=0;i<=31;i++)
{
int t=0xff; //0xa0 10100000
TM1622WrOneData(i,t);
}
} |