⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ltc1865_ok.c

📁 LTC1865,16BitADC
💻 C
字号:


#include<reg52.h>
#include<absacc.h>

#define uchar unsigned char
#define uint  unsigned int
#define ulong unsigned long 

#define TRUE  1
#define FALSE 0

#define disp_dot 0x2e
#define disp_V   0x56
#define disp_A   0x41
#define disp_W   0x57

#define data1602 P2                // 定义LCD1602数据接口

uchar code disp_code[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};

ulong voltage_data;
ulong current_data;


sbit rs1602 = P1^5;                  
sbit rw1602 = P1^6;
sbit e1602  = P1^7;

sbit SDI    = P1^3;
sbit CONV   = P1^2;
sbit SCK    = P1^1;
sbit SDO    = P1^0;

/*******************************************************************************
函数名称:delay(uchar n)
功能描述:延时
调用函数:
写入值  :n
返回值  :
*******************************************************************************/
void delay(uchar n)
{ 
 uchar i;
 for(;n>0;n--)
 for(i=250;i>0;i--);
}

/******************************************************************************
函数名称: write_1602(uchar wdata)
功能描述: LCD1602写入数据
调用函数: delay()
写入值  : wdata
返回值  :   
******************************************************************************/
void write_1602(uchar wdata)
{ 
 data1602 = wdata;
 rs1602   = 1;
 rw1602   = 0; 
 e1602    = 1; 
 delay(4);
 e1602    = 0;
}

/******************************************************************************
函数名称: write_order(uchar odata)
功能描述: LCD1602写命令
调用函数: delay()
写入值  : odata
返回值  :   
******************************************************************************/
void write_order(uchar odata)
{ 
 data1602 = odata;
 rs1602   = 0;
 rw1602   = 0;
 e1602    = 1;
 delay(4);
 e1602    = 0;
}

/******************************************************************************
函数名称: init_1602()
功能描述: LCD1602初始化
调用函数: write_order(uchar odata)
写入值  : 
返回值  :   
******************************************************************************/
void init_1602(void)
{
 write_order(0x01);					    // 清屏幕
 write_order(0x38);						// 数据长度为8位,双行显示,5*7字符
 write_order(0x0c);						// 打开显示开关,光标不闪烁
 write_order(0x06);						// 地址计数递增,显示屏不移动
}

/******************************************************************************
函数名称: position(uchar row,uchar colum)
功能描述: LCD1602写位置
调用函数: write_order()
写入值  : row,colum
返回值  :   
******************************************************************************/
position(uchar row,uchar colum)
{
 uchar pos;
 if(row==1)
   {
	 pos=0x80+colum;
   }
 else
   {
     pos=0xC0+colum;   
     
   }
 write_order(pos);
}

/******************************************************************************
函数名称: write_byte(uchar *s)
功能描述: LCD1602字符串
调用函数: write_1602()
写入值  : *s
返回值  :   
******************************************************************************/
write_byte(uchar *s)
{
 for(;*s!='\0';s++)
   {
     write_1602(*s);
   }
}

/******************************************************************************
函数名称: read_adc_ch0()
功能描述: 读LTC1865通道0的AD值
调用函数: 
写入值  : 
返回值  :   
******************************************************************************/
void read_adc_ch0(void)
{
 uchar j;
 voltage_data = 0;
 SDO = 1;
 CONV = 1;			               // 开启AD转换
 SCK  = 1;
 CONV = 0;
 SCK  = 0;
 for(j=15;j>0;j--)
   {
    SCK = 0;
	if(j == 15)
	  {
	    SDI = 1;
	  }
	if(j == 14)
	  {
	    SDI = 0;
	  }
	if(SDO==1)
	  {
	    voltage_data = voltage_data + 1;
	  }   
	voltage_data <<= 1;
	SCK = 1;
   }
  CONV = 1;
  SCK  = 1;
}

/******************************************************************************
函数名称: disp_voltage()
功能描述: 显示电压
调用函数: read_adc_ch0(),write_1602(),write_order()
写入值  : 
返回值  :   
******************************************************************************/
void disp_voltage(void)
{
 read_adc_ch0();
 voltage_data = (voltage_data*5000)/65535;		 // AD采样值处理
 write_order(0x89);
 write_1602(disp_code[voltage_data/1000]);
 write_order(0x8b);
 write_1602(disp_code[voltage_data%1000/100]);
 write_order(0x8c);
 write_1602(disp_code[voltage_data%1000%100/10]);
 write_order(0x8d);
 write_1602(disp_code[voltage_data%1000%100%10]);
}

/******************************************************************************
函数名称: disp_current()
功能描述: 显示电流
调用函数: write_1602(),write_order()
写入值  : 
返回值  :   
******************************************************************************/
void disp_current(void)
{
 write_order(0xc9);
 write_1602(disp_code[current_data/1000]);
 write_order(0xcb);
 write_1602(disp_code[current_data%1000/100]);
 write_order(0xcc);
 write_1602(disp_code[current_data%1000%100/10]);
 write_order(0xcd);
 write_1602(disp_code[current_data%1000%100%10]);
}

/******************************************************************************
函数名称: disp_data()
功能描述: 显示电压电流
调用函数: write_1602(),position(),disp_voltage(),disp_current()
写入值  : 
返回值  :   
******************************************************************************/
void disp_data(void)
{
 position(1,10);
 write_1602(disp_dot);
 position(1,15);
 write_1602(disp_V);
 position(2,10);
 write_1602(disp_dot);
 position(2,15);
 write_1602(disp_A);

 disp_voltage();
 disp_current();
} 

/******************************************************************************
函数名称: main()
功能描述: 主函数
调用函数: 
写入值  : 
返回值  :   
******************************************************************************/
void main(void)
{
 init_1602();
 position(1,0);
 write_byte("VOLTAGE=");
 position(2,0);
 write_byte("CURRENT=");
 while(1)
   { 
	disp_data();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -