News:

;) This forum is the property of Proton software developers

Main Menu

ADC _strange things

Started by Marcel_741, Jun 22, 2023, 11:19 PM

Previous topic - Next topic

Marcel_741

Hello,

As I was investigatin the working with ADC on a 12F675, it didn't went a I thought it would go. On GPIO.0 there is a potmeter and both end are connected via 4K7 resistors to ground and +5V. A red led is connected on GPIO.5, just as indication the circuit is powered and the pic makes the port high. A with LED is connected on GPIO.2, and this is for indication where the state of the ADC is.

The 4K7 resistors are chosen to have a range of circa 1V to 4.55V. I programmed the PIC and I was expect that the LED would go off and when I put the value on, for example 500. But it would not go on. In the lower range, around 40, it goes on but not off. This untill 190/193 and above that, it never goes on. Zo, I am complete lost and tried several values and part. 10K pot  / 25K pot, but no result.

'****************************************************************
'*  Name    : Example_2_simple.BAS                              *
'*  Author  : Marcel Alkema                                     *
'*  Notice  : Copyright (c) 2023 ALKEMA                         *
'*          : All Rights Reserved                               *
'*  Date    : 22-6-2023                                         *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
Device 12F675               ; used PIC

Config intrc_osc_noclkout,_
       wdt_off,_
       pwrte_on,_
       mclre_off
Xtal 4

All_Digital TRUE                         

Symbol yes = 1                            ; 1 is high
Symbol no = 0                             ; 0 is low

Symbol led = GPIO.2                       ; led on port_2
Symbol ledr = GPIO.5

Declare Adin_Res   = 10                   ; resolution 10 bits     
Declare Adin_Tad   = frc                  ; Set rc osc     
Declare Adin_Stime = 50                   ; sample time 5       
                                       
Dim potmeter As Byte                      ; potmeter
         ;543210                          ; helpline
GPIO   = %000000                          ; make ports low
TRISIO = %000001                          ; poort I/O

         ;76543210                        ; helpline control register
ADCON0 = %00000001                        ; A/D control register

run:   
 ledr = yes                               
 potmeter = ADIn 0                   
     If potmeter > 191 Then               
      led = yes
     EndIf
    potmeter = ADIn 0                     
     If potmeter < 191 Then           
      led = no                       
     EndIf 
     
                                                             
GoTo run                               
 
End                     

What am I doing wrong?

Thanks in front.

Marcel

trastikata

You forgot to set the ADFM bit.

Maxi

here is my working code

Device 12F675
Declare Xtal=4

Config CPD_OFF,CP_ON,BODEN_ON,MCLRE_OFF,PWRTE_ON,WDT_OFF,INTRC_OSC_NOCLKOUT
Declare Dead_Code_Remove = On

TRISIO=%00000010
GPIO=%00000000
ADCON0=%10000101
ANSEL =%00000010

OPTION_REG.7=0
WPU=%00000000
'vrcon.7=0
CMCON=7

Declare Adin_Res 10
Declare Adin_Tad FRC
Declare Adin_Stime 50

Marcel_741

Trastikata

Oke, searching for the ADFM and found:

* It is a part of the ADCON1 register.
* It is used for result justification.(Looks like the outcome is required this, as the result is poor)

What I miss is how to use it in the programm and the effect on the ADC. The second part I can read in the manual, but i am doubtfull in the first part.

Maxi

Your programm was combined (and adjusted) in what I have and during the full range from the POT, the LED went 3x on and 3x off. The hex shows a protected code mode. (Also in the config). I must be doing something wrong.

I add the programm combination in the txt file.

Not there yet.

Thanks for now.

Marcel

Example_adjust.txt

Marcel_741

#4
Hello,

Still strungling and learning.....

Latest what I got is in the code.  Result is a LED that blinks in the full range of the pot 3 x. So, more is happening but I can't explain why the routine has three activation point. Or 3x a 200 activation in one range.

Above a activation of 300, nothing happens, so the LED won't react on any range change of the POT. I expect it to be somewhere around max 900, because the max Uadc is 4,55V.

Regards,

Marcel
Device 12F675               ; Te gebruiken PIC

Config intrc_osc_noclkout,_
       wdt_off,_
       pwrte_on,_
       mclre_off
Xtal 4

All_Digital TRUE                  ;Alle ingangen digitaal

Symbol yes = 1                            ; 1 is high
Symbol no = 0                             ; 0 is low

Symbol led = GPIO.2                       ; led on port_2
Symbol ledr = GPIO.5

ADCON0bits_ADON = 1                       ; Enable the ADC         
ADCON0 = %00000001                        ; A/D control register
ADCON0bits_ADFM = 1                       ; Right justify the ADC for a 10-bit result

