PDA

View Full Version : New to coding - Please look at my code thanks!



erice1984
- 3rd July 2007, 22:52
Just started a few days ago, PBPro Compiler gives me a whole list of syntax errors etc.. Please let me know if what is wrong it.



' Set Internal Oscillator to 4MHz
' Pin 01 - V_DD - +5v
' Pin 08 - V_SS - GND
'
' GPIO.0 - GP0 - 07 - DIG - input for momentary button
' GPIO.1 - GP1 - 06 - DIG - output for op indicator LED
' GPIO.2 - GP2 - 05 - DIG - output to switch on sprayer
' GPIO.3 - GP3 - 04 - DIG - input to monitor fan op
' GPIO.4 - GP4 - 03 - DIG - NC
' GPIO.5 - GP5 - 02 - DIG - NC
'
'---------- Options ------------------------------------------------------------

define OSC $60 'Set oscillator (clk) to 4MHz
ANSEL = %000000 'Analog/Digital ports
INTCON = 'GPIO External interrupts
GPIO = %000000 'Set High/Low GPIO.5 -> GPIO.0 (all low)
TRISIO = %001001 'Set input/output GPIO.5 -> GPIO.0 (GP0 & 3 input)

'---------- Constants & Symbols/Variables --------------------------------------

M_BUT con GPIO.0 'Name bit 0 of GPIO to Momentary Button
LED con GPIO.1 'Name bit 1 of GPIO to LED
PUMP con GPIO.2 'Name bit 2 of GPIO to Pump
FAN con GPIO.3 'Name bit 3 of GPIO to Fan

symbol ON_OFF = 1

'---------- Subroutines & Loops ------------------------------------------------

PUMP_RUN: 'This is defined for the pump on/off
symbol X0 = 0 'X0 is the variable used in LED_F loop for counting
pump = 0 'Turn misting pump on
LED_RUN: 'LED Flash loop begins here
if X0 < 10 then
gosub LED_F
else
LED = 0 'Makes sure LED is off
pump = 0 'Turns relay controlling the pump off
return 'Returns to main code

LED_F:
LED 1 'Turns on the warning LED
pause 500 'Pause 500mS (1/2 second) with LED on
LED = 0 'Turns off warning LED
pause 500 'Pause 500mS (1/2 second) with LED off
X0 + 1 'This adds 1 to variable X0 each time it loops
goto LED_RUN 'Returns to begining of LED Flash loop


FAN_RUN:
if FAN = 1 then 'If cooling fans are on then...
gosub Pump_run 'Run pump sequence
pause 15000 'Pause for 15 seconds, no point in saturating
else
pause 5000 'Pause for 5 seconds
goto fan_run 'Returns to beginning to see if fan is running
ENDIF

'---------- Main Code ----------------------------------------------------------

if M_BUT = 1 then 'If the button is pressed then...
Pause 2000 'Wait 2 seconds before continuing
if M_BUT = 1 then 'If button is still pressed
gosub PUMP_RUN 'Then run the pump and flash LED
else
if ON_OFF = 1 then 'If auto-spray is on...
ON_OFF = 0 'Then turn it off
else 'Otherwise...
ON_OFF = 1 'Turn auto-spray on
ENdif
ENDIF
ENDIF



Only other question with this is, do I need to use an interrupt for the button if the fan trig has turned on the pump, and running the LED Flash loop?

skimask
- 3rd July 2007, 23:00
M_BUT VAR GPIO.0.....and so on for the rest of them
Read the manual. Con only assigns a number to a name, not a pin to a variable.

symbol ON_OFF = 1....SYMBOL not explicitly needed, unless you're using PicBasic compiler instead of PBP

if X0 < 10 then
gosub LED_F
else
LED = 0 'Makes sure LED is off
pump = 0 'Turns relay controlling the pump off
return 'Returns to main code
NO ENDIF HERE ---where's the ENDIF?

X0 + 1 'This adds 1 to variable X0 each time it loops
---That might be what you want it to do, but it's not going to do that
X0 = X0 + 1


Look at your code a little harder next time. You'll spot these errors a lot faster and easier if you've got the manual handy.

mister_e
- 3rd July 2007, 23:14
Hi, and welcome here !

Please, next time tell us which PIC # you're using ;)

I see some things wrong here

define OSC $60
should be 4,8 or whatever else crystal value you're using. Assuming you MAY use a 12F683 you will need to set the OSCCON register instead.

4MHz is the default speed, you don't really need to tell it to the compiler.
<hr>
NEXT!

INTCON =
it miss something here.. like a value ;)


M_BUT con GPIO.0 'Name bit 0 of GPIO to Momentary Button
LED con GPIO.1 'Name bit 1 of GPIO to LED
PUMP con GPIO.2 'Name bit 2 of GPIO to Pump
FAN con GPIO.3 'Name bit 3 of GPIO to Fan

