//****************************************************************************************** // 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(unsigned char *); void Wait(unsigned int); void Timer(void); void main(void) { unsigned char LED_list; // ***************************** // 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; // ***************************** // 2.Program main // ***************************** while(1){ PORTB = LED_list; Move_LED(&LED_list); Wait((unsigned int)1000); } } //****************************************************************************************** // Function Name : Move_LED // Title : Move LED // Input : // Output : //****************************************************************************************** void Move_LED(unsigned char *led_lst) { if(*led_lst < 0x80) *led_lst = *led_lst * 2; else *led_lst = 1; } //****************************************************************************************** // Function Name : Wait // Title : Wait // Input : number n ( 1[mS] * n) // Output : //****************************************************************************************** void Wait(unsigned int no) { 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: 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; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR1IF = 0; while(1){ if(PIR1bits.TMR1IF != 0 )break; } }