Declare Adin_Res   = 10                   ; resolution 10 bits     
Declare Adin_Tad   = frc                  ; Set rc osc     
Declare Adin_Stime = 50                   ; sample time 5       
                                       
Dim potmeter As Byte                      ; potmeter
         ;543210                          ; helpline
GPIO   = %000000                          ; make ports low
TRISIO = %000001                          ; poort I/O



run:   
 ledr = yes                               
 potmeter = ADIn 0                   
     If potmeter > 200 Then               
      led = yes
     EndIf
    potmeter = ADIn 0                     
     If potmeter < 200 Then           
      led = no                       
     EndIf 
     
                                                             
GoTo run                               
 
End                                        ; end programm

Pepe

Put
                                   
Dim potmeter As Word

Marcel_741

Pepe,

All answers seems to point to your advise. I add the working programm and I expand it a bit. As soon as the hex with the changed "Dim", was in the PIC, the LED did go out one time and also 1 time on. So it seems to work.

As an extra check I expand the programm with a second LED and changed the activation points (2). The LED were activated on two different points, so I am  sure it is oke. I am happy.

Thanks for that and thank to all.

Regards,

Marcel
Device 12F675               ; Te gebruiken PIC

Config intrc_osc_noclkout,_
       wdt_off,_
       pwrte_on,_
       mclre_off
Xtal 4

All_Digital TRUE                  ;Alle ingangen digitaal

Symbol yes = 1                            ; 1 is high
Symbol no = 0                             ; 0 is low

Symbol led = GPIO.2                       ; led on port_2
Symbol ledr = GPIO.5

ADCON0bits_ADON = 1                       ; Enable the ADC         
ADCON0 = %00000001                        ; A/D control register
ADCON0bits_ADFM = 1                       ; Right justify the ADC for a 10-bit result

Declare Adin_Res   = 10                   ; resolution 10 bits     
Declare Adin_Tad   = frc                  ; Set rc osc     
Declare Adin_Stime = 50                   ; sample time 5       
                                       
Dim potmeter As Word                      ; potmeter
         ;543210                          ; helpline
GPIO   = %000000                          ; make ports low
TRISIO = %000001                          ; poort I/O



run:   
                                 
 potmeter = ADIn 0                   
     If potmeter > 400 Then               
      led = yes
     EndIf
    potmeter = ADIn 0                     
     If potmeter < 400 Then           
      led = no                       
     EndIf 
potmeter = ADIn 0                   
     If potmeter > 800 Then               
      ledr = yes
     EndIf
    potmeter = ADIn 0                     
     If potmeter < 800 Then           
      ledr = no                       
     EndIf       
                                                             
GoTo run                               
 
End                                        ; end programm

Frizie

In your program above you program in the same loop:
if potmeter > 400
if potmeter < 400
if potmeter > 800
if potmeter < 800


You can shorten your program a bit (and make it clearer) by reading ADIN only once in the loop:

DO                             
  Potmeter = ADIN 0                   
  IF Potmeter > 400 THEN ledr = yes
  IF Potmeter < 400 THEN ledr = no
LOOP
Ohm sweet Ohm | www.picbasic.nl

Marcel_741

Frizie

You are absolute right and a summary of actions in relation to the activation points was the plan. The way it is written in de programm is a follow up from trial and error, instead of the beauty how it looks.

But thanks for that and it will be included.

regards,

Marcel

Marcel_741

Hello,

I have to come back on one challenge in relation to switching on LEDS and especially switching LEDS off. In my circuit, the programm is working fine, but the visual outcome is not completly correct. I add the circuit and the latest programm. The thing is, when the analog signal is reaching step 2 and exceed the value, to switch the second red LED, LED red 1 should switch off. It looks that is does, but a 0.77V remains as output on GPIO.1. That's repeats on step 3 and 4, so the LEDS that should be off are a bit shining. As soon as I turn the POT back to 0.20V, the LEDS will go out in the expected steps. So, conclusion is when I turn the power up, the LEDS that must go off, doesn't perform as wanted. What could be a explanation to this?

Please advise if possible.

Regards,

Marcel

Device 12F675               ; Te gebruiken PIC

Config intrc_osc_noclkout,_
       wdt_off,_
       pwrte_on,_
       mclre_off
Xtal 4

All_Digital TRUE                  ;Alle ingangen digitaal

Symbol yes = 1                            ; 1 is high
Symbol no = 0                             ; 0 is low

Symbol ledr1 = GPIO.1                     ; led red 1 on port_1
Symbol ledr2 = GPIO.2                     ; led red 2 On port_2
Symbol ledge = GPIO.4                     ; led yellow On port_4
Symbol ledgr = GPIO.5                     ; led green On port_5