use VAR instead of CON will solve the problem.




ON_OFF VAR bit
ON_OFF = 1
X0 VAR BYTE


PUMP_RUN: 'This is defined for the pump on/off
X0= 0
pump = 0 'Turn misting pump on
LED_RUN: 'LED Flash loop begins here
if X0 < 10 then
gosub LED_F
else
LED = 0 'Makes sure LED is off
pump = 0 'Turns relay controlling the pump off
return 'Returns to main code
endif

LED_F:
LED = 1 'Turns on the warning LED
pause 500 'Pause 500mS (1/2 second) with LED on
LED = 0 'Turns off warning LED
pause 500 'Pause 500mS (1/2 second) with LED off
X0 = X0 + 1 'This adds 1 to variable X0 each time it loops
goto LED_RUN 'Returns to begining of LED Flash loop



It compile without error... not sure if it do what you need :(

erice1984
- 3rd July 2007, 23:23
I appreciate it guys. Like I said, first time coding.

I missed somethings since I did it at 2am last night = half asleep

Anyhow yes, 12F683

Will I have any problems with bouncing on the button?

and I haven't found/read any good information on how to setup the INTCON or INTOSC on the chip.

THANKS A LOT GUYS! only error I get now is the INTCON and that one I knew would show up.

Really thanks a bunch.

mister_e
- 3rd July 2007, 23:33
;) did you tried with the datasheet ? ;) For the internal OSC, have a look for OSCCON register

Ok, what do you need to do exactly? There's more than one way to skin a cat anyways....

erice1984
- 3rd July 2007, 23:47
I am worried that the PIC will only be able to deal with one loop at a time, and if that is the case then...

When fans come on it will be stuck for 10 seconds doing one thing when maybe the user wants to turn the system off.

So if that is the case, then I may need to make use of interrupts.

and I will look at the datasheet in a little while, racing in Forza 2 ATM, damn Ferrari is hard to drive with AI at hardest difficulty.

mister_e
- 3rd July 2007, 23:53
:eek: could be worst if you had a Pagani Zonda on hand :eek:

So now, YES interrupts would be handy INDEED.

erice1984
- 4th July 2007, 00:00
i hate those cars, the turn in on them is horrible! worse than a car like the impreza that was designed to have understeer issues!

P.S. when you have understeer problems you have to purposely induce a drift/power slide to get the damn car to turn. I know I drive an Impreza in RL, a lot easier to get it to oversteer if you have the balls to go into a turn waaaay too fast, and punch the brakes to the thresh-hold and back waay off and it loosens the rear up (all while turning). But that helps with more money in the car than I actually paid for it 4 years ago.

Anyhow, the osccon register,

OSCCON = 0 110 0 1 1 1

bit 7 = nothing, so 0 is used
bit 6-4 = 4MHz
bit 3 = device running from secondary clock (Internal osc)
bit 2 = I want a stable high freq clock
bit 1 = no idea, INTRC, made it stable I shouldn't be using this but what do I know... stable girlfirend is better than unstable so I picked stable
bit 0 = internal osc is used because i dont want to waste space/money on ext osc.

erice1984
- 4th July 2007, 00:46
INTCON or Interrupt-on-Change IOCCON ?

INTCON = 00 0 0 0 0 0 1

bit 7-6 = 0 / unimplemented
bit 5 = gpio.5
bit 4 = gpio.4
bit 3 = gpio.3
bit 2 = gpio.2
bit 1 = gpio.1
bit 0 = gpio.0

GPIO.0 = M_BUT so 1 = enable :)

erice1984
- 5th July 2007, 02:56
So does this look right;



'---------- Options ------------------------------------------------------------

define OSC 4 'Set oscillator (clk) to 4MHz
OSCCON = %01100111 'Oscillator Settings
ANSEL = %000000 'Analog/Digital ports
INTCON = %00000001 'GPIO External interrupts
GPIO = %000000 'Set High/Low GPIO.5 -> GPIO.0 (all low)
TRISIO = %001001 'Set input/output GPIO.5 -> GPIO.0 (GP0 & 3 input)


Problem it is throwin some errors on it.

mister_e
- 5th July 2007, 13:52
no compilation error here...

erice1984
- 5th July 2007, 15:34
it is saying

thinks they are symbols

skimask
- 5th July 2007, 15:36
it is saying

thinks they are symbols

Doesn't look like you've got 12F683 selected in the drop down...

erice1984
- 5th July 2007, 18:42
ya got me, thanks.. I didn't realize that was important, thought it was only used for programming


All I need to do is make use of interrupts and I think this thing is ready for a try :)