我写的测单片机内部,外部温度,为什么,有错误,求大神指导。
#include "msp430x14x.h"
#define LCD_CON_DIR P2DIR
#define LCD_CON_OUT P2OUT
#define LCD_RS BIT0
#define LCD_RW BIT1
#define LCD_EN BIT2
#define LCD_PSB BIT3
#define LCD_RST BIT5
#define LCD_DATA_DIR P1DIR
#define LCD_DATA_IN P1IN
#define LCD_DATA_OUT P1OUT
#define DISPLAY_ON 0x0C //显示屏开命令字
#define DISPLAY_OFF 0x08 //显示屏关命令字
#define LCD_CLEAR 0x01 //清除显示屏
#define BIT_BACK 0x07 //地址归为
#define SET_FUNCTION 0x30 //定义功能
#define WRITE_DDRAM 0x91 //显示屏关命令字
#define DDRAM_CLEAR 0x02 //清除DDRAM
static void InitOsc(void);
static void Led_ShowD(char address,float data);
static void delay(void);
static void write_command(unsigned char command);
static void write_data(unsigned char data);
void main(void)
{
InitOsc(); //初始化晶振
InitPorts(); //初始化端口
InitLCD(); //初始化LCD并显示固定字符
while(1)
{
for(unsigned int t=0;i<=0xFFFF;i++);
DSTemp=DS18B20_ReadTemp();
Led_ShowD(0x80,DSTemp);
DSTemp=GetTempVal();
Led_ShowD(0x85,DSTemp);
}
}
static void InitOsc(void)
{
WDTCTL=WDTPW+WDTHOLD;
BCSCTL1&=~XT2OFF; //实际上就是 BCSCTL1=BCSCTL1&~XT2OFF;~优先级别高先算~XT2OFF,
//只有XT2OFF为0时,即打开的时候,~XT2OFF为1;BCSCTL1&1才能等于BCSCTL1。
do
{
IFG1&=~OFIFG; //置零
DelayCycles(100);
} while (IFG1&OFIFG);
BCSCTL2=SELM_2;
}
static void InitPorts(void)//初始化端口
{
LCD_CON_DIR|=LCD_PSB+LCD_RST;
LCD_CON_OUT=LCD_PSB+LCD_RST;
_NOP();
_NOP();
}
void write_command(unsigned char command) //写命令到LCD
{
LCD_DATA_DIR=0xFF; //清零
LCD_CON_DIR|=LCD_RW+LCD_RS+LCD_EN;
LCD_CON_OUT&=~(LCD_RW+LCD_RS); //RS=RW=0
delay();
LCD_CON_OUT&=~LCD_EN;
LCD_CON_OUT|=LCD_EN; //EN=1
delay();
LCD_DATA_OUT=command; //写命令
delay();
LCD_CON_OUT&=~LCD_EN; //EN=0
delay();
}
void write_data(unsigned char data) //写数据到LCD
{
LCD_DATA_DIR=0xFF;
LCD_CON_DIR|=LCD_RW+LCD_RS+LCD_EN;
LCD_CON_OUT|=LCD_RS;
delay();
LCD_CON_OUT&=~LCD_RW; //RS=1;RW=0
delay();
LCD_CON_OUT&=~LCD_EN;
LCD_CON_OUT|=LCD_EN;
delay();
LCD_DATA_OUT=data; //写数据
delay();
LCD_CON_OUT&=~LCD_EN; //EN=0
delay();
}
static void InitLCD(){
write_command(SET_FUNCTION);
write_command(LCD_CLEAR); //清楚液晶屏的显示
write_command(DISPLAY_ON); //开显示,输出中文‘外部温度’
write_command(0x80); //写出DDRAM地址
write_data(0xCD);
write_data(0xE2);
write_data(0xB2);
write_data(0xBF);
write_data(0xCE);
write_data(0xC2);
write_data(0xB6);
write_data(0xC8);
write_data(0x3A);
write_data(0x20);
//输出文字‘CPU温度’
write_command(0x88); //写输出地址
write_data(0x43);
write_data(0x50);
write_data(0x55);
write_data(0x20);
write_data(0xCE);
write_data(0xC2);
write_data(0xB6);
write_data(0xC8);
write_data(0x3A);
write_data(0x20);
}
static void Led_ShowD(char address,float data)//显示浮点数,其中整数位2位,小数位2位
{
int idata;
float fdata;
//将浮点数分拆成整数部分idata和小数部分fdata
idata=(int)data;
fdata=data-idata;
//分别显示四位数字和小数点
write_command(address); //设定显示地址
write_data(bank[data1/10]); //显示十位
write_data(bank[data%10]); //显示个位
write_data(0x2E); //显示小数点
write_data(bank[(int)(data2*10)]); //显示第一位小数
write_data(bank[(int)(data2*1000)%10]); //显示第二位小数
}
void delay(void)
{
for(int t=0;t<4000;t++)
{
_NOP();
}
}
|