PIC Timing question?

Thread Starter

Yoopercamp23

Joined Jan 30, 2014
7
I have been tasked with using a PIC12F1822 to create a 1MHz TTL level output with a 50% duty cycle. I am trying different methods of achieving this output. I wrote one and used just delays to create an output, the next one I wrote I used Timer 0 and T0 interrupt flag to increment a variable and produce a 1Mhz output.
I would like to find a method to improve the output level frequency and period to a more perfect 1 MHz freq and better 50% duty cycle other than using the delays and timer 0.
What do you all think I could use to improve on the level of output?
Timer 1? , 2?
interrupts?
What are your thoughts?
 

THE_RB

Joined Feb 11, 2008
5,438
Hard loop in assembler?

Assuming you can achieve a 10 MHz instruction speed;
Rich (BB code):
loop_1MHz
  bsf PORTB,0
  nop
  nop
  nop
  nop
  bcf PORTB,0
  nop
  nop
  goto loop_1MHz    ; takes 2 ticks
Gives exactly 1MHz and exactly 50% duty.
 

geko

Joined Sep 18, 2008
9
Or just configure it to run the internal oscillator at 4Mhz and output Fosc/4 on the RA4 pin. (CLKOUTEN bit of Config Word 1)

Then you don't actually need any code.

Also worth looking at the Reference Clock Module available in the 12F1822 which outputs a divided system clock with different divider ratios and duty cycle.
 

joeyd999

Joined Jun 6, 2011
5,287
You haven't mentioned what your clock speed is, but if you are running 4Mhz or better you can use the PWM module. Better than a hard software loop as you can then do something else at the same time.
 

Thread Starter

Yoopercamp23

Joined Jan 30, 2014
7
I have set fosc to 4MHz. I wanted to know what everyone thinks would be the best way to achieve a 1MHz ttl level output with 50% duty cycle. What about using timer 1 which is 16 bit?
I might use the pwm in the 1822 though. Thanks for the input!
 

joeyd999

Joined Jun 6, 2011
5,287
I have set fosc to 4MHz. I wanted to know what everyone thinks would be the best way to achieve a 1MHz ttl level output with 50% duty cycle. What about using timer 1 which is 16 bit?
I might use the pwm in the 1822 though. Thanks for the input!
A simple timer cannot solve the problem at fosc=4Mhz. The time base will be fosc/4 or 1Mhz. Minimally you'd need at least a 2Mhz timebase, assuming you can instantaneously toggle an output, which cannot be done in code.

The 1Mhz time base can be used with the PWM because it implements an additional 2 bits (based on the 4mhz oscillator) in the comparator.
 

geko

Joined Sep 18, 2008
9
The O/P wrote
I have been tasked with using a PIC12F1822 to create a 1MHz TTL level output with a 50% duty cycle.
This is all you need to do to get a 1Mhz square wave with 50% duty cycle output on RA4

Unless we're second guessing the O/P wants to do more than they stated anything else is making hard work of it.

Rich (BB code):
    list        p=12F1822      ; list directive to define processor
    #include    <p12F1822.inc> ; processor specific variable definitions

    __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_ON & _IESO_OFF & _FCMEN_OFF
    __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF

; RESET VECTOR
;------------------------------------------------------------------------------

    ORG     0x0000            
    goto    start             
        
start

    banksel   OSCCON
    movlw    b'01101000'    ;0xxxxxxx 4x PLL disabled
                            ;x1101xxx 4MHz Fosc    
                            ;xxxxxx00 Clock by FOSC<2:0> in Config Word 1

    movwf    OSCCON

    ; with CLKOUTEN_ON set in _CONFIG1 word
    ; RA4 (pin 3) now outputs a 1Mhz 50% duty cycle square wave
    
    goto    $            ; loop here.

    END
 
Last edited:
Top