
https://www.mikroe.com/mikroc-pic
Google Drive Download link to PICKIT 3 Programmer tool : https://drive.google.com/file/d/1h8sKc69PuWKozUjKmTzmBGTYX-g9jI_9/view
int read1;
void main() {
PWM1_Init(5000); /// 5000 Hz
ADC_Init();
while(1)
{
read1 = ADC_Read(0); //// ADC is max at 1024
read1 = read1 / 4;
PWM1_Start();
PWM1_Set_Duty(read1); /// 255 is max output
}
}
int count;
int read1;
int conv1;
void main() {
ADC_Init();
Delay_ms(500);
PWM1_Init(5000);
Delay_ms(500);
while(1)
{
read1 = ADC_Read(0);
Delay_ms(100);
conv1 = read1 / 4.1;
Delay_ms(50);
PWM1_Start();
Delay_ms(20);
PWM1_Set_Duty(conv1);
Delay_ms(100);
}
}
int count = 0;
void main() {
TRISD.F2 = 1;
PORTD.F2 = 1;
Delay_ms(100);
TRISD.F3 = 1;
PORTD.F3 = 1;
Delay_ms(100);
PWM1_Init(5000); // pin 17
Delay_ms(100);
count = 0;
while(1)
{
PWM1_Start();
Delay_ms(20);
if ( PORTD.F3 == 0 )
{
PWM1_Set_Duty(count); // 0 - 255 0 = 0 V while 255 = 5V
count = count + 5;
Delay_ms(50);
}
else if ( PORTD.F2 == 0 )
{
PWM1_Set_Duty(count); // 0 - 255 0 = 0 V while 255 = 5V
count = count - 5;
Delay_ms(50);
}
}
}
int duty;
void main() {
PWM1_Init(5000); /// 5000 = 5khz
Delay_ms(100);
duty = 255;
while(1)
{
PWM1_Start();
duty = duty - 1;
Delay_ms(200);
PWM1_Set_Duty(duty);
if ( duty == 0 )
{
duty = 255;
Delay_ms(200);
}
}
}
float joy;
float per;
void main() {
PWM1_Init(5000); // 5000 = 5KHz
Delay_ms(20);
ADC_Init();
Delay_ms(20);
while(1)
{
joy = ADC_Read(0);
Delay_ms(10);
per = joy / 4; /// it brings ADC value in range of 255 i.e max duty
per = per - 128; //// centre = 512 255/ 2 = 127 per/2;
if ( per < 0 )
{
per = 0;
}
per = per * 2; /// 0 to 255 is available
PWM1_Start();
PWM1_Set_Duty(per); /// max is 255
}
}
int adcval;
void main() {
ADC_Init();
Delay_ms(100);
PWM1_Init(5000);
Delay_ms(10);
PWM2_Init(5000);
Delay_ms(10);
while(1)
{
adcval = ADC_Read(0);
Delay_ms(20);
adcval = adcval - 176;
adcval = adcval / 2.67;
/*PWM1_Start();
PWM1_Set_Duty(adcval);
Delay_ms(25);*/
PWM2_Start();
PWM2_Set_Duty(adcval);
Delay_ms(25);
}
}
Pulse Width Modulation (PWM) with PIC16F877A Microcontroller Using mikroC
Welcome to Learning Microcontrollers, your go-to resource for mastering embedded systems through real-world applications and step-by-step tutorials.
In this course, you will learn how to utilize the PWM (Pulse Width Modulation) functionality of the PIC16F877A microcontroller using the mikroC for PIC compiler. PWM is a powerful technique used for controlling the speed of motors, dimming LEDs, and managing other devices that require variable power control.
The course begins with an essential foundation in microcontroller theory, introduces you to the PIC16F877A architecture, and helps you get started with the development environment. From there, you’ll dive into practical, hands-on projects that teach you how to generate and control PWM signals for a variety of real-world applications.
What You Will Learn
Understand what PWM is and how it works at the hardware level
Explore the architecture and pin configurations of the PIC16F877A
Set up the mikroC for PIC compiler and the PICKIT 3 programming tool
Use the built-in PWM library in mikroC to generate precise PWM signals
Control the brightness of LEDs and the speed of DC motors using PWM
Interface input devices (like potentiometers, push buttons, and joysticks) to control output behavior
Integrate motor drivers such as the L298N H-Bridge for motor control
Apply PWM in practical scenarios such as throttle control and analog input conversion
Why PWM?
PWM (Pulse Width Modulation) allows digital systems like microcontrollers to simulate analog behavior. It’s essential in applications such as:
Motor control (speed regulation)
LED dimming
Servo positioning
Audio signal modulation
Power delivery control
With mikroC’s built-in PWM library, implementing PWM-based control becomes intuitive and flexible, even for beginners. Once learned, these techniques are transferable to other microcontrollers supported by mikroC (such as PIC18, PIC12, and dsPIC families).
Course Highlights
This course focuses on practical implementations using the PWM module of the PIC16F877A. You’ll write working C code, simulate logic, and apply concepts directly to hardware using tried-and-tested examples.
Course Structure
Introduction to PIC16F877A microcontroller.
Lecture 1: Why PIC16F877A ?
Lecture 2: Introduction to PIC16F877A.
Lecture 3: Counting useful pins in the PIC16F877A microcontroller.
Lecture 4: How read datasheet of PIC16F877A.
Lecture 5: Setting up a PIC16F877A for programming.
Downloading and Installing MikroC for PIC.
Lecture 6: Downloading and installing MikroC for PIC.
Dowmloading and installing PICKIT 3 software.
Lecture 7: Download and Install PICKIT 3 programming tool.
PIC16F877A based PWM generation tutorials.
Lecture 8: PWM library in MikroC for PIC introduction and demo code on PIC16F877A.
Lecture 9: Use potentiometer to control brightness of an LED with PIC16F877A.
Lecture 10: Button based brightness control of an LED using PIC16F877A mcu.
Lecture 11: Use PIC16F877A to control speed of DC motor using PWM pin.
Lecture 12: DC motor speed control using Potentiometer with PIC16F877A's PWM pins.
Lecture 13: Joystick based speed control of a DC motor using PIC16F877A's PWM pins.
Lecture 14: Throttle handle based speed control of a DC motor with PIC16F877A.
Who Should Take This Course?
This course is ideal for:
Electronics and electrical engineering students
Embedded system enthusiasts and developers
DIY hobbyists looking to control motors or LEDs in projects
Anyone seeking a practical understanding of PWM signal generation
Beginners who want to move beyond simple digital output into real-world analog simulation
Real-World Applications You’ll Be Able to Build
Variable-speed fan or motor controller
LED dimming systems
Motorized throttle systems for robotics
Analog-to-PWM conversion systems
Interactive control systems using sensors and input devices
Code Portability and Broad Device Support
The techniques taught in this course are not limited to just the PIC16F877A. Thanks to the standardized mikroC libraries, you can apply the exact same code to other PIC families (PIC12, PIC16, PIC18, etc.) by modifying only the pin declarations and clock configurations. This makes your knowledge portable across hundreds of devices.
Instructor Support
You are never alone in this learning journey. If you face any issues with hardware connections, coding errors, or project implementation, feel free to reach out for help. All questions are welcome and will be answered promptly.
Start Building PWM-Controlled Projects Today
Whether you want to control a motor, fade a light, or build responsive embedded systems, PWM is a critical skill every microcontroller developer must master.
Enroll now and start building your own dynamic PWM-based projects with the PIC16F877A microcontroller and mikroC. This course will turn theory into working systems, one pin at a time.