Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

program to generate a pwm wave of 40 KHz needed

Status
Not open for further replies.
Re: pic pwm

Check this code @ **broken link removed**. It can be a good start for you.

By the way, which pic you are using and what is your crystal frequency?

--
Amr Ali
 

Hi,
Here is a sample code in mikroC:

Code:
//Program to generate 40kHz output at RC2(CCP1) pin
//Microcontroller: Microchip PIC18452
//Language: C
//Compiler: mikroC v8.20
//Programmer: Tahmid

void main (void){
     TRISC = 0;
     PORTC = 0;
     ADCON1 = 7;
     T2CON = 0;
     TMR2 = 0;
     PWM1_Init(40000); //40kHz
     PWM1_Change_Duty(128); //50% duty cycle
// Choose Duty cycle as such:
// PWM_Change_Duty(x);
// x = ( (Duty Cycle in %) / 100) * 255
     PWM1_Start(); //Start PWM
     while (1){ //Loop forever
// Whatever else might be needed to be done while PWM is running
     }
}

mikroBASIC:
Code:
'Program to generate 40kHz output at RC2(CCP1) pin
'Microcontroller: Microchip PIC18452
'Language: BASIC
'Compiler: mikroBASIC PRO for PIC v3.20
'Programmer: Tahmid

program PWM40kHz

main:
     TRISC = 0
     PORTC = 0
     ADCON1 = 7
     PWM1_Init(40000) '40kHz
     PWM1_Set_Duty(128) '50% duty cycle
' Choose Duty cycle as such:
' PWM_Set_Duty(x)
' x = ( (Duty Cycle in %) / 100) * 255
     PWM1_Start() 'Start PWM
     while (1) 'Loop forever
' Whatever else might be needed to be done while PWM is running
     wend
end.

Hope this helps.
Tahmid.
 
Hi,
Here is a sample code in mikroC:

Code:
//Program to generate 40kHz output at RC2(CCP1) pin
//Microcontroller: Microchip PIC18452
//Language: C
//Compiler: mikroC v8.20
//Programmer: Tahmid

void main (void){
     TRISC = 0;
     PORTC = 0;
     ADCON1 = 7;
     T2CON = 0;
     TMR2 = 0;
     PWM1_Init(40000); //40kHz


     PWM1_Change_Duty(128); //50% duty cycle
// Choose Duty cycle as such:
// PWM_Change_Duty(x);
// x = ( (Duty Cycle in %) / 100) * 255
     PWM1_Start(); //Start PWM
     while (1){ //Loop forever
// Whatever else might be needed to be done while PWM is running
     }
}

mikroBASIC:
Code:
'Program to generate 40kHz output at RC2(CCP1) pin
'Microcontroller: Microchip PIC18452
'Language: BASIC
'Compiler: mikroBASIC PRO for PIC v3.20
'Programmer: Tahmid

program PWM40kHz

main:
     TRISC = 0
     PORTC = 0
     ADCON1 = 7
     PWM1_Init(40000) '40kHz
     PWM1_Set_Duty(128) '50% duty cycle
' Choose Duty cycle as such:
' PWM_Set_Duty(x)
' x = ( (Duty Cycle in %) / 100) * 255
     PWM1_Start() 'Start PWM
     while (1) 'Loop forever
' Whatever else might be needed to be done while PWM is running
     wend
end.

Hope this helps.
Tahmid.


hello tamid
please help me here
i am using PIC18F4520 , i want to generate 4 pwm signals with phase shift of 90 degrees(delay), same frequency and same duty cycle
PWM of 500K frequency and crystal frequency of 20M
i also want to be able the change the pwm duty cycle based on ADC output
i am using MikroC pro
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top