OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 11164|回复: 1

我的AVR资料[程序篇] AVR驱动1602液晶+DS18B20(源码5)

[复制链接]

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2010-12-21 01:02:07 | 显示全部楼层 |阅读模式

      相当年,为了找一个可以用的DS18B20程序,不止在网上搜了多久,下过来的基本不能用.现在DS18B20的程序满天飞,这个CVAVR版本的DS18B20驱动程式,估计是我最老的版本了,这里连同1602,做了个类似温度计的程式.源码如下: 

#include <mega16.h>
#include<delay.h>
#define DQ PORTD.0//set appoint bit low
#define DDQ DDRD.0//set appoint bit high
#define GDQ PIND.0//get appoint bit level

#define LCD_DATA_PORT  PORTA   //the next three port should be the same
#define LCD_DATA_DDR   DDRA    //must use the high 4bit
#define LCD_DATA_PIN   PINA 

#define LCD_RS         PORTA.0 //RS lcd
#define LCD_WR         PORTA.1//WR lcd
#define LCD_EN         PORTA.2 //EN lcd
#define LCD_DRS        DDRA.0  //WR direction define
#define LCD_DWR        DDRA.1  //RS direction define
#define LCD_DEN        DDRA.2  //EN direction define
#define LCD_DATA       0xf0    //DATA PORT
#define uchar unsigned char
#define uint unsigned int

