//------AVR代碼-----------------
#include <iom16.h> //1602代碼
#include <intrinsics.h>
#include <intrinsics.h>
#define RS PORTB_Bit0
#define RW PORTB_Bit1
#define EN PORTB_Bit2
#define DATA PORTA
#define busy 0x80
#define RW PORTB_Bit1
#define EN PORTB_Bit2
#define DATA PORTA
#define busy 0x80
void delay(uint k)
{
uint i,j;
for(i=0;i<k;i++)
for(j=0;j<1140;j++);
}
{
uint i,j;
for(i=0;i<k;i++)
for(j=0;j<1140;j++);
}
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;
}
void writedata(uchar data)
{
wait();
RS=1;
RW=0;
__no_operation();
DATA=data;
__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);
}
void displaychar(uchar x,uchar y,uchar *p)
{
y&=0x01;
x&=0x0f;
while(*p!='\0')
{
if(x<=0x0f)
{
displayonechar(x,y,*p);
p++;
x++;
}
}
}
{
y&=0x01;
x&=0x0f;
while(*p!='\0')
{
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);
}
//-------------18B20代碼------------
#include <intrinsics.h>
uchar teml,temh;
uchar sign;
uchar Flag_1820Error;
uint tempp; //溫度值
/*********************************/
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<<m)) //寫數據,從低位開始
{PORTC&=~(1<<7);delay_5us(); //"0",5us
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<<n); //read"1"
else
temp&=~(1<<n); //read"0"
delay_15us(); //45us
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;
}
void write_1820(uchar x)
{
uchar m;
for(m=0;m<8;m++)
{
if(x&(1<<m)) //寫數據,從低位開始
{PORTC&=~(1<<7);delay_5us(); //"0",5us
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<<n); //read"1"
else
temp&=~(1<<n); //read"0"
delay_15us(); //45us
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;
}
//---串口代碼------------
#define UDRE 5
#define RXC 7
#define RXC 7
void serial_init() //串口初始化
{
UCSRB=0x00;
UCSRA=0x02;
UCSRC=0x06;
UBRRL=0x67;
UBRRH=0x00;
UCSRB=0x18;
}
{
UCSRB=0x00;
UCSRA=0x02;
UCSRC=0x06;
UBRRL=0x67;
UBRRH=0x00;
UCSRB=0x18;
}
void serial_sendbyte(uchar u)
{
while(!(UCSRA&(1<<UDRE)));
UDR=u;
}
{
while(!(UCSRA&(1<<UDRE)));
UDR=u;
}
uchar serial_receive()
{
while(!(UCSRA&(1<<RXC)));
return UDR;
}
{
while(!(UCSRA&(1<<RXC)));
return UDR;
}
void serial_str(uchar *p)
{
while(*p)
{
serial_sendbyte(*p);
p++;
}
}
{
while(*p)
{
serial_sendbyte(*p);
p++;
}
}
void serial_sendtemp(uint tt) //發送溫度值
{
serial_sendbyte((tt/100)+48);
serial_sendbyte((tt%100/10)+48);
serial_sendbyte('.');
serial_sendbyte((tt%10)+48);
serial_sendbyte('\r');
serial_sendbyte('\n');
}
{
serial_sendbyte((tt/100)+48);
serial_sendbyte((tt%100/10)+48);
serial_sendbyte('.');
serial_sendbyte((tt%10)+48);
serial_sendbyte('\r');
serial_sendbyte('\n');
}
//--------------按鍵代碼-------
#define k1 PIND_Bit4
#define k2 PIND_Bit5
#define k2 PIND_Bit5
void key1()
{
static uchar i,j;
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;
}
}
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;
}
#pragma vector = 0x24
__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;
}
}
__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 <iom16.h>
#define uchar unsigned char
#define uint unsigned int
#define light PORTB_Bit7 //過溫報警閃燈
uchar sec;
uchar displayflag; // 刷屏標志
uint tempset=25; //溫度設定
uchar displayflag; // 刷屏標志
uint tempset=25; //溫度設定
#include"1602.h"
#include"18b20.h"
#include"serial.h"
#include "time1.h"
#include "key.h"
#include"18b20.h"
#include"serial.h"
#include "time1.h"
#include "key.h"
void port_init()
{
PORTA=0xff;
DDRA=0xff;
PORTB=0xff;
DDRB=0xff;
PORTC=0xff;
DDRC=0xff;
PORTD=0xff;
DDRD=0x00;
}
{
PORTA=0xff;
DDRA=0xff;
PORTB=0xff;
DDRB=0xff;
PORTC=0xff;
DDRC=0xff;
PORTD=0xff;
DDRD=0x00;
}
void show_temp(uint k) //顯示溫度值
{
displayonechar(1,1,(k/100)+48);
displayonechar(2,1,(k%100/10)+48);
displayonechar(3,1,'.');
displayonechar(4,1,(k%10)+48);
{
displayonechar(1,1,(k/100)+48);
displayonechar(2,1,(k%100/10)+48);
displayonechar(3,1,'.');
displayonechar(4,1,(k%10)+48);
}
void show_sec(uchar k) //顯示秒表
{
displaychar(7,1,"Sec :");
displayonechar(13,1,(k/10)+48);
displayonechar(14,1,(k%10)+48);
}
{
displaychar(7,1,"Sec :");
displayonechar(13,1,(k/10)+48);
displayonechar(14,1,(k%10)+48);
}
void show_tempset(uchar k) //顯示設定值
{
displaychar(10,0,"SET:");
displayonechar(14,0,(k/10)+48);
displayonechar(15,0,(k%10)+48);
}
{
displaychar(10,0,"SET:");
displayonechar(14,0,(k/10)+48);
displayonechar(15,0,(k%10)+48);
}
void main()
{
{
uint TT; //顯示的溫度值
uint kk;
port_init();
serial_init();
time0_init();
delay(1000);
init1602();
displaychar(0,0,"Test");
uint kk;
port_init();
serial_init();
time0_init();
delay(1000);
init1602();
displaychar(0,0,"Test");
while(1)
{
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;
}
}
{
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;
}
}
key1(); //按鍵設定溫度值
}
}
}
如果網頁上的代碼發生格式錯亂請到這里下載完整版的: http://www.zg4o1577.cn/f/avrdsvb.rar
//---------------VB代碼----------
Option Explicit
Private Sub Command1_Click()
MSComm1.CommPort = 6
MSComm1.PortOpen = True
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeText
MSComm1.CommPort = 6
MSComm1.PortOpen = True
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeText
MSComm1.InputLen = 0
End Sub
Private Sub MSComm1_OnComm()
Text1.Text = MSComm1.Input
End Sub