//****************************************************************************************** // PIC12F1822 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H24.05.20 // Replace date : // History : // Laungage : HI_TECH PICC C Laungage Version 9.70 //****************************************************************************************** // Description // (1)LED Format(P0〜P5) // (P1)  (P2)    // (P5) (P0) (P5) // (P2) (P1) // // P4:Select LED Block // P3:Input Push-SW // // (2)OSC : Internal OSC 16MHz //****************************************************************************************** #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_27 & LVP_OFF ) ; void Disp_LED(unsigned char, unsigned char); void Wait_Inter(void); void Timer(void); #define PI_LED_BLK_1 0 #define PI_LED_BLK_2 1 #define PI_LOOP_1 25 #define PI_LPT 10 #define PI_LPANS 5 void main() { unsigned char cnt1,cnt2; unsigned int lp1,lp2; // ******************************** // 1.Initialized Segment // ******************************** // (1)Define OSC (Internal OSC 16MHz) OSCCON = 0b11111010; // (2)Define ANSELA (All Degital Port) ANSELA = 0x00; // (3)Define I/O PORT PORTA = 0x00; TRISA = 0b11001000; // (4)Clear numeric cnt1 = 0x00; cnt2 = 0x00; // ******************************** // 2.Program main // ******************************** while(1){ // (1)Release Push-Switch // Random numbers generetor 1 (upfoword octal counter) Disp_LED((unsigned char)0, PI_LED_BLK_1); while(1){ cnt1++; if(cnt1 > 5) cnt1 = 0; if((PORTA & 0b00001000) != 0)break; } // (2)Push Push-Switch // Random numbers generetor 2 (upfoword octal counter) Disp_LED((unsigned char)0, PI_LED_BLK_2); while(1){ cnt2++; if(cnt2 > 5) cnt2 = 0; if((PORTA & 0b00001000) == 0)break; } // (3)Move LED (demo) for(lp2=0;lp2 4) cnt1 = 0; else cnt1++; if(cnt2 < 1) cnt2 = 5; else cnt2--; } // (4)Stop LED (anser) for(lp2=0;lp2 = 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 = 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:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 16MHz / 4(FOSC/4) / 1(Prescale) / 1000(1000Hz)==> 0d4,000(Count) ==> 0x0FA0(Count) // TMR1 = 0xFFFF - 0x0FA0 ==> 0xF05F TMR1H = 0xf0; TMR1L = 0x5f; // ************************************************* // 4.Timer( Check TMR1IF ) // ************************************************* PIR1 = PIR1 & 0xfe; while((PIR1 & 0x01) == 0); }