原创 DS18B20温度传感器12864大液晶显示

2011-4-28 00:35 2755 8 8 分类: MCU/ 嵌入式

#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
//端口定义=======================================================
sbit ADCS=P2^7;//地址选通端
sbit LED_CS=P1^0;//流水灯片选端
sbit DU=P1^1;//数码管段码片选端
sbit WE=P1^2;//数码管位码片选端
sbit LCDEN_1602=P1^3;//1602液晶片选端
sbit LCDEN_12864=P1^4;//12864液晶片选端
sbit RS=P1^5;//液晶的RS端,数据命令选择端
sbit CSAD=P1^6;//AD0804转换片选端
sbit CSDA=P1^7;//DA0832转换片选端
sbit CS1=P3^7;//12864液晶左片选端
sbit CS2=P3^1;//12864液晶右片选端
//流水灯数据口  ,数码管数据口 1602  12864 AD DA 数据口为P0口
//一下是8*8点阵控制模块
sbit data_595=P3^4;//串行输入端
sbit yiwei_clk_595=P2^4;//上升沿时数据移位
sbit out_clk_595=P2^3;//上升沿时移位寄存器的数据进入数据存储器
//蜂鸣器
sbit spk=P2^0;
//继电器
sbit bell=P2^2;
#define seg_c P0 //数码管段码
#define bit_c P0 //数码管位码
sbit  T_RST = P2^5;//复位引脚
sbit  T_IO  = P3^2;//数据输入/输出引脚
sbit  T_CLK = P2^6;//串行时钟
//====================================================================
sbit rs=P2^5; //选择读/写数据或指令,0表示指令,1表示数据
sbit rw=P2^6; //选择读或写操作
sbit lcden=P2^7; //使能,下降沿锁存信号
sbit cs1=P3^1;  //左半屏显示
sbit cs2=P2^4;  //右半屏显示
sbit s1=P2^0; //键盘扫描按键,为选择键
sbit s2=P2^1; //按下加一
sbit s3=P2^2; //按下减一
sbit kong=P3^3;  //控制继电器工作,定时来控制灯
sbit dscs=P3^7;  // 时钟芯片片选
sbit dsas=P3^6;  //时钟芯片的地址信号
sbit dsrw=P3^5;  //时钟芯片的读写信号控制
sbit dsds=P3^4;  //时钟芯片的数据信号
sbit DS=P2^1;           //DS18B20的单总线信号
uint temp;
uchar th;
uchar tl;
sbit dsirq=P3^3;  //时钟芯片的中断允许请求
uchar miao,shi,fen,yue,ri,nian,xq,flag,s1num,flag1,x1,x2,x3;
uchar code hz[][32]={
{0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xC0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"一",0*/

{0x00,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x06,0x04,0x00,0x00,0x00,
0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x18,0x10,0x00},/*"二",1*/

{0x00,0x04,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x04,0x00,0x00,
0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00},/*"三",2*/

{0x00,0xFE,0x02,0x02,0x02,0xFE,0x02,0x02,0xFE,0x02,0x02,0x02,0x02,0xFE,0x00,0x00,
0x00,0x7F,0x28,0x24,0x23,0x20,0x20,0x20,0x21,0x22,0x22,0x22,0x22,0x7F,0x00,0x00},/*"四",3*/

{0x00,0x02,0x82,0x82,0x82,0x82,0xFE,0x82,0x82,0x82,0xC2,0x82,0x02,0x00,0x00,0x00,
0x20,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x30,0x20,0x00},/*"五",4*/

{0x10,0x10,0x10,0x10,0x10,0x91,0x12,0x1E,0x94,0x10,0x10,0x10,0x10,0x10,0x10,0x00,
0x00,0x40,0x20,0x10,0x0C,0x03,0x01,0x00,0x00,0x01,0x02,0x0C,0x78,0x30,0x00,0x00},/*"六",5*/

{0x00,0x00,0x00,0xFE,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x3F,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x3F,0x00,0x00,0x00,0x00},/*"日",6*/

{0x10,0x21,0x86,0x70,0x00,0x7E,0x4A,0x4A,0x4A,0x4A,0x4A,0x7E,0x00,0x00,0x00,0x00,
0x02,0xFE,0x01,0x40,0x7F,0x41,0x41,0x7F,0x41,0x41,0x7F,0x41,0x41,0x7F,0x40,0x00},//温,7

{0x00,0x00,0xFC,0x04,0x24,0x24,0xFC,0xA5,0xA6,0xA4,0xFC,0x24,0x24,0x24,0x04,0x00,
0x80,0x60,0x1F,0x80,0x80,0x42,0x46,0x2A,0x12,0x12,0x2A,0x26,0x42,0xC0,0x40,0x00},//度,8

{0x00,0x00,0x00,0xBE,0x2A,0x2A,0x2A,0xEA,0x2A,0x2A,0x2A,0x2A,0x3E,0x00,0x00,0x00,
0x00,0x48,0x46,0x41,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x49,0x49,0x41,0x40,0x00},/*"星",9*/

{0x00,0x04,0xFF,0x54,0x54,0x54,0xFF,0x04,0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00,
0x42,0x22,0x1B,0x02,0x02,0x0A,0x33,0x62,0x18,0x07,0x02,0x22,0x42,0x3F,0x00,0x00},/*"期",10*/

{0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//*"."11

{0x00,0x02,0x05,0xE2,0x18,0x04,0x02,0x02,0x02,0x02,0x02,0x02,0x04,0x1E,0x00,0x00,
0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x40,0x40,0x40,0x40,0x40,0x20,0x18,0x00,0x00},//*"℃",12

{0x20,0x10,0x08,0xFC,0x03,0x02,0x10,0x10,0x7F,0x88,0x88,0x84,0x86,0xE4,0x00,0x00,
0x00,0x04,0x04,0x05,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00},/*"华",13*/

{0x00,0x04,0x04,0xC4,0xB4,0x8C,0x87,0x84,0xF4,0x84,0x84,0x84,0x84,0x04,0x00,0x00,
0x00,0x00,0x20,0x18,0x0E,0x04,0x20,0x40,0xFF,0x00,0x02,0x04,0x18,0x30,0x00,0x00},/*"东",14*/

{0x00,0x08,0x88,0x48,0x38,0x28,0x09,0x0E,0x0C,0x08,0x98,0x28,0x68,0x48,0x08,0x00,
0x00,0x80,0x80,0x40,0x41,0x22,0x14,0x18,0x0C,0x16,0x23,0x61,0xC0,0x40,0x00,0x00},/*"交",15*/

{0x40,0x41,0xC6,0x00,0x00,0xF2,0x52,0x52,0x56,0xFA,0x5A,0x56,0xF2,0x00,0x00,0x00,
0x40,0x20,0x1F,0x20,0x40,0x5F,0x42,0x42,0x42,0x5F,0x4A,0x52,0x4F,0x40,0x40,0x00},/*"通",16*/

{0x20,0x20,0x20,0x20,0x20,0x20,0xA0,0x7F,0xA0,0x20,0x20,0x20,0x20,0x20,0x20,0x00,
0x00,0x80,0x40,0x20,0x10,0x0C,0x03,0x00,0x01,0x06,0x08,0x30,0x60,0xC0,0x40,0x00},/*"大",17*/

{0x40,0x30,0x10,0x12,0x5C,0x54,0x50,0x51,0x5E,0xD4,0x50,0x18,0x57,0x32,0x10,0x00,
0x00,0x02,0x02,0x02,0x02,0x02,0x42,0x82,0x7F,0x02,0x02,0x02,0x02,0x02,0x02,0x00},/*"学",18*/

{0x40,0x20,0x10,0x0C,0xE3,0x22,0x22,0x22,0xFE,0x22,0x22,0x22,0x22,0x02,0x00,0x00,
0x04,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x04,0x04,0x00},/*"年",19*/

{0x00,0x00,0x00,0x00,0x00,0xFF,0x11,0x11,0x11,0x11,0x11,0xFF,0x00,0x00,0x00,0x00,
0x00,0x40,0x20,0x10,0x0C,0x03,0x01,0x01,0x01,0x21,0x41,0x3F,0x00,0x00,0x00,0x00},/*"月",20*/

{0x00,0x10,0x1C,0x54,0x54,0xFC,0x54,0x55,0x56,0xFC,0x54,0x54,0x54,0x0C,0x04,0x00,
0x00,0x21,0x11,0x09,0x05,0x03,0x15,0x25,0xE9,0x4B,0x05,0x09,0x19,0x31,0x10,0x00},/*"寒",21*/
/* (16 X 16 , 宋体 )*/

{0x02,0x1C,0xC8,0x30,0x40,0x20,0x10,0x0C,0x23,0xC4,0x08,0x10,0x30,0x60,0x20,0x00,
0x02,0x7E,0x01,0x00,0x01,0x01,0x01,0x09,0x11,0x71,0x29,0x07,0x01,0x00,0x00,0x00},/*"冷",22*/
/* (16 X 16 , 宋体 )*/

{0x00,0xFC,0x44,0x44,0xFC,0x80,0xA6,0xAA,0xE2,0xA6,0xAA,0xA1,0xA9,0xA5,0x80,0x00,
0x00,0x0F,0x04,0x04,0x4F,0x20,0x58,0x46,0x27,0x2A,0x12,0x1A,0x26,0x62,0x20,0x00},/*"暖",23*/
/* (16 X 16 , 宋体 )*/

{0x00,0x24,0x24,0xA4,0xFC,0x22,0x22,0x22,0x00,0xF8,0x08,0x08,0x08,0xF8,0x00,0x00,
0x10,0x08,0x06,0x01,0xFF,0x01,0x0E,0x04,0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00},/*"和",24*/
/* (16 X 16 , 宋体 )*/

{0xA0,0xB0,0xAC,0xE3,0xA4,0xA8,0xB0,0x52,0x42,0x4A,0x52,0xF2,0x4A,0xC6,0x40,0x00,
0x00,0x7C,0x24,0x27,0x24,0x24,0x7C,0x00,0x00,0x20,0x40,0x3F,0x00,0x00,0x00,0x00},/*"舒",25*/
/* (16 X 16 , 宋体 )*/

{0x40,0x41,0xC6,0x00,0x10,0x10,0x92,0x92,0x92,0xFE,0x91,0x91,0x91,0x10,0x10,0x00,
0x40,0x20,0x1F,0x20,0x40,0x40,0x4F,0x48,0x48,0x48,0x48,0x48,0x4F,0x40,0x40,0x00},/*"适",26*/
/* (16 X 16 , 宋体 )*/

{0x00,0x80,0x80,0x50,0x4C,0x20,0x10,0xCF,0x90,0x28,0x24,0x42,0x40,0x00,0x00,0x00,
0x00,0x40,0x40,0x24,0x23,0x10,0x0C,0x03,0x04,0x08,0x14,0x33,0x20,0x60,0x20,0x00},/*"炎",27*/
/* (16 X 16 , 宋体 )*/

{0x44,0xC4,0x44,0xFF,0x24,0x04,0x14,0xA4,0x7F,0xC4,0x84,0x7C,0x84,0x00,0x00,0x00,
0xC0,0x62,0x34,0x03,0x14,0x62,0x01,0x08,0x30,0x60,0x01,0x08,0x33,0x64,0x07,0x00},/*"热",28*/
/* (16 X 16 , 宋体 )*/


};
uchar code shuzi[][16]={
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20
,0x10,0x0F,0x00},/*"0",0*/
{0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20
,0x20,0x00,0x00},/*"1",1*/
{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22
,0x21,0x30,0x00},/*"2",2*/
{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20
,0x11,0x0E,0x00},/*"3",3*/
{0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24
,0x3F,0x24,0x00},/*"4",4*/
{0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20
,0x11,0x0E,0x00},/*"5",5*/
{0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20
,0x11,0x0E,0x00},/*"6",6*/
{0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00
,0x00,0x00,0x00},/*"7",7*/
{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21
,0x22,0x1C,0x00},/*"8",8*/
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22
,0x11,0x0F,0x00},/*"9",9*/
{0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30
,0x00,0x00,0x00},/*":",10*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00
,0x00,0x00,0x00},/*".",11*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
,0x00,0x00,0x00}//清零
};
void delay(uint z)  //延时子函数
{
 uint x,y;
 for(x=z;z>0;z--)    
 for(y=11;y>0;y--);
}
void lcd_write(uchar a,uchar content) //液晶写操作,a=0,1
{
 WR=0; //写操作
 RS=a; //0表示写指令,1表示写数据
 P0=content;
 delay(1);
 LCDEN_12864=1;
 delay(1);
 LCDEN_12864=0;
 
    delay(1);
}


void clear() //清屏函数
{
 uchar i,j;
 CS1=1;CS2=1;
 for(i=0;i<8;i++)
 {
  lcd_write(0,0xb8+i); 
  lcd_write(0,0x40);
  for(j=0;j<64;j++)
  {
   lcd_write(1,0);
  }
 }
}
void display1(uchar x,uchar y,uchar n) //显示左半屏汉字
{
 uchar a,i,j;
 CS1=1;CS2=0;
 for(i=0;i<2;i++)
  {
   lcd_write(0,0xb8+x+i);
   lcd_write(0,0x40+y);
   a=i*16;
   for(j=0;j<16;j++)
   {
    lcd_write(1,hz[n][a+j]);
   
   }
 
  }
}

void display2(uchar x,uchar y,uchar n) //显示右半屏汉字
{
 uchar a,i,j;
 CS1=0;CS2=1;
 for(i=0;i<2;i++)
  {
   lcd_write(0,0xb8+x+i);
   lcd_write(0,0x40+y);
   a=i*16;
   for(j=0;j<16;j++)
   {
    lcd_write(1,hz[n][a+j]);
   
   }
 
  }
}
void display3(uchar x,uchar y,uchar n)//显示左半屏数字
{
 uchar a,i,j;
 CS1=1;CS2=0;
 for(i=0;i<2;i++)
  {
   lcd_write(0,0xb8+x+i);
   lcd_write(0,0x40+y);
   a=i*8;
   for(j=0;j<8;j++)
   {
    lcd_write(1,shuzi[n][a+j]);
   
   }
 
  }
}
void display4(uchar x,uchar y,uchar n)//显示右半屏数字
{
 uchar a,i,j;
 CS1=0;CS2=1;
 for(i=0;i<2;i++)
  {
   lcd_write(0,0xb8+x+i);
   lcd_write(0,0x40+y);
   a=i*8;
   for(j=0;j<8;j++)
   {
    lcd_write(1,shuzi[n][a+j]);
   
   }
 
  }
}
void write_ds(uchar add,uchar date)  //时钟芯片写子函数
{
 dscs=0;
 dsas=1;
 dsds=1;
 dsrw=1;
 P0=add;
 dsas=0;
 dsrw=0;
 P0=date;
 dsrw=1;
 dsas=1;
 dscs=1;    
}

uchar read_ds(uchar add)  //时钟芯片读子函数
{
  uchar ds_date;
 dsas=1;
 dsds=1;
 dsrw=1;
 dscs=0;
 P0=add;
 dsas=0;
 dsds=0;
 P0=0xff;
 ds_date=P0;
 dsds=1;
 dsas=1;
 dscs=1;
 return ds_date; 
}
void write_shijian(uchar x,uchar y,uchar date)  //液晶显示时间函数(左半屏数字)
{
 uchar shi,ge;
 shi=date/10;
 ge=date%10;
 display3(x,y,shi);
 display3(x,y+8,ge);
}
void write_sj(uchar x,uchar y,uchar date)  //液晶显示时间(右半屏数字)
{
 uchar shi,ge;
 shi=date/10;
 ge=date%10;
 display4(x,y,shi);
 display4(x,y+8,ge);
}
/*void set_time()
{
 write_ds(6,02);
 write_ds(7,23);
 write_ds(8,03);
 write_ds(9,10);
}*/
void dsreset(void)       //温度采集复位函数
{
  uint i;
  DS=0;
  i=103;
  while(i>0)i--;
  DS=1;
  i=4;
  while(i>0)i--;
}

bit tmpreadbit(void)       //read a bit
{
   uint i;
   bit dat;
   DS=0;i++;          //i++ for delay
   DS=1;i++;i++;
   dat=DS;
   i=8;while(i>0)i--;
   return (dat);
}

uchar tmpread(void)   //read a byte date
{
  uchar i,j,dat;
  dat=0;
  for(i=1;i<=8;i++)
  {
    j=tmpreadbit();
    dat=(j<<7)|(dat>>1);   //读出的数据最低位在最前面,这样刚好一个字节在DAT里
  }
  return(dat);
}

void tmpwritebyte(uchar dat)   //write a byte to ds18b20
{
  uint i;
  uchar j;
  bit testb;
  for(j=1;j<=8;j++)
  {
    testb=dat&0x01;
    dat=dat>>1;
    if(testb)     //write 1
    {
      DS=0;
      i++;i++;
      DS=1;
      i=8;while(i>0)i--;
    }
    else
    {
      DS=0;       //write 0
      i=8;while(i>0)i--;
      DS=1;
      i++;i++;
    }

  }
}

void tmpchange(void)  //DS18B20 begin change
{
  dsreset();
  delay(1);
  tmpwritebyte(0xcc);  // address all drivers on bus
  tmpwritebyte(0x44);  //  initiates a single temperature conversion发命令开始装换温度
}

void tmp()               //get the temperature
{
  float tt;
  dsreset();
  delay(1);
  tmpwritebyte(0xcc);
  tmpwritebyte(0xbe);
  tl=tmpread();
  th=tmpread();
  temp=th;
  temp<<=8;             //two byte  compose a int variable
  temp=temp|tl;
  tt=temp*0.0625;
  temp=tt*10+0.5;
 // return temp;

}
void Lcd_display()
{
 
 uchar a,a1,a2,a3;
 a1=temp/100; //百位
 a=temp%100;  //
 a2=a/10; //十位
 a3=a%10; //个位
 display3(0,32,a1);// bai
 display3(0,40,a2);//shi
 display3(0,56,a3);//ge
}
void set_alarm(uchar ashi,uchar afen,uchar amiao)  //时钟芯片定时函数
{
 write_ds(1,amiao);
 write_ds(3,afen);
 write_ds(5,ashi);
}
void Common()  //温度读显示函数
{
 tmpchange();
 tmp();
 Lcd_display();

}

void init() //初始化函数

// set_time();
 ADCS=1;
 LED_CS=1;
 P0=0xff;
 LED_CS=0;
 WE=0;
 DU=0;
 
 EA=1; //开总中断
 EX0=1; //开外部中断0
 IT0=1; //设置为电平触发方式
 set_alarm(18,00,00); //定时为下午六点
 write_ds(0x0B,0x26); //写定时器B寄存器
 read_ds(0x0c);   //读定时器C寄存器
 CS1=1;
 CS2=1;
    delay(1);
    lcd_write(0,0xc0); //显示起始行
    delay(1);
 lcd_write(0,0x3f); //开显示
 delay(1);
 display1(0,0,7); //显示"温"
 display1(0,16,8); //显示"度"
 display3(0,48,11); //显示"."
 display2(0,0,12); //显示"℃"
 display1(2,16,19); //显示年
 display1(2,48,20);  //显示月
 display2(2,16,6);  //显示日
 display3(4,16,10); //显示":"
 display3(4,40,10); //显示":"
 display2(4,16,9); //显示"星"
 display2(4,32,10); //显示"期"
 display1(6,0,13); //显示"华"
 display1(6,16,14);  //显示"东"
 display1(6,32,15); //显示"交"
 display1(6,48,16); //显示"通"
 display2(6,0,17); //显示"大"
 display2(6,16,18); //显示"学"
}
void main()

 clear(); //清屏
 init();
 while(1)
 {
  Common();
 }
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
8
关闭 站长推荐上一条 /3 下一条