//****************************************************************************************** // PIC12F1822 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H25.08.24 // History : // Laungage : HI_TECH PICC C Laungage Version 9.83 //****************************************************************************************** // Description // Oscillator : Internal Oscillator 16MHz // Output : P1A(RA2), P1B(RA0) // RA5 Monitor LED //****************************************************************************************** #include __CONFIG( FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF ); __CONFIG( WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_HI & LVP_OFF ) ; // #define CCP1SEL 0 #define P1BSEL 1 // void Init_PWM(void); void main() { // ***************************** // 1.Initialized // ***************************** // (1)Define OSC (31.25KHz, IntOSC) OSCCON = 0b01111010; // 16MHz, IntOSC // (2)Define ANSELA (ALL Digital Port) ANSELA = 0x00; // (3)Define I/O port PORTA = 0b00000000; TRISA = 0b00011010; // (4)Initialized PWM Init_PWM(); // ***************************** // 2.Program main // ***************************** // (1)There is no need to do anything PORTA |= 0b00100000; // Monitor LED ON while(1); } //****************************************************************************************** // Function Name : Init_PWM // Title : // Input : // Output : //****************************************************************************************** void Init_PWM(void) { // ******************************************************* // 1.ALTERNATE PIN FUNCTION CONTROL REGISTER // CCP1SEL // 0 = CCP1/P1A function is on RA2 // 1 = CCP1/P1A function is on RA5 // P1BSEL // 0 = P1B function is on RA0 // 1 = P1B function is on RA4 // ******************************************************* APFCON &= ~(1 << CCP1SEL) & ~(1 << P1BSEL) ; // CCP1SEL:0, P1BSEL:0 // ******************************************************* // 2.Define CCP1CON: CCP1 CONTROL REGISTER // CCP1CON[7-6] P1M<1:0>: Enhanced PWM Output Configuration bits(1) // Capture mode: Unused // Compare mode: Unused // PWM mode: // If CCP1M<3:2> = 00, 01, 10: // xx = P1A assigned as Capture/Compare input; P1B, P1C, P1D assigned as port pins ŠPIC16F/LF1823 only // If CCP1M<3:2> = 11: // 00 = Single output; P1A modulated; P1B, P1C, P1D assigned as port pins // 01 = Full-Bridge output forward; P1D modulated; P1A active; P1B, P1C inactive ŠPIC16F/LF1823 only // 10 = Half-Bridge output; P1A, P1B modulated with dead-band control; P1C, P1D assigned as port pins // 11 = Full-Bridge output reverse; P1B modulated; P1C active; P1A, P1D inactive ŠPIC16F/LF1823 only // CCP1CON[5-4] DC1B<1:0>: PWM Duty Cycle Least Significant bits // Capture mode: Unused // Compare mode: Unused // PWM mode: // These bits are the two LSbs of the PWM duty cycle. The eight MSbs are found in CCPR1L. // CCP1CON[3-0] CCP1M<3:0>: ECCP1 Mode Select bits // 0000 = Capture/Compare/PWM off (resets ECCP1 module) // 0001 = Reserved // 0010 = Compare mode: toggle output on match // 0011 = Reserved // 0100 = Capture mode: every falling edge // 0101 = Capture mode: every rising edge // 0110 = Capture mode: every 4th rising edge // 0111 = Capture mode: every 16th rising edge // 1000 = Compare mode: initialize ECCP1 pin low; set output on compare match (set CCP1IF) // 1001 = Compare mode: initialize ECCP1 pin high; clear output on compare match (set CCP1IF) // 1010 = Compare mode: generate software interrupt only; ECCP1 pin reverts to I/O state // 1011 = Compare mode: Special Event Trigger (CCP1 resets TMR1, sets CCP1IF bit, and starts A/D conversion if A/D module is enabled) // PWM mode: // 1100 = PWM mode: P1A, P1C active-high; P1B, P1D active-high // 1101 = PWM mode: P1A, P1C active-high; P1B, P1D active-low // 1110 = PWM mode: P1A, P1C active-low; P1B, P1D active-high // 1111 = PWM mode: P1A, P1C active-low; P1B, P1D active-low // ******************************************************* CCP1CON = 0b10001100; // ******************************************************* // 3.T2CON: TIMER2 CONTROL REGISTER // T2CON[7] Unimplemented: Read as e0f // T2CON[6-3] TOUTPS<3:0>: Timer Output Postscaler Select bits // 0000 = 1:1 Postscaler // 0001 = 1:2 Postscaler // 0010 = 1:3 Postscaler // 0011 = 1:4 Postscaler // 0100 = 1:5 Postscaler // 0101 = 1:6 Postscaler // 0110 = 1:7 Postscaler // 0111 = 1:8 Postscaler // 1000 = 1:9 Postscaler // 1001 = 1:10 Postscaler // 1010 = 1:11 Postscaler // 1011 = 1:12 Postscaler // 1100 = 1:13 Postscaler // 1101 = 1:14 Postscaler // 1110 = 1:15 Postscaler // 1111 = 1:16 Postscaler // T2CON[2] TMR2ON: Timer2 On bit // 1 = Timer2 is on // 0 = Timer2 is off // T2CON[1-0] T2CKPS<1:0>: Timer2 Clock Prescale Select bits // 00 = Prescaler is 1 // 01 = Prescaler is 4 // 10 = Prescaler is 16 // 11 = Prescaler is 64 // ******************************************************* // T2CON = 0b00000100; T2CON = 0b00000000; // ******************************************************* // 4.PSTR1CON: PWM STEERING CONTROL REGISTER // PSTR1CON[7-5] Unimplemented: Read as e0f // PSTR1CON[4] STR1SYNC: Steering Sync bit // 1 = Output steering update occurs on next PWM period // 0 = Output steering update occurs at the beginning of the instruction cycle boundary // PSTR1CON[3] STR1D: Steering Enable bit D(2) // 1 = P1D pin has the PWM waveform with polarity control from CCP1M<1:0> // 0 = P1D pin is assigned to port pin // PSTR1CON[2] STR1C: Steering Enable bit C(2) // 1 = P1C pin has the PWM waveform with polarity control from CCP1M<1:0> // 0 = P1C pin is assigned to port pin // PSTR1CON[1] STR1B: Steering Enable bit B // 1 = P1B pin has the PWM waveform with polarity control from CCP1M<1:0> // 0 = P1B pin is assigned to port pin // PSTR1CON[0] STR1A: Steering Enable bit A // 1 = P1A pin has the PWM waveform with polarity control from CCP1M<1:0> // 0 = P1A pin is assigned to port pin // ******************************************************* PSTR1CON = 0b00010011; // ******************************************************* // 5.Set the frequency (PR2 Register) // ******************************************************* // PR2 = 200; // 20KHz // PR2 = 80; // 50KHz // PR2 = 40; //100KHz PR2 = 20; //200KHz // PR2 = 4; //800KHz // ******************************************************* // 6.Set the delay (PWM1CON : ENHANCED PWM CONTROL Register) // PWM1CON[7] P1RSEN: PWM Restart Enable bit // 1 = Upon auto-shutdown, the CCP1ASE bit clears automatically once the shutdown event goes away; the PWM restarts automatically // 0 = Upon auto-shutdown, CCP1ASE must be cleared in software to restart the PWM // PWM1CON[6-0] P1DC<6:0>: PWM Delay Count bits // P1DC1 = Number of FOSC/4 (4 * TOSC) cycles between the scheduled time when a PWM signal // should transition active and the actual time it transitions active // ******************************************************* PWM1CON = PR2 / 4 ; // ******************************************************* // 7.Set the duty cycle (CCPR1L Register) // This makes 50 percent duty cycle // ******************************************************* CCPR1L = PR2 / 2; // ******************************************************* // 8.Start // ******************************************************* T2CON |= 0b00000100; }