⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8bit_led2.c

📁 学习C51时写的数码灯程序,提供给刚学单片机程序的朋友参考
💻 C
字号:
//8段数码管实验二
//功能:使用第一位,第二位数码管进行的99秒循环到计时。
#include <reg51.h>
#define const_time 200;   //100x5ms=500ms=0.5s
unsigned char buff_time;  //buffer for storing the counts
unsigned char data time;
sbit light0 = P1^0; //the light0 bit
sbit DAT164 =P0^6;  //the bit of HC164's data
sbit CLK164 =P0^7;  //the bit of HC164's clock
sbit onebit=P0^0;   //one of the "8bit led" 
sbit twobit=P0^1; 
bit LED_Buffer;
unsigned char data shiwei;
unsigned char data gewei;
unsigned char data wei;

unsigned char data LED;
unsigned char data index;
code unsigned char LEDMAP[] =  // define the table of the "8bit led" 
{
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E
};

void send164() //the function of send the data to the "74HC164"
{unsigned char data j;
for (j = 0; j < 8; j++) 
{if (LED & 0x80) DAT164 = 1; else DAT164 = 0;
      CLK164 = 1;
      CLK164 = 0;
      LED <<= 1;} 
}

void main() 
{ TMOD=0x01;   //set the timer's mode
  TH0=(65536-5000)/256; //fill the timer0's higher register 
  TL0=(65536-5000)%256; //fill the timer0's lower register
  IE=0x82;   //EA=1,IT0=1
  LED_Buffer=0;
  light0=0;
  buff_time=const_time;
  TR0=1;     //start the timer!
  index=9;
  shiwei=0;
  gewei=0;
  time=99;
  while(1){
  light0=LED_Buffer;
  }  
}


void T0Int() interrupt 1
{
 TH0=(65536-5000)/256; //fill the timer0's higher register 
 TL0=(65536-5000)%256; //fill the timer0's lower register 
 buff_time--;
 wei=buff_time%2;
 shiwei=time/10;
gewei=time%10;
 
 if (wei==1)
 {onebit=0;
  twobit=1;
  LED=LEDMAP[shiwei];
  send164();
  }
 else
 {onebit=1;
  twobit=0;
  LED=LEDMAP[gewei];
  send164();
  }

 if (buff_time==0)
{buff_time=const_time; // refill the buffer
 LED_Buffer=!LED_Buffer;  // opposite the LED display
 time--;
 if (time==0) time=99;
 }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -