Quantcast
Channel: MCS Electronics Forum
Viewing all articles
Browse latest Browse all 20606

BASCOM Project Blog : Working With MAX1238/9 12BIT 12 Channel I2C ADC : REPLY

$
0
0
Now an update to fix the problem of the bytes being swapped and to make the code simpler, use less flash, be self contained also work like getadc does in bascom. All that is now needed is to take a word variable and let it equal Getmax1239(x) so wordvariable = Getmax1239(x) where x is the port of the max1239/8 the variable will be loaded with the value from the port just like getadc in bascom. Note thus far I have only worked in single ended mode (unipolar) but soon will adjust for differential operation (bipolar) mode. Regards Paul [code:1:a9b4578c9d] '-----------MAX1238------------------------------------------------------------------------ '--MAX1238 slave address '--MAX1238 is a 12 bit 12 channel I2C ADC '--write '--This returns the value of the ADC port of the Max1238/9 ' Declare Function Getmax1239(byval Chno As Byte ) As Word 'Declare Sub Max1239_setup 'Dim Max1239b As Word 'Dim Max1239lh(2) As Byte At Max1239b Overlay 'lets you wor with each byte of the word 'Dim Volt As Single 'Const Wrmax1238 = &H6A '&B01101010 write to MAX1238 '--read 'Const Rdmax1238 = &H6B '&B01101011 read from MAX1238 '--setup byte 'use internal reference, internal clock, unipolar mode 'Const Setmax1238 = &HD2 ' &B11010010 '--configuration bytes '-scan all adc inputs 0 to 11 single ended 'Const Scanall = &H17 '&B00010111 '--convert one ADC channel single ended BINARY VALUE 'Const Scan0 = &H61 '&B01100001 scan adc 0 'Const Scan1 = &H63 '&B01100011 scan adc 1 'Const Scan2 = &H65 '&B01100101 scan adc 2 'Const Scan3 = &H67 '&B01100111 scan adc 3 'Const Scan4 = &H69 '&B01101001 scan adc 4 'Const Scan5 = &H68 '&B01101011 scan adc 5 'Const Scan6 = &H6D '&B01101101 scan adc 6 'Const Scan7 = &H6F '&B01101111 scan adc 7 'Const Scan8 = &H71 '&B01110001 scan adc 8 'Const Scan9 = &H73 '&B01110011 scan adc 9 'Const Scan10 = &H75 '&B01110101 scan adc 10 'Const Scan11 = &H77 '&B01110111 scan adc 11 '--HSmode 'Const Hsmax1238 = &H08 '&B00001000 '------------------------------------------------------------------------------- '--------------------------------------- '--These subs set up and read the values from '--the MAX1239 12 bit ADC '----this takes a channel number (Chno) sets up the ADC '----tells it to do the conversion '----and reads the value from the channel Function Getmax1239(byval Chno As Byte ) As Word Local Scan_val As Byte Local Adcval As Word '--move the bits to the left one place as the last bit '--is "1" as we are writing to the ADC Shift Chno , Left , 1 '--&H61 is to read channel 0 so if we add '--the channel number to it shifted left '--one place we will get the byte to read '--the channel number we want Scan_val = &H61 + Chno 'use internal reference, internal clock, unipolar mode I2csend Wrmax1238 , Setmax1238 I2csend Wrmax1238 , Scan_val 'Waitms 1 'this may be needed depending on the cpu clock speed '--read in the two bytes using I2creceive Rdmax1238 , Adcval 'Swap the two bytes of the word Swap Adcval '--move the word to the left 4 bits causing '--the first 4 bits to get lost then move it '--back 4 bits which will make them all 0s '--giving us only the 12bits we want Shift Adcval , Left , 4 Shift Adcval , Right , 4 Getmax1239 = Adcval End Function Sub Max1239_setup 'use internal reference, internal clock, unipolar mode I2csend Wrmax1238 , Setmax1238 End Sub [/code:1:a9b4578c9d]

Viewing all articles
Browse latest Browse all 20606

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>