1602液晶 液晶显示
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit en=P3^4;
sbit rs=P3^5;
void delay(int z)
{int a,b;
for(a=0;a<=z;a++)
for(b=0;b<=110;b++);
}
void writecom(uchar com)
{ en=1;
rs=0;
P1=com;
delay(5);
en=0;
delay(5);
en=1;
}
void writedate(uchar date)
{ en=1;
rs=1;
P1=date;
delay(5);
en=0;
delay(5);
en=1;
}
void main()
{
writecom(0x01);
writecom(0x38);
writecom(0x0f);
writecom(0x06);
writecom(0xc0);
writedate(0x41);
while(1);
}
ds18b20 测温电路
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds=P3^7;
uchar flag ;
uint temp;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code table1[]={
0xbf,0x86,0xdb,0xcf,
0xe6,0xed,0xfd,0x87,
0xff,0xef}; //带小数点数字编码
/*延时函数*/
void TempDelay (uchar us)
{
while(us--);
}
void delay(uint count)
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
/*数码管的显示 */
void display(uint temp)
{
uchar bai,shi,ge;
bai=temp/100;
shi=temp%100/10;
ge=temp%100%10;
P1=0xff;
P0=table[bai];
P1=0xfe;
delay(1);
P1=0xff; //延时约2ms
P0=table1[shi];
P1=0xfd;
delay(1);
P1=0xff;
P0=table[ge];
P1=0xfb;
delay(1);
P1=0xff;
}
void ds_reset(void)
{
ds=1;
_nop_();
ds=0;
TempDelay(80);
_nop_();
ds=1;
TempDelay(5);
_nop_();
_nop_();
_nop_();
if(ds==0)
flag=1;
else
flag=0;
_nop_();
_nop_();
ds=1;
}
bit ds_read_bit(void)
{
bit dat;
ds=0;
_nop_();
ds=1;
_nop_();
_nop_();
dat=ds;
TempDelay(10);
return(dat);
}
uchar ds_read_byte(void )
{
uchar value,i,j;
value=0;
for(i=0;i<8;i++)
{
j=ds_read_bit();
value=(j<<7)|(value>>1);
}
return(value);
}
void ds_write_byte(uchar dat)
{
uchar i;
bit onebit;
for(i=1;i<=8;i++)
{
onebit=dat&0x01;
dat=dat>>1;
if(onebit)
{
ds=0;
_nop_();
_nop_();
ds=1;
TempDelay(5);
}
else
{
ds=0;
TempDelay(8);
ds=1;
_nop_();
_nop_();
}
}
}
void tem_change()
{
ds_reset();
delay(1);
ds_write_byte(0xcc);
ds_write_byte(0x44);
}
/*----------------------------------------获得
温度:------------------------------------------*/
uint get_temperature()
{
float wendu;
uchar a,b;
ds_reset();
delay(1);
ds_write_byte(0xcc);
ds_write_byte(0xbe);
a=ds_read_byte();
b=ds_read_byte();
temp=b;
temp<<=8;
temp=temp|a;
wendu=temp*0.0625;
temp=wendu*10+0.5;
return temp;
}
void main()
{
uint a;
while(1)
{
tem_change();
for(a=10;a>0;a--)
{
display( get_temperature());
}
}
}
带存储功能电子琴 单片机c
电子琴 带存储
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar a=0,b=0,c=0,d=0;
sbit d1=P3^0;
sbit d2=P3^1;
sbit d3=P3^4;
sbit d4=P3^5;
sbit d5=P3^6;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uint code table1[]={
0,248,249,250,
250,251,251,252,
252,252,253,253,
253,253,254 };
uint code table2[]={
0,136, 62, 19,
104, 5,144, 12,
68,209, 9, 52,
130,200, 6 };
uchar table3[80];
uchar qm;
uchar mm,nn;
void display(uchar a,uchar b,uchar c,uchar d);
void delay(uint z);
void yinyue(uchar q);
uchar key(void);
void fangyin(void);
void lvyin(void);
void main(void)
{
uchar a1;
table3[0]=15;
display(a,b,c,d);
EA=1;
EX0=1;
EX1=1;
IP=0x08;
IT0=0;
IT1=0;
while(1)
{
table3[79]=15;
d2=1;
d3=0;
d4=0;
d5=0;
nn=17;
display(a,b,c,d);
P2=0xf0;
mm=P2;
mm=(mm&0xf0);
if(mm!=0xf0)
{
nn=key();
if(nn<=16)
{
d=c;
c=b;
b=a;
a=nn;
}
}
if(nn<=14)
{
yinyue(nn);
nn=0;
}
if(nn==15)
{
d3=1;
mm=0xf0;
while(mm==0xf0)
{
while(mm==0xf0)
{
P2=0xf0;
mm=P2;
mm=(mm&0xf0);
display(a,b,c,d);
}
display(a,b,c,d);
P2=0xf0;
mm=P2;
mm=(mm&0xf0);
}
a1=0;
while(a1<=250)
{
a1++;
display(a,b,c,d);
P2=0xf0;
mm=P2;
mm=(mm&0xf0);
if(mm==0xf0)
{
d3=1;
a1=253;
}
}
if(a1<=252)
{
display(a,b,c,d);
d3=0;
d4=1;
lvyin();
d4=0;
display(a,b,c,d);
display(a,b,c,d);
display(a,b,c,d);
}
if(a1>=253)
{
display(a,b,c,d);
d3=0;
d5=1;
fangyin();
d5=0;
display(a,b,c,d);
display(a,b,c,d);
display(a,b,c,d);
}
d3=0;
}
}
}
void yinyue(uchar q)
{
uint jia;
uchar ce;
TMOD=0x10;
ET1=1;
qm=q;
TH1=(65536-table1[q])/256;
TL1=(65536-table1[q])%256;
display(a,b,c,d);
if(q!=0)
{TR1=1;}
if(q==0)
{TR0=0;}
for(jia=0;jia<=300;jia++)
{
P2=0xf0;
ce=P2;
ce=(ce&0xf0);
if(ce!=0xf0)
jia=301;
display(a,b,c,d);
}
TR1=0;
}
void time1() interrupt 3
{ TH1=table1[qm];
TL1=table2[qm];
d1=~d1;
}
void display(uchar a,uchar b,uchar c,uchar d)
{
P0=0xff;
P1=table[a];
P0=0xfe;
delay(1);
P0=0xff;
P1=table[b];
P0=0xfd;
delay(1);
P0=0xff;
P1=table[c];
P0=0xfb;
delay(1);
P0=0xff;
P1=table[d];
P0=0xf7;
delay(1);
P0=0xff;
}
void delay(uint z)
{ uchar p,qq;
for(p=0;p<=z;p++)
for(qq=0;qq<=57;qq++);
}
uchar key(void)
{ uchar hu,num;
display(a,b,c,d);
display(a,b,c,d);
display(a,b,c,d);
P2=0xf0;
hu=P2;
hu=(hu&0xf0);
if(hu==0xf0)
num=17;
if(hu!=0xf0)
{
P2=0xfe;
hu=P2;
hu=(hu&0xf0);
switch(hu)
{
case 0xe0:num=3;
break;
case 0xd0:num=2;
break;
case 0xb0:num=1;
break;
case 0x70:num=0;
break;
}
P2=0xfd;
hu=P2;
hu=(hu&0xf0);
switch(hu)
{
case 0xe0:num=7;
break;
case 0xd0:num=6;
break;
case 0xb0:num=5;
break;
case 0x70:num=4;
break;
}
P2=0xfb;
hu=P2;
hu=(hu&0xf0);
switch(hu)
{
case 0xe0:num=11;
break;
case 0xd0:num=10;
break;
case 0xb0:num=9;
break;
case 0x70:num=8;
break;
}
P2=0xf7;
hu=P2;
hu=(hu&0xf0);
switch(hu)
{
case 0xe0:num=15;
break;
case 0xd0:num=14;
break;
case 0xb0:num=13;
break;
case 0x70:num=12;
break;
}
P2=0xf0;
hu=P2;
while(hu!=0xf0)
{
P2=0xf0;
hu=P2;
display(a,b,c,d);
display(a,b,c,d);
while(hu!=0xf0)
{ display(a,b,c,d);
P2=0xf0;
hu=P2;
}
}
}
return num;
}
void int0() interrupt 0
{
yinyue(0);
yinyue(1);
yinyue(2);
yinyue(3);
yinyue(4);
yinyue(5);
yinyue(6);
yinyue(7);
yinyue(8);
yinyue(9);
yinyue(10);
yinyue(11);
yinyue(12);
yinyue(13);
yinyue(14);
}
void int1() interrupt 2
{
yinyue(0);
yinyue(1);
yinyue(1);
yinyue(2);
yinyue(4);
yinyue(3);
yinyue(1);
yinyue(1);
yinyue(2);
yinyue(1);
yinyue(5);
yinyue(4);
yinyue(1);
yinyue(1);
yinyue(6);
yinyue(4);
yinyue(3);
yinyue(2);
yinyue(7);
yinyue(7);
yinyue(6);
yinyue(4);
yinyue(5);
yinyue(4);
yinyue(0);
yinyue(1);
yinyue(1);
yinyue(2);
yinyue(4);
yinyue(3);
yinyue(1);
yinyue(1);
yinyue(2);
yinyue(1);
yinyue(5);
yinyue(4);
yinyue(1);
yinyue(1);
yinyue(6);
yinyue(4);
yinyue(3);
yinyue(2);
yinyue(7);
yinyue(7);
yinyue(6);
yinyue(4);
yinyue(5);
yinyue(4);
}
void lvyin(void)
{ uchar c1=0,c2=0;
P2=0xf0;
mm=P2;
mm=(mm&0xf0);
display(a,b,c,d);
while(mm!=0xf0)
{
while(mm!=0xf0)
{
P2=0xf0;
mm=P2;
mm=(mm&0xf0);
display(a,b,c,d);
}
display(a,b,c,d);
P2=0xf0;
mm=P2;
mm=(mm&0xf0);
}
while(c2!=15)
{ display(a,b,c,d);
P2=0xf0;
mm=P2;
mm=(mm&0xf0);
if(mm!=0xf0)
{
nn=key();
if(nn<=15)
{
table3[c1]=nn;
d=c;
c=b;
b=a;
a=nn;
c2=nn;
yinyue(nn);
display(a,b,c,d);
c1++;
}
}
}
}
void fangyin(void)
{
uchar c4=0,c5=0;
while(c4!=15)
{
c4=table3[c5];
d=c;
c=b;
b=a;
a=c4;
display(a,b,c,d);
yinyue(c4);
c5++;
}
}