program crashes: is it my code or the chip?


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: program crashes: is it my code or the chip?

    I see what you are saying, Henrik. @ Acetronics2: I have been reading the reference manual and better understand now that a CONSTANT would not be the best modifier for this situation. I am trying to use a single push button switch to toggle between 2 different LEDs X 2, so that one half of the 1/2 PIC12F675a (GPIO.0,1,2) performs this function and the other half of 1/2 PIC12F675b (GPIO.3,4,5) performs the exact same function with an entirely different push button and set of LEDs. And also that both halves perform separate from one another.

    Here is my new code. The toggle of the LEDs works good, but I am still having the same problem as before.

    Code:
    LED1 var GPIO.0 ' Alias GPIO.0 to LED1
    LATCH1 var GPIO.1 'Alias GPIO.1 to LATCH1
    PB1 Var GPIO.2 ' Alias GPIO.2 to push button
    LED1on var word
    LED1off var word
    LED2 var GPIO.5
    LATCH2 var GPIO.4
    PB2 var GPIO.3
    LED2on var word
    LED2off var word
        
        ANSEL = 0 ' Set all digital
        CMCON = 7 ' Analog comparators off
        TRISIO = %11001100
        LED1 = 0
        LATCH1 = 1
        LED1off = LED1
        LED1on = LATCH1
        LED2 = 0
        LATCH2 = 1
        LED2off = LED2
        LED2on = LATCH2
        
    
    main:
        if PB1 = 0 then
            pause 10 
            gosub LED1status
        LED1on = 1
        LED1off = 0
        endif
        
        if PB2 = 0 then
            pause 10
            gosub LED2status
        LED2on = 1
        LED2off = 0
        endif
        
            do until PB1 = 1
                pause 5
            loop
            
            do until PB2 = 1
                pause 5
            loop
            
        pause 10
    
        
    goto main
    End
    
    LED1status:
        if LED1 = 1 then 
            LATCH1 = LED1on
            LED1 = LED1off
        elseif LED1 = 0 then
            LED1 = LED1on
            LATCH1 = LED1off
        endif
    return
    
    LED2status:
        if LED2 = 1 then
            LATCH2 = LED2on
            LED2 = LED2off
        elseif LED2 = 0 then
            LED2 = LED2on
            LATCH2 = LED2off
        endif
    return

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: program crashes: is it my code or the chip?

    Hi,
    I'm really struggling to follow that code with port bits being read into WORD variables and vice versa. I understand that you want to know WHY your code doesn't work but, and please don't take this the wrong way, that's just WAY to convoluted IMHO.

    The following compiles OK for a 12F675 but I have not tested it. Please see if you can follow the logic behind it, then try it on the actual hardware.
    Code:
    LED1    var GPIO.0              ' Alias GPIO.0 to LED1
    LATCH1  var GPIO.1              ' Alias GPIO.1 to LATCH1
    PB1     Var GPIO.2              ' Alias GPIO.2 to push button
    
    LED2    var GPIO.5              ' Same as aboove for second "port"
    LATCH2  var GPIO.4
    PB2     var GPIO.3
    
    Debounce VAR BYTE               ' Counter for the debounce rotutine.
    
    ANSEL = 0                       ' Set all digital
    CMCON = 7                       ' Analog comparators off
    GPIO = 0                        ' All outputs OFF
    TRISIO = %11001100              ' Set ins and outs
    
    '-------------------------------------------------------------------------------
    
    Main:
        IF PB1 = 0 THEN             ' Button 1 pressed?
    
            TOGGLE LED1
            TOGGLE LATCH1
            
            Debounce = 0            ' Clear counter variable
            
            While Debounce < 99     ' Signal must be stable for 100 counts in a row.
                IF PB1 = 1 THEN     ' Button is released (or bouncing).
                    Debounce = Debounce + 1
                ELSE
                    Debounce = 0    ' If button still active, clear counter.
                ENDIF
                PAUSEUS 100         ' 100 counts * 100us = 10ms "silence" before continuing         
            WEND
        ENDIF
    
    '-------------------------------------------------------------------------------
    
        IF PB2 = 0 THEN             ' Button 2 pressed?
    
            TOGGLE LED2
            TOGGLE LATCH2
            
            Debounce = 0            ' Clear counter variable
            
            While Debounce < 99     ' Signal must be stable for 100 counts in a row.
                IF PB2 = 1 THEN     ' Button is released (or bouncing).
                    Debounce = Debounce + 1
                ELSE
                    Debounce = 0    ' If button still active, clear counter.
                ENDIF
                PAUSEUS 100         ' 100 counts * 100us = 10ms "silence" before continuing         
            WEND
        ENDIF
    '-------------------------------------------------------------------------------
    
    Goto Main
    Now, this is ALSO a bit convoluted and can certainly be made more "compact" but I wanted to keep it as simple as possible for starters. Let me know if you try it out.

    /Henrik.

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default Re: program crashes: is it my code or the chip?

    and What about this one, using the Button command ?

    Code:
    '*******************************************************************************
    ' Name : BUTTONTEST2.pbp
    ' Compiler : PICBASIC PRO Compiler 3.0
    ' Assembler : PM
    ' Target PIC : 8-pin PIC12F675 or similar type
    ' Hardware : Kee
    ' Oscillator : 4MHz internal
    ' Keywords : HIGH, LOW
    ' Description : use a push button to toggle two LEDs. THere are two push buttons.
    'each push button toggles a separate set of LEDs. Program also debounces
    '*******************************************************************************
    #config 
        __CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF
    #endconfig
    
    DEFINE OSCCAL_1K 1 ' PIC12F675, Calibrate internal oscillatordefine MCLRE_OFF
    DEFINE BUTTON_PAUSE 255
    Delay    VAR Byte
    Button1 VAR GPIO.2
    Button2 VAR GPIO.3
    LED1    VAR GPIO.0 'Alias GPIO.0 to LED
    LED2    VAR GPIO.1 'Alias GPIO.1 to LED2
    LED3    VAR GPIO.4 'Alias GPIO.4 to LED3
    LED4    VAR GPIO.5 'Alias GPIO.5 to LED4
    CMCON = 7
    ANSEL = 0
    ADCON0 = 0
    GPIO    = %00001100
    TRISIO  = %00001100
    CLEAR
    '*******************************************************************************
    Main:
    '*******************************************************************************
    WHILE 1
    FIRST:' let's check Button1 ...
        Delay = 0
        button Button1, 0, 255, 0, Delay, 1,yes1
            no1:
            Pause 20 ; pace the scanning ... not compulsory
            goto Second
            yes1:
            toggle LED1    
            ' Stop until Button1 released
            WHILE !Button1 
            WEND
         
    SECOND:' let's check Button2
        Delay = 0
        button Button2, 0, 255, 0, Delay, 1,yes2
            no2:
            Pause 20 ; pace the scanning ... not compulsory
            goto Update
            yes2:
            toggle LED3
            'Stop until Button2 released
            WHILE !Button2
            WEND
    Update:        
    led2 = ! LED1 : LED4 = !LED3
    WEND
    END
    Alain
    Last edited by Acetronics2; - 25th March 2014 at 22:20.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. PICKit2 just crashes
    By johnbond in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 26th June 2011, 08:40
  2. Replies: 1
    Last Post: - 22nd May 2011, 20:37
  3. 16f690 crashes
    By Lincoln S. in forum Serial
    Replies: 8
    Last Post: - 26th November 2008, 09:32
  4. Program Code and Program Memory
    By DenFrod in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th February 2007, 15:51
  5. problems reading code from chip
    By leland in forum General
    Replies: 4
    Last Post: - 17th October 2004, 11:42

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