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.

PIC- Creating a particular frequency from the internal oscillator

Status
Not open for further replies.

VRL_15

New Member
How to obtain 1 hz signal from 1Mhz internal clock oscillator in PIC 16f887?

if anyone has an idea please let me know

Thank you
 
that algorithm uses TMR0

is there any way I can just use the 1 Mz oscillator without sing any timer and output 1Hz freq at a particular port?
 
At the moment I wish to know how to obtain 1Hz signal at an ouput port without using timer and by using only internal clock oscillator of 1 MHz

If you have any idea please let me know
 
At the moment I wish to know how to obtain 1Hz signal at an ouput port without using timer and by using only internal clock oscillator of 1 MHz

If you have any idea please let me know

Like this:

Start:

Set pin high.

Delay 0.5 seconds.

Set pin low.

Delay 0.5 seconds.

Goto Start.
 
Hello,

All you have to do is code a delay that takes exactly 1/2 second minus 2 clock periods and use that as a delay between setting and clearing an output port pin, then goto the start again. That makes the entire period exactly 1 second subject to the crystal tolerance and drift:

Start
call Delay_OneHalfSecondMinus2InstructionTimes
bsf GPIO,0
call Delay_OneHalfSecondMinus2InstructionTimes
bcf GPIO,0
goto start

Do you know how to code delays?

Remember though the timing is still subject to the crystal tolerance and drift. If you use the internal RC oscillator then the timing will be subject to that tolerance and drift which is even worse.

Here is sample code to generate a one half second (minus 2 instruction times) delay using the internal 4MHz oscillator or an external 4MHz crystal:

Code:
Delay_OneHalfSecondMinus2InstructionTimes
  movlw .83
  movwf d1
  movlw .138
  movwf d2
  movlw .3
  movwf d3
Loop_499998us
  decfsz d1,f  ;dec reg d1 and skip next instr if zero, place result in d1
  goto Loop_499998us
  decfsz d2,f  ;dec reg d2 and skip next instr if zero, place result in d2
  goto Loop_499998us
  decfsz d3,f  ;dec reg d3 and skip next instr if zero, place result in d3
  goto Loop_499998us
  goto Goto_499998us
Goto_499998us
  return
Note the above code does not generate a perfect 50 percent duty cycle either, if you need that then we would have to use this instead:

Start
call Delay_OneHalfSecondMinus3InstructionTimes
bsf GPIO,0
nop
nop
call Delay_OneHalfSecondMinus3InstructionTimes
bcf GPIO,0
goto start

and the one half second minus 3 instructions times delay is the same as the delay code above except change the line:
'goto Goto_499998us'
to:
'nop'


Code:
Delay_OneHalfSecondMinus3InstructionTimes
  movlw .83
  movwf d1
  movlw .138
  movwf d2
  movlw .3
  movwf d3
Loop_499997us
  decfsz d1,f  ;dec reg d1 and skip next instr if zero, place result in d1
  goto Loop_499997us
  decfsz d2,f  ;dec reg d2 and skip next instr if zero, place result in d2
  goto Loop_499997us
  decfsz d3,f  ;dec reg d3 and skip next instr if zero, place result in d3
  goto Loop_499997us
  nop
  return
 
Last edited:
Hi again,


You're going to have to be more descriptive about what you want.
 
I am using PIC 16f887.

I just want to know if I can use the 1Mhz clock signal to generate 1Hz signal at any output port.

without any timers or time delays
 
@MrAl

I have a code in which all three timers of PIC 16F887 are being used for some or the other functions.

My objective is to clear the counter on LCD display after every 1 second. When I try to clear one of the timers and use them for 1 second, I am not getting the desired result.
It is actually interfering the whole operation. The three timers in that code are used in the Interrupt Service routine of the program.

Hence I want to know if there is any way I can keep resetting the counter after every one second.

Also when I tried using the delay loop of 1 second it did not work.

Basically I want something similar to timer that will keep running for one second in the background and help me reset the counter after every one second.

I hope I made it clear.

Please give your suggestion

Thank you
 
Chances are you're using more timers than you really need. For example, I use a single timer for a 100-usec interrupt interval on a 16F1827 project. It's running with a 32-MHz clock so there are plenty of instruction cycles (800 cycles) between interrupts. Every 100-usec interrupt interval I sample and decode an IR decoder signal and I setup the PWM duty cycle for the next 100-usec PWM period (used for display brightness control). Every 10th interrupt (1-msec) I refresh the 4-digit 7-segment display. Every 200th interrupt (20-msecs) I sample and debounce the push-button switches. One timer interrupt, the TMR2 PWM interrupt, is doing everything with a modest 6% to 19% ISR overhead...
 
Last edited:
Is it possible to implement a real time clock and then use the constant 1 second interval from that clock?

If yes, please give your opinion
 
Status
Not open for further replies.

Latest threads

Back
Top