风云再一次涌起 发表于 2012-8-30 19:10:46

1602显示温度前两位总是显示为0

/*-----------------------------------------------
名称:DS18b20 温度检测液晶显示
论坛:www.doflye.net
编写:shifang
日期:2009.5
修改:无
内容:
------------------------------------------------*/
#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include<stdio.h>
#include "18b20.h"
#include "1602.h"
#include "delay.h"
unsigned char dis;
bit ReadTempFlag;//定义读时间标志
void display( uchar t);
void Init_Timer0(void);//定时器初始化
/*------------------------------------------------
            串口通讯初始化
------------------------------------------------*/
//void UART_Init(void)
//{
//    SCON= 0x50;                        // SCON: 模式 1, 8-bit UART, 使能接收
//    TMOD |= 0x20;               // TMOD: timer 1, mode 2, 8-bit 重装
//    TH1   = 0xFD;               // TH1:重装值 9600 波特率 晶振 11.0592MHz
//    TR1   = 1;                  // TR1:timer 1 打开                        
//    //EA    = 1;                  //打开总中断
//    //ES    = 1;                  //打开串口中断
//        TI=1;
//}
/*------------------------------------------------
                  主函数
------------------------------------------------*/
void main (void)
{                  
int temp;
float temperature;
//char displaytemp;//定义显示区域临时存储数组

LCD_Init();         //初始化液晶
DelayMs(20);          //延时有助于稳定
LCD_Clear();          //清屏
Init_Timer0();
//UART_Init();
Lcd_User_Chr();       //写入自定义字符
LCD_Write_String(0,0," www.doflye.net ");
LCD_Write_Char(13,1,0x01);//写入温度右上角点
LCD_Write_Char(14,1,'C'); //写入字符C

while (1)         //主循环
{

if(ReadTempFlag==1)
{
ReadTempFlag=0;
temp=ReadTemperature();
temperature=(long float)temp*62.5;
display( temperature);
LCD_Write_Char(0,1,dis);
LCD_Write_Char(1,1,dis);
LCD_Write_Char(2,1,dis);
LCD_Write_Char(3,1,dis);
LCD_Write_Char(4,1,dis);
LCD_Write_Char(5,1,dis);
}       
}
}
void display( uchar t)
{
        int i;
        i=t/10000;
        dis=i+'0';
        DelayMs(1);
    i=t%10000/1000;
        dis=i+'0';
        DelayMs(1);
        dis='.';
        DelayMs(1);
        i=t%1000/100;
        dis=i+'0';
        DelayMs(1);
        i=t%100/10;
        dis=i+'0';
        DelayMs(1);
        i=t%10;
        dis=i+'0';
}
/*------------------------------------------------
                  定时器初始化子程序
------------------------------------------------*/
void Init_Timer0(void)
{
TMOD |= 0x01;          //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响                     
//TH0=0x00;              //给定初值
//TL0=0x00;
EA=1;            //总中断打开
ET0=1;         //定时器中断打开
TR0=1;         //定时器开关打开
}
/*------------------------------------------------
               定时器中断子程序
------------------------------------------------*/
void Timer0_isr(void) interrupt 1
{
static unsigned int num;
TH0=(65536-2000)/256;                  //重新赋值 2ms
TL0=(65536-2000)%256;

num++;
if(num==300)      //
   {
    num=0;
    ReadTempFlag=1; //读标志位置1
        }
}

/*-----------------------------------------------
名称:18B20温度传感器
网站:www.doflye.net
编写:shifang
日期:2009.5
修改:无
内容:18B20单线温度检测的应用样例程序
------------------------------------------------*/
#include"delay.h"
#include"18b20.h"
/*------------------------------------------------
                  18b20初始化
------------------------------------------------*/
bit Init_DS18B20(void)
{
bit dat=0;
DQ = 1;    //DQ复位
DelayUs2x(5);   //稍做延时
DQ = 0;         //单片机将DQ拉低
DelayUs2x(200); //精确延时 大于 480us 小于960us
DelayUs2x(200);
DQ = 1;      //拉高总线
DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲
dat=DQ;      //如果x=0则初始化成功, x=1则初始化失败
DelayUs2x(25); //稍作延时返回
return dat;
}

