PDA

View Full Version : Help with code please



Tina10
- 5th January 2010, 00:38
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.


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

Archangel
- 5th January 2010, 03:36
Hi Tina10, welcome to the forum.
Here, totally untested, try this:


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