PDA

View Full Version : Need help in code



camolas
- 17th April 2008, 14:53
Hi,

Im traing to macke a code in PBP for a 18f877a (to add more things in futer) and im very new at this sow i can not macke one that works well one month traing :( .

I need 2 condictions to light the led (2 push buttons comands or input)
first only goto to secund if on (input=1)
secund waits intill is of (input=0)
then after 40s light the led for 3s
then off the led and goto to first condiction.


Thanks in advance and sory for my bad Inglish.

Acetronics2
- 17th April 2008, 15:12
Hi, Camolas

Welcome here ... and do not be sorry for your English.

YOU made this effort ... some don't dare.

Back to our subject, Her you won't find "already cooked" code ... except if you do a search over the forums ... for existing applications.


The habit is : YOU try to make something run ( by the way learn how the processor works , and which PicBasic commands you have to use ....) - and then , we help you.


The first thing you'll have to do is CONFIGURE your processor:

- Which will be the Inputs and Outputs ?

- How to disable Unwanted features like comparators and ADC ... that could interfere with PORTs

for that ... the '877 is not the simplest ! ... would you own an old 16F84 ??? ( not compulsory - do not buy one ... but would help you a lot ! )


And, once that done, begin to translate your " text" program into successive elementary PbP ( PicbasicPro ...) commands.

So, you'll need :

1) the 16F877a Datasheet ( from Microchip site )
2) the PicbasicPro Manual corresponding to your release ( which one ? 2.50 ? )
3) some paper sheets to note the 877 registers and data for the config.
4) a good pencil and rubber
5) a HUGE coffee cup (hot is better ...)
6) LOTs of patience

...

Welcome aboard and read you soon.

Alain

JD123
- 17th April 2008, 15:48
Alain, you forgot one...

7) A swear jar ;)

You too can retire rich!

Camolas, in a month's time you should have some code. Post it so we can take a look at it.

camolas
- 17th April 2008, 17:03
Hi,

Thanks for the starting hellp

PicbasicPro 2.50





My code

@ DEVICE pic16F877A, WDT_ON ' Watchdog Timer
@ DEVICE pic16F877A, PWRT_ON ' Power-On Timer
@ DEVICE pic16F877A, BOD_ON ' Brown-Out Detect
@ DEVICE pic16F877A, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F877A, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F877A, PROTECT_OFF ' Program Code Protection
@ DEVICE pic16F877A, WRT_OFF ' Flash Memory Word Enable

TRISB.0=1
TRISB.1=1
TRISC.7=0
LED var PORTC.7

inicio:

if PORTB.0 = 0 then
GOSUB liga
else
low LED
endif
goto inicio

liga:
if PORTB.1=1 then
high LED
PAUSE 200
low LED
else
low LED
endif
goto inicio


end

JD123
- 17th April 2008, 17:25
At first glance, replace the "goto inicio" at the end of the program with "return" (it was called under a "gosub").

camolas
- 17th April 2008, 17:41
I got 2 problems 1 de led dont turn of afther the periode and 2 i need the code to do like a cicle and stops when led off and only start again when "switch" 1 is on again.

skimask
- 17th April 2008, 17:46
I got 2 problems 1 de led dont turn of afther the periode
Fixed by post above?


and 2 i need the code to do like a cicle and stops when led off and only start again when "switch" 1 is on again.
Ok, so how do you determine if the LED is off?
How do you determine if 'switch 1' is on again?
If you can answer those 2 questions, then you should be able to write your code.

camolas
- 17th April 2008, 19:24
Fixed by post above?


Ok, so how do you determine if the LED is off?
How do you determine if 'switch 1' is on again?
If you can answer those 2 questions, then you should be able to write your code.


Fixed by post above?


Ok, so how do you determine if the LED is off?
How do you determine if 'switch 1' is on again?
If you can answer those 2 questions, then you should be able to write your code.


Hi, not fixed.


determine LED - i do not now if i anderstand you well (bad Inglish mine) but by having switch2 pressed led if off. (or if switch1 pressed is off to)

'switch 1' - I only put this switch to try to stop the code there from looping and looping because i only want that led is high 1 time at the time (just when switch1 be pressesd again), then wait for switch2 to be not pressed.

Thanks

JD123
- 17th April 2008, 19:42
One more thing...

Turn the WDT OFF since your not clearing it.

Ex:@ DEVICE pic16F877A, WDT_OFF ' Watchdog Timer off

mister_e
- 17th April 2008, 20:01
The compiler handle it for you :o

Acetronics2
- 17th April 2008, 20:05
Hi, Camolas

1) some contradiction ...

" text " tells :

if input1 = 1 THEN


WAIT for INPUT2 = 0 == WHILE INPUT2 = 1 : WEND ( loop if INPUT2 = 1 )

WAIT 40s == PAUSE 40000 ... not 200 !
LED ON for 3 s
LED Off
LOOP


the program shows input levels inverted ... ( may be active low pushbuttons ??? did you think to the pullup ? )

I think the first thing for you is to look at the different states ... and write succession of operations in order, more than write lines and try to make them work together.
That's called an ORGANIGRAM ... ( joined an example )

Now, I've almost written your program ... just translate it into PbP commands ( I gave you the "difficult part" command ... )

Alain

