//****************************************************************************************** // PIC12F1822 Sample Program // Project : // Program Name : // Programer : // Create date : // History : // Laungage : HI_TECH PICC C Laungage Version 9.83 //****************************************************************************************** // Description //****************************************************************************************** #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 ) ; // void Wait_01S(unsigned int); void Wait_100mS(void); void main() { // ***************************** // 1.Initialized // ***************************** // (1)Define OSC (16MHz, IntOSC) OSCCON = 0b01111010; // 16MHz, IntOSC // (2)Define ANSELA (ALL Digital Port) ANSELA = 0x00; // (3)Define I/O port PORTA = 0b00000000; TRISA = 0b00111000; // ***************************** // 2.Program main // ***************************** while(1){ PORTA = 0b00000000; Wait_01S((unsigned int)10); PORTA = 0b00000111; Wait_01S((unsigned int)10); }; } //****************************************************************************************** // Function Name : Wait_01S // Title : This function will wait n times of 0.1 seconds // Input : // Output : //****************************************************************************************** void Wait_01S(unsigned int n) { unsigned int lp; for(lp=0;lp = 1X. // T1CON(1) Unimplemented: Read as e0f // T1CON(0) TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // Clears Timer1 Gate flip-flop // ******************************************************* T1CON = 0b00110001; // ******************************************************* // 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 TMR1H = 0x3C; TMR1L = 0xAF; // ******************************************************* // 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 ); }