打印

求找错:LPC2114的温度显示

[复制链接]
1215|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
bylxy000|  楼主 | 2012-11-20 11:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*-----------------------------------------------
  名称:温度显示
  日期:2012.11
  P0.0-P0.7  LCD_1602 IO
  P0.8=LCD_RS,  P0.9=LCD_RW,
  P0.10=LCD_E,  P0.11=DQ
问题应该是在  uchar ReadOneChar(void)
刚接触ARM 求指点一下
------------------------------------------------*/
#include <LPC21xx.H>
#define uchar unsigned char
#define uint unsigned int
char table[]="l_Temp    ";
char table1[]=" :   ";
uchar num,temp,ftemp;
/*------------------------------------------------
                    延迟
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{   
while(--t);
}
void DelayMs(unsigned char t)
{
     
while(t--)
{
     //大致延时1mS
     DelayUs2x(245);
  DelayUs2x(245);
}
}
/*------------------------------------------------
                    初始化ds18b20
------------------------------------------------*/
void Init_DS18B20(void)
{
IO0SET=0x00000800;                          //DQ
DelayUs2x(5);   //稍做延时
IO0CLR=0x00000800;
DelayUs2x(100); //精确延时 大于 480us 小于960us
IO0SET=0x00000800;
DelayUs2x(50); //15~60us 后 接收60-240us的存在脉冲
}
/*------------------------------------------------
                    读取一个字节
------------------------------------------------*/
uchar ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
IO0DIR=0x000007ff;      //设置DQ为输入模式
for (i=8;i>0;i--)
{
  dat>>=1;
  IO0DIR=0x00000fff;
  IO0SET=0x00000800;
  IO0CLR=0x00000800;         
  IO0SET=0x00000800;
   IO0DIR=0x000007ff;      
  if(IO0PIN&0x00000800==0x00000800)            //读取引脚状态,DQ=1,插入1,DQ=0,直接移位
   dat|=0x00000080;
  DelayUs2x(10);
}
return(dat);
}
/*------------------------------------------------
                    写入一个字节
------------------------------------------------*/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
IO0DIR=0x00000fff;     //设置DQ为输出模式      
for (i=8; i>0; i--)
{
  IO0SET=0x00000800;
  IO0CLR=0x00000800;
  if(dat&0x00000001)
   IO0SET=0x00000800;
  else IO0CLR=0x00000800;
  DelayUs2x(10);
  IO0SET=0x00000800;
  dat>>=1;
}
DelayUs2x(25);
}
/*------------------------------------------------
                    读取温度
------------------------------------------------*/
int ReadTemperature(void)
{
  unsigned char a=0;
  unsigned int b=0;
  Init_DS18B20();
  WriteOneChar(0xCC); // 跳过读序号列号的操作
  WriteOneChar(0x44); // 启动温度转换
  DelayMs(10);
  Init_DS18B20();
  WriteOneChar(0xCC); //跳过读序号列号的操作
  WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就
  a=ReadOneChar();   //低位
  b=ReadOneChar();   //高位
  temp=b;
  temp<<=4;
  temp<<=4;     
  temp=temp|a;
  ftemp=temp*0.0625;   
  temp=ftemp*10+0.5;   
  ftemp=ftemp+0.05;   
  return temp;            
}
/*------------------------------------------------
                    LCD1602初始化
------------------------------------------------*/
void write_com(uchar com)
{
  IO0CLR=0x00000700;
  IO0SET=com;
  IO0CLR=(~com&0x000000ff);

  DelayMs(5);
  IO0SET=0x00000400;
  DelayMs(5);
  IO0CLR=0x00000400;
}
void write_date(uchar date)
{
  IO0CLR=0x00000200;
  IO0SET=0x00000100;
  IO0SET=date;
  IO0CLR=(~date&0x000000ff);
  DelayMs(5);
  IO0SET=0x00000400;
  DelayMs(5);
  IO0CLR=0x00000400;
}
void Init_1602()
{
   IO0CLR=0x00000600;
   write_com(0x38);
   write_com(0x0c);
   write_com(0x06);
   write_com(0x01);
   write_com(0x80+3);
   for(num=0;num<11;num++)
   {
     write_date(table[num]);
     DelayMs(30);
   }  
   write_com(0x80+0x40+4);
   for(num=0;num<9;num++)
   {
     write_date(table1[num]);
     DelayMs(30);
   }   
}
void write_1602(uint date)
{ uint qian,bai,shi;  
qian=date/100;
        write_com(0xc6);
        write_date(0x30+qian);
  
bai=date%100/10;
        write_com(0xc8);
        write_date(0x30+bai);
    shi=date%10;
  write_com(0xc9);
        write_date(0x30+shi);
  write_com(0xcb);
        write_date('C');
}
/*------------------------------------------------
                    主程序
------------------------------------------------*/
int main()
{
PINSEL0 = 0x00000000;  //低16位设置为I/O模式
IO0DIR = 0x00000fff;   //0-11输出
   
Init_1602();
while(1)
{
     write_1602(ReadTemperature());
     DelayMs(20);
}
}

相关帖子

沙发
阿南| | 2012-11-20 14:36 | 只看该作者
先说明是什么问题吧

使用特权

评论回复
板凳
bylxy000|  楼主 | 2012-11-20 16:02 | 只看该作者
2# 阿南
无法读取温度  显示模块没问题

使用特权

评论回复
地板
阿南| | 2012-11-20 16:51 | 只看该作者
那就确认一下DS18B20读写是否有问题

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

16

主题

83

帖子

1

粉丝