一个是LCD显示温湿度的程序如下
#include <reg51.h>
#include <intrins.h>
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define uint unsigned int
#define uchar unsigned char
sbit io=P2^2;
sbit rw=P2^6;
sbit rs=P2^7;
sbit ep=P2^5;
typedef bit BOOL;
uchar data_byte;
uchar RH,RL,TH,TL;
void delay(uchar ms)
{
uchar i;
while(ms--)
for(i=0;i<100;i++);
}
void delay1()
{
uchar i;
for(i=0;i<1;i++);
}
BOOL lcd_bz()
{
BOOL result;
rs=0;
rw=1;
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (BOOL)(P2&0x80);
ep=0;
return result ;
}
void write_cmd(uchar cmd)
{
while (lcd_bz());
rs=0;
rw=0;
ep=0;
_nop_();
_nop_();
P0=cmd ;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
ep=0;
}
void write_addr(uchar addr)
{
write_cmd(addr|0x80);
}
void write_byte(uchar dat)
{
while (lcd_bz());
rs=1;
rw=0;
ep=0;
_nop_();
_nop_();
P0=dat ;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
ep=0;
}
void lcd_init()
{
write_cmd(0x38);
delay(1);
write_cmd(0x0c);
delay(1);
write_cmd(0x06);
delay(1);
write_cmd(0x01);
delay(1);
}
void display(uchar addr,uchar q)
{
delay(10);
write_addr(addr);
write_byte(q);
delay(1);
}
void start()
{
io=1;
delay1();
io=0;
delay(25);
io=1;
delay1();
delay1();
delay1();
}
uchar receive_byte()
{
uchar i,temp;
for(i=0;i<8;i++)
{
while(!io);
delay1();
delay1();
delay1();
temp=0;
if(io==1)
temp=1;
while(io);
data_byte<<=1;
data_byte|=temp;
}
return data_byte;
}
void receive()
{
uchar T_H,T_L,R_H,R_L,check,num_check,i;
start();
io=1;
if(!io)
{
while(!io);
while(io);
R_H=receive_byte();
R_L=receive_byte();
T_H=receive_byte();
T_L=receive_byte();
check=receive_byte();
io=0;
for(i=0;i<7;i++)
delay1();
io=1;
num_check=R_H+R_L+T_H+T_L;
if(num_check==check)
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
check=num_check;
}
}
}
void main()
{
lcd_init();
while(1)
{
receive();
display(0x00,'R');
display(0x01,':');
display(0x02,RH/10+0x30);
display(0x03,RH%10+0x30);
display(0X04,'%');
display(0x40,'T');
display(0x41,':');
display(0x42,TH/10+0x30);
display(0x43,TH%10+0x30);
display(0x44,0xdf);
display(0x45,0x43);
}
}
另一个是PWN调光的程序如下
/*单片机实现PWM调节LED灯的亮度*/
#include<reg52.h>
#define uchar unsigned char
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
uchar zkb,i;// zkb指占空比
uchar zkb2,i;
void time0(void) interrupt 1 //中断函数
{
TH0=(65536-10)/256;
TL0=(65536-10)%256;
++i;
if(i>10)
{
i=0;
};
if(i<=zkb)
{
P2_0=1;
}
else P2_0=0;
if(i<=zkb2)
{
P2_1=1;
}
else P2_1=0;
}
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init() //初始化函数
{
TMOD=0X01;
TH0=(65536-10)/256;
TL0=(65536-10)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan() //键盘扫描
{
P1=0XFF;
if(key1==0)
{
delay(5);
if(key1==0)
{
while(!key1);
if(zkb<9)
{
zkb++;
}
}
}
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(zkb>0)
{
zkb--;
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
if(zkb2<9)
{
zkb2++;
}
}
}
if(key4==0)
{
delay(5);
if(key4==0)
{
while(!key4);
if(zkb2>0)
{
zkb2--;
}
}
}
}
void main() //主函数
{
zkb=2;
zkb2=2;
init();
while(1)
{
keyscan();
}
}
有没有大侠帮忙看下怎么合并在一起 两个程序单独都可以下载到板子里实现功能 想同时实现功能自己不会合并 本人学生新手小白一个 程序是网上找的 有没有大佬帮忙合并一下 想跟着学学
|