/*
LCD_Write(1,command)
1602 command:0x0f show cursor and flash
             0x0c do now show cursor
   
*/
void LCD_en_write(void)  //enable LCD
{
  LCD_DEN=1;//SET LCD_EN OUTPUT
  LCD_EN=1;//EN=1
  delay_us(10);
  LCD_EN=0;//EN=0
}
//cord:when 1,write command when 0,write data
//data:command or data you want to write to 1602
void LCD_Write(char cord,unsigned char data) //write data
{
  delay_us(25);
  LCD_DRS=1;//SET RS OUTPUT
  if(cord==0)LCD_RS=1; //RS=1,write data
  else LCD_RS=0;//RS=0,write command
  LCD_DATA_PORT&=0X0f;       //clr high 4bit
  LCD_DATA_PORT|=data&0xf0;  //wirte high 4bit
  LCD_en_write();
  data=data<<4;               //turn the low 4bit to high 4bit
  LCD_DATA_PORT&=0X0f;        //clr high 4bit
  LCD_DATA_PORT|=data&0xf0;   //write low 4bit
  LCD_en_write();
}
void LCD_init(void)       //lcd init
{
  LCD_DWR=1;//set en output
  LCD_WR=0;//write enable
  LCD_DATA_DDR|=LCD_DATA;   //set data port out
  LCD_EN=1;// set EN out
  LCD_RS=1;// set RS out
  delay_us(40);
  LCD_Write(1,0x28);  //4bit show
  LCD_Write(1,0x0c);  //do now show cursor
  LCD_Write(1,0x01);  //clr
  delay_ms(2);
}
//orientation a dress
//x:0 or 1
//y:0-15
void LCD_set_xy( unsigned char x, unsigned char y )  //write address funcation
{
    unsigned char address;
    if (y==0) address=0x80+x;
    else address=0xc0+x;
    LCD_Write(1,address);
}
//from a appointed address write string to 1602
//col x=0~15,row y=0,1 
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char flash *s)
{
    LCD_set_xy(X,Y); //write address
    while (*s)  // write the char to show
    {
      LCD_Write(0,*s);
      s++;
    }
     
}
//write a char to appointed address
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) //col x=0~15,row y=0,1
{
  LCD_set_xy(X,Y); //write address
  LCD_Write(0,data);
 
}
//reset ds1820
void res1820 (void)    // Reset TX
{
 DDQ=1;//SET PA0 OUTPUT
    DQ=0;//PA0 0
    delay_us(750);          // Approx 750 uS
    DQ=1;//PA0 1
    delay_us(15);//15US
}
//wait for presence of 1820
void check1820 (void)    // Wait for Presence RX 'ó?úó|'e
{
    DDQ=0;//SET PA0 INPUT
    while (GDQ==1);
    while (GDQ==0);
    delay_us(45);//??
}
//read bit data from 1820
unsigned char read1820bit (void)     // read one bit
{
    unsigned char dat;
 DDQ=1;//SET PA0 OUTPUT
    DQ=0;
 delay_us(1);
    DQ=1;
 DDQ=0;//SET PA0 INPUT
 delay_us(2);
    dat=GDQ;
    delay_us(45);
    return (dat);
}
//read one byte from 1820
unsigned char read1820byte (void)    // read one byte
{
    unsigned char i,j,dat;
    dat=0;
 for (i=1;i<=8;i++)
 {
        j=read1820bit();
        dat=(j<<7)|(dat>>1);
    }
    return (dat);
}
//write one byte to 1820
void write1820byte (unsigned char dat)    // write one byte
 {
    unsigned char j;
    unsigned char testb;
 DDQ=1;//SET PA0 OUTPUT;
    for (j=1;j<=8;j++) {
        testb=dat&0x01;
        dat=dat>>1;
        if (testb)
        {
            DQ=0;                           // Write 1
            delay_us(2);
            DQ=1;
            delay_us(45);
        }
        else
        {
            DQ=0;                          // Write 0
            delay_us(45);
            DQ=1;
            delay_us(2);
        }
    }
}
//start conver temperature
void start1820 (void)       // ds1820 start convert
{
    res1820 ();
    check1820 ();
    //delay_ms(1);
    write1820byte (0xcc);                    // skip rom
    write1820byte (0x44);                    // convert
}
//read temperature from1820
//TH,high 4 bit of temperature
//TL,low 8 bit of temperature
void read1820 (unsigned char *TH,unsigned char *TL)     // read temp
{
    res1820 ();
    check1820 ();
    //delay_ms(1);
    write1820byte (0xcc);                    // skip rom
    write1820byte (0xbe);                    // convert
    *TL=read1820byte();                    // LSB
    *TH=read1820byte();                    // MSB
}
//x^y function,do not beyond the range!
long pow(unsigned char x,unsigned char y)
{
    long ans;
    unsigned char i;
 ans=x;
 if(y==0)return 1;//x^0=1
 for(i=0;i<y-1;i++)ans*=x;
 return ans;
}
//get data from 1820
//return temperture range:-55.0~+125.9
int get1820temp(void)
{
    unsigned char TL,TH,j,jud;
    float t;   
    start1820 ();// ds1820 start convert
    read1820 (&TH,&TL);// read temperature
    jud=TH;//juder weather temperature is >0 or <0
    for(j=0;j<11;j++)//get the temperature
    {                       
        if((jud>>4)==0)//T>0
        {
             if(j<8)//low 8 bit
             {
                  t+=(TL&0x01)*pow(2,j);
                  TL>>=1;
             }
             else  //high 3 bit
             {
                  t+=(TH&0x01)*pow(2,j);
                  TH>>=1;             
             } 
        }
        else //t<0
        {
             if(j<8)//low 8 bit
             {
                  t+=(!(TL&0x01))*pow(2,j);
                  TL>>=1;
             }
             else  //high 3 bit
             {
                  t+=(!(TH&0x01))*pow(2,j);
                  TH>>=1;             
             } 
        }  
    }
    t*=0.625;//change
    if(jud>>4)return -t;//T<0
    else return t;//T>0
   
}
void main(void)
{               
   unsigned char conv[10]={'0','1','2','3','4','5','6','7','8','9'};
   int tem;
   delay_ms(200);//delay 4s
   LCD_init();
   LCD_write_string(0,0," Temperture is:");      
   while(1)
   {    
       tem=get1820temp(); 
       LCD_write_char(11,1,'C');
       LCD_write_char(10,1,223); 
       LCD_write_char(8,1,conv[tem%10]);
       LCD_write_char(7,1,'.');
       LCD_write_char(6,1,conv[(tem/10)%10]);
       LCD_write_char(5,1,conv[(tem/100)%10]);
       delay_ms(300);
   }
}

我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

10

主题

27

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
335
金钱
335
注册时间
2015-12-18
在线时间
73 小时
发表于 2016-4-4 17:21:55 | 显示全部楼层
源子哥,这个的延时函数有么?
无话可说
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-19 11:52

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表