//****************************************************************************************** // PIC12F1822 Sample Program // Project : // Program Name : // Programer : // Create date : // History : // Laungage : XC8 C Laungage Version 2.31 //****************************************************************************************** // Description //****************************************************************************************** // #include // #pragma config FOSC = INTOSC #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config MCLRE = OFF #pragma config CP = OFF #pragma config CPD = OFF #pragma config BOREN = OFF #pragma config CLKOUTEN = OFF #pragma config IESO = OFF #pragma config FCMEN = OFF // #pragma config WRT = OFF #pragma config PLLEN = OFF #pragma config STVREN = OFF #pragma config BORV = HI #pragma config LVP = OFF // #define P_LED_G PORTAbits.RA1 #define P_LED_R PORTAbits.RA2 #define P_CHECK PORTAbits.RA4 #define P_POWER PORTAbits.RA5 void Wait_01S(unsigned int); void Wait_100mS(void) ; void Wait_1mS(unsigned int); void Wait_01mS(void) ; void nop10(void) ; void main() { unsigned int lp = 0; // ***************************** // 1.Initialized // ***************************** // (1)Define OSC (16MHz, IntOSC) OSCCON = 0b01111010; // 16MHz, IntOSC // (2)Define ANSELA Register ANSELA = 0x00; // All degital port // (3)Define I/O port PORTA = 0b00000000; TRISA = 0b11011001; P_LED_R = 1; P_LED_G = 1; // ***************************** // 2.Program main // ***************************** while(1){ P_POWER = 1; nop10(); if(P_CHECK == 0){ P_LED_R = 1; }else{ P_LED_R = 0; } // Wait_1mS(1); nop10(); P_POWER = 0; nop10(); if( P_CHECK == 0){ P_LED_G = 0; }else{ P_LED_G = 1; } // Wait_1mS(1); nop10(); } } //****************************************************************************************** // Function Name : Wait_1mS // Title : This function will wait n times of 1 mS // Input : // Output : //****************************************************************************************** void Wait_1mS(unsigned int n) { unsigned int lp; for(lp=0;lp<( n * 10);lp++){ Wait_01mS(); } } //****************************************************************************************** // Function Name : Wait_01mS // Title : This function will wait 0.1mS // Input : // Output : //****************************************************************************************** void Wait_01mS(void) { // ******************************************************* // 1.Define T1CON(TIMER1 CONTROL REGISTER) Register // T1CON(7-6) TMR1CS1,TMR1CS0: Timer1 Clock Source Select bits // 11 =Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC) // 10 =Timer1 clock source is pin or oscillator: // If T1OSCEN = 0: External clock from T1CKI pin (on the rising edge) // If T1OSCEN = 1: Crystal oscillator on T1OSI/T1OSO pins // 01 =Timer1 clock source is system clock (FOSC) // 00 =Timer1 clock source is instruction clock (FOSC/4) // T1CON(5-4) T1CKPS1,T1CKPS0: 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: LP Oscillator Enable Control bit // 1 = Dedicated Timer1 oscillator circuit enabled // 0 = Dedicated Timer1 oscillator circuit disabled // T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Control bit // TMR1CS1,TMR1CS0 = 1X // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input with system clock (FOSC) // TMR1CS1,TMR1CS0 = 0X // This bit is ignored. Timer1 uses the internal clock when TMR1CS<1:0> = 1X. // T1CON(1) Unimplemented: Read as ??0?? // T1CON(0) TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // Clears Timer1 Gate flip-flop // ******************************************************* // T1CON = 0b00110001; T1CON = 0b00000001; // ******************************************************* // 2.Define T1GCON (TIMER1 GATE CONTROL REGISTER) Register // T1GCON(7) TMR1GE: Timer1 Gate Enable bit // If TMR1ON = 0: // This bit is ignored // If TMR1ON = 1: // 1 = Timer1 counting is controlled by the Timer1 gate function // 0 = Timer1 counts regardless of Timer1 gate function // T1GCON(6) T1GPOL: Timer1 Gate Polarity bit // 1 = Timer1 gate is active-high (Timer1 counts when gate is high) // 0 = Timer1 gate is active-low (Timer1 counts when gate is low) // T1GCON(5) T1GTM: Timer1 Gate Toggle Mode bit // 1 = Timer1 Gate Toggle mode is enabled // 0 = Timer1 Gate Toggle mode is disabled and toggle flip-flop is cleared // Timer1 gate flip-flop toggles on every rising edge. // T1GCON(4) T1GSPM: Timer1 Gate Single-Pulse Mode bit // 1 = Timer1 gate Single-Pulse mode is enabled and is controlling Timer1 gate // 0 = Timer1 gate Single-Pulse mode is disabled // T1GCON(3) T1GGO/DONE: Timer1 Gate Single-Pulse Acquisition Status bit // 1 = Timer1 gate single-pulse acquisition is ready, waiting for an edge // 0 = Timer1 gate single-pulse acquisition has completed or has not been started // This bit is automatically cleared when T1GSPM is cleared. // T1GCON(2) T1GVAL: Timer1 Gate Current State bit // Indicates the current state of the Timer1 gate that could be provided to TMR1H:TMR1L. // Unaffected by Timer1 Gate Enable (TMR1GE). // T1GCON(1-0) T1GSS1,T1GSS0: Timer1 Gate Source Select bits // 00 = Timer1 Gate pin // 01 = Timer0 overflow output // 10 = Comparator 1 optionally synchronized output (SYNCC1OUT) // 11 = Comparator 2 optionally synchronized output (SYNCC2OUT) // ******************************************************* T1GCON = 0b00000000; // ******************************************************* // 3.Define TMR1 Register // ******************************************************* // TMR1 data ( OSC:16MHz, FOSC/4, 1:8 Prescale value, 0.01[S];100Hz ) // 10mS(100Hz): 16MHz / 4(FOSC/4) / 8(Prescale) / 100(100Hz)==> 0d5,000(Count) ==> 0x1388(Count) // TMR1 = 0xFFFF - 0x1388 ==> 0xEC77 // TMR1 data ( OSC:16MHz, FOSC/4, 1:8 Prescale value, 0.1[S];10Hz ) // 100mS(10Hz): 16MHz / 4(FOSC/4) / 8(Prescale) / 10(10Hz) ==> 0d50,000(Count) ==> 0xC350(Count) // TMR1 = 0xFFFF - 0xC350 ==> 0x3CAF // TMR1 data ( OSC:16MHz, FOSC/4, 1:1 Prescale value, 0.0001[S];10000Hz ) // 100mS(10Hz): 16MHz / 4(FOSC/4) / 1(Prescale) / 10000(10000Hz) ==> 0d400(Count) ==> 0x0190(Count) // TMR1 = 0xFFFF - 0x0190 ==> 0xFE6F // TMR1H = 0x3C; // TMR1L = 0xAF; TMR1H = 0xFE; TMR1L = 0xA0; // ******************************************************* // 4.Check Timer1 // PIR1: PERIPHERAL INTERRUPT REQUEST REGISTER 1 // PIR1[7] TMR1GIF: Timer1 Gate Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[6] ADIF: A/D Converter Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[5] RCIF: USART Receive Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[4] TXIF: USART Transmit Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[3] SSP1IF: Synchronous Serial Port (MSSP) Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[2] CCP1IF: CCP1 Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[1] TMR2IF: Timer2 to PR2 Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[0] TMR1IF: Timer1 Overflow Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // ******************************************************* // (1)Clear the TMR1IF Flag bit PIR1 &= 0b11111110; // (2)Check the TMR1IF Flag bit while((PIR1 &= 0b00000001) == 0 ); } //****************************************************************************************** // Function Name : nop10 // Title : // Input : // Output : //****************************************************************************************** void nop10(void) { unsigned char lp; for(lp=0;lp<10;lp++){ asm("nop"); } }