每周學(xué)習(xí)記錄
主函數(shù)
#include <REGX52.H>
#include "key.h"
#include "Delay.h"
#include "Nixie.h"
#include "Buzzer.h"
unsigned char KeyNum;
void main()
{
Nixie(1,0);
while (1)
{
KeyNum=Key();
if (KeyNum)
{
Buzzer_Time(2000);
Nixie(1,KeyNum);
}
}
}
延遲模塊
#ifndef _DELAY_H__
#define _DELAY_H__
void Delay(unsigned int xms);
#endif
#include <REGX52.H>
void Delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms--)
{
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
鍵盤輸入模塊
#ifndef _KEY_H__
#define _KEY_H__
unsigned char Key();
#endif
#include <REGX52.H>
#include "Delay.h"
unsigned char Key()
{
char KeyNumber=0;
if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;}
if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;}
if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;}
if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;}
return KeyNumber;
}
數(shù)碼管顯示
#ifndef __NIXIE_H__
#define __NIXIE_H__
void Nixie(unsigned char Location,Number);
#endif
#include <REGX52.H>
#include "Delay.h" //包含Delay頭文件
//數(shù)碼管段碼表
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
//數(shù)碼管顯示子函數(shù)
void Nixie(unsigned char Location,Number)
{
switch(Location) //位碼輸出
{
case 1:P2_4=1;P2_3=1;P2_2=1;break;
case 2:P2_4=1;P2_3=1;P2_2=0;break;
case 3:P2_4=1;P2_3=0;P2_2=1;break;
case 4:P2_4=1;P2_3=0;P2_2=0;break;
case 5:P2_4=0;P2_3=1;P2_2=1;break;
case 6:P2_4=0;P2_3=1;P2_2=0;break;
case 7:P2_4=0;P2_3=0;P2_2=1;break;
case 8:P2_4=0;P2_3=0;P2_2=0;break;
}
P0=NixieTable[Number]; //段碼輸出
// Delay(1); //顯示一段時(shí)間
// P0=0x00; //段碼清0,消影
}
蜂鳴器模塊
#ifndef _BUZZER_H__
#define _BUZZER_H__
void Buzzer_Time(unsigned int ms);
#endif
#include <REGX52.H>
#include "Delay.h"
#include <INTRINS.H>
sbit Buzzer=P1^5;
void Buzzer_Delay500us() //@12.000MHz
{
unsigned char i;
_nop_();
i = 247;
while (--i);
}
void Buzzer_Time(unsigned int ms)
{
unsigned int i;
for (i=0;i<ms*2;i++)
{
Buzzer=!Buzzer;
Buzzer_Delay500us();
}
}