采用廉价的NTC热敏电阻设计高精度温度计的方案, 根据NTC热敏电阻的温度特性,采用了较具新意程序算法;摆脱了传统的查表显示温度的方法, 特点:程序中无须查表,打破了常规的查表显示温度的方法。
//
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define ERRORCOUNT 10
/*
unsigned char code keytable[]={0x18,0x14,0x44,0x24,0x42,0x22,0x12, //CW,Pulse,UP,DOWN,Left,Right,OK
0x41,0x21,0x11,0x00}; //MENU,Return,Reset
//******************
unsigned char code Blank[]=" ";
*/
sbit lcd_rs=P3^3;
sbit lcd_rw=P3^4;
sbit lcd_e =P3^5;
sbit DOG =P3^6;
//=========================== MAX512驱动程序 =======================================
定义/===================================================================
sbit MAX512_CS =P2^2;
sbit MAX512_DIN=P3^7;
sbit MAX512_CLK=P1^0;
/
//参数:
// Chanel: 输出口
// 0x01->A
// 0x02->B
// Vout=输出电压*100 范围:0-5 如:2.5 为250
void DAOut(unsigned char Command,unsigned int VOut)
{
unsigned char DA,Comd;
float Vref=4.986;
unsigned char i;
for (i=1;i<10;i++)
_nop_();
Comd=Command;
DA=((VOut/Vref)*256)/100.0+0.5;
MAX512_CS=0;
MAX512_CLK=0;
for ( i=0;i<8;i++)
{
MAX512_DIN=(bit)(Comd &0x80);
MAX512_CLK=1;
MAX512_CLK=0;
Comd<<=1;
}
MAX512_CLK=0;
for (i=0;i<8;i++)
{
MAX512_DIN=(bit)(DA&0x80);
MAX512_CLK=1;
MAX512_CLK=0;
DA<<=1;
}
MAX512_CS=1;
}
/初始化MAX512
void InitMAX512()
{
DAOut(0x07,0x00);//所有DAC有效
DAOut(0x37,0x00);//关断BC
}
/// END OF MAX512 //
/*========================= TLC2543驱动程序 ====================================
***************** 2543控制引脚宏定义 ********************/
sbit CLOCK = P1^0; /*2543时钟*/
sbit D_IN = P1^1; /*2543输入*/
sbit D_OUT = P1^2; /*2543输出*/
sbit _CS = P1^3; /*2543片选*/
/**************************************
名称:delay
功能:延时模块
输入参数:n要延时的周期数
输出参数:无
*************************************/
void TLC_delay(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
{
_nop_();
}
}
/**************************************
名称:read2543
功能:TLC2543驱动模块
输入参数:port通道号
输出参数:ad转换值
*************************************/
unsigned int read2543(unsigned char port)
{
unsigned int ad=0,i;
P1|=0xff;
TLC_delay(10);
CLOCK=0;
_CS=0;
port<<=4;
for(i=0;i<12;i++)
{
if(D_OUT) ad|=0x01;
D_IN=(bit)(port&0x80);
CLOCK=1;
TLC_delay(3);
CLOCK=0;
TLC_delay(3);
port<<=1;
ad<<=1;
}
_CS=1;
ad>>=1;
return(ad);
}
// END OF TLC2543 //
void Delay(unsigned char DelayCount)
{
while(DelayCount--) DOG=!DOG;
}
/*
//========================== 24LC64 ==================================================
//========================== 存储 ====================================================
sbit SDA=P2^1;
sbit SCL=P2^0;
enum eepromtype {M2401,M2402,M2404,M2408,M2416,M2432,M2464,M24128,M24256};
enum eepromtype EepromType;
extern bit RW24XX(unsigned char *DataBuff,unsigned char ByteQuantity,unsigned int Address,
unsigned char ControlByte,enum eepromtype EepromType)
{
void Delay(unsigned char DelayCount);
void IICStart(void);
void IICStop(void);
bit IICRecAck(void);
void IICNoAck(void);
void IICAck(void);
unsigned char IICReceiveByte(void);
void IICSendByte(unsigned char sendbyte);
unsigned char data j,i=ERRORCOUNT;
bit errorflag=1;
while(i--)
{
IICStart();
IICSendByte(ControlByte&0xfe);
if(IICRecAck())
continue;
if(EepromType>M2416)
{
IICSendByte((unsigned char)(Address>>8));
if(IICRecAck())
continue;
}
IICSendByte((unsigned char)Address);
if(IICRecAck())
continue;
if(!(ControlByte&0x01))
{
j=ByteQuantity;
errorflag=0; //********clr errorflag
while(j--)
{
IICSendByte(*DataBuff++);
if(!IICRecAck())
continue;
errorflag=1;
break;
}
if(errorflag==1)
continue;
break;
}
else
{
IICStart();
IICSendByte(ControlByte);
if(IICRecAck())
continue;
while(--ByteQuantity)
{
*DataBuff++=IICReceiveByte();
IICAck();
}
*DataBuff=IICReceiveByte(); //read last byte data
IICNoAck();
errorflag=0;
break;
}
}
IICStop();
if(!(ControlByte&0x01))
{
Delay(255);
Delay(255);
Delay(255);
Delay(255);
}
return(errorflag);
}
//==*****************以下是对IIC总线的操作子程序***==/
//==*****************启动总线**********************==/
void IICStart(void)
{
SCL=0; //
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
SDA=0;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
SDA=1; //
}
//==*****************停止IIC总线****************==/
void IICStop(void)
{
SCL=0;
SDA=0;
SCL=1;
_nop_();
_nop_();
_nop_();
SDA=1;
_nop_();
_nop_();
_nop_();
SCL=0;
}
//==**************检查应答位*******************==
bit IICRecAck(void)
{
SCL=0;
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
CY=SDA; //因为返回值总是放在CY中的
SCL=0;
return(CY);
}
//==***************对IIC总线产生应答*******************
void IICACK(void)
{
SDA=0;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
_nop_();
SDA=1;
}
//==*****************不对IIC总线产生应答***************
void IICNoAck(void)
{
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}
//=*******************向IIC总线写数据*********************
void IICSendByte(unsigned char sendbyte)
{
unsigned char data j=8;
for(;j>0;j--)
{
SCL=0;
sendbyte<<=1; //无论C51怎样实现这个操作,始终会使CY=sendbyte^7;
SDA=CY;
SCL=1;
}
SCL=0;
}
//==**********************从IIC总线上读数据子程序**********
unsigned char IICReceiveByte(void)
{
register receivebyte,i=8;
SCL=0;
while(i--)
{
SCL=1;
receivebyte=(receivebyte<<1)|SDA;
SCL=0;
}
return(receivebyte);
}
//===***************一个简单延时程序************************
void Delay(unsigned char DelayCount)
{
while(DelayCount--) DOG=!DOG;
}
END 24LCXX
*/
//======================== LCD =====================================================
//================================ LED MAX7219 =================================
//==******************
// 函 数 名: SentCommMax7219
// 功 能: 向 MAX7219 送命令(16 bit)
//*****************
sbit DIN = P2^5;
sbit CLK = P2^3;
sbit LOAD = P2^4;
/* 定义显示数字0-9数组 */
unsigned int code DispData_09[16]={0x7E,0x30,0x6D,0x79,0x33,0x5B,0x5F,0x70,0x7F,0x7B,0x01,0X4F,0X37,0X0E,0X67,0X00};
/* 定义显示位置L0-L3数组 */
unsigned int code DispPlace_07[8]={0x0100,0x0200,0x0300,0x0400,0x0500,0x0600,0x0700,0x0800};
//******************
void SentCommMax7219(unsigned int n)
{
unsigned int i;
CLK = 0;
LOAD = 0;
DIN = 0;
for (i = 0x8000; i >= 0x0001; i = i >> 1)
{
DOG=!DOG;
if ((n & i) == 0)
DIN = 0;
else DIN = 1;
CLK = 1;
CLK = 0;
}
LOAD = 1;
}
/********************************
//*******************************
函 数 名: InitMax7219
功 能: 初始化 MAX7219
//*******************************
*********************************/
void InitMax7219(void)
{
SentCommMax7219( 0x0C01 ); /* 置LED为正常状态 */
SentCommMax7219( 0x0A0a ); /* 置LED亮度为31/32 */
SentCommMax7219( 0x0B07 ); /* 置LED扫描范围DIGIT0-7 */
SentCommMax7219( 0x0900 ); /* 置LED显示为不译码方式 */
}
/*************************
函 数 名: ClsMax7219
功 能: 清除显示
**************************/
void ClsMax7219()
{
unsigned int i;
for (i = 0x0100; i <= 0x0800; i += 0x0100 )
SentCommMax7219(i); /* DIGIT0-7 = 0 */
}
/****************************
函 数 名: DispMax7219
功 能: Max7219显示数字
说 明: DP 显示小数点: 1xxxxxxx :ON | 0xxxxxxx:OFF
调用函数:
全局变量:
入口参数: 显示位置: H = 0 ~ 7 [7][6][5][4][3][2][1][0]
显示数值: n = 0 ~ 9
********************************/
void DispMax7219(unsigned char H, unsigned char n)
{
if ((n & 0x80 ) == 0)
{
SentCommMax7219(DispPlace_07[H] | DispData_09[n] );
}
else
{
SentCommMax7219(DispPlace_07[H] | DispData_09[n & 0x7F] | 0x80);
}
DOG=!DOG;
}
//----------------------------------
//*****************************
//Position 为位置,1或2
//Sign 为正负1为+,0位-
//如果为245,显示为24.5
//******************************
void MAX7219_ShowFloat(unsigned char Position,unsigned int Num,unsigned char Sign)
{
unsigned char temp[4];
DOG=!DOG;
temp[0]=Num/100;
Num=Num%100;
temp[1]=Num/10;
temp[1]|=0x80;//小数点
Num=Num%10;
temp[2]=Num;
if (!Sign)
temp[3]=0x0a;
else
temp[3]=0x00;
if (Position==1)
{
if (temp[3]==0x00) //正数
SentCommMax7219(0x0400);
else
DispMax7219(0x03, temp[3]);
if (temp[0]==0x00)
SentCommMax7219(0x0300);
else
DispMax7219(0x02, temp[0]);
DispMax7219(0x01, temp[1]);
DispMax7219(0x00, temp[2]);
}
else //=========Position=2
{
if (temp[3]==0x00)// 正数
SentCommMax7219(0x0800);
else
DispMax7219(0x07, temp[3]);
if (temp[0]==0x00)
SentCommMax7219(0x0700);
else
DispMax7219(0x06, temp[0]);
DispMax7219(0x05, temp[1]);
DispMax7219(0x04, temp[2]);
}
}
/ END MAX7219