//****************************************************************************************** // PIC18F14K50 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H22.11.10 // Replace date : // History : // Laungage : MPLAB C18 //****************************************************************************************** // Description // // 1)Define I/O PORT // PORTC(0) : DCM16117A(E) // PORTC(1) : DCM16117A(RS) // PORTC(7-4) : DCM16117A(DB7-4) // // PORTC(2) : Terminal(0) // PORTB(4) : Terminal(1) // PORTB(6) : Terminal(2) // // PORTB(5) : Serial PORT(Rx) // PORTB(7) : Serial PORT(Tx) // // 2)OSC // 48MHz (12MHz Crystal * PLL4) // //****************************************************************************************** #include #include #include #include #include #include #include #include // //Define configration // #pragma config CPUDIV=NOCLKDIV //CPU System Clock Selection bit(No CPU System Clock divide) #pragma config USBDIV = OFF //USB Clock Selection bit(USB Clock comes directly from the OSC1/OSC2 oscillator block; no divide) #pragma config FOSC = HS //Oscillator Selection bits(HS oscillator) #pragma config PLLEN = ON //X PLL Enable bit(Oscillator multiplied by 4) #pragma config PCLKEN = OFF //Primary Clock Enable Bit(Primary clock is under software control) #pragma config FCMEN = OFF #pragma config IESO = OFF //Internal/External Oscillator Switchover bit(Oscillator Switchover mode disabled) #pragma config PWRTEN = OFF,BOREN = OFF,BORV = 19 #pragma config WDTEN = OFF,WDTPS = 1 #pragma config MCLRE = OFF //MCLR Pin Enable bit(RE3 input pin enabled; MCLR disabled) #pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF #pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF,WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF,EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF // //Define function // void Demo01(void); void Demo02(void); void Init_POS0(void); void Init_POS1(void); void Init_LCD(void); void LCD_Disp(unsigned char); void LCD_CLR(void); void LCD_SETH(unsigned char); void LCD_SETL(unsigned char); void BCF(unsigned int); void BSF(unsigned int); void mnop(unsigned int); void Wait_10mS(unsigned int); void Timer(void); void LCD_Disp_S(unsigned char *, unsigned char); void LCD_SCR_CLR(void); void LCD_SCR_DSP(unsigned char); void LCD_SCR_DSPS(unsigned char *, unsigned int); void Int_Timer(void); void Dec_Counter(void); void Up_counter(void); void Up_counter2(void); void Dsp_tmr(void); void isr (void); // #define LCD_E 0 #define LCD_RS 1 #define LCD_DB4 4 #define LCD_DB5 5 #define LCD_DB6 6 #define LCD_DB7 7 #define TERM_0 2 #define TERM_1 4 #define TERM_2 6 #define PI_NORMAL 0 #define PI_FAILURE 1 #define PI_POS0 0 #define PI_POS1 1 #define PI_POSN 2 // // volatile unsigned int G_int_counter=0; volatile unsigned int G_counter=0; volatile unsigned int G_flag=0; void main(void) { unsigned char tmp_char; // ***************************** // 1.Initialized Segment // ***************************** // (1)Define I/O port ANSEL = 0b00000000; //All Degital PORT ANSELH = 0b00000000; //All Degital PORT TRISA = 0b11111111; TRISB = 0b11111111; TRISC = 0b00001000; PORTA = 0x00; PORTB = 0b00; PORTC = 0x00; // (2)Initialized LCD Init_LCD(); // (3)Define Timer // Int_Timer(); // ***************************** // 2.Program main // ***************************** // (1)Send Message Demo01(); // (2)Counter while(1){ if((PORTB & 0b01010000) == 0b01010000){ // BCF(TERM_0); }else{ if((PORTB & 0b01000000) == 0x00){ Up_counter(); }else{ Up_counter2(); } T1CON = T1CON & 0b11111110; //Stop Timer 1 INTCON = INTCON & 0b00111111; //Interrupt disables Dsp_tmr(); T1CON = T1CON | 0b00000001; //Start Timer 1 INTCON = INTCON | 0b11000000; //Interrupt enables Wait_10mS(10); if(G_flag==0){ G_int_counter = 0; Int_Timer(); BSF(TERM_0); G_flag=1; } } } } //****************************************************************************************** // Function name : Demo01 // Title : // Input : // Output : //****************************************************************************************** void Demo01(void) { unsigned char msg[9]; strcpypgm2ram(msg," Start P"); LCD_Disp_S(msg,PI_POS0); strcpypgm2ram(msg,"IC18F14 "); LCD_Disp_S(msg,PI_POS1); Wait_10mS(100); // LCD_CLR(); strcpypgm2ram(msg,"PIC Time"); LCD_Disp_S(msg,PI_POS0); strcpypgm2ram(msg,"r "); LCD_Disp_S(msg,PI_POS1); Wait_10mS(100); // LCD_CLR(); strcpypgm2ram(msg,"Set Time"); LCD_Disp_S(msg,PI_POS0); strcpypgm2ram(msg,"r "); LCD_Disp_S(msg,PI_POS1); } //****************************************************************************************** // Function name : Dsp_tmr // Title : // Input : // Output : //****************************************************************************************** void Dsp_tmr(void) { unsigned int tmp_i; unsigned char tmp_line[20]; LCD_CLR(); strcpypgm2ram(tmp_line,"Timer "); LCD_Disp_S(tmp_line,PI_POS0); // tmp_i = G_counter / 100 ; sprintf(tmp_line, "%2d",tmp_i); LCD_Disp_S(tmp_line,PI_POS1); // sprintf(tmp_line, ":"); LCD_Disp_S(tmp_line,PI_POSN); // tmp_i = G_counter % 100 ; sprintf(tmp_line, "%2.2d",tmp_i); LCD_Disp_S(tmp_line,PI_POSN); } //****************************************************************************************** // Function name : LCD_Disp_S // Title : // Input : // Output : //****************************************************************************************** void LCD_Disp_S(unsigned char in_str[], unsigned char flg) { unsigned int lp; unsigned int no; if(flg == PI_POS0){ Init_POS0(); }else if(flg == PI_POS1){ Init_POS1(); } no=strlen(in_str); 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: Timer1 Oscillator Enable bit // 1 = Timer1 oscillator is enabled // 0 = Timer1 oscillator is shut off // The oscillator inverter and feedback resistor are turned off to eliminate power drain. //* T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Select bit // When TMR1CS = 1: // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input // When TMR1CS = 0: // This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. //* T1CON(1) TMR1CS: Timer1 Clock Source Select bit // 1 = External clock from the T13CKI pin (on the rising edge) // 0 = Internal clock (FOSC/4) //* T1CON(0) bit 0 TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // ******************************************************* T1CON = 0b10110001; // 16-bit Read/Write Mode, 1:8 Prescale value // ******************************************************* // 2.Define TMR1 Register // Caution(16-bit Read/Write Mode) !!!!: // (a)Writing to TMR1H does not directly affect Timer1. // Instead, the high byte of Timer1 is updated with the contents of TMR1H when a write occurs to TMR1L. // This allows all 16 bits of Timer1 to be updated at once. // (b)A read from TMR1L will load the contents of the high byte of Timer1 into the Timer1 high byte buffer. // This provides the user with the ability to accurately read all 16 bits of Timer1 without the need to // determine whether a read of the high byte, followed by a read of the low byte, has become invalid due // to a rollover or carry between reads. // ******************************************************* // TMR1 data ( OSC:12MHz, 4X PLLON, 1:8 Prescale value, 0.01[S];100Hz ) // 10mS(100Hz): 12MHz * 4(PLLON) / 4(PIC) / 8(Prescale) / 100(100Hz)==> 0d15,000(Count) ==> 0x3A98(Count) // TMR1 = 0xFFFF - 0x3A98 ==> 0xC567 // TMR1H = 0xC5; TMR1L = 0x67; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR1IF = 0; while(1){ if(PIR1bits.TMR1IF != 0 )break; } } //****************************************************************************************** // Function Name : Int_Timer // Title : // Input : // Output : //****************************************************************************************** void Int_Timer(void) { // ******************************************************* // 1.Define Interrupts // Timer1 interrupt, if enabled, is generated on overflow, which is latched in the TMR1IF // interrupt flag bit of the PIR1 register. This interrupt can be enabled or disabled // by setting or clearing the TMR1IE Interrupt Enable bit of the PIE1 register. // ******************************************************* // (1)Define INTCON(INTERRUPT CONTROL)Register // INTCON(7) GIE/GIEH(Global Interrupt Enable bit) // INTCON(6) PEIE/GIEL(Peripheral Interrupt Enable bit) // 1 = Enables, 0 = Disables INTCON = INTCON | 0b11000000; // (2)Define PIR1(PERIPHERAL INTERRUPT REQUEST)Register // PIR1(0) TMR1IF(TMR1 Overflow Interrupt Flag bit) // 1 = Enables, 0 = Disables PIR1 = PIR1 | 0b00000001; // (3)Define PIE1(PERIPHERAL INTERRUPT ENABLE)Register // PIE1(0) TMR1IE(TMR1 Overflow Interrupt Enable bit) // 1 = Enables, 0 = Disables PIE1 = PIE1 | 0b00000001; // (4)Define IPR1(PERIPHERAL INTERRUPT PRIORITY REGISTER 1) // IPR1(0) TMR1IP(TMR1 Overflow Interrupt Priority bit) // 1 = High priority, 0 = Low priority // IPR1 = IPR1 | 0b00000001; IPR1 = IPR1 | 0b00000000; // ******************************************************* // 2.Define T1CON(TIMER1 CONTROL REGISTER) Register //* T1CON(7) RD16: 16-bit Read/Write Mode Enable bit // 1 = Enables register read/write of TImer1 in one 16-bit operation // 0 = Enables register read/write of Timer1 in two 8-bit operations //* T1CON(6) T1RUN: Timer1 System Clock Status bit // 1 = Main system clock is derived from Timer1 oscillator // 0 = Main system clock is derived from another source //* 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: Timer1 Oscillator Enable bit // 1 = Timer1 oscillator is enabled // 0 = Timer1 oscillator is shut off // The oscillator inverter and feedback resistor are turned off to eliminate power drain. //* T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Select bit // When TMR1CS = 1: // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input // When TMR1CS = 0: // This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. //* T1CON(1) TMR1CS: Timer1 Clock Source Select bit // 1 = External clock from the T13CKI pin (on the rising edge) // 0 = Internal clock (FOSC/4) //* T1CON(0) bit 0 TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // ******************************************************* T1CON = 0b10110001; // 16-bit Read/Write Mode, 1:8 Prescale value // ******************************************************* // 3.Define TMR1 Register // Caution(16-bit Read/Write Mode) !!!!: // (a)Writing to TMR1H does not directly affect Timer1. // Instead, the high byte of Timer1 is updated with the contents of TMR1H when a write occurs to TMR1L. // This allows all 16 bits of Timer1 to be updated at once. // (b)A read from TMR1L will load the contents of the high byte of Timer1 into the Timer1 high byte buffer. // This provides the user with the ability to accurately read all 16 bits of Timer1 without the need to // determine whether a read of the high byte, followed by a read of the low byte, has become invalid due // to a rollover or carry between reads. // ******************************************************* // TMR1 data ( OSC:12MHz, 4X PLLON, 1:8 Prescale value, 0.01[S];100Hz ) // 10mS(100Hz): 12MHz * 4(PLLON) / 4(PIC) / 8(Prescale) / 100(100Hz)==> 0d15,000(Count) ==> 0x3A98(Count) // TMR1 = 0xFFFF - 0x3A98 ==> 0xC567 // TMR1H = 0xC5; TMR1L = 0x67; } //****************************************************************************************** // Function Name : Dec_counter // Title : // Input : // Output : //****************************************************************************************** void Dec_counter(void) { unsigned int tmp_cnt; tmp_cnt = G_counter % 100 ; if(tmp_cnt == 0){ G_counter = G_counter - 100 + 59; }else{ G_counter = G_counter - 1 ; } } //****************************************************************************************** // Function Name : Up_counter // Title : // Input : // Output : //****************************************************************************************** void Up_counter(void) { unsigned int tmp_cnt; tmp_cnt = G_counter % 100 ; if(tmp_cnt > 58){ G_counter = G_counter + 100 - tmp_cnt; }else{ G_counter = G_counter + 1 ; } } //****************************************************************************************** // Function Name : Up_counter2 // Title : // Input : // Output : //****************************************************************************************** void Up_counter2(void) { G_counter = G_counter + 100 ; } //****************************************************************************************** // Function Name : isr // Title : interrupt // Input : // Output : //****************************************************************************************** #pragma code Int_addr=0x8 void Int_function (void) { _asm GOTO isr _endasm } #pragma code #pragma interruptlow isr void isr (void) { unsigned char tmp_line[20]; // ******************* // 1. 1 Second // ******************* if(G_int_counter < 100){ TMR1H = 0xC5; TMR1L = 0x67; PIR1bits.TMR1IF = 0; G_int_counter++; }else if(G_counter < 2){ // (1)Stop Timer 1 T1CON = T1CON & 0b11111110; //Stop Timer 1 INTCON = INTCON & 0b00111111; //Interrupt disables // (2)Put Data BCF(TERM_0); LCD_CLR(); strcpypgm2ram(tmp_line,"Timer "); LCD_Disp_S(tmp_line,PI_POS0); strcpypgm2ram(tmp_line,"End "); LCD_Disp_S(tmp_line,PI_POS1); // (3)Start Timer 1 // T1CON = T1CON | 0b00000001; //Start Timer 1 // TMR1H = 0xC5; // TMR1L = 0x67; // PIR1bits.TMR1IF = 0; //Clear TMR1IF G_flag = 0; }else{ // ******************* // 2. Else // ******************* // (1)Set timer TMR1H = 0xC5; TMR1L = 0x67; // PIR1bits.TMR1IF = 0; G_int_counter = 0; // (2)Set Counter Dec_counter(); // (3)Display Dsp_tmr(); } }