PDA

View Full Version : Battery backup with long delay



Dwayne
- 4th January 2004, 20:42
Hello folks,

Name here is Dwayne, I am very much a neophyte to the PIC chips, and trying to learn them... I used to program philips chips, and the transition is quite a bit different.
Through trial and error, and much help from a few folks out there, I have finally compiled the following Code for the 12f675 chip.

Could someone take a look at it, can check it out for possible errors that I may not know of? Especially on the pin groundings and switches?
I am accomplishing the following;

;When chip is started

;immediately take pin1a and ground it. (This activates the battery ;backup) .

Loop:

Begin Delay for 5 to 7 min.
;following 3 lines is a press of a switch
ground another pin1b.
Delay for 1/2 second.
unground same pin1b.

Delay for 1 min
Unground first pin1a (this cancels battery backup).

****if power is still active do the following 4 lines ****
counter++;
if(counter==3)unground pin2a (KILL power totally to chip)
delay 1 min.
Goto Loop;
*******************************************
If chip loses power, start at beginning of loop.



Seconds var byte ' 0-59
Minutes var byte ' 0-5
counter var byte ' 0-3
counter=0;

Loop:
;Battery backup pin toggles base of transistor to insure 5 volts
Low GPIO.3;

For Minutes=0 to 6
gosub Delaymin
Next Minutes

;the following routine acts like pressing a button..
;grounds pin 4 to ground (direct short to ground)
Low GPIO.4;
Pause 250;
;ungrounds pin 4
High GPIO.4;

gosub Delaymin

;takes battery backup off. If no power, chip stops.
;and starts over again when power is applied to it.
;if not, chip reassures battery backup for up to 3 times.
High GPIO.3
counter=counter+1
if counter=3 then Low GPIO.3

gosub Delaymin


goto Loop

Delaymin
For Seconds=0 to 59
Pause 1000
;flashing light for reference
toggle GPIO.5
Next Seconds
return

Melanie
- 6th January 2004, 16:08
Some of your code although it will work (errors excepted) appears to make no sense Dwayne... first a couple of hints...

Firstly. Download the Datasheet of the 12F675. Make friends with it. Look at the drawing of the Pinout at the start of Page 1. The first mistake you've made is assigned GPIO.3 as an output, when this is an INPUT ONLY pin. So this has to be assigned to a different pin.

Second. Assign names to your I/O lines... that way if you need to reassign pins (as you do now), you only need to do it in one place, and it makes it so much more readable and easier to follow... eg...

Battery var GPIO.3 ' (you need to change GPIO.3 to another pin)
ButtonPin var GPIO.4 ' This is your Button-press Simulation
LED var GPIO.5 ' three guesses what this pins got on it...

Now instead of in your Delaymin subroutine saying...

toggle GPIO.5

you now say...

toggle LED

see... reading the code instantly you can see what you're doing rather than thinking "what have I got connected to GPIO.5?" Also if you need to change pins (like you do with GPIO.3), you need only change it ONCE at the top of your code with the var assignment, rather than a million times in the intricate depths of your code.

Thirdly. You forgot the colon at the end of Delaymin at the start of your subroutine.

Fourthly. You've not assigned TRISIO or made the pins Digital. By default, the 12F675 comes up with the pins in Analogue mode... so you need...

ANSEL=0 ' see Datasheet section 7.2
CMCON=%00000111 ' see Datasheet section 6.0 & table 6.2

for TRISIO each pin on the 12F675 can be input or output. Assign a 1 for input and a '0' for output. So in the example below, pin 3 is input only (bit 3 is set to '1'), all the others are output. The 12F675 doesn't have pins 6 and 7 (bits 6 & 7), so they're ignored...

TRISIO=%00001000

Fifthly... if there is such a word... you're dropping in semicolons for comments. Semicolons ";" are comments for ASSEMBLER, use the single quote (' - apostrophe) for comments in BASIC.

Sixthly... scatter comments freely... they help you (and others) follow the code more easily...

Now your code rehashed looks something like this...

