lcd的c语言源代码,EM78P468 NTC lcd测温设计C语言源代码

/********************************************************

* Description: 468N RC temperature *

* Company: HANTA (suzhou) LTD. *

* Author: Sunli *

* Date: 03/09/2007

* 最后更新日期:13/09/2007 *

* Version: v1.0 *

*******************************************************/

#i nclude "em78x468xx.h"

#i nclude "table.h"

#define DISI() _asm{disi}

#define ENI() _asm{eni}

#define SLEP() _asm{slep}

#define NOP() _asm{nop}

#define WDTC() _asm{wdtc}

#define uchar unsigned char

#define ushort unsigned short

#define ulong unsigned long

#define Rp R55

#define Rc R61

#define Rm R60

#define SCL R63

#define SDA R62

#define LOW R65

#define MID R66

#define HI R67

#define COOL R85

#define HOT R86

#define COL_RST R87

#defineLEDR54

#define COOL_IF_L R57==0 //如果COOL关

#define COOL_IF_H R57==1 //如果COOL开

#define HEAT_IF_L R56==0 //如果HEAT关

#define HEAT_IF_H R56==1 //如果HEAT开

#define SCL_L R63=0 //I2总线时钟线

#define SCL_H R63=1

#define SDA_L R62=0 //I2总线数据线

#define SDA_H R62=1

#define Rp_TO_IN P5CR|=0x20;NOP()

#define Rp_TO_OUT P5CR&=0xdf;NOP()

#define SDA_TO_IN P6CR|=0x04; NOP() //设数据线位输入

#define SDA_TO_OUT P6CR&=0xfb; NOP() //设数据线位输出

#define SDA_IF_L R62==0 //如果sda为低

#define SDA_IF_H R62==1 //如果sda为高

#define DELAY_us NOP();NOP();NOP();NOP()

#define PAGE_SIZE 8

#define SIZE 0x00ff

#define ON_OFF 0x1e

#define MODE 0x1d

#define FAN 0x1b

#define INCREASE 0x17

#define DECREASE 0x0f

#define W_ADD_COM 0xa0 //写字节命令及器件地址(根据地址实际情况改变), 1010 A2 A1 A0 0

#define R_ADD_COM 0xa1 //读命令字节及器件地址(根据地址实际情况改变), 1010 A2 A1 A0 1

#defineLCD_init(init_v) _asm{mov a,@init_v}

_asm{mov %LCDCR,a}

uchar lcd_ram[10]=

{ // 0 , 1 , 2 , 3 , 4 , 5 , 6, 7 , 8 , 9

0xdf,0x0e,0xeb,0xaf,0x3e,0xbd,0xfd,0x0f,0xff,0xbf

};

ushort dly;

ushort test_temp[6];

ushort Rm_t,Rc_t;

uchar RC_num,Rmin_p,Rmax_p;

ushort Rmin_t,Rmax_t;

ulong Ttmp;

uchar key_state = 0;//按键值

uchar new_key = 0;

uchar key_tCC= 0;

uchar key_temp= 0;

uchar timer_cnt=0;

uchar r_buf[2];

uchar w_buf[2];

uchar set_temper =0;

uchar fan_st = 0;

uchar mode_on_off=0;

uchar timer1,times;

//ushort timer;

bit key_flag=0;

bit timer_2ms_ok=0;

bit COL_RST_FLAG=0;

/******************函数声明********************/

void main(void);

void sys_init(void);

void tms(uchar i);

void i2cstart(void);

uchar i2cwt(uchar a);

uchar i2crd(void);

void i2cstop(void);

uchar wt24c(uchar *p, uchar ad, uchar n);

void rd24c(uchar *p_dst, uchar ad_rsc, uchar num);

uchar KeySCAN(void);

void KeyDo(uchar key);

//void key_find(void);

void display(uchar v,uchar tamp_v,uchar set_tmp);

unsigned char test_temperature(void);

unsigned char Bin1toBcd(unsigned char Xbin1);

/*********************end*********************/

void sys_init(void)

{

SBPCR=0x17; //rc 2.13mHz,sleep->Idle mode,sub-cLOCk run program

IRCR=0; //dISAble IR/PWM,port5 is general i/o

ISR=0; //clear count1 interrupt flag bit

P5CR=0xc3; //port7 is lcd segment,port8 is general i/o

P6CR=0x00;

P7CR=0;

P8CR=0x1f;

CNT12CR=0x06;

CNT1PR=5;

CNT1EN=1;

HPWTPR=0;

LPWTPR=0;

IMR|=0x08; //enable count1 interrupt

// WUCR=0x70; //enable p60~3 wake up

// TCCCR=0x0f; //disable main interrupt,TCC pre-scaler 1:256

WDTCR=0xff; //0xf7; //disable WDT

HLPWTCR=0x77;

P6PH=0xff; //enable port6 internal pull high

P6OD=0; //disable por6 open-drain

P8PH=0xff; //disable port8 internal pull high

P6PL=0; //disable port6 internal pull down

PORT5=0x00;

PORT6=0x00;

PORT8=0x00;

//变量清零

COL_RST_FLAG=0;

}

