/* * PIC16F819-I/P * PWM lightting */ #include #include "picw.h" __CONFIG (INTCLK & WDTEN & PWRTEN & MCLREN & LVPDIS & UNPROTECT & DEBUGDIS & BORDIS & CCPRB3 ); // internal CLK setting void clk_sel(BBYTE csel){ //IRCF=110 4MHz csel &= 0x7; OSCCON |= (csel<<4); IOFS = 0; while(IOFS==0) continue; } //io format void ioini(void){ INTCON = 0x0; TRISA = 0b00110000; TRISB = 0b00000000; PORTA = 0xE; PORTB = 0b11100000; ADCON1 = 0x6; } //timer2 format void tim2ini(void){ //prescale=1MHz 1:1 TMR2IE = 0; T2CON = 0x0; } //CCP module format void ccpini(BBYTE ccp1set,BBYTE result){ //result = PWM resultion result &= 0x3; CCP1CON |= (result<<4); CCP1CON |= 0xF; CCPR1L = ccp1set; TMR2 = 0x0; PR2 = 0xFF; TMR2ON = 1; } void main(void){ static bit dutyset; volatile BWORD cnt = 0; clk_sel(0b110); ioini(); tim2ini(); ccpini(0x00,0b00); while(true){ if(CCPR1L==PR2) dutyset = false; else if(CCPR1L==0x0) dutyset = true; else ; if(cnt==0x60){ if(dutyset) CCPR1L++; else CCPR1L--; cnt = 0; } else cnt++; RB7 = RB3; } }