You are on page 1of 2

// ***************************************************************************

// File Name : pwm.c


// Version : 1.0
// Description : Pulse Width Modulation (PWM)
// Single Output, Steering Mode
// Author(s) : RWB
// Target(s) : PICJazz 16F690 Board
// Compiler : HITECT PICC-Lite Version 9.60PL1
// IDE : Microchip MPLAB IDE v8.00
// Programmer : PICKit2
// Last Updated : 03 Jan 2009
// ***************************************************************************
#include <pic.h>
/* PIC Configuration Bit:
** INTIO - Using Internal RC No Clock
** WDTDIS - Wacthdog Timer Disable
** PWRTEN - Power Up Timer Enable
** MCLREN - Master Clear Enable
** UNPROTECT - Code Un-Protect
** UNPROTECT - Data EEPROM Read Un-Protect
** BORDIS - Borwn Out Detect Disable
** IESODIS - Internal External Switch Over Mode Disable
** FCMDIS - Monitor Clock Fail Safe Disable
*/
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS);
// Using Internal Clock of 8 MHz
#define FOSC 8000000L
// Delay Function
#define _delay_us(x) { unsigned char us; \
us = (x)/(12000000/FOSC)|1; \
while(--us != 0) continue; }
void _delay_ms(unsigned int ms)
{
unsigned char i;
if (ms == 0) return;
do {
i = 4;
do {
_delay_us(164);
} while(--i);
} while(--ms);
}
void main(void)
{
unsigned int ipwm;
unsigned char state;
OSCCON=0x70; // Select 8 MHz internal clock
TRISC = 0x00; // Set All on PORTC as Output
ANSEL = 0x00; // Set PORT AN0 to AN7 digital I/O
ANSELH = 0x00; // Set PORT AN8 to AN11 as Digital I/O
PORTC = 0x00; // Turn Off all PORTC
/* Init PWM for Single Output */
CCP1CON=0b00001100; // Single PWM mode; P1A, P1C active-high; P1B, P1D active-
high
CCPR1L=0; // Start with zero Duty Cycle
T2CON=0b00000101; // Postscale: 1:1, Timer2=On, Prescale = 1:4
PR2=0x65; // Frequency: 4.90 kHz
TMR2=0; // Start with zero Counter
PSTRCON=0b00000100; // Enable Pulse Steering on P1C (RC3)
state=0; // Start with state 1
for(;;) {
ipwm=0;
while (ipwm < 255) {
CCPR1L=++ipwm;
_delay_ms(5); // Delay 5 millisecond
}

ipwm=0xff;
while (ipwm > 0) {
CCPR1L=--ipwm;
_delay_ms(5); // Delay 5 millisecond
}

_delay_ms(100); // Delay 100 millisecond


if (state == 0) {
state=1;
PSTRCON=0b00001000; // Enable Pulse Steering on P1D (RC2)
} else if (state == 1) {
state=2;
PSTRCON=0b00001100; // Enable Pulse Steering on P1C and P1D (RC3 and RC2)
} else {
state=0;
PSTRCON=0b00000100; // Enable Pulse Steering on P1C (RC3)
}
}
}

You might also like