Proteus 里没有ads1115模型但有一个ads1015.区别就是前者为16比特,后者为12比特
$regfile = "m16def.dat" ' specify the used micro
$crystal = 8000000 ' used crystal frequency
'$baud = 19200 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 32 ' default use 10 for the SW stack
$framesize = 64 ' default use 40 for the frame space
Declare Function Get_adc(byval Ain_x As Byte) As Integer
Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , Rs = Portb.0 , E = Portb.1
Config Lcd = 16 * 2 '配置液晶屏
Cursor Off Noblink
Dim Myinteger As Integer
Dim Voltage As Single
Dim Vol_str As String * 8
'采样接口引脚配置
Dim Channel(8) As Byte
'AIN0-AIN1 'AIN0-AIN3 'AIN1-AIN3 'AIN2-AIN3
Channel(5) = &B00000000 : Channel(6) = &B00010000 : Channel(7) = &B00100000 : Channel(8) = &B00110000
'AIN0-GND 'AIN1-GND 'AIN2-GND 'AIN3-GND
Channel(1) = &B01000000 : Channel(2) = &B01010000 : Channel(3) = &B01100000 : Channel(4) = &B01110000
'配置引脚以用于I2C总线
Config Scl = PortC.0 '是串行时钟SCL
Config Sda = PortC.1 '是串行数据SDA
I2cinit
'*************************************************************************************
Cls
Do
Myinteger = Get_adc(2)
'Cls
Locate 1 , 1
Lcd Hex(myinteger)
Voltage = Myinteger / 32768
Voltage = Voltage * 4096
Myinteger = Round(voltage)
Vol_str = Str(myinteger)
Vol_str = Format(vol_str , "0.000")
Locate 2 , 1
Lcd Vol_str
Waitms 600
Loop
End
'*************************************************************************************
Function Get_adc(byval Ain_x As Byte) As Integer
Local Config_high As Byte
Local Config_low As Byte
'addr_pin接地
'写入ads1115的地址
'W_addr = &H90
'从ads1115读取的地址
'R_addr = &H91
Config_high = &B10000000
Config_high = Config_high + Channel(2)
Config_high = Config_high + &B00000010 ' Pga(2) ±4.096V
'Config_high = Config_high + &B00000100 ' Pga(3) ±2.048V
Config_low = &B10000000 ' 128SPS
'初始化I2C
I2cstart
I2cwbyte &H90 ' W_地址
I2cwbyte &B00000001 ' 指向Config寄存器
I2cwbyte Config_high
I2cwbyte Config_low
I2cstop
'读_ad
I2cstart ' 生成起始码
I2cwbyte &H90 ' W_addr发送地址
I2cwbyte &B00000000 ' 指向转换寄存器
I2cstart ' 起动条件
I2cwbyte &H91 ' 读_addr的从机地址
I2crbyte Config_high , Ack
I2crbyte Config_low , Nack
I2cstop
Get_adc = Makeint(config_low , Config_high) '将两个字节压缩成一个字或整数。等效代码是:VARN=(256*MSB)+LSB
End Function
|