Am I heading in the right direction?


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Jul 2007
    Location
    Maryland, USA
    Posts
    15

    Arrow Am I heading in the right direction?

    PIC16f88, 4 LED's with 3904's, 4 push switches <---sums up the hardware used.

    Im on a new project, and am having some troubles sorting out my thoughts into code.

    The idea of the project is to light up lights, depending on the code received. An initial 1,2,3, or 4 will determine if light 1,2,3, or 4 will light up. If there is a following number, then Light1 + Initial number will light up. . .but. . . If another digit does not follow 2,3 or 4 nothing is to light up. a 1 followed by anything will light, light 1.. The code can be upto 4 digits long, and each digit come at intervals of 30seconds...

    I think i've got the basics down but I would love to get some feedback. Learning this new language is mind boggling at times. And the event order in a timed sequence is leaving me dumb founded.


    Code:
    @  DEVICE  11111101110100b           ; Internal Osc, WDT, PWRT, BOD, MCLR reset, CCP w/ RB2, LVprog disabled,
                                         ; Flash program memory write enabled, No Code Prot., No EEPROM Prot.
                                         ; Fail-safe clock monitor enabled, Internal external switch over enabled.
    
    '------( Symbols/ Labels )------
    '     
    Symbol Pswitch1 = portb.0                'Pin6/RB0 is assigned as switch1
    Symbol Pswitch2 = portb.1                'Pin7/RB1 is assigned as switch2
    Symbol Pswitch3 = portb.2                'Pin8/RB2 is assigned as switch3
    Symbol Pswitch4 = portb.3                'Pin9/RB3 is assigned as switch4
    Symbol LED1 = portb.7                 'Pin13/RB7 is assigned as LED1
    Symbol LED2 = portb.6                 'Pin12/RB6 is assigned as LED2
    Symbol LED3 = portb.5                 'Pin11/RB5 is assigned as LED3
    Symbol LED4 = portb.4                 'Pin10/RB4 is assigned as LED4
    
    '------( Defines  )------ 
    '    
        Define OSC          8               'Set oscillator at 8MHz
    
    '------( Variables )------
    '
        anInput var byte
    
    '------( Registers )------
    '            76543210
        TRISB = %00001111                   'RB0-RB3 = inputs, RB4-RB7 = outputs
        TRISA = %00000000                   'RA0-RA7 = outputs
        PORTA = %00000000                   'Turn off portA
        CMCON = %00000111                   'Turn comparators OFF   
    
    '------( Initialization )------
    ' 
    main:
    
    led1 = 0                                'Turn LED1 off
    led2 = 0                                'Turn LED2 off
    led3 = 0                                'Turn LED3 off
    led4 = 0                                'Turn LED4 off
    pswitch1 = 1                            'Define switch1 as not pressed
    pswitch2 = 1                            'Define switch2 as not pressed
    pswitch3 = 1                            'Define switch3 as not pressed
    pswitch4 = 1                            'Define switch4 as not pressed
    
    goto inputloop
    '------( Main Code )------
    '  
    Inputloop:                                                 'Looking for input loop
    
    'For 1 goto routine "one", for 2 goto routine "two", for 3 goto routine "three", if 4 goto routine "four"
        branch aninput, [nothing, one, two, three, four]
    
    goto inputloop
    '------( Subroutines )------
    '-------------------------------- this portion is for testing on my breadboard
    nothing:
        goto inputloop
    
    one: 
            if pswitch1 = 0 then                 'If one, then do stuff
                goto Ledone               
            endif
        
    goto inputloop  
    
    two: 
            if pswitch2 = 0 then                 'If two, then do stuff
                goto ledtwo
            endif                         
    
    goto inputloop 
    
    three:                                        
            if pswitch3 = 0 then                 'If three, then do stuff
                goto ledthree
            endif
    
    goto inputloop
    
    four:
            if pswitch4 = 0 then                 'If four, then do stuff
                goto ledfour
            endif
    
    goto inputloop
    '-------------------------------- end breadboard testing portion.
    LEDone: 
            if pswitch1 = 0 then                           'If switch1 is pressed light LED1
                 led1 = 1
             else
                goto continue
            endif
                                                       
            if pswitch1 = 0 and pswitch2 = 0 then          'If switch1 and switch2 are pressed light LED1
                led1 = 1
             else
                goto continue
            endif
    
            if pswitch1 = 0 and pswitch3 = 0 then          'If switch1 and switch3 are pressed light LED1
                led1 = 1
             else
                goto continue
            endif
        
            if pswitch1 = 0 and pswitch4 = 0 then          'If switch1 and switch4 are pressed light LED1
                led1 = 1
            endif
    
    goto main                                           'restart the process         
    
    LEDtwo:
            if pswitch2 = 0 then                           'If switch2 is pressed
                led2 = 0                                   'Light nothing
             else
                goto continue
            endif
            
            if pswitch2 = 0 and pswitch1 = 0 then          'If switch2 and switch1 are pressed
                led2 = 1 and led1 = 1                      'Light LED2 & LED1
             else
                goto continue
            endif
            
            if pswitch2 = 0 and pswitch3 = 0 then          'If switch2 and switch3 are pressed
                led2 = 1 and led1 = 1                      'Light LED2 & LED1
             else
                goto continue
            endif
            
            if pswitch2 = 0 and pswitch4 = 0 then          'If switch2 and switch4 are pressed
                led2 = 1 and led1 = 1                      'Light LED2 & LED1
            endif
    
    goto main                                           'restart the process
            
    LEDthree: 
            if pswitch3 = 0 then                           'If switch3 is pressed 
                led3 = 0                                   'Light nothing
            else
                goto continue
            endif
            
            if pswitch3 = 0 and pswitch1 = 0 then          'If switch3 and switch1 are pressed
                led3 = 1 and led1 = 1                      'Light LED3 & LED1
             else
                goto continue
            endif
            
            if pswitch3 = 0 and pswitch2 = 0 then          'If switch3 and switch2 are pressed
                led3 = 1 and led1 = 1                      'Light LED3 & LED1
             else
                goto continue
            endif
            
            if pswitch3 = 0 and pswitch4 = 0 then          'If switch3 and switch4 are pressed
                led3 = 1 and led1 = 1                      'Light LED3 & LED1
            endif
    
    goto main                                           'restart the process
        
    LEDfour:
            if pswitch4 = 0 then                           'If switch4 is pressed
                led4 = 0                                   'Light nothing
             else
                goto continue
            endif
            
            if pswitch4 = 0 and pswitch1 = 0 then          'If switch4 and switch1 are pressed
                led4 = 1 and led1 = 1                      'Light LED4 & LED1
             else
                goto continue
            endif
            
            if pswitch4 = 0 and pswitch2 = 0 then          'If switch4 and switch2 are pressed 
                led4 = 1 and led1 = 1                      'Light LED4 & LED1
             else
                goto continue
            endif
    
            if pswitch4 = 0 and pswitch3 = 0 then          'If switch4 and switch3 are pressed 
                led4 = 1 and led1 = 1                      'Light LED4 & LED1
            endif
    
    goto main                                           'restart the process
    cOntinue:
    If you have some time, please help me get my brain around this.
    Last edited by james; - 24th September 2007 at 16:53. Reason: I felt the need

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    You also need to disable the A/D converters. ANSEL=0

    Make sure your configuration fuses are correctly set (at least osc and lvp mode)

    a little schematic and a status of what is working and what don't would help.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jul 2007
    Location
    Maryland, USA
    Posts
    15


    Did you find this post helpful? Yes | No

    Default Update

    I added the fuses into the original post. The home-made schematic is attached to this post.

    MCLR is tied to 5V via 1k resistor

    Problem I'm having at the moment is only LED1 will light up. If I push Switch2 & Switch3, I should get LED2 + LED1..when any combination of buttons are pressed nothing will light up(not working correctly). Unless its a combination, with the first digit being 1 then only LED1 will light up(is working correctly).

    I am not sure how to go about storing the first digit, wait for 30secs until the second digit comes in which I can figure out what pattern of lights to display..

    Hope this helps clarify my situation.
    Attached Files Attached Files

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Sorry i can't read the file you have attach. I would suggest you to convert it in a BMP, PNG, or whatever else format.

    If your schematic software don't allow that, use PrimoPDF. This will act as a virtual printer. So you just need to select PrimoPDF before you print and it will create a PDF file.

    Nice nifty and FREE software -> http://www.primopdf.com/

    this..
    Code:
    @  DEVICE  11111101110100b           ; Internal Osc, WDT, PWRT, BOD, MCLR reset, CCP w/ RB2, LVprog disabled,
                                         ; Flash program memory write enabled, No Code Prot., No EEPROM Prot.
                                         ; Fail-safe clock monitor enabled, Internal external switch over enabled.
    don't looks familiar. I would suggest you to read the following post

    http://www.picbasic.co.uk/forum/showthread.php?t=543

    also make sure you have pull-up resistor on PORTB...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jul 2007
    Location
    Maryland, USA
    Posts
    15


    Did you find this post helpful? Yes | No

    Default Schematic Round 2. . . fight

    I learned the @ device command from my boss, for setting the fuses. The binary comes from page 130 of the datasheet for PIC16F88.

    Schematic should be readable now, although Im fairly positive my problem lies within my code. thank you thus far.
    Attached Images Attached Images  

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


    Did you find this post helpful? Yes | No

    Default

    Code is working the way it is written. Your IF THEN loops basicly check to see if logic 0 is detected, if so it checks the next switch and if good continues . . . , it then executes a GOTO main and never goes to the next subroutine. it goes to the subroutine Continue if the if then is false, which as shown is an empty subroutine. The MAIN loop reinitializes the LEDs as off and sets the internal registers as logic high.
    I think you only want to execute the code listed as main at startup. I would use gosub to execute the subroutines and return in the subroutine to cause the code to RETURN to the place just after it was sent there. something like:<p>
    ' code initialization

    led1 = 0 'Turn LED1 off
    led2 = 0 'Turn LED2 off
    led3 = 0 'Turn LED3 off
    led4 = 0 'Turn LED4 off
    pswitch1 = 1 'Define switch1 as not pressed
    pswitch2 = 1 'Define switch2 as not pressed
    pswitch3 = 1 'Define switch3 as not pressed
    pswitch4 = 1 'Define switch4 as not pressed

    ' here is your main loop
    main:
    gosub nothing ' check this sub routine and then gosub to the next sub r...
    gosub LEDtwo ' this is the next thing loop will check
    gosub LEDfour ' and then it will heck here
    goto main ' now the loop repeats


    nothing:
    BLA BLA BLA
    return

    LEDtwo:
    MORE BLA BLA
    return

    YAGETTHEIDEA:
    bla bla
    return
    end

    HTH
    JS
    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.

Similar Threads

  1. decoding quadrature encoders
    By ice in forum mel PIC BASIC Pro
    Replies: 93
    Last Post: - 28th February 2017, 09:02
  2. Math help - rolling average Wind Direction
    By wjsmarine in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 23rd July 2009, 00:08
  3. How to detect sound direction?
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th December 2008, 04:53
  4. Replies: 10
    Last Post: - 8th April 2008, 21:07
  5. Replies: 5
    Last Post: - 12th September 2007, 15:59

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