'
' Hardware Assignments
'
Battery var GPIO.6 ' I've reassined this from pin 3 to pin 6
ButtonPin var GPIO.4 ' This is your Button-press Simulation
LED var GPIO.5 ' OK you guessed it - this is your LED...
'
' Software Assignments
'
Seconds var byte ' 0-59
Minutes var byte ' 0-5
counter var byte ' 0-3
counter=0
'
' Initialise Hardware
'
ANSEL=0
CMCON=%00000111
TRISIO=%00001000
'
' Main Program Starts Here
'
Loop:
Low Battery
'Battery backup pin toggles base of transistor to insure 5 volts
'
' Six minute Delay
'
For Minutes=0 to 6
gosub Delaymin
Next Minutes
'
' Simulate Button Press
'
Low ButtonPin
Pause 250
High ButtonPin
'
' Wait another minute
'
gosub Delaymin
'
' The Confusing Bit
'
' takes battery backup off. If no power, chip stops.
' and starts over again when power is applied to it.
' if not, chip reassures battery backup for up to 3 times.
High Battery
counter=counter+1
if counter=3 then Low Battery
gosub Delaymin
'
' Run Main Loop Again
'
goto Loop

'
' Subroutine Area
'
Delaymin:
For Seconds=0 to 59
Pause 1000
Toggle LED ' flashing light for reference
Next Seconds
return
'
' End of Program
'
End

Now I label the end section "The Confusing Bit" because I can't see what you're trying to achieve here. You set Battey High, and only set it Low after three counts... but look... as soon as you loop back in your loop, it gets set Low anyway... kinda defeats the object...

Anyway... enough of me pulling your code apart...

Melanie

Dwayne
- 6th January 2004, 20:42
Hello Melanie,

Melanie >>Some of your code although it will work (errors excepted) appears to make no sense Dwayne... first a couple of hints...<<

Thanks!... This is exactly what I need.

Melanie >>Firstly. Download the Datasheet of the 12F675. Make friends with it. Look at the drawing of the Pinout at the start of Page 1. The first mistake you've made is assigned GPIO.3 as an output, when this is an INPUT ONLY pin. So this has to be assigned to a different pin.<<

I was finally able to find the chip pin schematics. I found it in a 5 meg file download that took forever on a dialup of 28.800 <g>.

Melanie >>Second. Assign names to your I/O lines... that way if you need to reassign pins (as you do now), you only need to do it in one place, and it makes it so much more readable and easier to follow... eg...<<

I tried this, but since I have no idea of the logic of the basic compiler, I couldn't get it to work.. None of my assembly code works with the MPASM. Denny helped me by telling me the way to control the pins, so I took it from there.

in assembly.... Second EQU byte. works well with Philips. not good with PICs <g>.

I have a small learning curve. I only have to figure out which commands equate to each other.

Melanie >>Battery var GPIO.3 ' (you need to change GPIO.3 to another pin) <<

Thank you so very much. I finally got the schematic and it said this pin is a Input/interupt. Philips chips, all ports on the 750 are in/out. (Another lesson I learned, Thank you!). I assumed GPIO was Port I/O off of Port 0 (since it only had port like letter of G and less than 8 pins) to be pin 3. This equated to PO.3 in the philips chips. P1.3 equates to Port2 pin 3.

Melanie >>
toggle GPIO.5
you now say...
toggle LED<<

Thank you very much... with the knowledge of assigning pin to a var, (other than using my failed attempt at equate <g>) things are much easier.

Melanie>>Thirdly. You forgot the colon at the end of Delaymin at the start of your subroutine.<<

Thanks!


Melanie>>Sixthly... scatter comments freely... they help you (and others) follow the code more easily...<<

I had comments in it, but the online compiler only allowed 900 bytes. So i had to be picky... I am sorry.

Melanie >>Now I label the end section "The Confusing Bit" because I can't see what you're trying to achieve here. You set Battey High, and only set it Low after three counts... but look... as soon as you loop back in your loop, it gets set Low anyway... kinda defeats the object...<<

Yes, it does seem like a defeat, but in actuallity, it is a safeguard check.

Melanie >>Anyway... enough of me pulling your code apart...<<

No Melanie, I want to thank you very much. I couldn't find commands of Basic, I had to find what I could, make do with what I could, and figure out how to get my program to compile within 900 bytes. My knowledge is zelch on these PIC's, and
you and Denny have helped beyond. The only thing i can say is thanks, and hope you really understand that I mean it.

Please feel free to do such things. I am a person with a open mind, and always look for easier and better ways to do things.

Dwayne

Melanie
- 6th January 2004, 21:38
>> I tried this, but since I have no idea of the logic of the basic compiler

You can download the manual free from the MeLabs website... buying a copy of PBP helps too (you can always start with PBC and work your way up as you need more features)... it means you can do far more complex projects, it supports MeLabs and keeps this website open for all to use and enjoy.