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.
/* 09:17 27 February 2013
* PIC18F4520 / MPLAB IDE 8.89 / XC8 v1.12
* E:\Electronics\PIC Projects\Electrotech\Shermona7\4520 PWM\PWM Main(1).c
*
*
*
*
*
*/
#include <xc.h>
/*------------------------------------------------------------------------*/
#pragma config CONFIG1H = 0X08;
#pragma config CONFIG2L = 0X19;
#pragma config CONFIG2H = 0X1E;
#pragma config CONFIG3H = 0X83; // MCLRE = off is 0x81
#pragma config CONFIG4L = 0X80;
#pragma config CONFIG5L = 0X0F;
#pragma config CONFIG5H = 0XC0;
#pragma config CONFIG6L = 0X0F;
#pragma config CONFIG6H = 0XE0;
#pragma config CONFIG7L = 0X0F;
#pragma config CONFIG7H = 0X40;
/*------------------------------------------------------------------------*/
#define _XTAL_FREQ (8000000) /* Not using xtal, only to make delays work as
it won't compile without this */
void pwm_init (void);
/*------------------------------------------------------------------------*/
void main (void)
{
OSCCON = 0b01110010; // Fosc = 8MHz
ADCON1 = 0xFF; // No analog inputs
pwm_init(); // Set up the pwm
/********* Start of main program loop **********/
while(1)
{}
/********** End of main program loop ************/
}
void pwm_init (void)
{
TRISCbits.TRISC2 = 1; // Disable pin output driver (CCP1)
PR2 = 0xFF; // PWM f = 1.95kHz / 0 to 100% duty cycle
CCP1CON = 0b00001100; // bit 7-6 Enhanced PWM bits (00 = single o/p)
// bit 5-4 Duty cycle LSBs (When 10 bit resolution)
// bit 3-0 ECCPx mode select bits (11xx = PWM)
CCPR1L = 0x80; // Duty cycle (8 bit)
PIR1bits.TMR2IF = 0; // Clear the interrupt flag
T2CON = 0b00000101; // Postscaler = 0, prescaler = 4, timer = on
while (!PIR1bits.TMR2IF) // Wait until the Timer overflows and the
{} // TMRxIF bit of the PIRx register is set.
TRISCbits.TRISC2 = 0; // Enable the CCPx pin output driver by
// clearing the associated TRIS bit.
}