#include <reg51.h>
#include <stdio.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="wck";
sbit RS=P3^0; //Òº¾§Êý¾ÝÖ¸ÁîÑ¡Ôñ¶Ë
sbit EN=P3^1; //Òº¾§Ê¹ÄܶË
sbit led=P2^2;
uchar count,count_time,sum;
void initial()
{
count=0;
sum=0;
count_time=0;
TMOD=0x02;
TH0=256-250;
TL0=256-250;
EA=1;
EX0=1;
ET0=1;
TR0=1;
IT0=0;
}
void time0() interrupt 1 using 1
{
count_time++;
}
void time1() interrupt 0
{
count++;
}
void counter_js()
{
if(count_time==4000)
{
sum=count;
count_time=0;
count=0;
}
}
void delayms(uint a) //ÑÓʱºÁÃë
{
uint x,y;
for(x=a;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)//дָÁî
{
EN=0;//¸ºÌø±ä¶ÁÈ¡ÐÅÏ¢
RS=0;//Ö¸Áî¼Ä´æÆ÷
P0=com;
delayms(1);
EN=1;//¶ÁÈ¡ÐÅÏ¢
delayms(1);
EN=0;
}
void write_data(uchar dat)//дÊý¾Ý
{
EN=0;
RS=1;//Êý¾Ý¼Ä´æÆ÷
P0=dat;
delayms(1);
EN=1;
delayms(1);
EN=0;
}
void init()
{
write_com(0x38);//ÉèÖÃ16x2ÏÔʾ£¬5X7µãÕó£¬8λÊý¾Ý½Ó¿Ú£¬
write_com(0x0c);//ÉèÖÿªÏÔʾ£¬²»ÏÔʾ¹â±ê
write_com(0x06);//дһ¸ö×Ö·ûºó£¬µØÖ·Ö¸Õë¼Ó1
write_com(0x01);//ÏÔʾÇå0£¬Êý¾ÝÖ¸ÕëÇå0
}
void main(void)
{
uchar i;
init();
initial();
counter_js();
write_com(0x80+0x20);//+0x10 µÚÒ»´ÎÐ޸Ĵ¦£¬µØÖ·ÓÉ0x80±äΪ0x80+0x10
write_data(sum);
delayms(5);
for(i=0;i<16;i++) //ÒÆÆÁ£¬ÏÔʾ
{
write_com(0x18);
delayms(100);
}
while (1)
{
led=0;
delayms(500);
led=1;
delayms(500);
}
}
此帖出自
小平头技术问答
楼主可以试试这一个例子
一周热门 更多>