PIC Timing question?

Discussion in 'Homework Help' started by Yoopercamp23, Apr 9, 2014.

  1. Yoopercamp23

    Thread Starter New Member

    Jan 30, 2014
    7
    0
    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?
     
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Hard loop in assembler?

    Assuming you can achieve a 10 MHz instruction speed;
    Code ( (Unknown Language)):
    1.  
    2. loop_1MHz
    3.   bsf PORTB,0
    4.   nop
    5.   nop
    6.   nop
    7.   nop
    8.   bcf PORTB,0
    9.   nop
    10.   nop
    11.   goto loop_1MHz    ; takes 2 ticks
    12.  
    Gives exactly 1MHz and exactly 50% duty.
     
  3. geko

    New Member

    Sep 18, 2008
    9
    4
    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.
     
    THE_RB likes this.
  4. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,692
    2,756
    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.
     
  5. Yoopercamp23

    Thread Starter New Member

    Jan 30, 2014
    7
    0
    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!
     
  6. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,692
    2,756
    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.
     
  7. geko

    New Member

    Sep 18, 2008
    9
    4
    The O/P wrote
    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.

    Code ( (Unknown Language)):
    1.  
    2.     list        p=12F1822      ; list directive to define processor
    3.     #include    <p12F1822.inc> ; processor specific variable definitions
    4.  
    5.     __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_ON & _IESO_OFF & _FCMEN_OFF
    6.     __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF
    7.  
    8. ; RESET VECTOR
    9. ;------------------------------------------------------------------------------
    10.  
    11.     ORG     0x0000            
    12.     goto    start            
    13.        
    14. start
    15.  
    16.     banksel   OSCCON
    17.     movlw    b'01101000'    ;0xxxxxxx 4x PLL disabled
    18.                             ;x1101xxx 4MHz Fosc    
    19.                             ;xxxxxx00 Clock by FOSC<2:0> in Config Word 1
    20.  
    21.     movwf    OSCCON
    22.  
    23.     ; with CLKOUTEN_ON set in _CONFIG1 word
    24.     ; RA4 (pin 3) now outputs a 1Mhz 50% duty cycle square wave
    25.    
    26.     goto    $            ; loop here.
    27.  
    28.     END
    29.  
     
    Last edited: Apr 9, 2014
Loading...