PDA

View Full Version : Memory Space of the PIC16F84...



Tear
- 29th June 2005, 19:59
Hello,

I have been looking this afternoon through datasheets trying to find out how many words the compiler can say before my PIC16F84 is full and I will run into problems. I am currently at 1005 words, and getting kind of worried that I might run out of memory since I still have about a quarter of the program left to write. I recall reading about 2000 being a "magic" number. Is this 2000 figure the amount of words I can have? Thanks for the help.

~Michael

Bruce
- 29th June 2005, 20:48
The PIC16F84 is a 1K part, so you're right at the edge with 1005. Your
program can be as large as 1024 words. 0 to 3FFh.

mister_e
- 29th June 2005, 21:40
It could be interesting to see if we can shrink your code. Do you mind to post it here to see if we can do it??

Of course with an explanation and schematic(if possible)

Tear
- 29th June 2005, 22:50
Hi,

Thanks guys. I will gladly past my code when it is finished. Right now it is all segmented and not really one solid piece. So, as it stands right now would be hard to decipher what I am doing. I should have it finished next week and will post it in this thread.

Thanks for the help!

~Michael

Tear
- 30th June 2005, 21:19
The 3rd post down is a much clearer post of what I am having problems with:

http://www.picbasic.co.uk/forum/showthread.php?t=1997





This is a repost, but I am hoping that you guys might see one of these.

I am working on a delay project which uses 2 buttons one for increasing the delay and one for decreasing the delay. I am using the PIC16F84 and have a button connected to PORTB pin 5,6. LED on PORTB 3,4,7 and PORTA pin 3.
PORTB pin 4 is for showing the length of the delay, pin 3 for showing when both buttons are pushed that it saves the delay to EEPROM, 7 for showing when IncreaseButton is pushed, and PORTA pin 3 for when DecreaseButton is pushed.

The problem I am having is when I program the PIC16F84 it is starting at a delay of what I believe to be 30s when it should be 15s. This is according to this line:

data word (0),(1500) 'Save 1.5s delay to EEPROM at programming

Also, it doesnt seem that the buttons are changing the delay. What I dont understand is that everything worked until I added this above line.

Here is the code that I am working on:

''Increase/Decrease Delay with Buttons

Delay var word 'Declare Variables
x var word
IncreaseButton var PORTB.5
DecreaseButton var PORTB.6

data word (0),(1500) 'Save 1.5s delay to EEPROM at programming
read 0, Delay.BYTE0 'Save value in EEPROM to delay
Read 1, Delay.Byte1

Loop:

high PORTB.4 'Turn LED on
low PORTB.3 'Turn on LED from "saving" delay

for x = 1 to Delay 'For loop to run delay/ Allows button to be
'read without interrupt
pause 1 'Pause 1 ms

if IncreaseButton = 1 then 'When IncreaseButton is pressed
high PORTB.7 'Turn LED on to show button is pushed

pause 50 'Pause for 50 ms

if DecreaseButton = 1 then'If both buttons pushed then write delay
write 0, Delay.BYTE0 'to EEPROM
write 1, Delay.BYTE1
high PORTB.3

SetDelayCycle1:

'Loop to wait until buttons are released before going on
if DecreaseButton = 0 or IncreaseButton = 0 then
goto Loop 'Restart the delay
else
goto SetDelayCycle1
endif

endif
Delay = Delay + 500 'increase delay
if Delay => 30000 then 'If delay is equal to or greater than 30 sec
Delay = 30000 'then keep delay at 30 sec max delay
pause 100 'If at max delay length flash LED
low PORTB.7
pause 100
high PORTB.7
endif

IncreaseButtonCycle:

'Loop to wait until button is released before going on
if IncreaseButton = 0 then
LOW PORTB.4 'Turn LED off
pause 500 'Pause to show LED off before restarting delay
low PORTB.7
goto loop 'Restart the delay
else
goto IncreaseButtonCycle
endif
endif

if DecreaseButton = 1 then 'When DecreaseButton is pressed
high PORTA.3
pause 50 'Pause for 50 ms

if IncreaseButton = 1 then'If both buttons pushed then write delay
write 0, Delay.BYTE0 'to EEPROM
write 1, Delay.Byte1
high PORTB.3

SetDelayCycle2:

if IncreaseButton = 0 or DecreaseButton = 1 then
goto loop 'Restart the delay
else
goto SetDelayCycle2
endif

endif
Delay = Delay - 500 'decrease delay
if Delay <= 500 then 'If delay is equal or less than 0.5 sec then
Delay = 500 'keep delay at 0.5 sec min delay
pause 100
low PORTA.3
pause 100
high PORTA.3
endif

DecreaseButtonCycle:

