资讯详情

AVR单片机+DS18B20简易VB显示程序

//------ 代码----------------- #include //1602代码 #include <intrinsics.h> #define RS PORTB_Bit0 #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++); } void wait() { uchar val; DATA=0xff; RS=0; RW=1; __no_operation(); __no_operation(); EN=1; __no_operation(); __no_operation(); A=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; } 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++; } } } 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; } //---串口代码------------ #define UDRE 5 #define RXC 7 void serial_init() //串口初始化 { UCSRB=0x00; UCSRA=0x02; UCSRC=0x06; UBRRL=0x67; UBRRH=0x00; UCSRB=0x18; } void serial_sendbyte(uchar u) { while(!(UCSRA&(1<<UDRE))); UDR=u; } uchar serial_receive() { while(!(UCSRA&(1<<RXC))); return UDR; } void serial_str(uchar *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'); } //--------------按键代码------- #define k1 PIND_Bit4 #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; } } //----------------定时器代码-------------- void time0_init() //定时器1 初始化 { TCCR0=0x05; //1024分频 TC 0=0x4b; MSK=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; } } //------------------------主函数---------------------- #include <iom16.h> #define uchar unsigned char #define uint unsigned int #define light PORTB_Bit7 //过温报警闪灯 uchar sec; uchar displayflag; // 刷屏标志 uint tempset=25; //温度设定 #include"1602.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; } 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); } void show_sec(uchar k) //显示秒表 { 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); } void main() { uint ; //显示的温度值 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) //秒钟变化后才刷屏
锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台