#include
#define RW PORTB_Bit1
#define EN PORTB_Bit2
#define DATA PORTA
#define busy 0x80
{
uint i,j;
for(i=0;i
}
void wait()
{
uchar val;
DATA=0xff;
RS=0;
RW=1;
__no_operation();
__no_operation();
EN=1;
__no_operation();
__no_operation();
DDRA=0x00;
val=PINA;
while(val&busy)
{
val=PINA;
}
EN=0;
DDRA=0xff;
}
void writecmd(uchar cmd)
{
wait();
RS=0;
RW=0;
__no_operation();
DATA=cmd;
__no_operation();
EN=1;
__no_operation();
__no_operation();
EN=0;
}
{
wait();
RS=1;
RW=0;
__no_operation();
DATA=data;
__no_operation();
EN=1;
__no_operation();
__no_operation();
EN=0;
}
void displayonechar(uchar x,uchar y,uchar dda)
{
y&=0x01;
x&=0x0f;
if(y)x|=0x40;
x|=0x80;
writecmd(x);
writedata(dda);
}
{
y&=0x01;
x&=0x0f;
while(*p!=' ')
{
if(x<=0x0f)
{
displayonechar(x,y,*p);
p++;
x++;
}
}
}
void init1602()
{
delay(15);
writecmd(0x38);
delay(5);
writecmd(0x38);
delay(5);
writecmd(0x38);
writecmd(0x80);
writecmd(0x01);
writecmd(0x06);
writecmd(0x0c);
}
uchar teml,temh;
uchar sign;
uchar Flag_1820Error;
/*********************************/
void delay_15us(void) //15us左右
{
uchar x=27;
while(x)
{
x--;
}
}
/********************************/
void delay_60us(void) //60us左右
{
uchar x=117;
while(x)
{
x--;
}
}
void init_1820(void)
{
uchar i;
uint j=0;
PORTC|=(1<<7); //"1"
PORTC&=~(1<<7); //"0"
for(i=0;i<8;i++)delay_60us();//480us以上
PORTC|=(1<<7); //"1"
DDRC&=~(1<<7); //"PINC7 is INPUT"
delay_15us(); //15~60us
delay_15us();
Flag_1820Error=0;
while(PINC&(1<<7))
{ delay_60us();
j++;
if(j>=18000){Flag_1820Error=1;break;}
}
DDRC|=(1<<7); //PORTC7 is OUTPUT
PORTC|=(1<<7); //"1"
for(i=0;i<4;i++)delay_60us(); //240us
}
/********************************/
void delay_5us(void) //5us左右
{
uchar x=7;
while(x)
{
x--;
}
}
void write_1820(uchar x)
{
uchar m;
for(m=0;m<8;m++)
{
if(x&(1<
PORTC|=(1<<7); //write"1"
delay_15us(); //15~45us
delay_15us();
delay_15us();
}
else
{PORTC&=~(1<<7);delay_15us();//"0",15us
delay_15us(); //write"0"
delay_15us(); //15~45us
delay_15us();
PORTC|=(1<<7); //"1"
}
}
PORTC|=(1<<7); //"1"
}
/*******************************/
uchar read_1820(void)
{
uchar temp,k,n;
temp=0;
for(n=0;n<8;n++)
{
PORTC&=~(1<<7); //"0"
delay_5us();
PORTC|=(1<<7); //"1"
delay_5us();
DDRC&=~(1<<7); //"PINC7 is INPUT"
k=(PINC&(1<<7)); //读数据,从低位开始
if(k)
temp|=(1<
temp&=~(1<
delay_15us();
delay_15us();
DDRC|=(1<<7); //PORTC7 is OUTPUT
}
return (temp);
}
/*************************************/
uint read_temper(void)
{
uchar TX; //小数位
uchar TZ; //整数位
init_1820(); //复位18b20
write_1820(0xcc); // 发出转换命令
write_1820(0x44);
;;;;;;
init_1820();
write_1820(0xcc); //发出读命令
write_1820(0xbe);
teml=read_1820(); //读数据byte1
temh=read_1820(); //byte2
TX=teml&0x0f;
temh=temh<<4;
temh|=(teml&0xf0)>>4;
TZ=temh;
tempp=TZ*10+TX;
return tempp;
}[page]
#define RXC 7
{
UCSRB=0x00;
UCSRA=0x02;
UCSRC=0x06;
UBRRL=0x67;
UBRRH=0x00;
UCSRB=0x18;
}
{
while(!(UCSRA&(1<
}
{
while(!(UCSRA&(1<
}
{
while(*p)
{
serial_sendbyte(*p);
p++;
}
}
{
serial_sendbyte((tt/100)+48);
serial_sendbyte((tt%100/10)+48);
serial_sendbyte('.');
serial_sendbyte((tt%10)+48);
serial_sendbyte(' ');
serial_sendbyte(' ');
}
#define k2 PIND_Bit5
void key1()
{
if(k1==0)
{
if(i==0)
{
j++;
if(j>2)
{
if(k1==0)
{
i=1;j=0;
tempset++;
if(tempset>50)tempset=0;
}
}
}
}
else
{
i=0;
j=0;
}
}
void time0_init() //定时器1 初始化
{
TCCR0=0x05; //1024分频
TCNT0=0x4b;
TIMSK=0x01;
SREG=0x80;
}
__interrupt void time0_event()
{
SREG=0x00;
TCNT0=0x4b;
static uchar i;
i++;
if(i==40)
{ i=0;
displayflag=1; //到达1秒刷屏标志启动
sec++;
if(sec==100)sec=0;
}
}
#include"18b20.h"
#include"serial.h"
#include "time1.h"
#include "key.h"
{
PORTA=0xff;
DDRA=0xff;
PORTB=0xff;
DDRB=0xff;
PORTC=0xff;
DDRC=0xff;
PORTD=0xff;
DDRD=0x00;
}
{
displayonechar(1,1,(k/100)+48);
displayonechar(2,1,(k%100/10)+48);
displayonechar(3,1,'.');
displayonechar(4,1,(k%10)+48);
}
{
displaychar(7,1,"Sec :");
displayonechar(13,1,(k/10)+48);
displayonechar(14,1,(k%10)+48);
}
{
displaychar(10,0,"SET:");
displayonechar(14,0,(k/10)+48);
displayonechar(15,0,(k%10)+48);
}
{
uint kk;
port_init();
serial_init();
time0_init();
delay(1000);
init1602();
displaychar(0,0,"Test");
{
TT=read_temper();
show_temp(TT);
serial_sendtemp(TT);
show_tempset(tempset);
if(displayflag==1) //秒钟变化后才刷屏
{
show_sec(sec);
displayflag=0;
}
if(TT>tempset*10) //温度大于设定值报警
{
kk++;
if(kk>20)
{
light=~light;
kk=0;
}
}
}
如果网页上的代码发生格式错乱请到这里下载完整版的: http://www.51hei.com/f/avrdsvb.rar
MSComm1.CommPort = 6
MSComm1.PortOpen = True
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeText
上一篇:AVR的EEPROM简单实验
下一篇:1602自定义字符显示摄氏度符号
推荐阅读最新更新时间:2024-03-16 14:04