if DecreaseButton = 0 then
low PORTB.4 'Turn LED off
pause 500 'Pause to show LED off before restarting delay
low PORTA.3
goto loop 'Restart the delay
else
goto DecreaseButtonCycle
endif
endif

next x

low PORTB.4 'Turn LED off after full delay cycle
pause 500 'Pause to show LED off before restarting delay

goto loop 'Restart the delay (Infinite Loop)
end 'End of program



This probably is not a very efficent piece of code, but it was doing the job until I tried doing the DATA command. I tried using different values other than the 1500 such as.. 100, 500, 3000 and all of the values were causing the same thing to happen with a delay of what I believe to be 30s the max delay that I have for the piece. I also tried the EEPROM command with the same conclusion. Any help would be appreciated.

Thanks,
Michael

mister_e
- 1st July 2005, 02:29
Hi Michael,
Try the following and post results. Where i am now i can't test it


' I am working on a delay project which uses 2 buttons one for increasing the
' delay and one for decreasing the delay. I am using the PIC16F84 and have a
'
' buttons:
' ========
' connected to PORTB pin 5,6.
' pin 5 : increase
' pin 6 : decrease
' LEDS:
' =====
' PORTB pin 4 : showing the length of the delay,
' pin 3 : showing when both buttons are pushed that it saves
' the delay to EEPROM,
' pin 7 : showing when IncreaseButton is pushed
'
' PORTA pin 3 for when DecreaseButton is pushed.

TRISB=%01100000 ' PORTB<6:5> as input
' others as output
'
TRISA=0 ' set all PORTA as output

LEDLength var PORTB.4
LEDSave var PORTB.3
LEDIncrease var PORTB.7
LEDDecrease var PORTA.3

Delay var word
Loop var word
PB var byte
Increase con 1
Decrease con 2
Both con 3

data @0,5,$DC ' store initial 1.5 sec

PORTB=0 '
' Clear all LEDs
PORTA=0 '

DelayLoop:
read 0,delay.highbyte ' Read delay
read 1,delay.lowbyte ' from EEPROM

ledlength=1 ' Enable Length LED

for loop=0 to delay ' Do the delay and do PushButton tests
pause 1
pb=(PORTB>>5)& 3 ' keep only PORTB.5 and PORTB.6 bits

if pb then ' If there's at least one push button press
select case Pb ' choose according thing to do
case increase
if delay<=60000 then
delay=delay+500
ledincrease=1
endif

case decrease
if delay>=500 then
delay=delay-500
leddecrease=1
endif

case both
ledsave=1
write 0, delay.highbyte
write 1, delay.lowbyte

end select
pause 100 ' kinda auto repeat delay... can be higher/lower

ledincrease=0 ' Clear all pushButtons
leddecrease=0 ' related
ledsave =0 ' LEDs
endif
next
ledlength=0 ' confirm delay is finished
pause 500 ' wait a little bit
goto DelayLoop ' redo the delay and blah blah

Tear
- 1st July 2005, 15:34
Thank you SOOOOOOO much Steve!!!!!! This code is exactly what I need to make it work! My poor, unefficent code was doing the job, but this is so much cleaner and easier to use!

I defiantly have a lot of learning to do. This was my first attempt at writing my own program.

Also, with your version of this code I save nearly half the words!

I have been studying the way you have wrote this code and I dont understand how this line works. I understand what it does but not how. I am just curious on this one?

pb=(PORTB>>5)& 3 ' keep only PORTB.5 and PORTB.6 bits

Thanks,
Michael

mister_e
- 1st July 2005, 19:55
Great, but there's a tons of way to it more efficient. i was just trying to do something readable. Glad to know it's work as you wish. Hard to test when you're out of the city in a hotel room with a simple laptop.

BTW let's look at
pb=(PORTB>>5)& 3 ' keep only PORTB.5 and PORTB.6 bits


this line read the whole PORTB and shift bits 5 position to the right and finnaly do a bitwise AND with 3.
WHY... easier to read...and get only push buttons state

let's see how it's work.

Let's assume that PORTB=%11100000
after >>5 = we have %00000111
after & 3 we have %00000011 = 3 => both push buttons

Of course we an also use PB=PORTB & %01100000
and modify the Increase, decrease and Both constant definition too. That will save some code space and will do the same job. Or skip the Constant definition and place the expected value in the Select Case, or, or or , or...Posibility are endless.

Sometimes, there's a choice to do. Having a readable code or a less readable but really tight. It's case by case.

My last project use a 18F4620 and the full 64K of code. I spent many hours to shrink it as i can each time i add a new feature BUT I was out of codespace to implement new feature(on customer request)and i spend several hours to shrink everything by using PBP macro and do some workaround. Less readable but work as well as it suppose to.

Great to know it's working fine for you.

Have fun!