中级会员
- 积分
- 312
- 威望
- 155
- 贡献
- 91
- 兑换币
- 92
- 注册时间
- 2015-1-8
- 在线时间
- 33 小时
- 毕业学校
- 沈阳航空航天大学
|
#include <hidef.h> /* common defines and macros */
#include "mc9s12xs128.h" /* derivative-specific definitions */
#define DS PORTB_PB6
#define RS PORTB_PB0 //1602端口的定义
#define RW PORTB_PB1
#define EN PORTB_PB2
#define DataPort PORTA
#define uchar unsigned char
#define uint unsigned int
void LCD_write_com(uchar com); //LCD1602函数申明
void LCD_write_data(uchar data);
void LCD_init();
//float temp1;
uchar a,b;
uint temp;
float f_temp;
uchar sh,ge;
uchar table1[]={"TEMPETURE: "}; //初始的数据的设置
uchar table2[]={"BEIJINGBIDALING"};
/*********** 延时函数***********/
void Delay(uchar t)
{
uchar loop_i,loop_j;
for(loop_i=0; loop_i<t;loop_i++)
{
for(loop_j=0;loop_j<1000;loop_j++)
{
;
}
}
}
void delay(uchar t)
{
uchar loop_i,loop_j;
for(loop_i=0; loop_i<t;loop_i++)
{
for(loop_j=0;loop_j<10;loop_j++)
{
;
}
}
}
/*************DS12B30温度传感器初始化函数*********/
void DS_Init() //DS1302初始化函数。延时一段长时间高电平,延时一段短时间高电平
{
DDRB_DDRB6=1;
DS=0;
delay(100);
DS=1;
delay(5);
}
uchar Readbyte() //读取一个字节
{
uint i;
uchar j,dat;
DDRB_DDRB6=0;
dat=0;
for(i=0;i<8;i++)
{
DS=0;
delay(1);
DS=1;
delay(1);
j=DS;
delay(4);
dat=(j<<7)|(dat>>1);
}
return dat;
}
void Writebyte(uchar dat)//写入一个字节数据
{
uchar i;
DS=1;
DDRB_DDRB6=1;
for(i=0;i<=8;i++)
{
DS=0;
dat=dat&0x01;
delay(6);
DS=1;
dat=dat>>1;
}
DS=1;
}
void Sendchangecom() //发送温度转换命令
{
DS_Init();
Delay(100);
Writebyte(0xcc);
Writebyte(0x44);
}
void Sendreadcom() //发送温度读取命令
{
DS_Init();
Delay(100);
Writebyte(0xcc);
Writebyte(0xbe);
}
uchar Get_temp( ) //读取寄存器里面存储的温度数据
{
//uchar a,b,temp;
delay(2);
Sendreadcom(); //读取寄存器里面存储的温度数据有问题,总是出现数据的丢失;
a=Readbyte(); //读低8位数据;
b=Readbyte(); //读高8位数据;
temp=b;
temp<<=8;
temp=temp|a;
f_temp=temp*0.0625;
temp=f_temp*10+0.5;
//temp1=temp*(0.0625);
// temp2=temp1*10+0.5;
return temp;
}
/************LCD1602显示函数**********/
void LCD_write_com(uchar com) //写入命令
{
RS=0;
RW=0;
EN=0;
delay(5);
DataPort=com;
delay(10);
EN=1;
delay(100);
EN=0;
}
void LCD_write_data(uchar data) //写入数据
{
RS=1;
RW=0;
EN=0;
delay(10);
DataPort=data;
delay(10);
EN=1;
delay(100);
EN=0;
}
void LCD_init() //LCD初始化函数
{
uchar i;
LCD_write_com(0x38);
LCD_write_com(0x0c); //开显示,光标不闪烁
LCD_write_com(0x06);
LCD_write_com(0x01); //清除LCD的显示内容
delay(5);
LCD_write_com(0x80);
delay(5);
for(i=0;i<11;i++)
{
LCD_write_data(table1[i]);
delay(3);
}
LCD_write_com(0x80+0x40);
delay(5);
for(i=0;i<15;i++)
{
LCD_write_data(table2[i]);
delay(3);
}
}
/*************读取温度数据函数************/
void Write_temp(uchar add1,uchar dat1) //温度显示函数
{
sh=dat1/16;
ge=dat1%16;
LCD_write_com(0x80+add1);
LCD_write_data(0x30+sh);
LCD_write_data(0x30+ge);
LCD_write_data(0xdf);
LCD_write_data(0x43);
}
/****************主函数***************/
void main(void) {
/* put your own code here */
uchar ad;
DDRA=0xff;
DDRB=0x4f;
DS_Init();
LCD_init();
// EnableInterrupts;
while(1)
{
Sendreadcom();
ad=Get_temp();
Write_temp(11,ad);
}
//for(;;) {
//_FEED_COP(); /* feeds the dog */
//} /* loop forever */
/* please make sure that you never leave main */
|
|