Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
/*
Lab 9: Pulse Width Modulation
Copyright @ Rajendra Bhatt, 2010.
Description: CCP module generating a PWM signal
MCU: PIC16F676
Oscillator: XT, 4.0 MHz, MCLR Enabled
*/
/*
16F676 Configuration
O = Output, I = Input
_________
Vdd | 1 14 | Vss
(I) SW1 --> RA5 | 2 13 | AN0 <-- Startup_Value (I)
(I) SW2 --> RA4 | 3 12 | AN1 <-- Mains_Sense (I)
MCLR | 4 11 | RA2 --> (O)
(O) L1 <-- RC5 | 5 10 | RC0 --> (O)
(O) L1 <-- RC4 | 6 9 | RC1 --> (O)
(O) <-- RC3 | 7 8 | RC2 --> (O)
---------
*/
sbit UP at RA5_bit;
sbit DOWN at RA4_bit;
unsigned short new_DC, current_DC;
void debounce(){
Delay_ms(300);
}
void main() {
ANSEL = 0; // All I/O pins are configured as digital
CMCON = 0x07; // Disable comparators
PORTA = 0x00;
PORTC = 0x00;
TRISA = 0b00110000; // RB0, RB1 input, RB3 (PWM1) output
PWM1_Init(5000); // PWM module initialization (5KHz)
new_DC = 0; // Initial value of variable Duty Cycle
current_DC = 0;
PWM1_Start(); // Start PWM1 module with Zero DC
PWM1_Set_Duty(current_DC);
do {
if (!UP){ // If the button connected to RB0 is pressed
debounce();
if (new_DC < 250) // Don't go above 250
new_DC = new_DC + 25 ; // increment Duty Cycle by 25
}
if (!DOWN) { // If the button connected to RB1 is pressed
debounce();
if (new_DC !=0) // Don't go below 0
new_DC= new_DC - 25 ; // Decrement Duty Cycle by 25
if (current_DC != new_DC) {
current_DC = new_DC ;
PWM1_Set_Duty(current_DC); // Change the current DC to new value
}
}
} while(1);
} // END main()
void interrupt timer_isr()
{
if(TMR0IF==1) // Timer flag has been triggered due to timer overflow -> set to overflow for every 0.1ms
{
TMR0 = 248; //Load the timer Value
TMR0IF=0; // Clear timer interrupt flag
count++; //Count increments for every 0.1ms -> count/10 will give value of count in ms
}
if (count <= (T_ON) )
RD1=1;
else
RD1=0;
if (count >= (T_TOTAL*10) )
count=0;
}
Hi mike,
The frequency of servo is 50Hz which is 20 milli sec.
So if we control the motor with resolution of 0.1ms we get (20/0.1) about 200 possible positions.