pwm generation in microbasic compiler for pic

Thread Starter

majoka

Joined Sep 10, 2009
8
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
 

Tahmid

Joined Jul 2, 2008
343
Hi,
Here's the program

Rich (BB code):
program osc_38 'I used mikroBasic PRO for PIC v2.50

main:

     PWM1_Init(38000)
     PWM1_Set_Duty(64) 'For mikrobasic 7.2 this becomes PWM1_Change_Duty(64)
     PWM1_Start
     TMR2 = 0                            ' ADDED THIS LINE

     while true
           nop
     wend

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:

Tahmid

Joined Jul 2, 2008
343
@ 4 MHz:..............
Rich (BB code):
CCP1CON = %00001100                          ' PWM setting
PR2=25                                                ' Khz=38.462  Duty Max = 105
T2CON = %00000100                             ' Timer2 = ON + 1:1 prescale
CCPR1L=52                                          ' Duty register value for 50% duty cycle
CCPR1H=0                                            ' Duty register high byte
@ 20 MHz:..........
Rich (BB code):
CCP1CON = %00001100                         ' PWM setting
PR2=131                                             ' Khz=37.879  Duty Max = 526
T2CON = %00000100                            ' Timer2 = ON + 1:1 prescale
CCPR1L=8                                           ' Duty register low byte (50% duty)
CCPR1H=1                                           ' Duty register high byte
Over here, for 50% duty cycle, CCPR1L should be 13, not 52 (4MHz), CCPR1L = 66, CCPR1H = 0 (20MHz)
 
Last edited:

Tahmid

Joined Jul 2, 2008
343
Hi Majoka,
Have you succeeded in generating the 38khz pwm clock signal using ccp module for pic16f873A in Mikro Basic?
 
Top