#include <reg51.h>
typedef unsigned char uchar;
typedef unsigned int uint;
//lcd 管脚口定义
#define LCD1602_DATAPINS P0
//开拓者
sbit LCD1602_E=P2^7;
sbit LCD1602_RW=P2^5;
sbit LCD1602_RS=P2^6;
sbit cgq = P3^0;
sbit beep = P1^5;
////自己焊的板子
//sbit LCD1602_RS = P1^0;
//sbit LCD1602_RW = P1^1;
//sbit LCD1602_E = P1^2;
//sbit cgq = P2^1;
//sbit beep = P1^4;
uint count = 0; //计数值
uint a = 0;
static uint p = 0; //中断标号
uint flag = 0,m = 0; //中断标志位
uint num1[] = {'0','1','2','3','4','5','6','7','8','9'}; //数字
uchar line1[] = " **Counter** "; //首行显示字符
uint num2[16]; //存储数值
uchar blank[] = " "; //存储16个空格
void delay_1ms(uint x)
{
uint y;
for(;x > 0;x--)
for(y = 110;y > 0;y--);
}
void Lcd1602_Delay1ms(uint c) //误差 0us
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
void LcdWriteCom(uchar com) //写入命令
{
LCD1602_E = 0; //使能
LCD1602_RS = 0; //选择发送命令
LCD1602_RW = 0; //选择写入
LCD1602_DATAPINS = com; //放入命令
Lcd1602_Delay1ms(1); //等待数据稳定
LCD1602_E = 1; //写入时序
Lcd1602_Delay1ms(5); //保持时间
LCD1602_E = 0;
}
void LcdWriteData(uchar dat) //写入数据
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 1; //选择输入数据
LCD1602_RW = 0; //选择写入
LCD1602_DATAPINS = dat; //写入数据
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //写入时序
Lcd1602_Delay1ms(5); //保持时间
LCD1602_E = 0;
}
void LcdDisplay(uint num)
{
uint i;
num2[15-2] = num/100; //百位
if(num > 99)
num2[15-1] = num/10%10;
else
num2[15-1] = num/10; //十位
num2[15] = num%10; //个位
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //设置数据指针起点
for(i=0;i<16;i++)
{
LcdWriteData(line1[i]);
}
LcdWriteCom(0x80+0x40); //光标移动到第二行
for(i=0;i<16;i++)
{
if(i < 13)
LcdWriteData(blank[i]);
else
LcdWriteData(num1[num2[i]]);
}
}
void LcdInit() //LCD初始化子程序
{
LcdWriteCom(0x38); //开显示
LcdWriteCom(0x0c); //开显示不显示光标
LcdWriteCom(0x06); //写一个指针加1
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //设置数据指针起点
}
//传感器计数
void CgqCounter()
{
if(cgq == 0)
{
delay_1ms(20);
if(cgq == 0)
{
while((!cgq));
count++;
LcdDisplay(count);
}
}
}
void Timer0_Init()
{
TMOD |= 0x01;
TH0 = 0xfc;
TL0 = 0x18;
EA = 1;
ET0 = 1;
TR0 = 0;
}
void main()
{
Timer0_Init();
LcdInit();
LcdDisplay(count);
while(1)
{
CgqCounter();
if((count % 12 == 0) && (count >= 12) && (m != count))
{
flag = 1;
TR0 = 1;
}
}
}
void Timer0() interrupt 1
{
TH0 = 0xfc;
TL0 = 0x18;
p++;
if(flag == 1)
{
beep = !beep;
if(p == 2000)
{
TR0 = 0;
p = 0;
flag = 0;
m = count;
}
}
}
|