12F675 A/D and GPIO sleep interrupt


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Jul 2008
    Location
    devon
    Posts
    15

    Default 12F675 A/D and GPIO sleep interrupt

    Hi,
    This project opens a valve if it's closed and has 5V at the A/D input, and closes it if it is open and has a 0V at the A/D input, going to sleep after 10s. It wakes if the GP5 input goes high. This is OK except that it hangs if powered up in the closed position. Could anyone advise if my logic in the code is wrong or if the A/D reading is set up wrong? The ADCON0.0 = 0 doesn't seem to deactivate the A/D converter either. Here is the code: Thanks


    @ device pic12F675, intrc_osc_noclkout, wdt_off, pwrt_on, mclr_on, protect_off, bod_off
    CMCON = %00000111 'CMCON = 7 comparators off
    trisio = %00101001 'INPUTS =1 OUTPUTS=0 (GP3 always i/p)
    DEFINE OSCCAL_1K 1 'Set OSCCAL for 1K device (tunes to exactly 4MHz)
    ANSEL = %01010001 '16tosc, gpio0 analogue input
    ADCON0 = %10000000 'right just , Vref=Vdd , ch.0 , A/D off
    INTCON=%10001000 'Interrupt Control Register
    ioc = %00100000 'Set interrupt on change to GP5

    'variables
    POTVALUE VAR word 'POTENTIOMETER VALUE VARIABLE
    ValveState var bit 'ValveState: 0 = Valve closed 1 = Valve open
    TimeOut var byte 'variable to countup timeout

    'Start up procedure
    pause 1000
    GPIO = %00010100 'IN1 and enable Dir1 (close) for start condition
    pause 2000
    GPIO = %00000000
    let valvestate = 0
    let timeout = 0

    'loop
    START:
    pause 1000 'loop time (determines timeout)
    GPIO = %00000000 'keeps o/ps off, also clears interrupt GPIF
    adcon0.0 = 1 'turn on a/d
    pause 10
    ADCON0.1 = 1 'start A/D
    while ADCON0.1 = 1 'do until A/D finished (use GO/DONE bit?)
    wend
    potvalue.highbyte = adresh 'high byte of 10bit value
    potvalue.lowbyte = adresl 'low byte of 10bit value
    'Is this formatting the result correctly?

    If (POTvalue < 250) and (valvestate = 1) THEN
    GPIO = %00010100 'IN1 and enable Dir1 (close)
    pause 3000
    valvestate = 0
    adcon0.0 = 0 'turn off a/d until next loop
    endif

    IF (POTvalue >= 250) and (valvestate = 0) THEN
    GPIO = %00000110 'IN2 and enable Dir (open)
    pause 3000
    valvestate = 1
    adcon0.0 = 0 'turn off a/d until next loop
    endif

    if (valvestate = 0) then timeout = timeout +1
    if (valvestate = 1) then timeout = 0
    if (timeout > 10) then goto SLEEP_IT 'timeout value * loop time
    goto start

    'subroutines
    SLEEP_IT: 'use asm code for sleep as pbp has a fixed period
    GPIO = %00000000
    pause 1000
    INTCON.0 = 0 'clear interrupt flag if set (GPIO should do this)
    @ sleep
    @ nop
    goto start

    end

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Maybe I'm not seeing the big picture . . .
    Code:
    SLEEP_IT: 'use asm code for sleep as pbp has a fixed period
    <font color=red>GPIO = %00000000</font color>
    pause 1000
    INTCON.0 = 0 'clear interrupt flag if set (GPIO should do this)
    @ sleep
    @ nop
    goto start
    in your sub routine you command all GPIOs as outputs, how will it receive
    the wake up?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    With global interrupts enabled it will wake up and jump to location 4 expecting an interrupt
    handler.

    Disable global interrupts. Interrupt on change will wake the PIC from sleep, then execute
    the instruction after sleep without vectoring to location 4.

    Read the section on Power-Down Mode (SLEEP) for details on why this happens.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Jul 2008
    Location
    devon
    Posts
    15


    Did you find this post helpful? Yes | No

    Default

    Thanks Joe.S and Bruce.
    The SLEEP routine does actually work!!

    Doesn't the GPIO = %00000000 command set all pins which are outputs to 0?
    TRISIO configures them as either inputs or outputs?
    It could be that the GPIO commands are causing my problem though.

    The problem is that the system stays in closed position if it is started in the closed position.
    If the system is started in the open position it closes it (as in the start-up routine) then opens closes and sleeps as intended.

    Any chance of a 2nd look?
    Cheers

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Can you post a schematic?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Jul 2008
    Location
    devon
    Posts
    15


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,
    Here it is. Basically driving a H-Bridge with 2 inputs and an enable.
    Hope this helps. The analog input and sleep interrupt is derived from one 0/12V signal, hence stepped down.
    Thanks
    Attached Images Attached Images  

Similar Threads

  1. Interrupt question
    By ultiblade in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th November 2009, 20:12
  2. Is it a correct way to deal with an interrupt?
    By financecatalyst in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 15th November 2009, 22:44
  3. rfPIC Serin problem
    By gavo in forum mel PIC BASIC Pro
    Replies: 52
    Last Post: - 11th March 2008, 17:52
  4. Scale 8bit ADC RGB LED voltmeter
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd February 2008, 03:37
  5. 12f675 programming question
    By puggy in forum General
    Replies: 3
    Last Post: - 30th November 2004, 23:34

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts