1
编程实现点亮第二个LED灯
我的答案:
#include<reg52.h>
sbit LED=P0^0;
sbit ADDR0=P1^0;
sbit ADDR1=P1^0;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
void main()
{
ENLED=0;
ADDR3=1;
ADDR2=1;
ADDR1=1;
ADDR0=0;
LED=0;
while(1);
}
2
编程实现点亮8个LED灯
我的答案:
#include<reg52.h>
sbit D1=P0^0;
sbit D2=P0^1;
sbit D3=P0^2;
sbit D4=P0^3;
sbit D5=P0^4;
sbit D6=P0^5;
sbit D7=P0^6;
sbit D8=P0^7;
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
void main()
{
ADDR0=0;
ADDR1=1;
ADDR2=1;
ADDR3=1;
ENLED=0;
D1=0;
D2=0;
D3=0;
D4=0;
D5=0;
D6=0;
D7=0;
D8=0;
while(1);
}
1
编程实现8个Led右移流水灯
我的答案:
#include<reg52.h>
#include<intrins.h>
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void main( )
{
P1=0x0e;
P0=0x7f;
while(1)
{
P0=_cror_(P0,1);
delay_ms(100);
}
}
2
编程实现一个左移到头接着右移,右移到头接着左移的花样流水灯程序。
我的答案:
#include<reg52.h>
#include<intrins.h>
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void main( )
{
P1=0x0e;
P0=0xfe;
while(1)
{
P0=_crol_(P0,1);
delay_ms(100);
if(P0==0x7f)
{
while(1)
{
P0=_cror_(P0,1);
delay_ms(100);
if(P0==0xfe)
break;
}
}
}
}
1
编程实现如下功能:第一次按下K1键,LED2亮,第二次按下K1键,LED2灭,如此反复。
我的答案:
#include<reg52.h>
#include<stdio.h>
sbit KeyIn1=P2^4;
sbit KeyOut1=P2^3;
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void main()
{
P1=0x0e;
KeyIn1=1;
KeyOut1=0;
while(1)
{
if(KeyIn1==0)
{
delay_ms(10);
if(KeyIn1==0)
{
P0=0xfe;
while(KeyIn1==0);
}
}
if(KeyIn1==0)
{
delay_ms(10);
if(KeyIn1==0)
{
P0=0xff;
while(KeyIn1==0);
}
}
}
}
1
要求用一个按键控制流水灯,按下一次键点亮第一个灯,按第二次键点亮第二个灯,如此下去,成流水灯。
我的答案:
#include<reg52.h>
sbit KeyIn1=P2^4;
sbit KeyOut1=P2^3;
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--){
for(i=0;i<=110;i++);
}
}
unsigned char code LedChar[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void main()
{ unsigned cnt=0;
P1=0x0e;
KeyIn1=1;
KeyOut1=0;
while(1)
{
if(KeyIn1==0)
{
delay_ms(10);
if(KeyIn1==0)
{ P0=LedChar[cnt++];
while(KeyIn1==0);
}
if(cnt==8)
{
cnt=0;
}
}
}
}
VIDEO_20190926_104457.mp4
2
要求按键K1按下,当松开后,数码管DS1循环显示0~F。
我的答案:
#include<reg52.h>
sbit KeyIn1=P2^4;
sbit KeyOut1=P2^3;
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
unsigned char code LedChar[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void main()
{ unsigned cnt=0;
ENLED=0;
ADDR3=1;
ADDR2=0;
ADDR1=0;
ADDR0=1;
KeyIn1=1;
KeyOut1=0;
while(1)
{
if(KeyIn1==0)
{
delay_ms(10);
if(KeyIn1==0)
{ P0=LedChar[cnt++];
while(KeyIn1==0);
}
if(cnt==16)
{
cnt=0;
}
}
}
}
1
1、修改矩阵键盘扫描程序,当某个键被按下以后用开发板上最右边的4个LED灯指示按键编号,灯亮代表高电平,灯灭代表低电平,如“9”号键被按下后,4个LED灯分别为“亮,灭,灭,亮”,表示二进制1001,即数字9。
2、使6位数码管依次循环显示0~5。
我的答案:
///第一题 18446221 秦宇欣
#include<reg52.h>
unsigned char code KeyCode[]={0xE7,0XD7,0XB7,0X77,0XEB,0XDB,0XBB,0X7B,0XED,0XDD,0XBD,0X7D,0XEE,0XDE,0XBE,0X7E};
unsigned char code Led[]={0xfe,0xfd,0xfb,0xf7};
unsigned char x;
unsigned char d;
unsigned char key;
unsigned char y;
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void scankeyboard()
{
unsigned char scan1,scan2,keyboard;
unsigned char i;
P2=0xf0;
scan1=P2;
if(((scan1)&0xf0)!=0xf0)
{
delay_ms(10);
scan1=P2;
if(((scan1)&0xf0)!=0xf0)
{
P2=0x0f;
scan2=P2;
keyboard=scan1|scan2;
while((P2&0x0f)!=0x0f);
for(i=0;i<15;i++)
{
if(keyboard==KeyCode[i])
key=i;
}
}
}
}
void main()
{P1=0x0e;
while(1)
{
scankeyboard();
// unsigned char x;
x=key+1;
d=4;
while(d--)
{ y=x%2;
x=x/2;
if(d==3)
{
if(y==1)
P0=Led[0];
}
if(d==2)
{
if(y==1)
P0=Led[1];
}
if(d==1)
{
if(y==1)
P0=Led[2];
}
if(d==0)
{
if(y==1)
P0=Led[3];
}
}
}
}
///第二题 18446221 秦宇欣
#include<reg52.h>
unsigned char code LedChar[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92}; //亮几
unsigned char code p1[]={0x08,0x09,0x0A,0x0B,0x0C,0x0D}; //哪一个数码管
unsigned char key=16;
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void light()
{ unsigned char cnt=0;
while(1)
{
P0=LedChar[cnt++];
delay_ms(1000);
if(cnt==6)
return ;
}
}
void main()
{
unsigned char x=0;
while(1)
{ P1=p1[x++];
light();
if(x==6)
x=0;
}
}
1
编程设计一个99.9S~0S的倒计时器;
我的答案:
#include<reg52.h>
#define uchar unsigned char
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
uchar cnt=0;
uchar sec=99,mic=9;
unsigned char code LED[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char code led1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void main()
{
uchar temp;
ENLED=0;
ADDR3=1;
TMOD=0x01;
TH0=0x4C;
TL0=0x00;
IE=0x82;
TR0=1;
while(1)
{
ADDR2=0;
ADDR1=0;
ADDR0=0;
temp=mic%10;
P0=led1[temp];
delay_ms(2);
ADDR2=0;
ADDR1=0;
ADDR0=1;
temp=sec%10;
P0=LED[temp];
delay_ms(2);
delay_ms(2);
ADDR2=0;
ADDR1=1;
ADDR0=0;
temp=sec/10;
P0=led1[temp];
delay_ms(2);
}
}
2
修改电子时钟程序,实现整点提醒功能(整点的时候用8个LED灯闪烁10S来提示)。
我的答案:
VIDEO_20191020_120645.mp4
#include<reg52.h>
#define uchar unsigned char
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
int flag=0;
uchar cnt=0;
unsigned char temp;
uchar sec=58,min=59,hour=0;
unsigned char code LedChar[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void main()
{
ENLED=0;
ADDR3=1;
TMOD=0X01;
TH0=0X4C;
TL0=0X00;
EA=1;
ET0=1;
TR0=1;
while(1)
{
ADDR2=0;
ADDR1=0;
ADDR0=0;
temp=sec%10;
P0=LedChar[temp];
delay_ms(2);
ADDR2=0;
ADDR1=0;
ADDR0=1;
temp=sec/10;
P0=LedChar[temp];
delay_ms(2);
ADDR2=0;
ADDR1=1;
ADDR0=0;
temp=min%10;
P0=LedChar[temp];
delay_ms(2);
ADDR2=0;
ADDR1=1;
ADDR0=1;
temp=min/10;
P0=LedChar[temp];
delay_ms(2);
ADDR2=1;
ADDR1=0;
ADDR0=0;
temp=hour%10;
P0=LedChar[temp];
delay_ms(2);
ADDR2=1;
ADDR1=0;
ADDR0=1;
temp=hour/10;
P0=LedChar[temp];
delay_ms(5);
}
}
void clock()interrupt 1
{
cnt++;
TH0=0x4C;
TL0=0x00;
if(cnt==20)
{
cnt=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
hour=0;
}
}
if(sec==0&&min==0)
{
flag=1;
}
if(flag==1&&sec<=10)
{
P1=0x0e;
P0=0x00;
delay_ms(1);
P0=0xff;
if(sec==10)
flag=0;
}
}
}
1
编写程序,用外部中断控制八盏LED循环点亮,即每按下一次外部中断按键,点亮一盏LED灯,依次循环。
2、要求用串口同时发送多个字符。
3、修改串口接收程序,通过串口助手发送一个字符到单片机,单片机接收以后将字符回传给PC机,PC机在接收缓冲区可以查看PC机发送的字符。
我的答案:
#include<reg52.h>
sbit KeyOut1=P2^3;
sbit KeyIn1=P2^4;
unsigned int ct=-1;
unsigned char code led[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void main()
{
P1=0x0e;
EA=1;
EX0=1;
IT0=1;
KeyOut1=0;
KeyIn1=1;
while(1)
{
P0=led[ct];
}
}
void ExInt() interrupt 0
{
if(KeyIn1==0)
{
delay_ms(10);
if(KeyIn1==0)
{
ct++;
if(ct==8)
ct=0;
while(KeyIn1==0);
}
}
}
2.
#include<reg52.h>
sbit KeyOut1=P2^3;
sbit KeyIn1=P2^4;
unsigned char temp[]="gfgdgrgrgsfefg";
unsigned char i;
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<110;i++);
}
}
void main()
{ KeyIn1=1;
KeyOut1=0;
EA=1;
EX0=1;
IT0=1;
SCON=0x50;
TMOD=0x20;
PCON=0x00;
TH1=0xFD;
TL1=0xFD;
TR1=1;
while(1)
{
if(KeyIn1==0)
{
delay_ms(10);
if(KeyIn1==0)
{
while(KeyIn1==0);
for(i=0;i<14;i++)
{SBUF=temp[i];
delay_ms(1);
}
}
}
}
}
3.
#include<reg52.h>
unsigned char cn;
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
unsigned char code led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char temp=0;
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void main()
{
unsigned char dat;
ADDR3=1;
ENLED=0;
EA=1;
ES=1;
SCON=0x50;
PCON=0x00;
TMOD=0x20;
TH1=0xFD;
TL1=0xFD;
TR1=1;
EX0=1;
IT0=1;
while(1)
{
dat=temp%16;
ADDR0=0;
ADDR1=0;
ADDR2=0;
P0=led[dat];
delay_ms(5);
dat=temp/16;
ADDR0=1;
P0=led[dat];
delay_ms(5);
}
}
void uart() interrupt 4
{
temp=SBUF;
cn=temp;
while(RI==0);
RI=0;
SBUF=cn;
while(TI==0);
TI=0;
}
1
编程实现通过串口调试助手发送字符在LCD1602上显示出来。
我的答案:
#include<reg52.h>
#define uchar unsigned char
#define LCD1602_DB P0
unsigned char str;
unsigned char temp=0;
sbit RS=P1^0;
sbit RW=P1^1;
sbit E=P1^5;
unsigned char x=0;
void LcdWaitReady()
{
uchar sta;
LCD1602_DB=0xFF;
RS=0;
RW=1;
do
{
E=1;
sta=LCD1602_DB;
E=0;
}while(sta&0x80);
}
void LcdWriteCmd(uchar cmd)
{
LcdWaitReady();
RS=0;
RW=0;
LCD1602_DB=cmd;
E=1;
E=0;
}
void LcdWriteDat(uchar dat)
{
LcdWaitReady();
RS=1;
RW=0;
LCD1602_DB=dat;
E=1;
E=0;
}
void LcdSetCursor(uchar x,uchar y)
{
uchar addr;
if(y==0)
addr=0x00+x;
else
addr=0x40+x;
LcdWriteCmd(addr|0x80);
}
void LcdShowStr(uchar x,uchar y,uchar *str)
{
LcdSetCursor(x,y);
while(*str!='\0')
{
LcdWriteDat(*str++);
}
}
void LcdShowDat(uchar x,uchar y,uchar dat)
{
LcdSetCursor(x,y);
LcdWriteDat(dat);
}
void InitLcd1602()
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
LcdWriteCmd(0x06);
}
void main()
{
TMOD=0x21;
TH0=0x4C;
TL0=0x00;
IE=0x82;
TR0=1;
EX0=1;
IT0=1;
EA=1;
ES=1;
SCON=0x50;
PCON=0x00;
TH1=0xFD;
TL1=0xFD;
TR1=1;
InitLcd1602();
}
void uart() interrupt 4
{
str=SBUF;
while(RI==0);
RI=0;
SBUF=str;
LcdShowDat(0,0,SBUF);
while(TI==0);
TI=0;
}
2
查阅资料,用LCD1602滚屏显示电子时钟。
我的答案:
#include<reg52.h>
#define uchar unsigned char
#define LCD1602_DB P0
sbit RS=P1^0;
sbit RW=P1^1;
sbit E=P1^5;
unsigned char sec=0,min=0,hour=0;
uchar cnt=0;
void LcdWaitReady()
{
uchar sta;
LCD1602_DB=0xFF;
RS=0;
RW=1;
do
{
E=1;
sta=LCD1602_DB;
E=0;
}while(sta&0x80);
}
void LcdWriteCmd(uchar cmd)
{
LcdWaitReady();
RS=0;
RW=0;
LCD1602_DB=cmd;
E=1;
E=0;
}
void LcdWriteDat(uchar dat)
{
LcdWaitReady();
RS=1;
RW=0;
LCD1602_DB=dat;
E=1;
E=0;
}
void LcdSetCursor(uchar x,uchar y)
{
uchar addr;
if(y==0)
addr=0x00+x;
else
addr=0x40+x;
LcdWriteCmd(addr|0x80);
}
void LcdShowStr(uchar x,uchar y,uchar *str)
{
LcdSetCursor(x,y);
while(*str!='\0')
{
LcdWriteDat(*str++);
}
}
void LcdShowDat(uchar x,uchar y,uchar dat)
{
LcdSetCursor(x,y);
LcdWriteDat(dat);
}
void delayms(unsigned char cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void InitLcd1602()
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
RS=0;
RW=0;
E=0;
LcdWriteCmd(0x1c);
}
void main()
{ unsigned char a;
TMOD=0x01;
TH0=0x4C;
TL0=0x00;
IE=0x82;
TR0=1;
while(1)
{
InitLcd1602();
delayms(500);
a=hour/10;
a+=0x30;
LcdShowDat(0,1,a);
a=hour%10;
a+=0x30;
LcdShowDat(1,1,a);
a=0x3A;
LcdShowDat(2,1,a);
a=min/10;
a+=0x30;
LcdShowDat(3,1,a);
a=min%10;
a+=0x30;
LcdShowDat(4,1,a);
a=0x3A;
LcdShowDat(5,1,a);
a=sec/10;
a+=0x30;
LcdShowDat(6,1,a);
a=sec%10;
a+=0x30;
LcdShowDat(7,1,a);
}
}
void clock() interrupt 1
{
cnt++;
TH0=0x4C;
TL0=0x00;
if(cnt==20)
{
cnt=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
hour=0;
}
}
}
}
1
如何将AD转换采集到的数值显示到数码管上?
我的答案:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define unit unsigned int
#define I2CDelay(){_nop_();_nop_();_nop_();_nop_();}
sbit I2C_SCL=P3^7;
sbit I2C_SDA=P3^6;
unsigned char b;
#define LCD1602_DB P0
uchar str[9];
sbit RS=P1^0;
sbit RW=P1^1;
sbit E=P1^5;
bit flag300ms=1;
unsigned char T0RH=0;
unsigned char T0RL=0;
void ConfigTimer0(unsigned int ms);
unsigned char GetADCValue(unsigned char chn);
unsigned int val;
unsigned char i,a;
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
unsigned char code led[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
unsigned char code led2[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0E};
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void I2CStart()
{
I2C_SDA=1;
I2C_SCL=1;
I2CDelay();
I2C_SDA=0;
I2CDelay();
I2C_SCL=0;
}
void I2CStop()
{
I2C_SCL=0;
I2C_SDA=0;
I2CDelay();
I2C_SCL=1;
I2CDelay();
I2C_SDA=1;
I2CDelay();
}
bit I2CWrite(unsigned char dat)
{
bit ack;
unsigned char mask;
for(mask=0x80;mask!=0;mask>>=1)
{
if((mask&dat)==0)
I2C_SDA=0;
else
I2C_SDA=1;
I2CDelay();
I2C_SCL=1;
I2CDelay();
I2C_SCL=0;
}
I2C_SDA=1;
I2CDelay();
I2C_SCL=1;
ack=I2C_SDA;
I2CDelay();
I2C_SCL=0;
return ack;
}
unsigned char I2CReadNAK()
{
unsigned char mask;
unsigned char dat;
I2C_SDA=1;
for(mask=0x80;mask!=0;mask>>=1)
{
I2CDelay();
I2C_SCL=1;
if(I2C_SDA==0)
dat&=~mask;
else
dat|=mask;
I2CDelay();
I2C_SCL=0;
}
I2C_SDA=1;
I2CDelay();
I2C_SCL=1;
I2CDelay();
I2C_SCL=0;
return dat;
}
void main()
{
ENLED=0;
ADDR3=1;
ADDR2=0;
ADDR1=0;
ADDR0=0;
EA=1;
ConfigTimer0(10);
while(1)
{
if(flag300ms==1)
{
flag300ms=0;
a=GetADCValue(0);
str[0]=a/10000000%10;
str[1]=a/1000000%10;
str[2]=a/100000%10;
str[3]=a/10000%10;
str[4]=a/1000%10;
str[5]=a/100%10;
str[6]=a/10%10;
str[7]=a%10;
b=str[0]*128+str[1]*64+str[2]*32+str[3]*16+str[4]*8+str[5]*4+str[6]*2+str[7]*1;
ADDR2=0;
ADDR1=0;
ADDR0=0;
P0=led[b%10];
delay_ms(2);
ADDR2=0;
ADDR1=0;
ADDR0=1;
P0=led2[b/10%10];
delay_ms(2);
}
}
}
unsigned char GetADCValue(unsigned char chn)
{
unsigned char val;
I2CStart();
if(I2CWrite(0x48<<1))
{ I2CStop();
return 0;
}
I2CWrite(0x40|chn);
I2CStart();
I2CWrite(0x48<<1|0x01);
val=I2CReadNAK();
I2CStop();
return val;
}
void ConfigTimer0(unsigned int ms)
{
unsigned long tmp;
tmp=11059200/12;
tmp=(tmp*ms)/1000;
tmp=65536-tmp;
tmp=tmp+32;
T0RH=(unsigned char)(tmp>>8);
T0RL=(unsigned char)tmp;
TMOD&=0xF0;
TMOD|=0x01;
TH0=T0RH;
TL0=T0RL;
ET0=1;
TR0=1;
}
void InterruptTimer0() interrupt 1
{
static unsigned char tmr300ms=0;
TH0=T0RH;
TL0=T0RL;
tmr300ms++;
if(tmr300ms>=30)
{
tmr300ms=0;
flag300ms=1;
}
}
1
1、怎么把测量到的温度通过LCD显示出来?(写出显示程序)。
2、 当温度超过设置值时,怎么设置使蜂鸣器发出某首音乐声?
我的答案:
1.
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit IO_18B20=P3^2;
sbit BUZZ=P1^6;
uint temp=0;
uint warn_temp=300;
uchar ledwei[6]={0,0,0,0,0,0};
void display(uint temp);
uint Get18B20Temp();
void start_18B20();
void Delay10us(uint t);
#define LCD1602_DB P0
#define uchar unsigned char
sbit RS=P1^0;
sbit RW=P1^1;
sbit E=P1^5;
void LcdWaitReady()
{
uchar sta;
LCD1602_DB=0xFF;
RS=0;
RW=1;
do
{
E=1;
sta=LCD1602_DB;
E=0;
}while(sta&0x80);
}
void LcdWriteCmd(uchar cmd)
{
LcdWaitReady();
RS=0;
RW=0;
LCD1602_DB=cmd;
E=1;
E=0;
}
void LcdWriteDat(uchar dat)
{
LcdWaitReady();
RS=1;
RW=0;
LCD1602_DB=dat;
E=1;
E=0;
}
void LcdSetCursor(uchar x,uchar y)
{
uchar addr;
if(y==0)
addr=0x00+x;
else
addr=0x40+x;
LcdWriteCmd(addr|0x80);
}
void LcdShowDat(uchar x,uchar y,uchar dat)
{
LcdSetCursor(x,y);
LcdWriteDat(dat);
}
void LcdShowStr(uchar x,uchar y,uchar *str)
{
LcdSetCursor(x,y);
while(*str!='\0')
{
LcdWriteDat(*str++);
}
}
void InitLcd1602()
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
LcdWriteCmd(0x06);
LcdWriteCmd(0x01);
}
void main()
{
InitLcd1602();
start_18B20();
Get18B20Temp();
Delay10us(40000);
while(1)
{
start_18B20();
display(Get18B20Temp());
}
}
void Delay10us(uint t)
{
do{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}while(--t);
}
bit init_18B20()
{
bit ack;
IO_18B20=1;
_nop_();
_nop_();
IO_18B20=0;
Delay10us(50);
IO_18B20=1;
Delay10us(5);
ack=IO_18B20;
Delay10us(20);
IO_18B20=1;
return ack;
}
void Write18B20(uchar date)
{
uchar sum;
for(sum=0x01;sum!=0;sum<<=1)
{
IO_18B20=0;
_nop_();
_nop_();
if((sum&date)==0)
IO_18B20=0;
else
IO_18B20=1;
Delay10us(6);
IO_18B20=1;
}
}
uchar Read18B20()
{
uchar date;
uchar sum;
for(sum=0x01;sum!=0;sum<<=1)
{
IO_18B20=0;
_nop_();
_nop_();
IO_18B20=1;
_nop_();
_nop_();
if(!IO_18B20)
date&=~sum;
else
date|=sum;
Delay10us(6);
}
return date;
}
void start_18B20()
{
bit ack;
ack=init_18B20();
if(ack==0)
{
Write18B20(0xcc);
Write18B20(0x44);
}
}
uint Get18B20Temp()
{
uchar MSB,LSB;
float sum;
bit ack;
ack=init_18B20();
if(ack==0)
{
Write18B20(0xCC);
Write18B20(0xBE);
LSB= Read18B20();
MSB= Read18B20();
temp=((int)MSB<<8)+LSB;
sum=temp*0.0625;
temp=sum*10+0.5;
}
return temp;
}
void display(uint temp1)
{
static uchar i=0;
ledwei[0]=temp1%10;
ledwei[0]+=0x30;
LcdShowDat(6,1,ledwei[0]);
ledwei[1]=0x2E;
LcdShowDat(5,1,ledwei[1]);
ledwei[2]=temp1/10%10;
ledwei[2]+=0x30;
LcdShowDat(4,1,ledwei[2]);
ledwei[3]=temp1/100%10;
ledwei[3]+=0x30;
LcdShowDat(3,1,ledwei[3]);
}
2.
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit IO_18B20=P3^2;
sbit BUZZ=P1^6;
uint temp=0;
uint warn_temp=200;
uchar ledwei[6]={0,0,0,0,0,0};
void display(uint temp);
uint Get18B20Temp();
void start_18B20();
void Delay10us(uint t);
#define LCD1602_DB P0
#define uchar unsigned char
sbit RS=P1^0;
sbit RW=P1^1;
sbit E=P1^5;
unsigned int code Frequency[]={523,587,659,698,784,880,988,988,880,698,659,587,523,784};
uchar TH0RL;
uchar TL0RL;
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void timer0() interrupt 1
{
BUZZ=~BUZZ;
TH0=TH0RL;
TL0=TL0RL;
}
void LoadInitialVal(unsigned int f)
{
unsigned int X;
X=65536-11059200/12/(2*f);
TH0RL=(unsigned char)(X>>8);
TL0RL=(unsigned char)X;
}
void LcdWaitReady()
{
uchar sta;
LCD1602_DB=0xFF;
RS=0;
RW=1;
do
{
E=1;
sta=LCD1602_DB;
E=0;
}while(sta&0x80);
}
void LcdWriteCmd(uchar cmd)
{
LcdWaitReady();
RS=0;
RW=0;
LCD1602_DB=cmd;
E=1;
E=0;
}
void LcdWriteDat(uchar dat)
{
LcdWaitReady();
RS=1;
RW=0;
LCD1602_DB=dat;
E=1;
E=0;
}
void LcdSetCursor(uchar x,uchar y)
{
uchar addr;
if(y==0)
addr=0x00+x;
else
addr=0x40+x;
LcdWriteCmd(addr|0x80);
}
void LcdShowDat(uchar x,uchar y,uchar dat)
{
LcdSetCursor(x,y);
LcdWriteDat(dat);
}
void LcdShowStr(uchar x,uchar y,uchar *str)
{
LcdSetCursor(x,y);
while(*str!='\0')
{
LcdWriteDat(*str++);
}
}
void InitLcd1602()
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
LcdWriteCmd(0x06);
LcdWriteCmd(0x01);
}
void main()
{
InitLcd1602();
TMOD=0x01;
EA=1;
ET0=1;
TR0=1;
start_18B20();
Get18B20Temp();
Delay10us(40000);
while(1)
{
start_18B20();
display(Get18B20Temp());
if(temp>warn_temp)
{
unsigned char i;
for(i=0;i<14;i++)
{
LoadInitialVal(Frequency[i]);
delay_ms(1000);
}
}
}
}
void Delay10us(uint t)
{
do{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}while(--t);
}
bit init_18B20()
{
bit ack;
IO_18B20=1;
_nop_();
_nop_();
IO_18B20=0;
Delay10us(50);
IO_18B20=1;
Delay10us(5);
ack=IO_18B20;
Delay10us(20);
IO_18B20=1;
return ack;
}
void Write18B20(uchar date)
{
uchar sum;
for(sum=0x01;sum!=0;sum<<=1)
{
IO_18B20=0;
_nop_();
_nop_();
if((sum&date)==0)
IO_18B20=0;
else
IO_18B20=1;
Delay10us(6);
IO_18B20=1;
}
}
uchar Read18B20()
{
uchar date;
uchar sum;
for(sum=0x01;sum!=0;sum<<=1)
{
IO_18B20=0;
_nop_();
_nop_();
IO_18B20=1;
_nop_();
_nop_();
if(!IO_18B20)
date&=~sum;
else
date|=sum;
Delay10us(6);
}
return date;
}
void start_18B20()
{
bit ack;
ack=init_18B20();
if(ack==0)
{
Write18B20(0xcc);
Write18B20(0x44);
}
}
uint Get18B20Temp()
{
uchar MSB,LSB;
float sum;
bit ack;
ack=init_18B20();
if(ack==0)
{
Write18B20(0xCC);
Write18B20(0xBE);
LSB= Read18B20();
MSB= Read18B20();
temp=((int)MSB<<8)+LSB;
sum=temp*0.0625;
temp=sum*10+0.5;
}
return temp;
}
void display(uint temp1)
{
static uchar i=0;
ledwei[0]=temp1%10;
ledwei[0]+=0x30;
LcdShowDat(6,1,ledwei[0]);
ledwei[1]=0x2E;
LcdShowDat(5,1,ledwei[1]);
ledwei[2]=temp1/10%10;
ledwei[2]+=0x30;
LcdShowDat(4,1,ledwei[2]);
ledwei[3]=temp1/100%10;
ledwei[3]+=0x30;
LcdShowDat(3,1,ledwei[3]);
}
1
1、编写程序通过红外遥控器控制步进电机的加速和减速,并详细描述你的控制原理和方法?。
我的答案:
#include<reg52.h>
sbit IR_INPUT=P3^3;
bit irflag=0;
unsigned char ircode[4];
unsigned char code counterclockwise[8]={0x0E,0x0C,0x0D,0x09,0x0B,0x03,0x07,0x06}; //逆时针
unsigned char code clockwise[8]={0x06,0x07,0x03,0x0B,0x09,0x0D,0x0C,0x0E}; //顺时针
unsigned char count1=0,count2=0,temp=0;
unsigned char T0RH=0;
unsigned char T0RL=0;
void InitInfrared()
{
IR_INPUT=1;
TMOD&=0X0F;
TMOD|=0x10;
TR1=0;
ET1=0;
IT1=1;
EX1=1;
}
unsigned int GetHighTime()
{
TH1=0;
TL1=0;
TR1=1;
while(IR_INPUT)
{
if(TH1>0x40)
{
break;
}
}
TR1=0;
return (TH1*256+TL1);
}
unsigned int GetLowTime()
{
TH1=0;
TL1=0;
TR1=1;
while(!IR_INPUT)
{
if(TH1>0x40)
{
break;
}
}
TR1=0;
return (TH1*256+TL1);
}
void ExInt1() interrupt 2
{
unsigned int time;
unsigned char i,j;
unsigned char byt=0;
time=GetLowTime();
if((time<7833)||(time>8755))
{
IE1=0;
return;
}
time=GetHighTime();
if((time<3686)||(time>4608))
{
IE1=0;
return;
}
for(i=0;i<4;i++)
{
for(j=0;j<8;j++)
{
time=GetLowTime();
if((time<313)||(time>718))
{
IE1=0;
return;
}
time=GetHighTime();
if((time>313)&&(time<718))
{
byt>>=1;
}
else if((time>1345)&&(time<1751))
{
byt>>=1;
byt|=0x80;
}
else
{
IE1=0;
return;
}
}
ircode[i]=byt;
}
irflag=1;
IE1=0;
}
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}
int main()
{ unsigned int speedjia=2;
unsigned int speedjian=2;
EA=1;
InitInfrared();
while(1)
{
unsigned char tmp,temp=0;
if(irflag==1)
{
if(ircode[2]==0x4a)
{
tmp=P1;
tmp=tmp&0xF0;
tmp=tmp|clockwise[count1];
P1=tmp;
count1=count1+1;
if(count1>7)
count1=0;
delay(4);
}
else if(ircode[2]==0x09)
{ tmp=P1;
tmp=tmp&0xF0;
tmp=tmp|clockwise[count1];
P1=tmp;
count1=count1+1;
if(count1>7)
count1=0;
delay(speedjia--);
if(speedjia<=0)
speedjia=1;
}
else if(ircode[2]==0x15)
{
tmp=P1;
tmp=tmp&0xF0;
tmp=tmp|clockwise[count1];
P1=tmp;
count1=count1+1;
if(count1>7)
count1=0;
delay(speedjian+0.5);
if(speedjia>=5)
speedjia=5;
}
else if(ircode[2]==0x45)
{
P1=tmp|0x00;
}
}
}
}
单片机--学期总结
最新推荐文章于 2022-12-13 11:57:01 发布