Skip to main content

Example of how to generate PWM in mikroC using the CCP module


//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
     }
}

In mikroC, you set the duty cycle by using the function PWM1_Change_duty(x). The value you put within the parentheses (x) has to be between 0 and 255. 0 means 0 duty cycle, 255 means 100% duty cycle, so 128 means 50%.

You get PWM output at RC2 (pin 17).

Comments

Popular posts from this blog

Mantis 9 1 CNC Mill

The Mantis 9.1 design is a radical departure from version 8 and earlier. Most notably, the part count has been almost halved! The current design has 13 parts, all of which can be made with a handsaw and a drill press. Also, I’ve traded away my alignment free exactly-constrained design for extra stiffness. Several unsuccessful attempts to eradicate the last of the slop in the Z axis on version 8 lead me back to the world of over-constrained parallel rods. My previous attempts at an over-constrained design (versions 1-5) all failed because I was unable to make the rods sufficiently parallel to avoid jamming. What to do? [ ]

Electronic Extended Play Circuit Diagram

This is a Electronic Extended Play Circuit Diagram. A single op amp-one of four contained in the popular LM324-is operating in a variable pulse width, free-running square wave oscillator circuit, with its timed output driving two transistors that control the on/ off cycle of the tape-drive motor. The Oscillator` s positive feedback path holds the secret to the successful operation of the variable on/ off timing signal.   Electronic Extended Play Circuit Diagram The two diodes and pulse width potentiometer R8 allows the setting of the on and off time, without affecting the oscillator`s operating frequency. One diode allows only the discharge current to flow through it and the section of R8 that it`s connected to. The other diode, and its portion of R8, sets the charge time for the timing capacitor, C3. Since the recorder`s speed is controlled by the precise off/on timing of the oscillator, a simple voltage-regulator circuit (Ql, R3, and D4) is included.  Connecting the spe...

25 Watt Audio Amplifier Circuits Diagram

25 Watt Audio Amplifier Circuits Diagram Parts: R1 ,R4 _________47K   1/4W Resistors R2____________ 4K7   1 /4W Resistor R3____________ 1K5   1 /4W Resistor R5__________390R    1/4W Resistor R6__________470R    1/4W Resistor R7___________33K    1/4W Resistor R8__________150K    1/4W Resistor R9___________15K    1/4W Resistor R10__________27R    1/4W Resistor R11_________500R    1/2W Trimmer Cermet R12 ,R13,R16 __10R    1/4W Resistors R14 ,R15 _____220R    1/4W Resistors R17___________8R2     2W Resistor R18____________R22    4W Resistor ( wirewound )   C1___________ 470nF   63V Polyester Capacitor C2___________ 330pF   63V Polystyrene Capacitor C3 ,C5 ________470µF   63V Electrolytic Capacitors C4 ,C6,C8,C11 _100nF   63V Polyester Capacitors C7___________100µ F   25V Electrolytic Capacitor C9____________ 10pF   63V Polys...