圣龙扬特-AVR电子

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3613|回复: 1
打印 上一主题 下一主题

BASCOM-AVR Read ADS1115

[复制链接]

67

主题

4

好友

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
2
帖子
343
精华
0
注册时间
2013-2-20
在线时间
366 小时
跳转到指定楼层
楼主
发表于 2020-1-5 22:13:02 |只看该作者 |倒序浏览
$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

回复

使用道具 举报

0

主题

0

好友

33

积分

新手上路

Rank: 1

UID
669
帖子
6
精华
0
注册时间
2020-7-14
在线时间
0 小时
沙发
发表于 2020-7-20 18:12:27 |只看该作者
以前不懂,看贴总是不回,一直没提升等级和增加经验;现在我明白了,反正回贴可以升级,也可以赚经验,而升级又需要经验,我就把这句话复制下来,遇贴就回,捞经验就闪。
https://www.youxuan68.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

Archiver|手机版|圣龙扬特-AVR电子 ( 鲁ICP备05022832号 )

GMT+8, 2024-4-24 16:23 , Processed in 0.208721 second(s), 17 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部