//PWM1 //Program to test PWM feature //Written for PIC12F683 and CC5X compiler //Personal and non-commercial use encouraged //Copyright 2007 - David Theunissen - See www.flyelectric.ukgateway.net //PREPROCESSOR section ======================================================== #include "12F683.H" //PIC chip file #pragma config = 0b.11.1100.0100//Core device configuration //VARIABLES section =========================================================== #define LED GPIO.0 //FUNCTIONS section =========================================================== void main() { //Set port levels to known state GPIO = 0; //Configure ports as Input (1) or Output (0) TRISIO.0 = 0; //Sets GP0 to be Output for LED TRISIO.2 = 0; //Sets GP2 (CCP1) to be Output for PWM //Set ADC ports ANSEL.0 = 0; //Set ADC port AN0 to Digital ANSEL.2 = 0; //Set ADC port AN2 to Digital //Set comparitor CMCON0 = 0b.111; //Switches all 3 comparator ports off //Set clock frequency (011 = 500khz) OSCCON.6 = 0; OSCCON.5 = 1; OSCCON.4 = 1; //Set up Timer 2 (used by PWM) T2CON.2 = 1; //Enables Timer2 T2CON.1 = 1; //Sets Timer2 prescaler to 16 T2CON.0 = 0; //Part of above //Set PWM period / frequency PR2 = 156; //20ms with 500khz clock //Set PWM duty cycle ('on' time) - the 3 registers concatenate into one 10bit number //32 = 0b100000 = ~1ms high with 500khz clock CCPR1L = 0b1000; //High 8bits CCP1CON.5 = 0; //2nd lowest bit CCP1CON.4 = 0; //Lowest bit //Enable PWM CCP1CON.3 = 1; //CCP1 mode select - enables PWM mode CCP1CON.2 = 1; //Part of above LED = 1; //So you know circuit is powered up while(1); //Keeps checking the while condition but does nothing else //(PIC needs to keep 'busy' for PWM to keep running too) } //end main