?? counter.c
字號:
/*****************************************************************/
/* */
/*名稱: 外部計數(shù)+顯示程序 */
/*功能: 單片機外部輸入脈沖信號,定時器參與計數(shù) */
/* 計數(shù)值由數(shù)碼管顯示出來。 */
/*難度等級: 中 */
/* 作者: zengrenliang */
/* 版權(quán): http://www.51kaifa.com */
/*編寫日期: 2006.05.19 */
/*****************************************************************/
#include<reg52.h>
#define uchar unsigned char
sbit addr0 = P1^4;//系統(tǒng)片選地址線0
sbit addr1 = P1^5;//系統(tǒng)片選地址線1
sbit addr2 = P1^6;//系統(tǒng)片選地址線2
sbit addr3 = P1^7;//系統(tǒng)片選地址線3
//行掃描數(shù)組
uchar code scan[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//row0--row7
//數(shù)碼管顯示的段碼表
uchar code table[18] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,//0,1,2,3,4,5
0x82,0xf8,0x80,0x90,0x88,0x83,//6,7,8,9,a,b
0xc6,0xa1,0x86,0x8e,0xbf,0xff};//c,d,e,f,-,空格
uchar dispbuf[8]; //顯示緩沖區(qū)
unsigned int count;
/*****************************延時函數(shù)**************************/
void delay (unsigned int us)
{
while(us--);
}
void initial()
{ uchar j;
count=0;//秒清零
for(j=0;j<6;j++)
dispbuf[j] = 0;//清數(shù)碼管顯示緩沖區(qū)
TMOD = 0x05;//T0 T1工作于方式1
TL0 = 0xff;
TH0 = 0xff;//50ms定時初值
ET0 = 1;//允許T1中斷
PT0 = 1;//T1中斷優(yōu)先級高
EA = 1;//開中斷
TR0 = 1;//T1開始計時
}
/*************************************填充顯示緩沖區(qū)****************************/
void feedbuffer()
{
unsigned int temp;
temp = count;
dispbuf[5] = temp % 10;//分個位
temp = temp / 10;
dispbuf[4] = temp % 10;//分十位
temp = temp / 10;
dispbuf[3] =temp % 10;//時百位
temp = temp / 10;
dispbuf[2] =temp % 10;//時千位
temp = temp / 10;
dispbuf[1] =temp % 10;//時萬位
temp = temp / 10;
dispbuf[0] =temp % 10;//時十萬位
}
/******************************顯示+讀鍵*************************/
void vLedKey_Scan()
{ unsigned char i,value;
for(i=0;i<8;i++){
addr3 = 0;
addr0 = 0;
addr1 = 1;
addr2 = 0;//開發(fā)板上U4(74HC574)的片選地址
value =table[dispbuf[i]]; //取一行顯示數(shù)據(jù)
P0 = value;
addr3 = 1;
addr3 = 0;//在U4的11腳(鎖存信號)產(chǎn)生上升沿
P2 = scan[i];//取row0--row7行掃描數(shù)據(jù)
delay(50); //延時50us
P2 = 0xff;//關(guān)顯示
}
}
/*******************************主函數(shù)***********************/
main()
{
initial();
while(1){
vLedKey_Scan(); //顯示,讀鍵掃描
}
}
//計數(shù)中斷服務(wù)
void int50ms() interrupt 1
{
TR0=0;//關(guān)定時器T0
TL0= 0xff;
TH0= 0xff;//賦定時初值
TR0=1;//開定時器T0
count++;//50ms計數(shù)加1
feedbuffer();
}
/********************************結(jié)束************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -