+ Reply to Thread
Results 1 to 13 of 13

Thread: Need help for ADC problem

  1. #1
    swapan Newbie
    Join Date
    Aug 2009
    Location
    KOLKATA (INDIA)
    Posts
    42

    Default Need help for ADC problem

    Friends,
    I have decided to make an automatic voltage stabilizer using 16F676. While testing a portion of the project a peculiar working of the PIC is faced. Please see my codes. A pot (4.7K) has been used to feed analogue adc supply from 5V rail. when I rotate the pot to and fro at a point towards lower side the RA1 pin sets and clears. It is OK. Rotating the pot to and fro at a point towards higher side, the same RA1 pin sets and clears again. The RA0 pin remains set all along even I rotate the pot towards extreme high i.e. adc input is 5V. Please suggest any way.

    Swapan

    __CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _HS_OSC

    cblock 0x20
    AD_VALUE1
    endc

    start
    CLRF PORTA
    CLRF PORTC
    BSF STATUS, RP0
    MOVLW b'00111000'
    MOVWF TRISA
    MOVLW b'00111100'
    MOVWF TRISC
    MOVLW 0XC0
    MOVWF ANSEL
    MOVLW b'00100000'
    MOVWF ADCON1
    BCF STATUS, RP0
    MOVLW 0X07
    MOVWF CMCON

    MAIN_ON:

    MOVLW b'00011001'
    MOVWF ADCON0
    BSF ADCON0,GO

    INIT:

    BTFSC ADCON0,GO
    GOTO INIT
    MOVF ADRESH,0
    MOVWF AD_VALUE1
    MOVLW D'240'
    SUBWF AD_VALUE1
    BTFSS STATUS,C
    GOTO RL_1
    MOVLW B'00000000'
    MOVWF PORTA

    RL_1:

    MOVLW D'220'
    SUBWF AD_VALUE1
    BTFSC STATUS,C
    GOTO MAIN_ON
    MOVLW b'00000001'
    MOVWF PORTA
    MOVLW D'200'
    SUBWF AD_VALUE1
    BTFSC STATUS,C
    GOTO MAIN_ON
    MOVLW b'00000011'
    MOVWF PORTA

    GOTO MAIN_ON

    END


  2. #2
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,796
    Blog Entries
    13

    Default

    hi,
    On a Oshonsoft sim test, your program sets AN0 and AN1 pins as outputs , not analog.
    Eric " Good enough is Perfect "
    I will NOT answer PM's requesting technical help, please use the Forum
    PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/

  3. #3
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,796
    Blog Entries
    13

    Default

    hi,
    Look at the small changes to your prog.
    It makes AN0 and AN1 analog.
    Attached Files
    Eric " Good enough is Perfect "
    I will NOT answer PM's requesting technical help, please use the Forum
    PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/

  4. #4
    swapan Newbie
    Join Date
    Aug 2009
    Location
    KOLKATA (INDIA)
    Posts
    42

    Default

    Thanks Mr. ericgibbs for your cooperation. Actually, I have defined RA0, RA1, RA2 as outputs and RC2/AN6 and RC3/AN7 as AD input.

  5. #5
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,796
    Blog Entries
    13

    Default

    Quote Originally Posted by swapan View Post
    Thanks Mr. ericgibbs for your cooperation. Actually, I have defined RA0, RA1, RA2 as outputs and RC2/AN6 and RC3/AN7 as AD input.
    hi,
    The way I read your first post was that you are using AN0 and AN1 as analog.?

    If you could explain the problem in more detail, I am sure we can help.

    BTW: when you post code, select it all and then click on the '#' in the above menu bar, that way the formatting will not be lost.

    Makes it easier to read.
    Eric " Good enough is Perfect "
    I will NOT answer PM's requesting technical help, please use the Forum
    PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/

  6. #6
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,796
    Blog Entries
    13

    Default

    hi,
    Look at this image, you are RESETTING PORTA.1 when you SET PORTA.0
    Attached Images
    Eric " Good enough is Perfect "
    I will NOT answer PM's requesting technical help, please use the Forum
    PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/

  7. #7
    swapan Newbie
    Join Date
    Aug 2009
    Location
    KOLKATA (INDIA)
    Posts
    42

    Default

    Thank you Sir. What I want to do is driving the electromagnetic relays of an automatic voltage stabilizer using PIC. Before proceeding further I wanted to carry a test of the adc module. For this purpose I have arranged in the following manner. Two relays are driven by RA0 and RA1. RC2/AN6 and RC3/AN7 are used for AD input. A 4.7K Pot is used as potential driver connecting to 5V supply rail and ground so that any value of AD Sampling voltage could be provided (0V to 5V) by rotating the pot. Now according to the code, when the AD input is 5V, both RA0 and RA1 will go low making the relays OFF. At a point where AD input is lower than D'220', RA1 will go high making one of the relays ON. Similarly, at a point where AD input is lower than D'200', both RA0 and RA1 will go high making all the relays ON. In practical, when the AD input is below D'200', both RA0 and RA1 is held high. On gradual rotating the pot, making AD input above D'200', RA1 becomes clear. Upto this, the operation is OK. But rotating the pot further, making AD input above D'220', the RA1 goes again high whereas as per code it should remain low. At AD input above D'240' both RA0 and RA1 should held low. But making the AD input about 5V (D'255') both RA1 and RA0 remain high. Please see if there is any error in my code.

    Swapan

  8. #8
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,796
    Blog Entries
    13

    Default

    hi swapan
    Try this, now works OK in simulation.

    EDIT: a zipped avi file showing the program working using the Oshonsoft simulator.
    Attached Files
    Last edited by ericgibbs; 8th October 2009 at 07:27 AM.
    Eric " Good enough is Perfect "
    I will NOT answer PM's requesting technical help, please use the Forum
    PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/

  9. #9
    swapan Newbie
    Join Date
    Aug 2009
    Location
    KOLKATA (INDIA)
    Posts
    42

    Default

    Thanks Mr. Ericgibbs. I have not yet tested the modified code in practical. I have carefully avoided the instruction bcf and bsf as one of the friends of a forum suggested me not to use them because of Read-Modify-Write feature. Sir, would you spare your valuable time to detail the RMW feature of microcontrollers?

    Swapan

  10. #10
    kpatz Good kpatz Good
    Join Date
    May 2009
    Location
    NH
    Posts
    305

    Default

    Certain instructions, such as bcf and bsf, will read the entire register, perform some operation on it, and then write the result back to the register. For example, the instruction bsf REG1,2, as you know, sets bit 2 of REG1. What really happens inside the PIC is that all 8 bits of REG1 are read into an internal work register inside the PIC's ALU, bit 2 is set there, and then all 8 bits are written back into REG1.

    The snag that can happen with RMW instructions is when they are used on PORT registers where there are pins set as input. Reading a port register actually reads the state of the pins themselves. Writing a port register writes to the output data latches of the port. A RMW instruction may change bits in the output latch that weren't intended. For example, let's say that RB0 is a pin that's multiplexed and used as both an input and an output. RB7 is an output pin that we want to turn on with a bsf instruction. Last time you set RB0 when it was an output, you set it to a 0, so that's what's in the output latch for that bit. When RB0 is set to output, it will be low. But when it's an input, it will read whatever the pin voltage is at (high or low). So, if you do a bsf PORTB,RB7 and RB0 as an input is high, the output data latch will end up with RB0 = 1 instead of 0.

    The solution in this case is to use a shadow register to retain output pin values, do your RMW instructions there, then move the shadow register to the actual port.

    Code:
        bsf    portb_shadow,7    ; Set portb to output in shadow
        movfw portb_shadow     ; Get shadow register
        movwf PORTB               ; and write to port B
    

  11. #11
    swapan Newbie
    Join Date
    Aug 2009
    Location
    KOLKATA (INDIA)
    Posts
    42

    Default

    It gives me great pleasure to say that the code is working fine. Really I am very thankful to you. Thank you again.

    Swapan

  12. #12
    swapan Newbie
    Join Date
    Aug 2009
    Location
    KOLKATA (INDIA)
    Posts
    42

    Default

    Thanks Mr. kpatz. Now the RMW feature of PIC is quite clear.

  13. #13
    sahu Okay
    Join Date
    Oct 2009
    Posts
    129

    Red face

    Friends,
    PL HELP ME...
    I have desing to make an automatic voltage stabilizer using 16F676.A tremer

    (2K) has been used to feed analogue adc supply from 5V rail in RA0[PIN. 13].
    its arrangment in the following manner.

    relay 4- RC2/8 .....using as o\p relay.

    relay 3- RC1/9......using as fx. relay.

    relay 2- RC0/10 .....using as fx.relay.

    relay 1- RA1/12......using as fx.relay.

    adc in -RA0/13.......feed analogue adc supply from 5V rail

    bz -RC3/7.......o\p high volt[250]cut off [relay 4] it sbeep 1\2 sec

    led[g] -RC4/6......i\p normal ind.

    led[r] -RC5/5......o\p high volt[250]cut off ind.

    low volt-RA4/3.......low voltage cut off enebal when it contact GND.

    ins start-RA5/2......instant start[offter 3sec relay 4 driven]when it contact

    GND.
    ONLY ONE I\P (2K) has been used to feed analogue adc supply from 5V rail in

    RA0[PIN. 13].
    Attached Files
    Last edited by sahu; 8th November 2009 at 04:27 PM.

+ Reply to Thread

Similar Threads

  1. Problem with ADC
    By timbash in forum Micro Controllers
    Replies: 4
    Latest: 10th July 2008, 07:10 PM
  2. ADC problem
    By sheng_james in forum General Electronics Chat
    Replies: 1
    Latest: 23rd April 2008, 03:56 PM
  3. Different ADC problem
    By kalaman in forum Micro Controllers
    Replies: 8
    Latest: 1st December 2007, 09:16 PM
  4. ADC problem
    By DanD in forum Micro Controllers
    Replies: 6
    Latest: 28th June 2006, 12:50 AM
  5. ADC problem
    By zedman in forum Micro Controllers
    Replies: 6
    Latest: 7th March 2006, 02:19 PM

Tags for this Thread