//****************************************************************************************** // PIC18F14K50 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H21.12.20 // History : // Laungage : MPLAB C18 //****************************************************************************************** // (1)Description // //****************************************************************************************** #include #include #include #include #include #include #include #include // //Define configration // #pragma config CPUDIV=NOCLKDIV //CPU System Clock Selection bit(No CPU System Clock divide) #pragma config USBDIV = OFF //USB Clock Selection bit(USB Clock comes directly from the OSC1/OSC2 oscillator block; no divide) #pragma config FOSC = HS //Oscillator Selection bits(HS oscillator) #pragma config PLLEN = ON //X PLL Enable bit(Oscillator multiplied by 4) #pragma config PCLKEN = OFF //Primary Clock Enable Bit(Primary clock is under software control) #pragma config FCMEN = OFF #pragma config IESO = OFF //Internal/External Oscillator Switchover bit(Oscillator Switchover mode disabled) #pragma config PWRTEN = OFF,BOREN = OFF,BORV = 19 #pragma config WDTEN = OFF,WDTPS = 1 #pragma config MCLRE = OFF //MCLR Pin Enable bit(RE3 input pin enabled; MCLR disabled) #pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF #pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF,WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF,EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF // //Define function // void Timer(void); void Wait(unsigned int); void InitCCP(void); //****************************************************************************************** // Program : // Title : // Input : // Output : //****************************************************************************************** void main(void) { unsigned int lp; // ***************************** // 1.Initialized Segment // ***************************** // (1)Define I/O Port TRISA = 0xFF; TRISB = 0xFF; TRISC = 0b11011111; // (2)Initialize CCP Module InitCCP(); // ***************************** // 2.Program main // ***************************** while(1){ for(lp=0;lp<0xFF;lp++){ CCPR1L = (unsigned char)lp; Timer(); } }; } //****************************************************************************************** // Function Name : InitCCP // Title : // Input : // Output : //****************************************************************************************** void InitCCP(void) { // ******************************************************* // 1.Define CCP1CON(ENHANCED CAPTURE/COMPARE/PWM CONTROL) Register // CCP1CON(7-6) P1M<1:0>: Enhanced PWM Output Configuration bits // If CCP1M<3:2> = 00, 01, 10: // xx = P1A assigned as Capture/Compare input/output; P1B, P1C, P1D assigned as port pins // If CCP1M<3:2> = 11: // 00 = Single output: P1A, P1B, P1C and P1D controlled by steering (See Section 14.4.7 gPulse Steering Modeh). // 01 = Full-bridge output forward: P1D modulated; P1A active; P1B, P1C inactive // 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 // CCP1CON(5-4) DC1B<1:0>: PWM Duty Cycle bit 1 and bit 0 // Capture mode: Unused. // Compare mode: Unused. // PWM mode: // These bits are the two LSbs of the 10-bit PWM duty cycle. The eight MSbs of the duty cycle are found in CCPR1L. // CCP1CON(3-0) CCP1M<3:0>: Enhanced CCP Mode Select bits // 0000 = Capture/Compare/PWM off (resets ECCP 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 CCP1 pin low, set output on compare match (set CCP1IF) // 1001 = Compare mode, initialize CCP1 pin high, clear output on compare match (set CCP1IF) // 1010 = Compare mode, generate software interrupt only, CCP1 pin reverts to I/O state // 1011 = Compare mode, trigger special event (ECCP resets TMR1 or TMR3, start A/D conversion, sets CC1IF bit) // 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 = 0b00001100; // ******************************************************* // 2.Define T2CON(TIMER2 CONTROL) Register // T2CON(7) Unimplemented: Read as e0f // T2CON(6-3) T2OUTPS<3:0>: Timer2 Output Postscale Select bits // 0000 = 1:1 Postscale // 0001 = 1:2 Postscale // E // E // E // 1111 = 1:16 Postscale // 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 // 1x = Prescaler is 16 // ******************************************************* T2CON = 0b00000110; // ******************************************************* // 3.Defalt value // ******************************************************* PR2 = 0xff; //48000000Hz(FOSC)/4/16(Prescaler)/0xff==>2941[Hz] // CCPR1L = 0x7F; //About 50% CCPR1L = 0x0; } //****************************************************************************************** // Function Name : Wait // Title : wait function // Input : timer(10mS * timer) // Output : //****************************************************************************************** void Wait(unsigned int timer) { unsigned int lp; for(lp=0;lp: Timer1 Input Clock Prescale Select bits // 11 = 1:8 Prescale value // 10 = 1:4 Prescale value // 01 = 1:2 Prescale value // 00 = 1:1 Prescale value //* T1CON(3) T1OSCEN: Timer1 Oscillator Enable bit // 1 = Timer1 oscillator is enabled // 0 = Timer1 oscillator is shut off // The oscillator inverter and feedback resistor are turned off to eliminate power drain. //* T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Select bit // When TMR1CS = 1: // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input // When TMR1CS = 0: // This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. //* T1CON(1) TMR1CS: Timer1 Clock Source Select bit // 1 = External clock from the T13CKI pin (on the rising edge) // 0 = Internal clock (FOSC/4) //* T1CON(0) bit 0 TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // ******************************************************* T1CON = 0b10110001; // 16-bit Read/Write Mode, 1:8 Prescale value // ******************************************************* // 2.Define TMR1 Register // Caution(16-bit Read/Write Mode) !!!!: // (a)Writing to TMR1H does not directly affect Timer1. // Instead, the high byte of Timer1 is updated with the contents of TMR1H when a write occurs to TMR1L. // This allows all 16 bits of Timer1 to be updated at once. // (b)A read from TMR1L will load the contents of the high byte of Timer1 into the Timer1 high byte buffer. // This provides the user with the ability to accurately read all 16 bits of Timer1 without the need to // determine whether a read of the high byte, followed by a read of the low byte, has become invalid due // to a rollover or carry between reads. // ******************************************************* // TMR1 data ( OSC:12MHz, 4X PLLON, 1:8 Prescale value, 0.01[S];100Hz ) // 10mS(100Hz): 12MHz * 4(PLLON) / 4(PIC) / 8(Prescale) / 100(100Hz)==> 0d15,000(Count) ==> 0x3A98(Count) // TMR1 = 0xFFFF - 0x3A98 ==> 0xC567 // TMR1H = 0xC5; TMR1L = 0x67; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR1IF = 0; while(1){ if(PIR1bits.TMR1IF != 0 )break; } }