ADCON0bits_ADON = 1                       ; Enable the ADC         
ADCON0 = %00000001                        ; A/D control register
ADCON0bits_ADFM = 1                       ; Right justify the ADC for a 10-bit result

Declare Adin_Res   = 10                   ; resolution 10 bits     
Declare Adin_Tad   = frc                  ; Set rc osc     
Declare Adin_Stime = 50                   ; sample time 5       
                                       
Dim potmeter As Word                      ; potmeter (0K2 - 23K2)
         ;543210                          ; helpline
GPIO   = %000000                          ; make ports low
TRISIO = %000001                          ; poort I/O



run:   
                                 
 potmeter = ADIn 0
                   
     If potmeter < 194 Then ledr1 = no   ;LED Red 1 off when < 194
         
     If potmeter > 194 Then ledr1 = yes  ;LED Red 1 on when > 194 (1,01V)             
     
     If potmeter < 388 Then ledr2 = no   ;LED Rood 2 off when < 388
     If potmeter > 388 Then             
     ledr2 = yes                         ;LED Rood 2 on when > 388 (2,04V)
     ledr1 = no                          ;LED Rood 1 off when > 388
     End If
     
     If potmeter < 582 Then ledge = no   ;LED Yellow off when < 582
     If potmeter > 582 Then             
     ledge= yes                          ;LED Yellow on when > 582 (3,05V)
     ledr2 = no                          ;LED Red 2 off when > 582
     End If                             
     
     If potmeter < 776 Then ledgr = no   ;LED Green 1 off when < 776
     If potmeter > 776 Then             
     ledgr = yes                         ;LED Green on when > 776 (4,07)
     ledge = no                          ;LED Yellow off when > 776
     End If                             
     
                                                             
GoTo run                               

 
     
End                                        ; end programm

trastikata

The way you have written your code, it will run trough each IF condition and will execute accordingly. You see the effects of fast switching ON and then OFF of LED1 and LED2. 


Marcel_741

trastikata

I see your point. I will try to make it work with case or else. And add to that better managment of switching off, when situation is there.

Thanks for the light.

Marcel

trastikata

Better use Select ... Case ... Endselect Here's a tip how to, the rest I leave it for you to figure it out.

While 1 = 1
    potmeter = ADIn 0
 
    Select potmeter
        Case ... To ...
            ....
        Case ... To ...
            ....
    EndSelect       
Wend 


See_Mos

#13
I note the supply on your diagram is shown as 5.3 volts.  Unless you are using an external voltage regulator to supply the 12F675 I see a problem with the supply voltage changing slightly as the LED's are turned on/off so <400 and >400 may not work and you may need to increase the hysteresis, for example <390 > 410.

If the device is supplied from a battery you will need to modify your circuit to add an external stabilised Vref on GPIO.1 as the 12F675 does not have an internally stabilised Vref


 


Marcel_741

See_Mos

I put a 1N4001 parallel in the supply, so Vin became 4.65V and effect didn't changed.

Thanks anyway

Marcel


tumbleweed

Since the pot and the adc use the same reference everything is relative. Just don't exceed the max VDD rating for the chip.

Dim potmeter As Word                 ; potmeter (0K2 - 23K2)

Symbol ledr1 = 1                     ; led red 1 on port_1
Symbol ledr2 = 2                     ; led red 2 On port_2
Symbol ledge = 4                     ; led yellow On port_4
Symbol ledgr = 5                     ; led green On port_5

; add in all setup statements

run:
    potmeter = ADIn 0

    If potmeter > 776 Then             
        GPIO = 1 << ledgr
    Else If potmeter > 582 Then             
        GPIO = 1 << ledge
    Else If potmeter > 388 Then             
        GPIO = 1 << ledr2
    Else If potmeter > 194 Then
        GPIO = 1 << ledr1
    Else
        GPIO = %000000                  ; all off
    EndIf
    GoTo run

Marcel_741

Quote from: Marcel_741 on Jun 26, 2023, 01:57 PMSee_Mos

I put a 1N4001 parallel in the supply, so Vin became 4.65V and effect didn't changed.

Thanks anyway

Marcel

Ps: I mean 1N4001 in serie with the supply.




Marcel_741

Tumbleweed

Agree on the Vdd and thanks.

Marcel

Pepe

demo in proteus
12f675.zip

Marcel_741

Tumbleweed

LEDS are going on and off seperatly, without any output remains high when it is not required. So, good work and nice to see, what I was aiming for.....

Now I can continu with a LDR and work on histeresis.

Thanks


Pepe

Thanks for the files. I must say that I don't use proteus because I don't think I have it. Also not looked for it. I have the Proton Compiler (paid version) and when I activate ISIS, then I will come in a demo version. Sorry for not be able to check your files, in relation to that.

But thanks,


Regards,

Marcel