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.

minute/second timer

Status
Not open for further replies.

throbscottle

Well-Known Member
I'd like to make a little programmable timer to countdown minutes and seconds. Doesn't even have to be particularly accurate. I have a 16C57 (bought long ago) I can use and a 16F57 to test on, which I know are ancient but still plenty adequate for this. It needs to (indirectly) drive a multiplexed LED clock display and a relay as the output. Input buttons would be something like: m/s select, up, down, start - 4 buttons.
My brain is rather fried and I can't even think where to start the program - some sort of flow chart would be a big help! Any offers? The 16x57 doesn't have interrupts btw. I can write assembly, get rather lost when people start talking about libraries and things.
Any gotcha's for people testing code on 16F and using it on 16C?
 
I keep forgetting about the tutorials here! I'll just mosey on down...
It will only be 3 or 4 digit. It's for my UV box so timings around half a minute are going to be it's usual setting. I did order a timer module off eBay but when that failed to arrive I thought I may as well make one (in the meantime I'll just have to stick in a good ole' 555 as I have found "elephant" to not be the most consistent unit of time)
 
Why not 'graduate' to a 16F?
Also you could make use of the TIM1 and a 32Khz watch crystal on the T1OCI/OSO pins.
32.768hz can be represented in 15 bits so by setting the MSB of the timer the clock rolls over every second.
I just replicated 555 a missing pulse timer with a 12f1822 as this has an internal 32Khz clock capability.
Max.
 
Well, I have both an F and a C, the main (only?) difference is that the C can only be programmed once. So I may as well use it once I've proved the code works, and then I can still experiment with the F for something else.
I don't think I have a watch crystal, but that's certainly a handy trick if I've understood you correctly Max. I have a few crystals of various frequencies so I'll see what I can make best use of. As mentioned above, accuracy isn't important.
 
You can get 20 for $2.00 from Hong Kong!
I have a demo program for the 16F870 if needed to just blink a LED.
Max.
 
UV is only 25~30 minutes... Strange!!! Both my pcb etcher and eeprom eraser have timers built in...

The pic16c57 will do the job very easy... Instead of buttons, you can get a dead cheap rotary encoder with push button... Up / down and start /stop in one little switch.. 80p at RS https://uk.rs-online.com/web/p/mechanical-rotary-encoders/7899649/
 
Ahhh, home made pcb dry film and solder resist exposure box, only comes equipped with what I equip it with... But about 24 elephants is the time I've been using :)
I have a rotary encoder similar to that waiting for me to use it for something. Don't think it has a push button though. Was going to be for something else I never got round to. Ah ha!
Max - I'd like to have a look at that demo program. I've previously written one program using the timer and it did my head in!
 
Ahahaha - just remembered I do have a watch crystal somewhere - still attached to a scrap pcb from something!
 
Here it is.
Max.
Code:
;=======SECONDS.ASM=================================9/30/02==
  list  p=16f870
  #include "p16f870.inc"  ;processor specific variable definitions
   __config  h'3f71'
  radix  hex
;------------------------------------------------------------
;seconds demo
;   external oscillator, 32768 Hz, timer 1, prescaler 1:1
;------------------------------------------------------------
;  cpu equates (memory map)

;------------------------------------------------------------
;------------------------------------------------------------
  org  0x000
   goto   start   ;skip over location pointed to by
       ;  interrupt vector
   org   0x004
   goto   iserv
;
start:
     bsf     STATUS,   RP0  ;switch to bank 1
     movlw  b'00000000' ;port B outputs
  movwf   TRISB
     movlw   b'00000110' ;turn off A/D, port A
     movwf   ADCON1
     bcf     STATUS,   RP0  ;switch back to bank 0
     movlw   b'00000000' ;port B lines low
     movwf   PORTB
     bcf     INTCON,   7  ;disable global interrupts
     bcf     INTCON,   6  ;disable peripheral interrupts
     bsf     STATUS,   RP0  ;bank 1
     bcf     PIE1,   0   ;disable tmr1 interrupts
     bcf     STATUS,   RP0  ;bank 0
     bcf     PIR1,   0    ;clear timer 1 interrupt flag
     movlw   b'00001010' ;prescaler and tmr1 setup,
     movwf     T1CON    ;  tmr1 off
     clrf     TMR1H   ;clear timer 1 high
     clrf     TMR1L   ;clear timer 1 low, clear prescaler
     bsf     INTCON,   7  ;enable global interrupts
     bsf     INTCON,   6  ;enable peripheral interrupts
     bsf     STATUS,   RP0  ;bank 1
     bsf     PIE1,   0   ;enable tmr1 interrupts
     bcf     STATUS,   RP0  ;bank 0
     bsf     T1CON,   0   ;timer 1 on
circle:
     goto   circle
;------------------------------------------------------------
iserv:
     bcf     PIR1,   0   ;clear timer 1 interrupt flag
     bsf     TMR1H,   7   ;set up timer 1 to roll over at
       ;  32,768 counts
     btfss   PORTB,   0   ;port B, bit 0 STATUS?
     goto   setbit   ;bit is clear
clrbit:
     bcf     PORTB,   0   ;clear port B, bit 0
     retfie     ;return from interrupt
setbit:
     bsf     PORTB,   0   ;set port B, bit 0
     retfie     ;return from interrupt
;------------------------------------------------------------
  end
;------------------------------------------------------------
;at device programming time, select:
;  memory unprotected
;  watchdog timer disabled (default is enabled)
;  standard crystal XT (using 4 MHz osc for test)
;  power-up timer on
;   brown-out reset enabled
;   lvp disabled
;   debug mode disabled
 
When using the 32Khz XTAL on the TMR1 osc pins a 22pf/25pf capacitor from each pin to GND is also used.
Incidentally ref the sample program, now the preferred method for output is to use the LATA/LATB inst. etc when output to a port pin is used.
Max.
 
So, does the program just pause at
bsf TMR1H, 7
until it times out, then execute the next line? That simple? Wow!
 
The program just rotates around the Circle: location and waits for the timer to roll over at 1 sec it then sets the interrupt and jumps to the Iserv routine where is either sets, or resets the LED dependent on its last state, and sets the timer to count again. ad-infinitum.
Max.
 
The program just rotates around the Circle: location and waits for the timer to roll over at 1 sec it then sets the interrupt and jumps to the Iserv routine where is either sets, or resets the LED dependent on its last state, and sets the timer to count again. ad-infinitum.
Max.
I thought he was using the pic16c57..... There is no ISR.... It would have to be a delay..
 
I actually found this, so I'm reading through the code to find what I need. No interrupts needed!
https://ww1.microchip.com/downloads/en/AppNotes/00590c.pdf
I wasn't sure if the timer generates an interrupt or not (both subjects being somewhat mysterious to me), but you are right Ian, it doesn't, nor does it have latch registers. I don't even know what one of those is...
 
Personally, if doing any extensive programming in Assembly, I would recommend graduating to the later IC's., particularly the 18F series.
For small footprint projects the latest 12F series have many of the same features, just that you have to watch the bank switching which is less of a problem concern on the 18F.
Here is an one source of code it is a little dated now, but you may find something of use. https://pic-rosa.blogspot.ca/2007/09/source-code-for-pic.html
Max.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top