#include<MSP430F2122.h>
#define uchar unsigned char
#define uint unsigned int
#define port P1OUT
#define bflag (P1OUT&BIT7)
//P3.5连接DQ
#define DQ1 P3OUT&=~BIT5
#define DQ0 P3OUT|=BIT5
#define DQIN P3DIR &= ~BIT5
#define DQOUT P3DIR |= BIT5
float Temper=0.0;
int temperature=0;
unsigned char Error = 0;
uchar num[]={
/*-- 文字: 0 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0xF0,0xF8,0x08,0x68,0xF8,0xF0,0x00,0x00,0x07,0x0F,0x0B,0x08,0x0F,0x07,
/*-- 文字: 1 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x20,0x20,0x30,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x0F,0x00,0x00,
/*-- 文字: 2 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x30,0x38,0x08,0x88,0xF8,0x70,0x00,0x00,0x0C,0x0E,0x0B,0x09,0x08,0x08,0x00,
/*-- 文字: 3 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x30,0x38,0x88,0x88,0xF8,0x70,0x00,0x00,0x06,0x0E,0x08,0x08,0x0F,0x07,0x00,
/*-- 文字: 4 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0xF8,0xF8,0x00,0xE0,0xE0,0x00,0x00,0x03,0x03,0x02,0x02,0x0F,0x0F,0x02,
/*-- 文字: 5 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0xF8,0xF8,0x88,0x88,0x88,0x08,0x00,0x00,0x08,0x08,0x08,0x0C,0x07,0x03,0x00,
/*-- 文字: 6 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0xC0,0xE0,0x78,0x58,0xC8,0x80,0x00,0x00,0x07,0x0F,0x08,0x08,0x0F,0x07,0x00,
/*-- 文字: 7 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x08,0x08,0x88,0xE8,0x78,0x18,0x00,0x00,0x00,0x0E,0x0F,0x01,0x00,0x00,0x00,
/*-- 文字: 8 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x70,0xF8,0xC8,0x88,0xF8,0x70,0x00,0x00,0x07,0x0F,0x08,0x09,0x0F,0x07,0x00,
/*-- 文字: 9 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0xF0,0xF8,0x08,0x08,0xF8,0xF0,0x00,0x00,0x00,0x09,0x0D,0x0F,0x03,0x01,0x00,
/*-- 文字: . --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x0C,0x0C,0x00,0x00
};
uchar charzimo[]={
/*-- 文字: 当 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00,
0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00,
/*-- 文字: 前 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00,
0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00,
/*-- 文字: 温 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,
0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00,
/*-- 文字: 度 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00,
0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00,
/*-- 文字: 为 --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x20,0x22,0x2C,0x20,0x20,0xE0,0x3F,0x20,0x20,0x20,0x20,0xE0,0x00,0x00,0x00,
0x80,0x40,0x20,0x10,0x08,0x06,0x01,0x00,0x01,0x46,0x80,0x40,0x3F,0x00,0x00,0x00,
/*-- 文字: : --*/
/*-- Fixedsys12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
//清左半屏
void left()
{
P3OUT&=~BIT0;//cs1=0;
P3OUT|=BIT1;//cs2=1;
}
//右半屏
void right()
{
P3OUT|=BIT0;//cs1=1;
P3OUT&=~BIT1;//cs2=0;
}
//判忙
void busy_12864()
{
do{
P3OUT&=~BIT4;//e=0;
_NOP();
P3OUT&=~BIT2;//rs=0;
P3OUT|=BIT3;//rw=1;
P1OUT=0x00;
P3OUT|=BIT4;//e=1;
P3OUT&=~BIT4;//e=0;
}while(!(bflag==0X00));
}
//命令写入
void wreg(uchar c)
{
busy_12864();
P3OUT&=~BIT2;//rs=0;
P3OUT&=~BIT3;//rw=0;
P3OUT&=~BIT4;//E=0
_NOP();
_NOP();
P1OUT=c;
_NOP();
_NOP();
_NOP();
_NOP();
P3OUT|=BIT4;//e=1;
_NOP();
_NOP();
_NOP();
_NOP();
P3OUT&=~BIT4;//e=0;
}
//数据写入
void wdata(uchar c)
{
busy_12864();
P3OUT|=BIT2;//rs=1
P3OUT&=~BIT3;//rw=0
P1OUT=c;
_NOP();
_NOP();
_NOP();
_NOP();
P3OUT|=BIT4;//e=1
_NOP();
_NOP();
_NOP();
_NOP();
P3OUT&=~BIT4;
}
//显示初始页
void pagefirst(uchar c)
{
uchar i=c;
c=i|0xb8;
busy_12864();
wreg(c);
}
//设置显示初始列
void linefirst(uchar c)
{
uchar i=c;
c=i|0x40;
busy_12864();
wreg(c);}
//清屏
void ready_12864()
{
uint i,j;
left();
wreg(0x3f);
right();
wreg(0x3f);
left();
for(i=0;i<8;i++)
{
pagefirst(i);
linefirst(0x00);
for(j=0;j<64;j++)
{
wdata(0x00);
}
}
right();
for(i=0;i<8;i++)
{
pagefirst(i);
linefirst(0x00);
for(j=0;j<64;j++)
{
wdata(0x00);
}
}
}
//8*8字符显示,纵向取模,字节倒序
void display(uchar *s,uchar page,uchar line)
{
uchar i,j;
pagefirst(page);
linefirst(line);
for(i=0;i<8;i++)
{
wdata(*s);
s++;
}
pagefirst(page+1);
linefirst(line);
for(j=0;j<8;j++)
{
wdata(*s);
s++;
}
}
//16*6汉字显示,纵向取模,字节倒序
void displaychange(uchar *s,uchar page,uchar line)
{
uchar i,j;
pagefirst(page);
linefirst(line);
for(i=0;i<16;i++)
{
wdata(*s);
s++;
}
pagefirst(page+1);
linefirst(line);
for(j=0;j<16;j++)
{
wdata(*s);
s++;
}
}
void showTag(){
int i;
for(i = 0;i < 4;i++){
displaychange(charzimo + 32*i,0x03,16*i);
}
right();
for(i = 4;i < 6;i++){
displaychange(charzimo + 32*i,0x03,16*i);
}
}
void showTemperatureData(float Data)
{
//Data = 36.6;
int TensDigit; //十位
int UnitsDigit;//个位
int DecimalDigit;//十分位
TensDigit = ((int)Data)/10;
UnitsDigit = ((int)Data)%10;
DecimalDigit = (int)((Data - TensDigit * 10 - UnitsDigit) * 10);
display(num + TensDigit * 16,0x03,8 * 3);
display(num + UnitsDigit * 16,0x03,8 * 4);
display(num + 10 * 16,0x03,8 * 5);//点
display(num + DecimalDigit * 16,0x03,8 * 6);
}
//功能:us 级别延时// n=10,则延时10*5+6=56uS
//----------------------------------
void DelayNus(unsigned int n){
while(n--){};
}
void Init (void){
DQ1;
_NOP();
DQ0;
DelayNus(80); //*800us
_NOP();
DQ1;
DelayNus(14);
_NOP();
_NOP();
_NOP();
DQIN; //输入
if((P3IN&BIT5) == BIT5) //0001 1111b=1f
{
display(num + 0 * 16,0x01,16);
Error =1; //失败1
P3DIR|=BIT5;
}else
{
display(num + 1 * 16,0x01,16);
Error = 0;//初始化成功
P3DIR|=BIT5;
DQOUT;
DelayNus(20); //*600us
DQ1;
}
/*DQ0;
DelayNus(50);//延时500us
DQ1;
DelayNus(17);//延时90us
P3DIR&=~BIT5;
if((P3IN&BIT5) == BIT5) //0001 1111b=1f
{
display(num + 0 * 16,0x01,16);
Error =1; //失败1
P3DIR|=BIT5;
}else
{
display(num + 1 * 16,0x01,16);
Error = 0;//初始化成功
P3DIR|=BIT5;
DQ1;
}*/
}
//-----------------------------------
//功能:写18B20
//-----------------------------------
void Write_18B20(unsigned char n)
{
/* unsigned char i;
for(i=0;i<8;i++)
{
DQ0;
DelayNus(1);//延时13us 左右
if((n&0X01)==0X01)
DQ1;
else
DQ0;
n=n>>1;
DelayNus(9);//延时50us 以上
DQ1;
}
*/
unsigned char i;
for(i=0;i<8;i++)
{
DQOUT;
DQ0;
_NOP();
DelayNus(1);//延时13us 左右
if((n&0X01)==0X01)
DQ1;
else
DQ0;
DelayNus(8);//延时50us 以上
_NOP();
_NOP();
n=n>>1;
DQ1;
}
DQ1;
}
void Skip(void){
Write_18B20(0xcc);
}
void Convert (void){
Write_18B20(0x44);
}
void ReadDo (void){
Write_18B20(0xbe);
}
//------------------------------------
//功能:读取18B20
//------------------------------------u
unsigned char Read_18B20(void){
/*unsigned char i;
unsigned char temp;
for(i=0;i<8;i++)
{
temp=temp>>1;
DQ0;
_NOP();//延时1us
DQ1;
_NOP();
_NOP();//延时5us
_NOP();
_NOP();
_NOP();
P3DIR&=~BIT5;
if((P3IN&BIT5)==0)
{
temp=temp&0x7F;
}else
{
temp=temp|0x80;
}
DelayNus(7);//延时40u s
P3DIR|=BIT5;
DQ1;
}
return temp;
*/
unsigned char i;
unsigned char temp;
for(i=0;i<8;i++)
{
temp=temp>>1;
DQ0;
_NOP();
_NOP();
DQ1;
_NOP();
_NOP();//延时5us
_NOP();
_NOP();
_NOP();
P3DIR&=~BIT5;
DQIN;
_NOP();
_NOP();
_NOP();
if((P3IN&BIT5)==0)
{
temp=temp&0x7F;
}else
{
temp=temp|0x80;
}
DelayNus(7);//延时40u s
P3DIR|=BIT5;
DQOUT;
_NOP();
_NOP();
_NOP();
DQ1;
}
return temp;
}
void ReadTemp (void){
char temp_low,temp_high; //温度值
temp_low=Read_18B20(); //读低位
temp_high=Read_18B20(); //读高位
temperature=(temp_high&0x0f);
temperature<<=8;
temperature|=temp_low;
//Temper=temperature*0.0625;
Temper=temperature * 0.0625;
}
float ReadTemperatureData(void){
/* Init();
Skip(); //comand 0xcc
Convert(); //comand 0x44
DelayNus(200);
Init();
Skip(); //comand 0xcc
ReadDo(); //comand 0xbe
ReadTemp();
return Temper;
*/
Init();
Write_18B20(0xcc); //comand 0xcc
Write_18B20(0x44); //comand 0x44
//DelayNus(200);
Init();
Write_18B20(0xcc); //comand 0xcc
Write_18B20(0xbe); //comand 0xbe
ReadTemp();
return Temper;
}
//主函数
main()
{
WDTCTL = WDTPW + WDTHOLD;
P1DIR|=0XFF;
P3DIR|=BIT0+BIT1+BIT2+BIT3+BIT4;
DQ1;
ready_12864();
left();
showTag();
float TemperatureData = 0.0;
//TemperatureData = ReadTemperatureData();
showTemperatureData(TemperatureData);
int i = 0 ;
P3OUT&=~BIT6;
display(num + 10 * 16,0x01,0);
while(1){
DelayNus(60000);
DelayNus(60000);
DelayNus(60000);
TemperatureData = ReadTemperatureData();
showTemperatureData(TemperatureData);
i++;
if(i > 10)
i = 0;
display(num + i * 16,0x01,0);
}
}
/*
//display(num,0x03,0);
// display(num+16,0x3,8);
//display(num+32,0x03,16);
//display(num+64,0x03,32);
//display(num+96,0x03,48);
//right();
//display(num+128,0x03,64);
//display(num+160,0x03,80);
//display(num+192,0x03,96);
//display(num+224,0x03,112);
*/
|