基于51的ads1115程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/*------------------------------------------------------------------------------------------------------------------------------------------*//*--*************************功能:【ADS1115单端输入检测电压】********************--*//*--*************************CPU:【STC12C5A60S2】*******************************--*//*--*************************ADC芯片:【ADS1115(2差分输入或4单端输入)】***********--*//*--*************************液晶:【LCD1602】************************************--*//*--*************************ADC与单片机连接:【SCL:P1.0,SDA:P1.1】*******************--*//*--*************************检测范围:【0.00~4.99V】*********************************--*//*----------------------------------------------------------------------------------------------------------------------------------------*/#includereg52.h#includeintrins.h#defineuintunsignedint#defineucharunsignedchar#define_Nop()_nop_()/*------------------------以下为LCD1602显示模块端口定义及函数声明-----------------------*/sbitlcd_rs_port=P1^7;/*定义LCD控制端口*/sbitlcd_rw_port=P1^6;sbitlcd_en_port=P2^4;#definelcd_data_portP0unsignedcharnum_char_table[]={0123456789ABCDEF};unsignedcharLcd_Dis1_table[]={Volt:V};//电压显示框架unsignedcharpos_char_table[]={67.9ABV};//电压显示位置voidlcd_delay(ucharms);//LCD1602延时voidlcd_busy_wait();//LCD1602忙等待voidlcd_command_write(ucharcommand);//LCD1602命令字写入voidlcd_system_reset();//LCD1602初始化voidlcd_char_write(ucharx_pos,y_pos,lcd_dat);//LCD1602字符写入voidlcd_bad_check();//LCD1602坏点检查voidNum_to_Disp(uintNum);//LCD1602数据处理voidLcdDisp(uintnum);//LCD1602数据显示voiddelay_1ms(ucharx);//LCD1602延时1ms/*------------------------以下为ADS1115模块端口定义及函数声明--------------------------*/sbitSCL=P1^0;sbitSDA=P1^1;#defineTRUE1#defineFALSE0unsignedintConfig;unsignedcharWritebuff[4],Readbuff[3];unsignedintResult[2];intD_ADS;//转换的数字量#defineAccuracy32768//定义ADC精度,ADS1115单端输入时15位charVCC=4.78;//VCC,电压参考值#defineADDRESS_00x90//ADDRPIN-GND#defineADDRESS_10x92//ADDRPIN-VDD#defineADDRESS_20x94//ADDRPIN-SDA#defineADDRESS_30x96//ADDRPIN-SCL/************POINTERREGISTER*****************/#definePointer_00x00//Convertionregister#definePointer_10x01//Configregister#definePointer_20x02//Lo_threshregister#definePointer_30x03//Hi_threshregister/************CONFIGREGISTER*****************/#defineOS_00x0000#defineOS_10x8000#defineMUX_00x0000//AINp=AIN0,AINn=AIN1#defineMUX_10x1000//AINp=AIN0,AINn=AIN3#defineMUX_20x2000//AINp=AIN1,AINn=AIN3#defineMUX_30x3000//AINp=AIN2,AINn=AIN3#defineMUX_40x4000//AINp=AIN0,AINn=GND#defineMUX_50x5000//AINp=AIN1,AINn=GND#defineMUX_60x6000//AINp=AIN2,AINn=GND#defineMUX_70x7000//AINp=AIN3,AINn=GND#definePGA_00x0000//FS=6.144V#definePGA_10x0200//FS=4.096V#definePGA_20x0400//FS=2.048V#definePGA_30x0600//FS=1.024V#definePGA_40x0800//FS=0.512V#definePGA_50x0A00//FS=0.256V#definePGA_60x0C00//FS=0.256V#definePGA_70x0E00//FS=0.256V#defineMODE_00x0000#defineMODE_10x0100#defineDR_00x0000//DataRate=8#defineDR_10x0020//DataRate=16#defineDR_20x0040//DataRate=32#defineDR_30x0060//DataRate=64#defineDR_40x0080//DataRate=128#defineDR_50x00A0//DataRate=250#defineDR_60x00C0//DataRate=475#defineDR_70x00E0//DataRate=860#defineCOMP_MODE_00x0000#defineCOMP_MODE_10x0010#defineCOMP_POL_00x0000#defineCOMP_POL_10x0008#defineCOMP_LAT_00x0000#defineCOMP_LAT_10x0040#defineCOMP_QUE_00x0000#defineCOMP_QUE_10x0001#defineCOMP_QUE_20x0002#defineCOMP_QUE_30x0003//*************ADDRInitial********************/#defineADDRESSADDRESS_0//ADDRPIN-GND#defineADDRESS_WADDRESS|0x00//写地址#defineADDRESS_RADDRESS|0x01//读地址/*************ConfigInitial*********************/#defineOSOS_1#defineMUX_A0MUX_4//AINp=AIN0,AINn=GND#defineMUX_A1MUX_5//AINp=AIN1,AINn=GND#defineMUX_A2MUX_6//AINp=AIN2,AINn=GND#defineMUX_A3MUX_7//AINp=AIN3,AINn=GND#definePGAPGA_1//FS=6.144V#defineMODEMODE_1//Continuousconversionmode#defineDRDR_7//DataRate=860#defineCOMP_QUECOMP_QUE_3voidDelay_us_15(void);voidDelay_ms_1(void);//功能:延时voidStart(void);//功能:完成IIC的起始条件操作voidStop(void);//功能:完成IIC的终止条件操作voidACK(void);//功能:完成IIC的主机应答操作voidNACK(void);//功能:完成IIC的主机无应答操作unsignedcharCheck(void);//功能:检查从机的应答操作voidWrite_1_Byte(unsignedcharDataByte);//写1个字节unsignedcharWrite_N_Byte(unsignedchar*writebuffer,unsignedcharn);//写N个字节unsignedcharRead_1_Byte(void);//读1个字节voidRead_N_Byte(unsignedint*readbuff,unsignedcharn);//读N个字节voidInitADS1115(bitS_MUX_0,bitS_MUX_1);//ADS1115初始化voidWriteWord(void);//写voidReadWord(void);//读unsignedintADS1115(bitS_MUX_0,bitS_MUX_1);//ADS1115取值/*--------------------------------------------------------------------------------------------------------------------------------*//*--*********************************【主函数】*******************************--*//*-------------------------------------------------------------------------------------------------------------------------------*/voidmain(){unsignedintresult;lcd_system_reset();//LCD1602初始化lcd_bad_check();//LCD1602坏点检查while(1){result=ADS1115(0,0);//ADS1115取值_AIN0//result=ADS1115(0,1);//ADS1115取值_AIN1//result=ADS1115(1,0);//ADS1115取值_AIN2//result=ADS1115(1,1);//ADS1115取值_AIN3LcdDisp(result);//LCD1602显示电压值}}/*--------------------------------------

1 / 12
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功