標(biāo)題: mega16單片機(jī)利用ADS1115讀取16位ADC [打印本頁]

作者: 991005    時間: 2024-2-24 15:51
標(biāo)題: mega16單片機(jī)利用ADS1115讀取16位ADC
讀取結(jié)果總是不對,高位始終是0,麻煩大佬們幫忙看看哪兒有問題

image.jpg (1.16 MB, 下載次數(shù): 49)

image.jpg

作者: taotie    時間: 2024-2-27 10:40
你不妨用ADS1015代替ADS1115在Proteus 仿真調(diào)試驗(yàn)證一下。
作者: 991005    時間: 2024-2-27 16:31
你好,我能請教你一些問題嗎?我參照了您的那個帖子ADS1110的,但是我使用的是mega16,但是讀取不出來數(shù)據(jù),想請教一下您

作者: taotie    時間: 2024-2-27 22:00
Proteus 里沒有ads1115模型但有一個ads1015.區(qū)別就是前者為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                                        '是串行數(shù)據(jù)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發(fā)送地址
   I2cwbyte &B00000000                                      ' 指向轉(zhuǎn)換寄存器
   I2cstart                                                 ' 起動條件
   I2cwbyte &H91                                            ' 讀_addr的從機(jī)地址
   I2crbyte Config_high , Ack
   I2crbyte Config_low , Nack
   I2cstop

   Get_adc = Makeint(config_low , Config_high) '將兩個字節(jié)壓縮成一個字或整數(shù)。等效代碼是:VARN=(256*MSB)+LSB

End Function







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1