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.

Newbie question about 16f628a program

Status
Not open for further replies.

cyb0rg777

New Member
I have this 16f628a running a timing critical program.
It is running off the internal 4mhz oscillator.
I need a clock of 30k-50khz.
I was hoping to add this clock output to my program,but I can't mess up the timing.
I am not sure how to do this.
I know the clock output is 1mhz ,but how would i get 30k-50khz without messing up my timing.
I was hoping there was an easy way.
I am unfamiliar with TMR0.
Can it run in the background somehow or do I have to keep checking and setting a pin to get my clock output?
Thanks.
 
cyb0rg777 said:
I have this 16f628a running a timing critical program.
It is running off the internal 4mhz oscillator.
I need a clock of 30k-50khz.
I was hoping to add this clock output to my program,but I can't mess up the timing.
I am not sure how to do this.
I know the clock output is 1mhz ,but how would i get 30k-50khz without messing up my timing.
I was hoping there was an easy way.
I am unfamiliar with TMR0.
Can it run in the background somehow or do I have to keep checking and setting a pin to get my clock output?
Thanks.

You should bear in mind that tmr0 is the poorest of the 16F628 timers (only there for compatability with older devices), I would suggest using one of the other (more modern ones), such as tmr2?.

You can use tmr2 to trigger an interrupt as it times out (the time out point can be set in a register for tmr2), and the interrupt routine toggles the output pin and resets the timer.

This obviously takes some time from the main program, but you don't mention why (or how) time critical the main routine might be, nor why you want 30-40Khz?.
 
You can use tmr2 to trigger an interrupt as it times out (the time out point can be set in a register for tmr2), and
the interrupt routine toggles the output pin and resets the timer.

This is a good tip to start with,do you know any code examples ?

This obviously takes some time from the main program, but you don't mention why (or how) time critical the main routine might be, nor why you want 30-40Khz?.

The code I am using is in this thread https://www.electro-tech-online.com/threads/continuity.20577/
It is for a mouse emulator .
I'm learning more about it ,but not enough to start making changes yet.
The 30-40khz is for the rest of the circuit that uses modulated infrared light.
The frequency is just the common frequency of infrared transmitters.
Do you think the 1mhz clock would work for my infrared transmitter?
I'm trying to pack everything into one pic.
Thanks
 
You can use PWM unit of 16F628 to produce required freqency without affecting main program. You just set it up, and it will run independantly.
 
You can use the PWM to get a 30kHz output on RB3.

You set it up as follows.
Code:
                bsf     STATUS,RP0
                movlw   0x1f
                movwf   PR2
                bcf     TRISB,3
                bcf     STATUS,RP0
                movlw   15
                movwf   CCPR1L
                movlw   b'00000100'
                movwf   T2CON
                movlw    B'00001100';	enable PWM on CCP1
                movwf    CCP1CON;

HTH

Mike.
 
Genius!
Would have never figured that out by myself.
I checked it out real good to make sure it would fit but in the end it was copy and paste!
I will try to fill you in on the details if my project turns out like it should.
Thanks.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top