View previous topic :: View next topic |
Author |
Message |
mastermindjp
Joined: 18 Dec 2012 Posts: 51 Location: INDIA
|
Posted: Thu Sep 21, 2017 1:46 pm Post subject: Two 12bit spi adc |
|
|
Hello friends,
With past suggestions i have completed first task to get data from two SPI based 12BIT ADC, MAX189. Now, while attempting to receive uninterrupted values from the ADC's, I get a undefined jump in between the ADC value, like first ADC provide output as 3035, which remains for there with very slight change to 3039, then suddenly it jumps to a value of 1181, then come back to 3039. Throughout this process i never changed the input for ADC.
I am unable to find the cause to this problem?
Here is the code:
Code: | $crystal = 16000000
$hwstack = 32
$regfile = "m32def.dat"
$swstack = 10
$framesize = 40
'$baud = 9600
'$sim
Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 1024
Config Lcdpin = Pin , Db7 = Portc.2 , Db6 = Portc.3 , Db5 = Portc.4 , Db4 = Portc.5 , E = Portc.6 , Rs = Portc.7
Config Lcd = 20 * 4
Cursor Off Noblink
'Config Adc = Single , Prescaler = Auto
'Stop Adc
Ss Alias Portb.1
Ss1 Alias Portb.3
Dout Alias Pinb.2
'Dout1 Alias Pind.2
Clock Alias Portb.0
'Clock1 Alias Porta.0
Config Ss = Output
Config Ss1 = Output
Config Portd.6 = Output
Config Portd.4 = Output
Config Portd.5 = Output
Config Portd.3 = Output
'Config Pind.2 = Input
Config Pinb.4 = Input
Config Pinb.5 = Input
Config Pinb.6 = Input
Config Pinb.7 = Input
Dim V As Word , V1 As Word
Dim Fat As Single , Fat1 As Single , Fat3 As String * 5 , Fat4 As Single
Dim A As Word , B As Word , Fat5 As Single
'Set Ss1
Pwm1a = 255
Reset Ss
Waitms 5
Do
Loop Until Dout = 1
Shiftin Dout , Clock , V , 0 , 16
Shift V , Right , 4
'Lcd "V= " ; V
Set Ss
Waitms 200
Do
B = 255
Pwm1b = B
Pwm1a = 255
Portd.6 = 0
Cls
Lcd "READY"
If Pinb.6 = 0 Then
B = 25
Portd.6 = 1
Waitms 200
A = 75
Pwm1a = A
Wait 1
Waitms 100
A = 255
Pwm1a = A
Waitms 200
Portd.6 = 0
Waitms 100
Do
If Pinb.5 = 0 Then B = B + 1
If Pinb.4 = 0 Then B = B - 1
Pwm1b = B
Set Ss1
Waitms 50
Reset Ss
Waitms 5
Do
Loop Until Dout = 1
Shiftin Dout , Clock , V , 0 , 16
Shift V , Right , 4
Set Ss
Wait 1
Set Ss
Waitms 50
Reset Ss1
Waitms 5
Do
Loop Until Dout = 1
Shiftin Dout , Clock , V1 , 0 , 16
Shift V1 , Right , 4
Set Ss1
Waitms 100
Cls
Lcd "ADC1=" ; V
Lowerline
Lcd "ADC2=" ; V1
Thirdline
Lcd "PWMB=" ; B
Waitms 200
Loop Until Pinb.7 = 0
Waitms 100
End If
loop |
Kindly provide some more guidance over this topic.
Thank You
Regards
Aniruddha
(BASCOM-AVR version : 2.0.8.0 , Latest : 2.0.7.8 ) _________________ We are not innovating, we are just manipulating innovations done by Mother Nature....... |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Thu Sep 21, 2017 2:39 pm Post subject: |
|
|
Try slowing down the clock by adding delay.
Quote: | SHIFTIN pin , pclock , var , option [, bits , delay ] |
|
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 584 Location: Cleveland, OH
|
Posted: Thu Sep 21, 2017 4:25 pm Post subject: |
|
|
Both good suggestions above!
Have you looked at the data sent from the ADC chip to the micro with either an O'scope or a Logic Analyzer to verify that the ADC chip is sending the correct data, and that, therefore, the problem is within the micro?
Is the input to the ADC filtered and noise free, (at least for your initial testing)?
It is helpful to know, with certainty, where the problem originates, before trying to track it down.
JC |
|
Back to top |
|
|
mastermindjp
Joined: 18 Dec 2012 Posts: 51 Location: INDIA
|
Posted: Fri Sep 22, 2017 7:11 am Post subject: |
|
|
Hello,
I will try both the suggestions, from MWS and Evert.
Quote: |
Have you looked at the data sent from the ADC chip to the micro with either an O'scope or a Logic Analyzer to verify that the ADC chip is sending the correct data, and that, therefore, the problem is within the micro?
Is the input to the ADC filtered and noise free, (at least for your initial testing)?
|
in this case i have implied same code and hardware for single ADC and its working superb, even if i just go with first ADC in this case also it is working fine.
Still, i go through your opinion too, will check with O-Scope.
Thank you
Regards
Aniruddha _________________ We are not innovating, we are just manipulating innovations done by Mother Nature....... |
|
Back to top |
|
|
mastermindjp
Joined: 18 Dec 2012 Posts: 51 Location: INDIA
|
Posted: Fri Sep 22, 2017 7:36 am Post subject: |
|
|
Went through both the suggestions but still getting that jump reading.
Quote: | Increase the stack. |
Quote: | Try slowing down the clock by adding delay. |
_________________ We are not innovating, we are just manipulating innovations done by Mother Nature....... |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Sep 22, 2017 8:25 am Post subject: |
|
|
Code should be more readable by proper indent of code lines. Then founding problems comes easy.
I clean your code and change slightly to see what can happen...
If you remmember frequency for PWM can be calculated by dividing uC freq/Prescaler/PWM resolution.
So 16MHz/1024/256=61Hz
This is relatively slow frequency and your code/ADCs can some time probing when current is off.
I dont know what this device doing but you should consider:
-decrease Timer proscaler to 256, 64 or even 8 if that low frequency is not needed
-if that low freq is needed you should build some filter on the ADCs input (resistor+capacitor for example)
-you can also filter results in software by probing more times and reject some results or make average of them
I think code looking like that is more readable
Code: | $crystal = 16000000
$hwstack = 32
$regfile = "m32def.dat"
$swstack = 10
$framesize = 64
' [ BE AWARE THAT PWM HAVE 16MHz/1024/256=61Hz ]
Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 1024
Config Lcdpin = Pin , Db7 = Portc.2 , Db6 = Portc.3 , Db5 = Portc.4 , Db4 = Portc.5 , E = Portc.6 , Rs = Portc.7
Config Lcd = 20 * 4
Cursor Off Noblink
Ss Alias Portb.1
Ss1 Alias Portb.3
Dout Alias Pinb.2
Clock Alias Portb.0
Config Ss = Output : Set Ss
Config Ss1 = Output : Set Ss1
Config Portd.6 = Output
Config Portd.4 = Output
Config Portd.5 = Output
Config Portd.3 = Output
Config Pinb.4 = Input : Set Portb.4
Config Pinb.5 = Input : Set Portb.5
Config Pinb.6 = Input : Set Portb.6
Config Pinb.7 = Input : Set Portb.7
Dim V As Word , V1 As Word
Dim Fat As Single , Fat1 As Single , Fat3 As String * 5 , Fat4 As Single
Dim A As Word , B As Word , Fat5 As Single
Pwm1a = 255
Reset Ss
Waitms 5
Bitwait Dout , Set
Shiftin Dout , Clock , V , 0 , 16
Shift V , Right , 4
Set Ss
'Lcd "V= " ; V
Waitms 200
Do
Pwm1b = 255
Pwm1a = 255
Portd.6 = 0
Cls : Lcd "READY"
'-------------------------------------
If Pinb.6 = 0 Then
B = 25
Portd.6 = 1
Waitms 200
A = 75 : Pwm1a = A
Waitms 1100
Pwm1a = 255
Waitms 200
Portd.6 = 0
Waitms 100
'-----------------------------------
Do
'-[some switches]---------------
If Pinb.5 = 0 Then
If B < 255 Then B = B + 1
Pwm1b = B
End If
If Pinb.4 = 0 Then
If B > 0 Then B = B - 1
Pwm1b = B
End If
'------------------------------
Wait 1
'-[ADC1]-----------------------
Reset Ss
Waitms 5
Bitwait Dout , Set
Shiftin Dout , Clock , V , 0 , 16
Shift V , Right , 4
Set Ss
'-[ADC2]-----------------------
Reset Ss1
Waitms 5
Bitwait Dout , Set
Shiftin Dout , Clock , V1 , 0 , 16
Shift V1 , Right , 4
Set Ss1
'-[LCD]------------------------
Cls
Lcd "ADC1=" ; V
Lowerline
Lcd "ADC2=" ; V1
Thirdline
Lcd "PWMB=" ; B
Waitms 200
Loop Until Pinb.7 = 0
'----------------------------------
Waitms 100
End If
'------------------------------------
Loop |
|
|
Back to top |
|
|
mastermindjp
Joined: 18 Dec 2012 Posts: 51 Location: INDIA
|
Posted: Fri Sep 22, 2017 11:40 am Post subject: |
|
|
Thanks for the nice example to write code, EDC.
Although i have already implemented filtration at the input of ADC, using resistance and capacitor, again "if i just connect one ADC and write code for single ADC it works perfectly fine".
do i need to put resistance in between ADC and Micro controller pins, common pins for both ADC, DOUT and CLOCK (PINB.2 and PORTB.0) ?? _________________ We are not innovating, we are just manipulating innovations done by Mother Nature....... |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Fri Sep 22, 2017 12:11 pm Post subject: |
|
|
Hi,
the MAX189 Datasheet says: "Data changes state at SCLK’s falling edge."
You should shiftin when clock goes high (option 1). Possible you have to throw away the first bit = "End Of Conversion" (see Fig. 8 in Datasheet). |
|
Back to top |
|
|
mastermindjp
Joined: 18 Dec 2012 Posts: 51 Location: INDIA
|
Posted: Fri Sep 22, 2017 12:33 pm Post subject: |
|
|
Hi,
thanks for your suggestion "Laborratte", but its not about receiving data from MAX189 in correct order, that i am getting, its about interfacing two MAX189 with one controller and obtaining correct data without the undefined jump in between readings... _________________ We are not innovating, we are just manipulating innovations done by Mother Nature....... |
|
Back to top |
|
|
mastermindjp
Joined: 18 Dec 2012 Posts: 51 Location: INDIA
|
Posted: Fri Sep 22, 2017 2:16 pm Post subject: |
|
|
Hello everyone..
I got the answer, it was a pair of 1K resistance in between the ADC's and controller at DOUT and CLOCK pins. Now its working flawlessly... Thanks to all of you for such a good guidance and enthusiasm.
Thanks once again. will surely post the entire project once it gets done, along with video.
Regards
Aniruddha _________________ We are not innovating, we are just manipulating innovations done by Mother Nature....... |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|