|
- /***********74HC595動(dòng)態(tài)掃描1位數(shù)碼管************/
- //MCU:AT89S52RC
- //晶振:11.0592M.
- //采用1位數(shù)碼管
- /***************************************************/
- #include<reg51.h>
- #include <INTRINS.H>
- #define uint unsigned int
- #define uchar unsigned char
- #define nop() _nop_();_nop_();
- sbit CLK=P2^0; //74HC595的11引腳
- sbit DAT=P2^1; //74HC595的14引腳
- sbit RCLK=P2^2; //74HC595的12引腳
- //本程序支持1位共陽(yáng)和共陰數(shù)碼管可根據(jù)你的數(shù)碼管是共陰還是共陽(yáng),開(kāi)啟下面匹配的tab[]
- // 0 1 2 3 4 5 6 7 8 9 A B C D E F 全亮 全滅
- //uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00,0xff}; //共陽(yáng)數(shù)碼管 不顯示0xff 全亮0x00 只顯示點(diǎn)0x7f
- uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0xff,0x00}; //共陰數(shù)碼管 不顯示0x00 全亮0xff 只顯示點(diǎn)0x80
- uchar t;
- uchar count = 0;
- /* //延時(shí)函數(shù)
- void delay(uchar z)
- {
- uchar x,y;
- for(x=z;x>0;x--)
- for(y=120;y>0;y--);
- }
- */
- //寫(xiě)數(shù)碼管函數(shù)
- void write595(unsigned char word,uchar k1)
- {
- uchar num,c; //變量
- if(k1==1){ //如果k1的值為1,則點(diǎn)亮小數(shù)點(diǎn))
- num=tab[word]|0x80; //關(guān)鍵就在|0x80
- } else {
- num=tab[word];
- }
- //循環(huán)8個(gè)LED
- for(c=0;c<8;c++)
- {
- CLK=0;
- DAT=num&0x80;
- num=num<<1;
- CLK=1;
- }
- RCLK=0;
- //nop();
- //nop();
- RCLK=1;
- }
- void init() //啟動(dòng)定時(shí)器
- {
- TMOD=0X01;
- TH0=(65535-46080)/256;
- TL0=(65535-46080)%256;
- ET0=1;
- EA=1;
- TR0=1;
- }
- void T0_time() interrupt 1 //定時(shí)器
- {
- if (count == 20) {
- t++;
- count = 0;
- } else {
- ++count;
- }
- }
- int main(void) //主程序
- {
- //unsigned char i;
- init(); //啟動(dòng)定時(shí)器
- while(1)
- { //無(wú)限循環(huán)
- if(t>9) {t=0;} //當(dāng)t>9,則初始為0
- write595(t,0); //要寫(xiě)的值
- //for(i=0;i<10;i++) _nop_();
- }
- }
復(fù)制代碼 |
|