void tms(uchar i)

{

uchar j;

for(;i>0;i--)

{

for(j=0;j<200;j++);

WDTC();

}

}

/**********************W/R EEPROM*******************************************/

//起始信号

void i2cstart(void)

{

SCL_L; DELAY_us;

SDA_H; DELAY_us;

SCL_H; DELAY_us;

SDA_L; DELAY_us;

SCL_L; DELAY_us;

}

//把一个字节数据输入器件,并以收到应答信号为止

//写入成功返回1,失败返回0

uchar i2cwt(uchar a)

{ uchar i;

for(i=0;i<8;i++)

{ SCL_L; DELAY_us;

if((a< else SDA_L;

DELAY_us;

SCL_H; DELAY_us;

}

SCL_L; DELAY_us;

SDA_H; DELAY_us;

SCL_H; DELAY_us;

SDA_TO_IN;

if(SDA_IF_L) //测试有无应答

{ SDA_TO_OUT;

return(1); //有应答

}

else

{ SDA_TO_OUT;

return(0); //无应答

}

}

//i2c读要调用的函数

//从器件读出一个字节

uchar i2crd(void)

{ uchar i,temp;

for(i=0;i<8;i++)

{ SCL_L; DELAY_us;

SDA_H; DELAY_us; //置数据线接上内部上拉(数据输入方式),此为必须

SCL_H; DELAY_us;

temp<<=1;

SDA_TO_IN;

if(SDA_IF_H) temp+=1;

DELAY_us;

SDA_TO_OUT;

}

SCL_L; DELAY_us; //主器件应答脉冲

SDA_L; DELAY_us;

SCL_H; DELAY_us;

return(temp);

}

//停止信号

void i2cstop(void)

{ SCL_L; DELAY_us;

SDA_L; DELAY_us;

SCL_H; DELAY_us;

SDA_H;

}

uchar wt24c(uchar *p, uchar ad, uchar n)

{ uchar t=0;

i2cstart(); //发送起始信号

if(i2cwt(W_ADD_COM)) //发送写字节命令及器件地址

{

i2cwt(ad); //ad_dst的低位到器件

for(;n>0;n--) //发送要写入的数据

{ i2cwt(*p);

p++;

}

}

// else syserr=I2C_ERR; //写字节命令及器件地址错

i2cstop();

tms(6); //延时6ms

return(*p);

}

//从24cxx读出数据

//参数: *p_dst要读入数据的主机内存地址指针; ad_rsc要输出数据的i2c的地址(整形); num数据个数(整形)

//参数条件: ad_dst+(num-1)不能大于器件的最高地址; num必须>0;

void rd24c(uchar *p_dst, uchar ad_rsc, uchar num)

{

uchar t=0;

i2cstart(); //发送起始信号

if(i2cwt(W_ADD_COM)) //发送写字节命令及器件地址

{

i2cwt(ad_rsc); //ad_rsc的低位

i2cstart(); //再发送起始信号

i2cwt(R_ADD_COM); //发送SLA_R, 读命令字节及器件地址

for(;num>0;num--)

{ *p_dst=i2crd(); //从器件读出一个字节

p_dst++;

}

}

// else syserr=I2C_ERR; //写字节命令及器件地址错或对方无应答

i2cstop();

}

/***************************************************************************/

unsigned char Bin1toBcd(unsigned char Xbin1)

{

unsigned short xBCD=0;

do

{

//if(Xbin1>=100)

//{

// Xbin1-=100;

// xBCD+=0x100;

//}

//else

//{

if(Xbin1>=10)

{

Xbin1-=10;

xBCD+=0x10;

}

else

{

xBCD+=Xbin1;

break;

}

//}

}while(1);

return xBCD;

}

unsigned char test_temperature(void)

{

uchar tamp=0;

Ttmp=0;

RC_num=0;

do

{

P6CR&=0xfc;

Rp_TO_OUT;

Rm=0;

Rc=0;

Rp=0;

for(dly=0xa00;dly;dly--)

{

WDTC();

}

Rm_t=0;

P6CR|=0x02;

Rp_TO_IN;

Rm=1; //用基准电阻充电

while(1)

{

if(Rp)

{

break;

}

Rm_t++;

}

WDTC();

test_temp[RC_num]=Rm_t;

RC_num++;

}while(RC_num<6);

RC_num=0;

Rmin_p=0;

Rmax_p=5;

Rmin_t=test_temp[0];

Rmax_t=test_temp[5];

do

{

if(test_temp[RC_num+1] {

Rmin_t=test_temp[RC_num+1];

Rmin_p=RC_num+1;

}

if(test_temp[4-RC_num]>Rmax_t)

{

Rmax_t=test_temp[4-RC_num];

Rmax_p=4-RC_num;

}

RC_num++;

}while(RC_num<5);

RC_num=0;

Rm_t=0;

do

{

if(RC_num!=Rmin_p)

{

if(RC_num!=Rmax_p)

{

Rm_t+=test_temp[RC_num];

}

}

RC_num++;

}while(RC_num<6);//去max,min,取平均值

Rm_t>>=2;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值