0~65535
每隔1us计数加1
总共定时时间65535us
64535离计数器溢出差值1000,所以计时时间为1ms
#include <REGX51.H>
void inittimer0()
{
TMOD=0x01;//0000 0001
TF0=0;//SCON可位寻址,TF=1产生中断
TR0=1;//定时器启动
TL0=64535%256;//定时1ms
TH0=64536/256;
ET0=1;
EA=1;
PT0=0;
}
void main()
{
inittimer0();
while(1)
{
}
}
unsigned int T0count;
void timer0_isr() interrupt 1
{
TL0=64535%256;//得重新调用,
TH0=64536/256;
T0count++;
if(T0count>=1000)
{
T0count=0;
P1_0=~P1_0;
}
}
按键控制LED流水灯亮灭
#include <REGX51.H>
void delay(unsigned int t)
{
unsigned int i=0,j=0;
for(i=0;i<t;i++)
{
for(j=0;j<120;j++);
}
}
unsigned char key()
{
unsigned char keynumber=0;
if(P1_0==0)
{
delay(20);
while(P1_0==0);
delay(20);
keynumber=1;
}
if(P1_1==0)
{
delay(20);
while(P1_1==0);
delay(20);
keynumber=2;
}
if(P1_2==0)
{
delay(20);
while(P1_2==0);
delay(20);
keynumber=3;
}
if(P1_3==0)
{
delay(20);
while(P1_3==0);
delay(20);
keynumber=4;
}
return keynumber;
}
unsigned char keynum;
void main()
{
while(1)
{
keynum=key();
if(keynum!=0)
{
if(keynum==1){P3_3=~P3_3;}
if(keynum==2)P3_4=~P3_4;
if(keynum==3)P3_5=~P3_5;
if(keynum==4)P3_6=~P3_6;
}
}
}
unsigned char a=0x01;
a=_crol_(a,1); //0x02,左移(循环移位),移到0x08继续左移会回到最低位
#include <REGX51.H>
#include <INTRINS.H>
void delay(unsigned int t)
{
unsigned int i=0,j=0;
for(i=0;i<t;i++)
{
for(j=0;j<120;j++);
}
}
void inittimer0()
{
TMOD=0x01;//0000 0001
TF0=0;//SCON可位寻址,TF=1产生中断
TR0=1;//定时器启动
TL0=64535%256;//定时1ms
TH0=64536/256;
ET0=1;
EA=1;
PT0=0;
}
unsigned char key()
{
unsigned char keynumber;
if(P1_0==0)
{
delay(20);
while(P1_0==0);
delay(20);
keynumber=1;
}
return keynumber;
}
unsigned char keynum,ledmode;
void main()
{ P3=0xFE;
inittimer0();
while(1)
{
keynum=key();
if(keynum!=0)
{
if(keynum==1)
{
ledmode++;
if(ledmode>=2)ledmode=0;
}
}
}
}
unsigned int T0count;
void timer0_isr() interrupt 1
{
TL0=64535%256;//得重新调用,
TH0=64536/256;
T0count++;
if(T0count>=500)
{
T0count=0;
if(ledmode==0)
{
P3=_crol_(P3,1);
}
if(ledmode==1)
{
P3=_cror_(P3,1);
}
}
}
#include <REGX51.H>
sbit RS=P3^0; //**
sbit RW=P3^1; //**
sbit E=P3^2; //**
int count;
unsigned char sec,min,hour;
void delay(unsigned int t)
{
unsigned int i=0,j=0;
for(i=0;i<t;i++)
{
for(j=0;j<120;j++);
}
}
void writedat(unsigned char dat)
{
RS=1;
RW=0;
E=0;
P2=dat;
delay(5); //看情况要不要延时
E=1;
E=0;
}
void writecom(unsigned char com)
{
RS=0;
RW=0;
E=0;
P2=com;
delay(5);
E=1;
E=0;
}
void initlcd()
{
writecom(0x38);
writecom(0x0c);
writecom(0x06);
writecom(0x01);
}
void inittimer()
{
TMOD=0x01; //16位
TH0=0x3C; //定时50ms,计数20次,相当于1s(65536-50000=15536=3CB0)
TL0=0xB0;
ET0=1;
EA=1;
TR0=1;
}
/**
* @brief LCD1602设置光标位置
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @retval 无
*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
writecom(0x80|(Column-1));
}
else if(Line==2)
{
writecom(0x80|(Column-1+0x40));
}
}
/**
* @brief 在LCD1602指定位置上显示一个字符
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @param Char 要显示的字符
* @retval 无
*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
writedat(Char);
}
/**
* @brief 在LCD1602指定位置开始显示所给字符串
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串
* @retval 无
*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
writedat(String[i]);
}
}
/**
* @brief 返回值=X的Y次方
*/
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
/**
* @brief 在LCD1602指定位置开始显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~65535
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
writedat(Number/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:-32768~32767
* @param Length 要显示数字的长度,范围:1~5
* @retval 无
*/
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i;
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
writedat('+');
Number1=Number;
}
else
{
writedat('-');
Number1=-Number;
}
for(i=Length;i>0;i--)
{
writedat(Number1/LCD_Pow(10,i-1)%10+'0');
}
}
/**
* @brief 在LCD1602指定位置开始以十六进制显示所给数字
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~0xFFFF
* @param Length 要显示数字的长度,范围:1~4
* @retval 无
*/
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i,SingleNumber;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
SingleNumber=Number/LCD_Pow(16,i-1)%16;
if(SingleNumber<10)
{
writedat(SingleNumber+'0');
}
else
{
writedat(SingleNumber-10+'A');
}
}
}
void main()
{
initlcd();
inittimer();
LCD_ShowString(1,1,"Clock:");
LCD_ShowString(2,3,":");
LCD_ShowString(2,6,":");
while(1)
{
LCD_ShowNum(2,1,hour,2);
LCD_ShowNum(2,4,min,2);
LCD_ShowNum(2,7,sec,2);
}
}
void timer0_isr() interrupt 1 //**
{
TH0=(65535-500000)/256; //定时50ms,计数20次,相当于1s(65536-50000=15536=3CB0)
TL0=(65535-500000)%256;
count++;
if(count==20) //1s
{
sec=sec+1;
count=0;
}
if(sec==60)
{
min=min+1;
sec=0;
}
if(min==60)
{
hour=hour+1;
min=0;
}
if(hour==24)
{
hour=0;
}
}