扫描嘛,书上有基本原理的介绍,也有最基本的实现方法。去看看,理解了才好下手! //===================================== #include "main.h" #include "IO_Port.h" #include "18b20.h" //=====================================
unsigned char keyBuf[3],key; unsigned int KCountL,KCountH;
unsigned char event; unsigned char ClearScr;
#define NKDBONCE 5 #define NKPAUSE 5 #define NKCOUNT 5 //===================================== //函数声明 //-------- void Ram_Init(void); void IO_Init(void); void delay(unsigned int t); void onEventProc(void);
extern void CT2_Init(void); extern void CT1_Init(void); //===================================== code unsigned char NUM_TAB[]={NUM_0,NUM_1,NUM_2,NUM_3,NUM_4,NUM_5,NUM_6,NUM_7,NUM_8,NUM_9,NUM_A,NUM_b,NUM_C,NUM_d,NUM_E,NUM_F}; //#define _NUM_DEC 0x0B //#define _NUM_BLANK 0x0D
const unsigned char SCAN_Port[4]={0x80,0x40,0x20,0x10};
unsigned char myNum; unsigned char Led7s_Buf[4];
unsigned char Counter_T1,Counter_2ms,Counter_500ms,Counter_1Kms; unsigned char rCOM_S7LED,ucScanBIT; unsigned char rFlag_T_En;
/*-------------------------------*/ #define COUNTER_2MS 4 #define COUNTER_500MS 250 #define COUNTER_1KMS 2
/*-------------------------------*/ unsigned int uiHEX2BCD(unsigned int uintdata); /*主程序开始 ================================= //主程序 --------------------------------- */ void main(void) {
Ram_Init(); IO_Init(); //delay(20000);//上电延时,等待电源稳定
//lcdinit(); //clrscr();
rCOM_S7LED = 0xff; pCOM_S7LED = rCOM_S7LED; pDATA_S7LED = NUM_ALL; delay(50000);//上电延时,等待电源稳定
rCOM_S7LED = 0xf0; pCOM_S7LED = rCOM_S7LED; pDATA_S7LED = NUM_BLANK; delay(20000);
TemperatuerResult(); Led7s_Buf[0] = NUM_TAB[Temperature & 0x000f]; Led7s_Buf[1] = NUM_TAB[(Temperature & 0x00f0)>>4]; Led7s_Buf[2] = NUM_TAB[(Temperature & 0x0f00)>>8]; Led7s_Buf[3] = NUM_TAB[(Temperature & 0xf000)>>12];
CT2_Init(); CT1_Init(); EA = 1;
while(1) { if((Counter_1Kms&0x01)&&rFlag_T_En) { GetTemperature(); Temperature = uiHEX2BCD(Temperature); ET1 = 0; Led7s_Buf[0] = NUM_TAB[Temperature & 0x000f]; Led7s_Buf[1] = NUM_TAB[(Temperature & 0x00f0)>>4]; Led7s_Buf[2] = NUM_TAB[(Temperature & 0x0f00)>>8]; Led7s_Buf[3] = NUM_TAB[(Temperature & 0xf000)>>12]; Led7s_Buf[1] = Led7s_Buf[1]|SEG_P; rCOM_S7LED = rCOM_S7LED^0x01;
rFlag_T_En = 0; ET1 = 1; } //KeyScan(); //if(event) //{ // event = 0; onEventProc(); // key = 0; //} } }
//====================================== /* void KeyScan(void) { if(key) { event = EVENT_K_DOWN; //beepout(20); } else { key=0; } } */ //============================================ // //----------------------- void Ram_Init(void) { Counter_T1 = 0; myNum = 0; rCOM_S7LED = 0x00; ucScanBIT = 0; Led7s_Buf[0] = 0; Led7s_Buf[1] = 0; Led7s_Buf[2] = 0; Led7s_Buf[3] = 0; }
//============================================ // //----------------------- void IO_Init(void) { pDATA_S7LED = NUM_BLANK; pCOM_S7LED = rCOM_S7LED;
P1 = 0x00; P3 = 0x00; }
//============================================ // //----------------------- void delay(unsigned int t) { unsigned int i,j; for(i=t;i>0;i--) { j=0; j=1; j=2; } }
//============================================ // //----------------------- void onEventProc(void) {
} //=========================================== void INT_CT2_PROC(void) interrupt INT_CT2 using 1 {
}
//=========================================== //8位重装模式,间隔:250*2us = 500us //------------------------------------------- void INT_CT1_PROC(void) interrupt INT_CT1 using 1 {//500us间隔
P1_0 = ~P1_0; Counter_T1++; if(Counter_T1>=COUNTER_2MS) {//2ms间隔 Counter_T1 = 0; //处理... rCOM_S7LED = rCOM_S7LED&0x0F; //清除LED公共端口备份寄存器高位 pCOM_S7LED = rCOM_S7LED;//关闭公共端口,准备更新数据
pDATA_S7LED = Led7s_Buf[ucScanBIT]; rCOM_S7LED = rCOM_S7LED|SCAN_Port[ucScanBIT];//计算扫描端口 pCOM_S7LED = rCOM_S7LED;//输出扫描状态
ucScanBIT++; if(ucScanBIT >=4 ) { ucScanBIT = 0; } Counter_2ms++; if(Counter_2ms >= COUNTER_500MS) {//500ms间隔 Counter_2ms = 0; //处理... Counter_500ms++; if(Counter_500ms >= COUNTER_1KMS) {//1Kms间隔 Counter_500ms = 0; //处理... Counter_1Kms++; rFlag_T_En = 1;
} } } }
//=========================================== //unsigned int 16进制转换为BCD //------------------------------------------- unsigned int uiHEX2BCD(unsigned int uintdata) { unsigned int Tempr[5],i; for(i=0;i<5;i++) { Tempr=0; } if(uintdata>=10000) { Tempr[4]=uintdata/10000; uintdata=uintdata%10000; } if(uintdata>=1000) { Tempr[3]=uintdata/1000; uintdata=uintdata%1000; } if(uintdata>=100) { Tempr[2]=uintdata/100; uintdata=uintdata%100; }
Tempr[1]=uintdata/10; Tempr[0]=uintdata%10;
uintdata=(Tempr[3]<<12)+(Tempr[2]<<8)+(Tempr[1]<<4)+Tempr[0]; return uintdata;
} /**/ |