camolas
- 17th April 2008, 21:12
Thas i need to put WDT OFF or not?

Yes im going to use low pullup pushbuttons rhas i have to do one more line code for it?

Im very confuse :(

skimask
- 17th April 2008, 21:40
Turn the WDT OFF since your not clearing it.
PBP inserts WDT clearing instructions for you......unless you:
DEFINE NO_CLRWDT 1
in which case, it doesn't. And if you're not using the WDT and if you have it turned off, this can also save a LOAD of code space.

skimask
- 17th April 2008, 21:42
Thas i need to put WDT OFF or not?
I wouldn't worry about it...


Yes im going to use low pullup pushbuttons rhas i have to do one more line code for it?
Sounds like you might need to back up a bit and work on the simple stuff first, i.e. make the blinky LED, make the LED follow a single button under SOFTWARE CONTROL, and so on and so on...build up to what you need.
This 'jumping in with both feet' approach doesn't seem to be working out too well...

mackrackit
- 17th April 2008, 22:00
Up in post#5 you are setting the fuses in code space. Have you commented out the lines in the *.inc file?
http://www.picbasic.co.uk/forum/showthread.php?t=543
Read post #1 and #5...

And I do not see how the OSC is set or a

DEFINE OSC ?? ?? being the speed.

mister_e
- 17th April 2008, 22:12
When you're using PM, you shouldn't need to bother with it. Just to confirm it, i just uncommented the one in a .INC file and write over them... no compilation errors for PM, and the config fuses are set as expected when i look them in my PIC Programmer software.

If no OSC is defined, PBP will consider 4 MHz.. no big deal. Default XT fuses for PM is XT. it seems to have some activity, seems something work, i worry more about the program flow. I'm still trying to understand what the OP need to do :o

Camolas, can you ask your question in your own language (i feel spanish) so maybe someone else here could help you to translate it for us (maybe google translation but :eek: )

camolas
- 17th April 2008, 23:01
Hi again,

Im Portugese next Europen winers cup (lol i hope) :)

In Portugese:

Pretendo controlar com um botao low pullup (ou mais se for necessario) um led da seguinte forma.
-Quando o botao esta desligado demore 40s a acender o led e depois fique aceso durante 3s.
Depois de apagado fique a espera de outro sinal (aqui podera ser um outro botao ou entao o mesmo quando fique no estado de ligado) e so volte a fazer a mesma rutina depois que o botao seja desligado led aceso.... ........

Now more dificult in my best Inglish:

I need to control a led with one low pullup pushbutton (or more if neded) to perform this tasck.
-Wen pushbutton is not pressed tacks 40s then turn on led for 3s.
So the led is now off then the code must whait for another signal ( i can use one more pushbutton or the same because e is on at this time) and then when this signal go to off e stars the rutine again, Wen pushbutton is not pressed tacks 40s then turn on led for 3s...


Thanks all

mackrackit
- 17th April 2008, 23:12
camolas,

Do you have a schematic you could post?

camolas
- 17th April 2008, 23:49
camolas,

Do you have a schematic you could post?

Yes i have please see it. (i put 2 bottuns but with a good code one i think can do the jobe)

Thanks

camolas
- 18th April 2008, 00:50
Hi,

Just solve it now and with only one pushbutton :)

Code:


@ DEVICE pic16F877A, WDT_ON ' Watchdog Timer
@ DEVICE pic16F877A, PWRT_ON ' Power-On Timer
@ DEVICE pic16F877A, BOD_ON ' Brown-Out Detect
@ DEVICE pic16F877A, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F877A, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F877A, PROTECT_OFF ' Program Code Protection
@ DEVICE pic16F877A, WRT_OFF ' Flash Memory Word Enable

TRISB.0=1
TRISC.7=0

LED var PORTC.7
Bot1 var PORTB.0

CMCON=%00000111 ' Disable Comparators
CVRCON=%00000000 ' Disable Reference Module
OPTION_REG.7=0 ' Enable Weak Pull-Up's


ini:
Low LED
WHILE Bot1=0 :WEND
PAUSE 10000
high LED
PAUSE 300
Low LED
GOSUB FIM

FIM:
WHILE Bot1=1 :WEND
GOTO ini
End



Thanks you all

Acetronics2
- 18th April 2008, 09:14
Hi, Camolas




...

GOSUB FIM

FIM:
WHILE Bot1=1 :WEND
GOTO ini

...



Those two lines ( GOSUB FIM and FIM: ) can be simply wiped off : the only use is to loose two clock cycles ... say nothing for us.


" Im Portugese next Europen winers cup"

too much ... vinho verde ???


Alain

camolas
- 18th April 2008, 13:51
Hi, Camolas




...

GOSUB FIM

FIM:
WHILE Bot1=1 :WEND
GOTO ini

...



Those two lines ( GOSUB FIM and FIM: ) can be simply wiped off : the only use is to loose two clock cycles ... say nothing for us.


" Im Portugese next Europen winers cup"

too much ... vinho verde ???


Alain

Hi,

I will try that later.

About the vinho verde not at all for the moment ;)

camolas
- 21st April 2008, 23:46
Thanks for the hellp Alain, when you came to Prtugal i will buy you a botle of vinho verde.

Im from the city that mackes the bert Portugese vinho verde Casal Garcia, Aveleda...

By by