//****************************************************************************************** // Project : PIC16F1939 Samples // Program Name : // Programer : // Create date : Version 1.0 H24.05.20 // Replace date : // History : // Laungage : HI-TECH PLCC C LANGUAGE //****************************************************************************************** // Description // 1)OSC : 16MHz Internal OSC // //****************************************************************************************** #include // //Define configration // __CONFIG( FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF ); __CONFIG( WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_27 & DEBUG_OFF & LVP_OFF ); void Move_LED(void); void Init_Timer(void); volatile unsigned char LED_list; volatile unsigned int G_cnt; #define PI_TIM_LP 1000 void main(void) { // ***************************** // 1.Initialized Segment // ***************************** // (1)Define OSC (16MHz, IntOSC) OSCCON = 0b11111010; // (2)Define I/O port TRISA = 0b11111111; TRISB = 0b00000000; TRISC = 0b11111111; TRISD = 0b11111111; TRISE = 0b11111111; PORTB = 0x00; // (3)Initialize LED_list = 0x01; G_cnt = 0; // (4)Initialized timer Init_Timer(); // ***************************** // 2.Program main // ***************************** while(1); } //****************************************************************************************** // Function Name : Move_LED // Title : Move LED // Input : // Output : //****************************************************************************************** void Move_LED(void) { if(LED_list < 0x80) LED_list = LED_list * 2; else LED_list = 1; PORTB = LED_list; } //****************************************************************************************** // Function Name : Init_Timer // Title : wait function (1mS) // Input : // Output : //****************************************************************************************** void Init_Timer(void) { // ******************************************************* // 1.Define T1CON(TIMER1 CONTROL REGISTER) Register // 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) T1CKPS<1:0>: 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 // TMR1CS<1:0> = 1X // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input with system clock (FOSC) // TMR1CS<1:0> = 0X // This bit is ignored. Timer1 uses the internal clock when TMR1CS<1:0> = 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 = 0b00000101; // ******************************************************* // 2.Define T1GCON(TIMER1 GATE CONTROL REGISTER) Register // ******************************************************* T1GCON = 0b00000000; // ******************************************************* // 3.Define TMR1 Register // ******************************************************* // TMR1 data ( OSC:16MHz, 1:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 16MHz / 4(FOSC/4) / 1000(1000Hz)==> 0d4000(Count) ==> 0x0FA0(Count) // TMR1 = 0xFFFF - 0x0FA0 ==> 0xF05F TMR1H = 0xF0; TMR1L = 0x5F; // ******************************************************* // 4.Define Interrupts // ******************************************************* PIR1bits.TMR1IF = 0; //Timer1 Overflow Interrupt Flag bit INTCONbits.GIE = 1; //Grobal Interrupt Ebable (->1) INTCONbits.PEIE = 1; //Peripheral Interrupt Ebable (->1) PIE1bits.TMR1IE = 1; //Timer1 Overflow Interrupt Enable bit(->1) } //****************************************************************************************** // Function Name : Int_tim // Title : Interrupt function // Input : // Output : //****************************************************************************************** void interrupt Int_tim(void) { // 1.Initialized timer Init_Timer(); // 2.Check Global Counter if(G_cnt < PI_TIM_LP ){ G_cnt++; }else{ G_cnt=0; Move_LED(); } }