- 阅读权限
- 200
- UID
- 2
- 帖子
- 343
- 精华
- 0
- 注册时间
- 2013-2-20
- 在线时间
- 367 小时
- UID
- 2
- 帖子
- 343
- 精华
- 0
- 注册时间
- 2013-2-20
- 在线时间
- 367 小时
|
$regfile = "m8def.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
Osccal = &H9F
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 'configure lcd screen
Cursor Off Noblink
'Cursor On Blink 'show cursor
Dim Myinteger As Integer
Dim Voltage As Single
Dim Vol_str As String * 8
'(
Write to Config register:
First byte: 0b10010000 (first 7-bit I2C address followed by a low read/write bit) (H90/H91 OK)
Second byte: 0b00000001 (points to Config register)
Third byte: 0b10000100 (MSB of the Config register to be written)
Fourth byte: 0b10000011 (LSB of the Config register to be written)
Write to Pointer register:
First byte: 0b10010000 (first 7-bit I2C address followed by a low read/write bit) (H90/H91 OK)
Second byte: 0b00000000 (points to Conversion register)
Read Conversion register:
First byte: 0b10010001 (first 7-bit I2C address followed by a high read/write bit)
Second byte: the ADS1113/4/5 response with the MSB of the Conversion register
Third byte: the ADS1113/4/5 response with the LSB of the Conversion register
')
'在14-12位,是 MUX 配置,决定了 ADS1115 的 A0-A3 接口的测量方式。
'比如,当设置为 0x04 时测量的为 A0-GND 的电压,当设置为 0x00 时测量的为 A0-A1 间的电压。
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
'Configure pins to to use for the I2C bus
Config Scl = Portb.7 'Is serial clock SCL
Config Sda = Portb.6 'Is serial data 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
'(
'在11-9位,是 PGA 配置。这决定了 ADS1115 的量程。
Dim Pga(8) As Byte
'FS=±6.144V 'FS=±4.096V 'FS=±2.048V 'FS=±1.024V
Pga(1) = &B00000000 : Pga(2) = &B00000010 : Pga(3) = &B00000100 : Pga(4) = &B00000110
'FS=±0.512V 'FS=±0.256V 'FS=±0.256V 'FS=±0.256V
Pga(5) = &B00001000 : Pga(6) = &B00001010 : Pga(7) = &B00001100 : Pga(8) = &B00001110
')
'(
'在第7-5位,是每秒采样次数的配置。
Dim Data_rate(8) As Byte
'8SPS '16SPS '32SPS '64SPS
Data_rate(1) = &B00000000 : Data_rate(2) = &B00100000 : Data_rate(3) = &B01000000 : Data_rate(4) = &B01100000
'128SPS '250SPS '475SPS '860SPS
Data_rate(5) = &B10000000 : Data_rate(6) = &B10100000 : Data_rate(7) = &B11000000 : Data_rate(8) = &B11100000
')
'Local Lobyte As Byte
'Local Hibyte As Byte
'Local W_addr As Byte
'Local R_addr As Byte
Local Config_high As Byte
Local Config_low As Byte
'addr_pin grounded
'address for write to ads1115
'W_addr = &H90
'address for read from ads1115
'R_addr = &H91
Config_high = &B10000000
'Ain_x-> 1-8
If Ain_x > 8 Or Ain_x < 1 Then Ain_x = 1
Config_high = Config_high + Channel(ain_x)
Config_high = Config_high + &B00000010 ' Pga(2) ±4.096V
'Config_high = Config_high + &B00000100 ' Pga(2) ±2.048V
'Config_low = &B00000000
'Config_low = Config_low + Data_rate(5)
Config_low = &B10000000 ' 128SPS
'init I2C
I2cstart
I2cwbyte &H90 ' W_addr
I2cwbyte &B00000001 ' 指向Config寄存器
I2cwbyte Config_high
I2cwbyte Config_low
I2cstop
'Read_ad
I2cstart ' Generate start code
I2cwbyte &H90 ' W_addr ' send address
I2cwbyte &B00000000 ' 指向转换寄存器
I2cstart ' start condition
I2cwbyte &H91 ' R_addr ' slave address
I2crbyte Config_high , Ack
I2crbyte Config_low , Nack
I2cstop
Get_adc = Makeint(config_low , Config_high)
End Function
|
|