单片机常用代码大全

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1、位码(共阴):0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f
2、断码(共阳):0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
3、独立管码(共阳):0x02,0x9e,0x24,0x0c,0x98,0x48,0x40,0x1e,0x00,0x08,0x10,0xc0,0x62,0x84,0x60,0x70
4、循环函数intrins
_crol_字符循环左移
_cror_字符循环右移
_irol_整数循环左移
_iror_整数循环右移
_lror_长整数循环左移
_lror_长整数循环右移
_nop_空操作8051 NOP 指令
_testbit_测试并清零位8051 JBC 指令
5、中段代码
interrupt 0 外部中断0
interrupt 1 定时器0中断
interrupt 2 外部中断1
interrupt 3 定时器1中断
interrupt 4 串口中断
interrupt 5 定时器2中断(52里面,51没有)
6、IIC总线
开始信号:
void IICstart()
{
scl=1;
delay();
sda=1;
delay();
sda=0;
delay();
scl=0;
}
结束信号
void IICstop()
{
scl=1;
delay();
sda=0;
delay();
sda=1;
}
应答信号
void IICyingda()
{
uchar j;
scl=1;
delay();
while((sda==1)&&j<250)j++;
//sda=0;
//delay();
scl=0;
delay();
}
写信号(写入一8位数据)
void write_byte(uchar w)
{
uchar i,temp;
temp=w;
for(i=0;i<8;i++)
{
scl=0;
delay();
temp=temp<<1;
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=0;
delay();
}
读信号(读出一个8位数据)uchar read_byte(void)
{
uchar i,j,k;
sda=1;
for(i=0;i<8;i++)
{
scl=1;
delay();
j=sda;
k=(k<<1)|j;
scl=0;
delay();
}
scl=0;
delay();
return k;
}
7、时钟调试键盘扫描
void keyscan()
{
if(k1==0)
{
delay(5);
if(k1==0)
{
while(!k1); //
k1num++;
}
if(k1num==1)
{
TR0=0;
write_zhiling(0x80+0x40+11);
write_zhiling(0x0f);
}
if(k1num==2)
{
TR0=0;
write_zhiling(0x80+0x40+8);
}
if(k1num==3)
{
TR0=0;
write_zhiling(0x80+0x40+5);
}
if(k1num==4)
{
k1num=0;
write_zhiling(0x0c);
TR0=1;//
}
}
if(k1num!=0)
{
if(k2==0)
{
delay(5);
if(k2==0)
{
while(!k2);
if(k1num==1)
{
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
write_jingwei(7,fen);
}
write_jingwei(10,miao);
write_jingwei(7,fen);
write_zhiling(0x80+0x40+11);
}
write_jingwei(10,miao);
write_zhiling(0x80+0x40+11);
}
if(k1num==2)
{
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
write_jingwei(4,shi);
}
write_jingwei(7,fen);
write_jingwei(4,shi);
write_zhiling(0x80+0x40+8);
}
write_jingwei(7,fen);
write_zhiling(0x80+0x40+8);
}
if(k1num==3)
{
shi++;
if(shi==24)
{
shi=0;
write_jingwei(4,shi);
write_zhiling(0x80+0x40+5);
}
write_jingwei(4,shi);
write_zhiling(0x80+0x40+5);
}
}
}
}
if(k1num!=0)
{
if(k3==0)
{
delay(5);
if(k3==0)
{
while(!k3);
if(k1num==1)
{
miao--;
if(miao==-1)
{
miao=59;
fen--;
if(fen==-1)
{
fen=59;
write_jingwei(7,fen);
}
write_jingwei(10,miao);
write_jingwei(7,fen);
write_zhiling(0x80+0x40+11);
}
write_jingwei(10,miao);
write_zhiling(0x80+0x40+11); }
if(k1num==2)
{
fen--;
if(fen==-1)
{
fen=59;
shi--;
if(shi==-1)
{
shi=23;
write_jingwei(4,shi);
}
write_jingwei(7,fen);
write_jingwei(4,shi);
write_zhiling(0x80+0x40+8);
}
write_jingwei(7,fen);
write_zhiling(0x80+0x40+8); }
if(k1num==3)
{
shi--;
if(shi==-1)
{
shi=23;
write_jingwei(4,shi);
write_zhiling(0x80+0x40+5);
}
write_jingwei(4,shi);
write_zhiling(0x80+0x40+5);
}
}
}
}
}。

相关文档
最新文档