/*------------------------------------------------
                  读取一个字节
------------------------------------------------*/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
   dat|=0x80;
DelayUs2x(25);
}
return(dat);
}
/*------------------------------------------------
                  写入一个字节
------------------------------------------------*/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
DelayUs2x(25);
DQ = 1;
dat>>=1;
}
DelayUs2x(25);
}

/*------------------------------------------------
                  读取温度
------------------------------------------------*/
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
DelayMs(10);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();   //低位
b=ReadOneChar();   //高位

b<<=8;
t=a+b;

return(t);
}


/*-----------------------------------------------
名称:LCD1602
论坛:www.doflye.net
编写:shifang
日期:2009.5
修改:无
内容:
引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK
------------------------------------------------*/
#include "1602.h"
#include "delay.h"

sbit RS = P2^4;   //定义端口
sbit RW = P2^5;
sbit EN = P2^6;

#define RS_CLR RS=0
#define RS_SET RS=1

#define RW_CLR RW=0
#define RW_SET RW=1

#define EN_CLR EN=0
#define EN_SET EN=1

#define DataPort P0

/*------------------------------------------------
            判忙函数
------------------------------------------------*/
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
}
/*------------------------------------------------
            写入命令函数
------------------------------------------------*/
void LCD_Write_Com(unsigned char com)
{
while(LCD_Check_Busy()); //忙则等待
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
/*------------------------------------------------
            写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
while(LCD_Check_Busy()); //忙则等待
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}

/*------------------------------------------------
                清屏函数
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
/*------------------------------------------------
            写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{   
if (y == 0)
        {   
       LCD_Write_Com(0x80 + x);   //表示第一行
        }
else
        {      
        LCD_Write_Com(0xC0 + x);      //表示第二行
        }      
while (*s)
        {   
LCD_Write_Data( *s);   
s ++;   
        }
}
/*------------------------------------------------
            写入字符函数
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{   
if (y == 0)
        {   
        LCD_Write_Com(0x80 + x);   
        }   
else
        {   
        LCD_Write_Com(0xC0 + x);   
        }      
LCD_Write_Data( Data);
}
/*------------------------------------------------
            初始化函数
------------------------------------------------*/
void LCD_Init(void)
{
   LCD_Write_Com(0x38);    /*显示模式设置*/
   DelayMs(5);
   LCD_Write_Com(0x38);
   DelayMs(5);
   LCD_Write_Com(0x38);
   DelayMs(5);
   LCD_Write_Com(0x38);
   LCD_Write_Com(0x08);    /*显示关闭*/
   LCD_Write_Com(0x01);    /*显示清屏*/
   LCD_Write_Com(0x06);    /*显示光标移动设置*/
   DelayMs(5);
   LCD_Write_Com(0x0C);    /*显示开及光标设置*/
   }
/*------------------------------------------------   
设定二个自定义字符,LCD1602中自定义字符的地址为0x00--0x07,
即可定义8个字符
这里我们设定把一个自定义字符放在0x00位置(000),
另一个放在0x01位子(001)
------------------------------------------------*/
void Lcd_User_Chr(void)
{ //第一个自定义字符
LCD_Write_Com(0x40); //"01 000 000"第1行地址 (D7D6为地址设定命令形式D5D4D3为字符存放位置(0--7),D2D1D0为字符行地址(0--7))
LCD_Write_Data(0x00); //"XXX 11111" 第1行数据(D7D6D5为XXX,表示为任意数(一般用000),D4D3D2D1D0为字符行数据(1-点亮,0-熄灭)
LCD_Write_Com(0x41); //"01 000 001"第2行地址
LCD_Write_Data(0x04); //"XXX 10001" 第2行数据
LCD_Write_Com(0x42); //"01 000 010"第3行地址
LCD_Write_Data(0x0e); //"XXX 10101" 第3行数据
LCD_Write_Com(0x43); //"01 000 011"第4行地址
LCD_Write_Data(0x0e); //"XXX 10001" 第4行数据
LCD_Write_Com(0x44); //"01 000 100"第5行地址
LCD_Write_Data(0x0e); //"XXX 11111" 第5行数据
LCD_Write_Com(0x45); //"01 000 101"第6行地址
LCD_Write_Data(0x1f); //"XXX 01010" 第6行数据
LCD_Write_Com(0x46); //"01 000 110"第7行地址
LCD_Write_Data(0x04); //"XXX 11111" 第7行数据
LCD_Write_Com(0x47); //"01 000 111"第8行地址
   LCD_Write_Data(0x00); //"XXX 00000" 第8行数据
//第二个自定义字符

LCD_Write_Com(0x48); //"01 001 000"第1行地址
LCD_Write_Data(0x03); //"XXX 00001" 第1行数据
LCD_Write_Com(0x49); //"01 001 001"第2行地址
LCD_Write_Data(0x03); //"XXX 11011" 第2行数据
LCD_Write_Com(0x4a); //"01 001 010"第3行地址
LCD_Write_Data(0x00); //"XXX 11101" 第3行数据
LCD_Write_Com(0x4b); //"01 001 011"第4行地址
LCD_Write_Data(0x00); //"XXX 11001" 第4行数据
LCD_Write_Com(0x4c); //"01 001 100"第5行地址
LCD_Write_Data(0x00); //"XXX 11101" 第5行数据
LCD_Write_Com(0x4d); //"01 001 101"第6行地址
LCD_Write_Data(0x00); //"XXX 11011" 第6行数据
LCD_Write_Com(0x4e); //"01 001 110"第7行地址
LCD_Write_Data(0x00); //"XXX 00001" 第7行数据
LCD_Write_Com(0x4f); //"01 001 111"第8行地址
LCD_Write_Data(0x00); //"XXX 00000" 第8行数据
}


millwood0 发表于 2012-8-30 19:18:59

名称:DS18b20 温度检测液晶显示...you probably want to learn how to think logically - the first step in debugging your code.

posting so much unrelated garbage doesn't help your cause.

linucos 发表于 2012-8-30 19:21:46

这么多代码,看着头疼,帮你顶顶!

风云再一次涌起 发表于 2012-8-30 19:27:14

millwood0 发表于 2012-8-30 19:18 static/image/common/back.gif
you probably want to learn how to think logically - the first step in debugging your code.

posting...

Thank you for your advise!

风云再一次涌起 发表于 2012-8-30 20:03:18

linucos 发表于 2012-8-30 19:21 static/image/common/back.gif
这么多代码,看着头疼,帮你顶顶!

谢谢,已解决

millwood0 发表于 2012-9-1 02:05:01

谢谢,已解决

assholes like you who don't give back to the community should have their accounts permanently banned.

风云再一次涌起 发表于 2012-9-5 10:00:47

millwood0 发表于 2012-9-1 02:05 static/image/common/back.gif
assholes like you who don't give back to the community should have their accounts permanently bann ...

Why are you so mean? I do don't have the right to ask questions?I am just a new gay.

millwood0 发表于 2012-9-5 19:26:11

Why are you so mean?

because you are an asshole.

You came here to get help to solve your problem. the community here helped you solve your problem. so now it is time for you to give back to the community, by telling people who you had the problem and how you solved. that way, people encountering the same problem in the future can follow through quickly.

instead, you did none of that. you are simple a parasite leeching off other people.

I am just a new gay.

we know that already.

devcang 发表于 2012-9-6 13:21:53

we know that already.

We knew !{:titter:}
页: [1]
查看完整版本: 1602显示温度前两位总是显示为0