proteus软件仿真
部分文件原创~~
资料很全~~学习中各个过程的难题都有例子
文件头—————————————————————————————————————————必要
#include
#define uint unsigned int
#define uchar unsigned char
移位函数——————————————————————————————————————————走马灯
#include
temp=_cror_(temp,1);
数码管数字代码———————————————————————————————————————从0到15
正
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
反
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90};
延时函数——————————————————————————————————————————z毫秒
#define uint unsigned int
#define uchar unsigned char
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
定时器0使用——————————————————————————————————————工作方式1
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
}
定时器1使用———————————————————————————————————————工作方式1
TMOD=0x11;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET1=1;
TR1=1;
void time1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
}
外部中断0使用————————————————————————————————————————检测下降沿
EA=1;
EX0=1;
IT0=1;
TCON=0x01;
数码管显示数字函数——————————————————————————————————————只显示百十个,可以扩展
void display(uchar bai,uchar shi,uchar ge)
{
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[bai];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
dula=1;
P0=table[shi];
dula=0;
delay(1);
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[ge];
dula=0;
delay(1);
}
控制液晶—————————————————————————————————限16*2小液晶
sbit rs=P3^5;
sbit e= P3^4 ;
sbit wela=P2^7;
sbit dula=P2^6;
uchar love1[]={"i say love."};
uchar love2[]={"i love you."};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
int write_com(uchar com)
{
P0=com;
rs=0;
e=1;
delay(5);
e=0;
}
int write_date(uchar date)
{
P0=date;
rs=1;
e=1;
delay(5);
e=0;
}
init()
{
wela=0;
dula=0;
e=0;
write_com(0x38);
write_com(0x0e);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
串口通信——————————————————————————————————————————波特率 9600
TMOD=0x20;
TH1=0xfd; //9600
TL1=0xfd;
PCON=0x00;
SCON=0x50;//方式1
EA=1;
ES=1;
TR1=1;
ES=0;
SBUF=**;
while(!TI);
TI=0;
ES=1;
void ser() interrupt 4
{
**=SBUF;
RI=0;
}
IIC总线的应用————————————————————————————————————————————————EEPROM上测试
sbit sck=P2^1;
sbit sda=P2^0;
sbit ACC7=ACC^7;
sbit ACC0=ACC^0;
void delay(int z)
{
uchar i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void deluy()
{;;}
void start()
{
sda=1;
deluy();
sck=1;
deluy();
sda=0;
deluy();
}
void stop()
{
sda=0;
deluy();
sck=1;
deluy();
sda=1;
deluy();
}
respond()
{
uchar i;
sck=1;
while(sda!=0&&i<250)i++;
sck=0;
deluy();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
sck=0;
deluy();
sda=CY;
deluy();
sck=1;
deluy();
}
sck=0;
deluy();
sda=1;
deluy();
}
uchar read_byte()
{
uchar i;
sck=0;
deluy();
sda=1;
deluy();
for(i=0;i<8;i++)
{
ACC<<=1;
sck=1;
deluy();
ACC0=sda;
sck=0;
}
return ACC;
}
write_date(uchar add,uchar date)
{
start();
write_byte(0xa0);
respond();
write_byte(add);
respond();
write_byte(date);
respond();
stop();
}
read_date(uchar add)
{
uchar k;
start();
write_byte(0xa0);
respond();
write_byte(add);
respond();
start();
write_byte(0xa1);
respond();
k=read_byte();
stop();
return k;
}
DS18b28的应用—————————————————————————————————————————————一些寄存器可以按需修改
bit DS18B20_init()
{
bit flag;
DQ=1;
for(time=0;time<2;time++);
DQ=0;
for(time=0;time<200;time++);
DQ=1;
for(time=0;time<10;time++);
flag=DQ;
for(time=0;time<200;time++);
return (flag);
}
void writecom(uchar com)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=1;
_nop_();
DQ=0;
DQ=com&0x01;
for(time=0;time<10;time++);
DQ=1;
for(time=0;time<1;time++);
com>>=1;
}
for(time=0;time<4;time++);
}
uchar readdat()
{
uchar i,dat;
dat=0;
for(i=0;i<8;i++)
{
DQ=1;
_nop_();
DQ=0;
dat>>=1;
_nop_();
DQ=1;
for(time=0;time<2;time++);
if(DQ==1)
dat|=0x80;
else
dat|=0x00;
for(time=0;time<8;time++);
}
return(dat);
}
void get_temp()
{
DS18B20_init();
writecom(0xCC);
writecom(0x44);
for(time=0;time<100;time++);
DS18B20_init();
writecom(0xCC);
writecom(0xBE);
}
void main()
{
uchar i,tl,th;
uchar zhen,xiao;
while(1)
{
get_temp();
tl=readdat();
th=readdat();
zhen=(th*256+tl)/16;
xiao=(tl%16)*10/16;
}
}
AD转换——————————————————————————————————————————————ADC0804,其他不适用,temp 0~255之间 关键是接口
sbit wr=P3^6;
sbit rd=P3^7;
sbit cs=P3^2;
void init()
{
cs=0;
}
void start()
{
wr=1;
wr=0;
wr=1;
}
void huoqu ()
{
uchar temp;
temp=0;
start();
rd=0;
temp=P1;
rd=1;
retrn temp;
}
DA转换—————————————————————————————————————————————关键是接口
sbit wr=P3^6;
sbit da=P3^2;
init()
{
da=0;
}
start()
{
wr=1;
wr=0;
wr=1;
}
键盘的使用—————————————————————————————————————————4X4 含消抖效果
uchar key;
void scan(void)
{
uchar scode,rcode;
P3=0xf0;
if((P3&0xf0)!=0xf0)
{
delay(5);
if((P3&0xf0)!=0xf0)
{
scode=0xfe;
while((scode&0x10)!=0)
{
P3=scode;
if((P3&0xf0)!=0xf0)
{
rcode=(P3&0xf0)|0x0f;
key=(~scode)+(~rcode);
switch(key)
{
case 0x11:key=0;break;
case 0x21:key=1;break;
case 0x41:key=2;break;
case 0x81:key=3;break;
case 0x12:key=4;break;
case 0x22:key=5;break;
case 0x42:key=6;break;
case 0x82:key=7;break;
case 0x14:key=8;break;
case 0x24:key=9;break;
case 0x44:key=10;break;
case 0x84:key=11;break;
case 0x18:key=12;break;
case 0x28:key=13;break;
case 0x48:key=14;break;
case 0x88:key=15;break;
default :
key=16;break;
}
}
else
scode=(scode<<1)|0x01;
}
}
}
else
key=16;
}
2022-06-13 10:49:41
10.84MB
51
单片机
整合
1