PDA

View Full Version : Am I heading in the right direction?



james
- 24th September 2007, 15:34
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.




@ 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.

mister_e
- 24th September 2007, 15:55
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.

james
- 24th September 2007, 18:17
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.

mister_e
- 24th September 2007, 19:06
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..
@ 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...

james
- 24th September 2007, 19:22
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.

Archangel
- 25th September 2007, 02:06
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

james
- 26th September 2007, 15:23
I dont think my orginal explanation of this project was clear enough.

Digits come at intervals of 30 seconds. Total digits per input = 1. Total inputs per code = 4 So I need to scan for 2 minutes, then once I have all four digits, I need to send an output based on the input received.

Look for input1 for 30seconds = TS1
Look for input2 for 30seconds = TS2
Look for input3 for 30seconds = TS3
Look for input4 for 30seconds = TS4


so if I get a 1 as my first input, then the other 3 inputs dont matter, LED1 lights up.
If my first input is a 2 but the other inputs are all zero's, then no LED's light up
If my first input is a 2 and any other input is 1-4, then LED2 + LED1 light up
my examples of the first inputs being 2 correspond with the first inputs being 3 or 4 aswell.

Example of 5 different codes received and their output operation:
ts1---ts2---ts3---ts4
1-----0-----1-----0---= HIGH LED1
2-----0-----0-----0---= NO LED'S HIGH
2-----1-----0-----0---= HIGH LED1 and LED2
3-----0-----0-----0---= NO LED'S HIGH
3-----0-----2-----0---= HIGH LED1 and LED3


hopefully this will help clarify the operation. i've changed my code a bit, I "think" its starting to get to where it needs to be, but I still dont know how to assign 30seconds per input received.



'Code Development / Debug Device Setting
' DCBA9876543210
@ 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

'------( Constants )------
'

'------( Defines )------
'
Define OSC 8 'Set oscillator at 8MHz

'------( Variables )------
'
GotInfo var byte
GotInfo1 var byte
GotInfo2 var byte
GotInfo3 var byte
empty var byte 'Time variable not used
tS1 var byte 'Time variable for 1st digit
tS2 var byte 'Time Variable for 2nd digit
tS3 var byte 'Time Variable for 3rd digit
tS4 var byte 'Time Variable for 3th digit

'------( Registers )------
' 76543210
OSCCON = %01110110 'Set Osc for 8Mhz
TRISB = %00001111 'RB0-RB3 = inputs, RB4-RB7 = outputs
TRISA = %00000000 'RA0-RA7 = outputs
PORTA = %00000000 'Turn off portA
CMCON = %00000111 'Turn comparators OFF
ANSEL = %00000000 'A/D converter set as Digital I/0

'------( Disable Interrupts/Registers )------
' 76543210
INTCON = %00000000
PIE1 = %00000000
PIR1 = %00000000
OPTION_REG = %00000000
'------( 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: 'Wait here for input

if pswitch1 = 0 then 'If switch1 is pressed
led1 = 1 'Light LED1
pause 3000 'Pause 3 seconds
led1 = 0 'Turn LED1 off
goto inputloop 'check for more inputs
endif


if pswitch2 = 0 then 'If Switch2 is pressed
tS1 = gotinfo 'rename Switch2, tS1
while ts1 = gotinfo '
'----------------------------
if pswitch1 = 0 then '
ts2 = Gotinfo1
else
ts2 = empty
'----------------------------
if pswitch3 = 0 then
ts2 = gotinfo1
else
ts2 = empty
'----------------------------
if pswitch4 = 0 then
ts2 = gotinfo1
else
ts2 = empty
'----------------------------
if ts2 = empty then
goto checkTs3
endif
endif
endif
endif
wend
endif

checkTS3:
while ts1 = gotinfo and ts2 = empty

if pswitch1 = 0 then
ts3 = gotinfo2
else
ts3 = empty
'----------------------------
if pswitch3 = 0 then
ts3 = gotinfo2
else
ts3 = empty
'----------------------------
if pswitch4 = 0 then
ts3 = gotinfo2
else
ts3 = empty
'----------------------------
if ts3 = empty then
goto checkts4
endif
endif
endif
endif
wend


checkTS4:
WHILE TS1 = gotinfo and ts2 = empty and ts3 = empty

if pswitch1 = 0 then
ts4 = gotinfo3
else
ts4 = empty
'----------------------------
if pswitch3 = 0 then
ts4 = gotinfo3
else
ts4 = empty
'----------------------------
if pswitch4 = 0 then
ts4 = gotinfo3
else
ts4 = empty
'----------------------------
if ts4 = empty then
goto nothing

endif
endif
endif
endif
wend


if ts1 = gotinfo then
led1 = 0
led2 = 0
led3 = 0
led4 = 0
goto inputloop
endif

if ts1 = gotinfo and ts2 = gotinfo1 then
led2 = 1
led1 = 1
pause 3000
led2 = 0
led1 = 0
goto inputloop
endif

if ts1 = gotinfo and ts3 = gotinfo2 then
led2 = 1
led1 = 1
pause 3000
led2 = 0
led1 = 0
goto inputloop
endif

if ts1 = gotinfo and ts4 = gotinfo3 then
led2 = 1
led1 = 1
pause 3000
led2 = 0
led1 = 0
goto inputloop
endif

Archangel
- 26th September 2007, 22:01
Hi James,
try this for your config setting instead of the @ devise xxx you have, and use the osccon settings below to set osc speed, just remove the comment for the one you need. MPASM assembler settings

@ __config _CONFIG1, _INTRC_IO & _WDT_ON & _LVP_OFF & _CP_OFF

' for 8mhz
'OSCCON=%01111000
'for 4MHZ
'OSCCON=%01101000

You might give this a try too:
If PortB != %00001111 then PAUSE 30000 ' pause 30 seconds

JS