|
近日做一個小項目,要用到3位雙色數(shù)碼管。馬云家雙色數(shù)碼管品種不多,受客觀條件限制選擇一款0.8吋雙色數(shù)碼管,其產(chǎn)品只有共陽沒有陰。3位雙色數(shù)碼管按6位算選擇驅(qū)動芯片TM1639。硬件電路設(shè)計好后著手寫代碼。此時發(fā)現(xiàn)TM1639驅(qū)動共陽數(shù)碼管有點麻煩,遂想在網(wǎng)上搜索案例參考。不料TM1639驅(qū)動共陽數(shù)碼管案例極其匱乏。天微公司網(wǎng)站也不提供示例代碼。只得根據(jù)產(chǎn)品手冊自己寫驅(qū)動代碼。項目現(xiàn)已完成。經(jīng)整理將TM1639驅(qū)動共陽數(shù)碼管的C代碼選出寫一個示例以饗壇友。
- //測試MCU型號:STC8G1K08A-8PIN
- #include <STC8G.H>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit DIO=P3^3;//數(shù)據(jù)口
- sbit CLK=P5^4;//時鐘口
- sbit SBT=P5^5;//片選口
- uchar code table[]={//共陰數(shù)碼管段碼"0~f-."
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71,0x40,0x80};
- uchar data dis_buf[8]; //數(shù)碼管緩存數(shù)組
- uchar data doc_buf[3]; //數(shù)據(jù)分解緩存數(shù)組
- uchar bright=5;//亮度0~7
- uchar Key_value;//鍵值
- uchar num; //計數(shù)
- uint count;//計數(shù)
- void Delay1us(uchar i)//延時函數(shù)us @12.000MHz
- {
- i*= 2;
- while(--i);
- }
- void Delay_ms(uint t)//1T@12MHz
- {
- uint i,j;
- for(i=t;i>0;i--)
- for(j=1200;j>0;j--);
- }
- void Write_TM1639_date(uchar date)//寫數(shù)據(jù)到TM1639,由低位到高位
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- CLK=0;
- if((date & 0x01) !=0)
- DIO=1;
- else
- DIO=0;
- _nop_();_nop_();
- CLK=1;
- date=date>>1;
- }
- }
- char Read_TM1639_date()//讀取TM1639數(shù)據(jù),由低位到高位
- {
- uchar i,date;
- DIO=1;
- for(i=0;i<8;i++)
- {
- CLK=0;
- date=date>>1;
- CLK=1; //送時鐘的上升沿
- if(!DIO)date=date & 0x7f;
- else date=date | 0x80;
- }
- return date;
- }
- /******************************************************/
- //指令格式
- //指令 空位 正常/測試模式 自加/固定地址 讀/寫模式
- // 01 00 0/1 0/1 10/00
- //
- //指令 空位 開/關(guān)顯示 亮度等級
- // 10 00 1/0 000/111
- //
- //指令 空位 顯示地址
- // 11 00 0000/1111
- //
- //指令 空位 數(shù)據(jù)低4位/ 高4位 先傳低4位,后傳高4位
- // 00 00 0000xxxx/0000xxxx x=有效數(shù)據(jù)
- /*******************************************************/
- void Init_TM1639()//初始化TM1639
- {
- uchar i;
- SBT=0;
- Write_TM1639_date(0x40);//普通模式、自增地址、寫模式
- SBT=1;
- SBT=0;
- Write_TM1639_date(0xc0);//顯示地址
- for(i=0;i<16;i++)//16個顯示寄存器的所有低4位有效,高4位無效
- Write_TM1639_date(0x00);//顯示內(nèi)容(清顯存)
- SBT=1;
- SBT=0;
- Write_TM1639_date(0x88+bright);//開顯示、0~7級亮度
- SBT=1;
- }
- char Scan_TM1639_Key()//掃描1639按鍵函數(shù)
- {
- uchar rekey,H_key,L_key;
- SBT=0;
- Write_TM1639_date(0x42); //寫入讀按鍵命令 0100 0010
- Delay1us(5);//小延時
- DIO=1;
- L_key=Read_TM1639_date();
- Delay1us(5);//小延時
- H_key=Read_TM1639_date();
- SBT=1;
- rekey=H_key<<4|L_key;//合并8位有效鍵值
- return rekey; //返回鍵值
- }
- void key_service() //按鍵服務(wù)函數(shù)
- {
- static uint time=0; //消抖計數(shù)變量
- static bit sign_Key=0; //按鍵狀態(tài)標(biāo)志
- uchar Key_value;
- Key_value=Scan_TM1639_Key(); //讀取鍵值
- if(Key_value==0x80||Key_value==0x08) //有按鍵按下
- {
- if(++time>=10 && sign_Key==0)//消抖
- {
- sign_Key=1; //按鍵狀態(tài)標(biāo)志置1
- if(Key_value==0x80)
- {
- if(bright<7)bright++; //亮度+
- }
- if(Key_value==0x08)
- {
- if(bright>0)bright--; //亮度-
- }
- SBT=0;
- Write_TM1639_date(0x88+bright);//刷新亮度設(shè)置
- SBT=1;
- }
- }
- else //松手
- {
- sign_Key=0; //按鍵狀態(tài)標(biāo)志清0
- time=0; //計數(shù)變量清0
- }
- }
- //用于共陰碼轉(zhuǎn)換為共陽管顯示 即 SEG接位選 GRID接段選。
- //共陰數(shù)碼管段碼的8個位拆解分別存入8個TM1639顯示寄存器的同一位。
- //所以要正確顯示需要8個8位二進(jìn)制的數(shù)中各取同一位來顯示一個數(shù)。
- void data_split(uchar s) //數(shù)據(jù)處理函數(shù)
- {
- uchar i,j;
- doc_buf[0]=table[s/100%10];//百位
- doc_buf[1]=table[s/10%10]; //十位
- doc_buf[2]=table[s%10]; //個位
- doc_buf[3]=table[0]; //顯示"0"
- doc_buf[4]=table[0];
- doc_buf[5]=table[0];
- doc_buf[6]=table[0];
- doc_buf[7]=table[0];
- for(j=0;j<8;j++)
- {
- for(i=0;i<8;i++)
- {
- dis_buf[i]|=((doc_buf[j]&0x01)<<j);
- doc_buf[j]>>=1;
- }
- }
- }
- void display_TM1639()//顯示函數(shù)
- {
- uchar i;
- SBT=0;
- Write_TM1639_date(0xc0);//顯示寄存器首地址
- for(i=0;i<8;i++)
- {
- Write_TM1639_date(dis_buf[i]&0x0f);//低位地址
- Write_TM1639_date(dis_buf[i]>>4&0x0f);//高位地址
- }
- SBT=1;
- }
- void main(void)
- {
- P3M0 = 0x00; //P3準(zhǔn)雙向
- P3M1 = 0x00;
- P5M0 = 0x00; //P5準(zhǔn)雙向
- P5M1 = 0x00;
- Init_TM1639(); //初始化TM1639
- while(1)
- {
- if(++count>=1000)//約1秒
- {
- count=0;
- num++;
- }
- data_split(num);//數(shù)據(jù)處理函數(shù)
- display_TM1639();//顯示函數(shù)
- key_service(); //讀鍵函數(shù)
- Delay_ms(1); //延時控制主循環(huán)周期約1ms
- }
- }
復(fù)制代碼
|
|