Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

12c508a - Power Timer

Status
Not open for further replies.

PDubya

New Member
Apologies if this is overly obvious, but I'm new to pic programming.

I've got some little 12c508a's - and would like to just setup a simple power timer. The idea is basically to have a push-button that when it's pressed turns the circuit "on" and after say 2 hours turns it off. If the button is pressed anytime within that 2 hour window, the countdown would reset again.

I finally can do some basic stuff with the 16f628a (ooooh, blinkey lights!)- but don't want to use a huge pic just for a timer if I don't have to.

Thanks everybody!
 
You should start with an input that has an internal pullup. The button should ground the input. The program should look for a transition from 1 to 0. Once it "sees" the transition it should wait for some tens of milliseconds and sample the input again. If it is still low then it is a valid push. Conversely if the 0 to 1 transition is detected then you should wait some tens of milliseconds and sample the input again to see if it has returned to the high state. There are many variations on the basic debounce algorithm but the idea is the same.

The next challenge is to figure out how to count two hours worth of timer ticks. For a counter of this length you want the slowest clock you can get. I'll consult the data sheet and get back to you with specific recommendations.

Edit:
It looks like you can use GP3 as an input with a pullup. You can use GP0 or GP1 as your output. If you want, you can make them complementary. Pins GP2, GP4, and GP5 are uncommitted at the moment. The supply voltage needs to be between 3.0 and 5.5 Volts.

Use the internal 4MHz. oscillator to save parts and select the MCLR connected to Vdd option. there are no interrupts and only two stack locations. In addition we need to keep the program less than 256 words so we dont have tor worry about PC bit 8 being cleared when there is a call or a goto.

The reset address is 0x1FF which picks up the oscillator calibration value with a movlw instruction and rolls over to address 0x000.

Let us run Timer0 from the internal oscillator, assign the prescaler to Timer0, and select the maximum prescaler value.
Code:
Oscillator Frequency f = 4 MHz.
Timer0 Input f/4 = 1 MHz.
Maximum Prescaler is divide by 256
Timer0 Input = 3.90625 kHz., Period = 256 microseconds
Timer0 Overflows every 65.536 milliseconds
Now two hours is 7200 seconds and so
Code:
7200 / .065536 = 109,863
This means we will need a seventeen bit counter to time out two hours
 
Last edited:
The next challenge is to figure out how to count two hours worth of timer ticks. For a counter of this length you want the slowest clock you can get. I'll consult the data sheet and get back to you with specific recommendations.

I wouldn't bother trying to come up with clock rate for a 2 hr timer. just figure out how to time a small period - say 100 milliseconds. Then, in software, count those units. 2 hrs would be 120*60*10 of them. When it reaches the terminal count you do what ever you want. If the button is pushed, you zero the counter (and start over again). You will need to learn:
- using the timer
- interrupts
- multibyte addition
and as PB said,
- switch debounce

It's a good app to take you to the next level. Nothing overwhelming and it might be useful!
 
If you are familiar with the 628 then you may get a shock when you try using the 509. No interrupts, 2 word deep stack and no return instruction :eek: .

The 509 really is a microcontroller.

Mike.
 
wow, no interrupts.

no matter, just read the timer until the count you want is reached, then increment the time count.
 
Because of the nature of the project, software key debouncing handling gives no additional benefit to the operation.

Just loop to see if the key is LOW, if so reset counter. Key bounce would result in resetting counter multiple times but is insignificant compares to the two hour timing that follows.

No timer or no interrupt required, just use 4 registers and decreasing them in a loop to get 7200 seconds delay. Sample code follows:



Code:
; Delay = 7200 seconds
; Clock frequency = 4 MHz

; Actual delay = 7200 seconds = 7200000000 cycles
; Error = 0 %

	cblock
	d1
	d2
	d3
	d4
	endc

			;7199999998 cycles
begin

        ; turns output ON here

	movlw	0xFF
	movwf	d1
	movlw	0x08
	movwf	d2
	movlw	0xB0
	movwf	d3
	movlw	0x30
	movwf	d4
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	$+2
	decfsz	d4, f
	goto	Delay_0

			;2 cycles
	goto	$+1

       ; turns output off

You still have to provide the rest of the key handling and I/O toggling yourself.

The tricky bit is to insert a key checking routine inside the above code and jump to label "begin" when the key is LOW.
 
Last edited:
Pommie said:
If you are familiar with the 628 then you may get a shock when you try using the 509. No interrupts, 2 word deep stack and no return instruction :eek: .

Rather misleading! - the 12C509 doesn't have THE 'Return' instruction, but it does have A 'Return' instruction - RETLW, which does the same thing, but has added functionality which the 'Return' instruction doesn't.

As for a 2 hour delay, you can easily do this using software loops, and the PICList even has a generator to create the code for you. Personally though I'd use the generator to create a shorter delay and enclose that in an extra loop to give the two hours required.

I'd also suggest adding a flashing LED would be a good idea, so you know it's still working!.
 
I like the flashing LED idea by Nigel.

Personally, I would make the LED flashs like crazy during the last 10 minute of timing or so.
 
Nigel Goodwin said:
Rather misleading! - the 12C509 doesn't have THE 'Return' instruction, but it does have A 'Return' instruction - RETLW, which does the same thing, but has added functionality which the 'Return' instruction doesn't.

OK, yes, rather misleading. I just wanted to get across the severe limitations of the 509.

I once wrote some code using a 628 and then ordered some 509s thinking the code would just port over to this 8 pin chip. I (wrongly) assumed that as they were both 16 series chips that converting the code would be simple.

Mike.
 
Pommie said:
OK, yes, rather misleading. I just wanted to get across the severe limitations of the 509.

I once wrote some code using a 628 and then ordered some 509s thinking the code would just port over to this 8 pin chip. I (wrongly) assumed that as they were both 16 series chips that converting the code would be simple.

And you still haven't noticed your BIG mistake? :p

For a clue:

16F628 and 12C509, why did you think they were both 16 series chips?, 16 series is 14 bit, and 12 series is 12 bit (and no, I don't know why the 16 series aren't called 14).
 
Nigel Goodwin said:
And you still haven't noticed your BIG mistake? :p

For a clue:

16F628 and 12C509, why did you think they were both 16 series chips?, 16 series is 14 bit, and 12 series is 12 bit (and no, I don't know why the 16 series aren't called 14).
The following 8-pin 12F' series devices use the 14-bit instruction core;

12F629 (4-MHz INTOSC)
12F636 (8-MHz INTOSC)
12F675 (4-MHz INTOSC)
12F683 (8-MHz INTOSC)
 
Just to confuse everyone, Microchip made the 16F505 with a 12 bit core :confused:

Mike.
 
Well got home excited to start fiddling - and then realized that it's not supported by PicSimulator <grumble>. I'm getting some odd errors, is it acceptable to post the code for review?
 
PDubya said:
Well got home excited to start fiddling - and then realized that it's not supported by PicSimulator <grumble>.

I'm not familiar with PicSimulator, but MPLAB SIM supports the 509.

I'm getting some odd errors, is it acceptable to post the code for review?

Post away, just make sure you use the code tag so it's easier to read.

Mike
 
Mike said:
The following 8-pin 12F' series devices use the 14-bit instruction core;

12F629 (4-MHz INTOSC)
12F636 (8-MHz INTOSC)
12F675 (4-MHz INTOSC)
12F683 (8-MHz INTOSC)

Yes, there are a few exceptions, but not in the ones you were involved with, there's also the 16C505 which is 12 bit.

But 'generally' it holds true.
 
Status
Not open for further replies.

Latest threads

Back
Top