pwm generation in microbasic compiler for pic

Discussion in 'Embedded Systems and Microcontrollers' started by majoka, Sep 11, 2009.

  1. majoka

    Thread Starter New Member

    Sep 10, 2009
    8
    0
    i need to generate a 38khz pwm clock signal using ccp module for pic16f873A.i am using microbasic compiler for pic but i can not succeeded to do it .i want that continuously 38khz signal generated by RC2 pin by hardware timer of pic and i dnt care that in programming
    i try it with this code

    TRISC.2 = 0 ' CCP1 (PortC.2 = Output)
    Pwm_Init(38000) ' Set the PWM frequency to 38000 hz
    Pwm_Change_Duty(63) ' 25% duty cycle
    Pwm_Start ' Start outputing the pulse stream


    but its not working
    anyone can help me
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
  3. majoka

    Thread Starter New Member

    Sep 10, 2009
    8
    0
    i am using 4mhz crystal but may b in future i will use 20mhz
     
  4. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    Here's the program

    Code ( (Unknown Language)):
    1. program osc_38 'I used mikroBasic PRO for PIC v2.50
    2.  
    3. main:
    4.  
    5.      PWM1_Init(38000)
    6.      PWM1_Set_Duty(64) 'For mikrobasic 7.2 this becomes PWM1_Change_Duty(64)
    7.      PWM1_Start
    8.      TMR2 = 0                            ' ADDED THIS LINE
    9.  
    10.      while true
    11.            nop
    12.      wend
    13.  
    14. end.
    Unless you write 0 to TMR2, there becomes a problem. So, hope this helps.
    Same code for 4MHz and 20MHz, only change the clock in mikroBasic project.
     
    Last edited: Sep 17, 2009
  5. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Over here, for 50% duty cycle, CCPR1L should be 13, not 52 (4MHz), CCPR1L = 66, CCPR1H = 0 (20MHz)
     
    Last edited: Sep 17, 2009
  6. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi Majoka,
    Have you succeeded in generating the 38khz pwm clock signal using ccp module for pic16f873A in Mikro Basic?
     
Loading...