void Num_to_Disp(uint Num) /******LCD显示数据处理*****/ //Start_1-Over_1:显示电压值,Start_2-Over_2:显示ADS1115输出数值 { float NUM; //Start_1 int xx, yy, zz, zzz; NUM = (Num / (float)Accuracy) * VCC; //ADS1115取值 = Accuracy × (Vin / VCC) xx = (int)NUM; yy = (int)((NUM - (float)(xx)) * 10); zz = (int)((NUM - (float)(xx)) * 100); zzz = (int)((NUM - (float)(xx)) * 1000);
pos_char_table[6] = num_char_table[xx / 10]; //电压值十位 pos_char_table[7]= num_char_table[xx % 10]; //电压值个位 pos_char_table[9]= num_char_table[yy]; //电压值小数点后一位 pos_char_table[10]= num_char_table[zz]; //电压值小数点后两位 pos_char_table[11]= num_char_table[zzz]; //电压值小数点后三位 //Over_1 /* pos_char_table[6] = num_char_table[Num / 10000]; //Start_2 pos_char_table[7] = num_char_table[(Num % 10000) / 1000]; pos_char_table[8]= num_char_table[((Num % 10) % 1000) / 100]; pos_char_table[9]= num_char_table[(Num / 10) % 10]; pos_char_table[A]= num_char_table[Num % 10]; //Over_2 */ }
void LcdDisp(uint num) /************LCD显示*************/ { uint i=0; for (i=0;i<16;i++) { lcd_char_write(i,0,Lcd_Dis1_table[i]); //显示框架 } Num_to_Disp(num); for(i = 6; i < 12; i++) { lcd_char_write(i,0,pos_char_table[i]); //显示电压 } delay_1ms(100); }
void delay_1ms(uchar x) /*******1MS为单位的延时程序******/ {
uchar j; while(x--){
for(j=0;j<125;j++)
{;} } }
/*------------------------以下为ADS1115模块端口函数定义--------------------------*/
void Delay_us_15 (void) /*延时程序*/ {
unsigned char i;
for(i = 0;i < 15;i++) _nop_(); }
void Delay_ms_1 (void) { unsigned char i; for(i = 150;i > 0;i--) _nop_(); }
///******************************************* //函数名称:Start
//功 能:完成IIC的起始条件操作 //参 数:无 //返回值 :无
//********************************************/ void Start(void) {
SCL=1; SDA=1;
Delay_us_15 (); SDA=0;
Delay_us_15 (); SCL=0;
Delay_us_15 (); } //
///******************************************* //函数名称:Stop
//功 能:完成IIC的终止条件操作 //参 数:无 //返回值 :无
//********************************************/ void Stop(void) {
SDA=0;
Delay_us_15 (); SCL=1;
Delay_us_15 (); SDA=1;
Delay_us_15 ();
}
///******************************************* //函数名称:ACK
//功 能:完成IIC的主机应答操作 //参 数:无 //返回值 :无
//********************************************/ void ACK(void) {
SDA=0;
_nop_(); _nop_(); SCL=1;
Delay_us_15 (); SCL=0; _nop_(); _nop_(); SDA=1;
Delay_us_15 (); }
//******************************************* //函数名称:NACK
//功 能:完成IIC的主机无应答操作 //参 数:无 //返回值 :无
//********************************************/ void NACK(void) { SDA=1; _nop_(); _nop_(); SCL=1; Delay_us_15 (); SCL=0; _nop_(); _nop_(); SDA=0; Delay_us_15 (); }
//**********检查应答信号函数******************/ ///*如果返回值为1则证明有应答信号,反之没有*/ ///******************************************* //函数名称:Check
//功 能:检查从机的应答操作 //参 数:无
//返回值 :从机是否有应答:1--有,0--无
//********************************************/ unsigned char Check(void) {
unsigned char slaveack;
SDA=1;
_nop_(); _nop_();
SCL=1;
_nop_(); _nop_();
_nop_(); _nop_();
slaveack = SDA; //读入SDA数值 SCL=0;
Delay_us_15 ();
if(slaveack) return FALSE; else return TRUE; }
/***************Write a Byte****************/ void Write_1_Byte(unsigned char DataByte) { int i; for(i=0;i<8;i++) { if(DataByte&0x80) //if((DataByte<
Delay_us_15 (); SCL=1;
Delay_us_15 (); SCL=0;
Delay_us_15 (); DataByte <<= 1; }
SDA=1; _nop_(); }
/***************Write N Byte****************/
unsigned char Write_N_Byte(unsigned char *writebuffer,unsigned char n) { int i; for(i=0;i } } Stop(); return TRUE; } //***************Read a Byte****************/ unsigned char Read_1_Byte(void) { unsigned char data_Value = 0, FLAG, i; for(i=0;i<8;i++) { SDA=1; Delay_us_15 (); SCL=1; Delay_us_15 (); FLAG=SDA; data_Value <<= 1; if( FLAG) data_Value |= 0x01; SCL=0; Delay_us_15 (); } return data_Value; } //***************Read N Byte****************/ void Read_N_Byte(unsigned int*readbuff, unsigned char n) { unsigned char i; for(i=0;i //*****************初始化******************/ void InitADS1115(bit S_MUX_0, bit S_MUX_1) {