Created
November 13, 2014 12:56
-
-
Save sapher/e4dfc6c1d4725c7bae65 to your computer and use it in GitHub Desktop.
PIC12F615 PWM example on AF pin GP5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "xc.h" | |
#include "config.h" | |
#define _XTAL_FREQ 8000000 | |
/* Variables */ | |
unsigned int i; | |
void setDuty(unsigned int duty) { | |
CCPR1L = duty >> 2; | |
CCP1CON &= 0xCF; | |
CCP1CON |= (0x30 & (duty << 4)); | |
} | |
void main(void) { | |
/* CONFIGURE GPIO */ | |
ANSEL = 0; // disable analog | |
TRISA = 0; // disable pin | |
/* CONFIGURE PWM on GPIO5 */ | |
// Pin configuration | |
GP5 = 0; // GP5 put low | |
TRISIO5 = 0; // GP5 as output | |
P1ASEL = 1; // GP5 as AF > P1A (PWM) | |
// Configure PWM | |
T2CONbits.T2CKPS = 0b00; // set prescaler as 1:1 (default) | |
CCP1CONbits.P1M = 0; // single / P1A modulated | |
CCP1CONbits.CCP1M = 0b1100; // set PWM mode | |
// Timer configuration | |
TMR2 = 0; // reset TMR2 count | |
TMR2ON = 1; // enable TMR2, start PWM | |
while(1) { | |
for(i = 0; i < 255; i++) { | |
setDuty(i); | |
__delay_ms(10); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment