Help with code please


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2010
    Posts
    51

    Question Help with code please

    Hi, I am new to the field of PIC programming but trying to catch up. I need some help guys to write my code.
    I have bought 12F508 and pins GP0 to 4 are pulled high by 10K with pushbuttons attached to the ground. I would like to have an shortest possible code to read the ports and send an serial command through GPIO.5 based on combinations of the buttons pressed. I have tried other ways using AND's but the PIC runs out of memory with this kind of approach.

    I tried with the following but it's not working. I would like to know if the problem is with the code or my hardware. Once I make sure the code is ok then I will jump on to check the hardware settings.
    Code:
    define  NO_CLRWDT  1
    define  OSC        4       ' OSCCON defaults to 4MHz on reset
    
    Include "modedefs.bas"
    
    @ __CONFIG _IntRC_OSC & _WDT_OFF &_MCLRE_OFF & _CP_ON
    
    Tx  	    var     GPIO.5
    i			var		byte
    
    pause 50
    main:
            ' Initialize the processor
            TRISIO = %011111        ' 1 bits are inputs - O are outputs
            Tx=0			        ' Put Tx pin low
            OSCCAL = 0 
    PAUSE 50
                         
    start:
    while 1
    i=gpio
    pause 500
    if i=%11110 then      ' if gpio.0 is pressed
    'Serout............few bytes
    endif
    
    if i=%01110 then      ' if gpio.0 + gpio.4 is pressed
    'Serout............few bytes
    wend
    endif
    I am little confused as to when I write i=gpio, does it stores i[0]=gpio.0 OR i[7]=gpio.0
    Are the if statements above correct, I mean what will the PIC do with remaining 3 bits in i variable
    Please help me understand how this will work. Thanks

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


    Did you find this post helpful? Yes | No

    Default

    Hi Tina10, welcome to the forum.
    Here, totally untested, try this:
    Code:
    define  NO_CLRWDT  1
    define  OSC        4       ' OSCCON defaults to 4MHz on reset
    
    Include "modedefs.bas"
    
    @ __CONFIG _IntRC_OSC & _WDT_OFF &_MCLRE_OFF & _CP_OFF
    trisio = %00011111
    MyVar var byte
    index var byte
    
    mainloop:
    ' alias ports to a variable
    myvar = gpio
    MyVar.0 = gpio.0
    MyVar.1 = gpio.1
    Myvar.2 = gpio.2
    MyVar.3 = gpio.3
    MyVar.4 = 0 ' zero the upper 1/2 byte
    MyVar.5 = 0
    MyVar.6 = 0
    MyVar.7 = 0
    'convert binary port value to ascii representation of the numbers
    lookup myvar,["0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"]index
    'send out port 5 serially
    Serout gpio.5,T2400,[254,128,index]
    
    goto mainloop
    Last edited by Archangel; - 5th January 2010 at 03:51.
    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. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

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