//****************************************************************************************** // PIC18F14K50 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H24.02.08 // Replace date : // History : // Laungage : MPLAB C18 //****************************************************************************************** // Description // (1)OSC 48MHz (12MHz Crystal * PLL4) // (2)I/O PORT // PORTC(0) : SD1602HU0B-XA(E) // PORTC(1) : SD1602HU0B-XA(RS) // PORTC(7-4) : SD1602HU0B-XA(DB7-4) // // PORTB(4) : Charge Capacitor // PORTB(6) : Check mode // PORTC(2)AN6 : Check voltage // //****************************************************************************************** #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 // #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_SUP_C 4 #define TERM_RANGE 6 #define TERM_INP_C 2 #define PI_NORMAL 0 #define PI_FAILURE 1 #define PI_OVER 2 #define PI_RANGE_100 0 #define PI_RANGE_100K 1 // #define PI_ENBL_N 10 //#define PI_COUNT_MAX 655350 //測定値の上限を設定する #define PI_COUNT_MAX 100000 //測定値の上限を設定する // #define PI_DSPLINE_1 0x00 #define PI_DSPLINE_2 0x40 #define PI_POS00 0x00 #define PI_POS01 0x08 #define PI_POS10 0x40 #define PI_POS11 0x48 // // //Define function // void Init_LCD(void); void Demo01(void); void LCD_Disp(unsigned char); void LCD_CLR(void); void LCD_SETH(unsigned char); void LCD_SETL(unsigned char); void LCD_set_addr(unsigned char); void BCF(unsigned int); void BSF(unsigned int); void TERM_BCF_B(unsigned int); void TERM_BSF_B(unsigned int); void mnop(unsigned int); void Int_Timer1(void); void Timer0(unsigned char); void Timer3(void); void Wait_1mS(unsigned int); void Init_ADC(void); void IntFVR(void); void GetADC(unsigned int *); void LCD_Disp_S(unsigned char *, unsigned char); void Dsp_data(unsigned int); void check_range(void); void anal_cap(long *); void disp_data(long); void isr (void); volatile long G_int_counter=0; // -2,147,483,648 <= LONG(32bit) <= 2,147,483,647 volatile unsigned char G_count_over=PI_NORMAL; volatile unsigned char G_res_status=PI_NORMAL; volatile unsigned char G_range=PI_RANGE_100; void main(void) { long l_tmp1; // -2,147,483,648 <= LONG(32bit) <= 2,147,483,647 //0-65535 double d_tmp1; long cap_data; // ************************************* // 1.Initialize Segment // ************************************* // (1)Define I/O port PORTA = 0b00000000; PORTB = 0b00000000; PORTC = 0b00000000; TRISA = 0b11111111; TRISB = 0b11101111; TRISC = 0b00001100; // (2)Select A/D Converter ANSEL = 0b00000000; //ALL Degital PORT ANSELH = 0b00000000; //ALL Degital PORT // (3)Initialized LCD Init_LCD(); // (4)Initialized Fixed Voltage Reference IntFVR(); // (5)Initialized A/D Converter Init_ADC(); // (6)Check range check_range(); // (7)Initialized Timer (interrupt) Int_Timer1(); // (8)Start message Demo01(); // ************************************* // 2.Program Main // ************************************* while(1){ // (1)Analized anal_cap(&cap_data); // (2)Output disp_data(cap_data); } } //****************************************************************************************** // Function name : Demo01 // Title : // Input : // Output : //****************************************************************************************** void Demo01(void) { unsigned char msg[17]; // ******************** // 1.Display message // ******************** if(G_range==PI_RANGE_100){ strcpypgm2ram(msg,"Start 18F14K50 "); LCD_Disp_S(msg, PI_DSPLINE_1); strcpypgm2ram(msg,"Selected 100ohm"); LCD_Disp_S(msg, PI_DSPLINE_2); }else{ strcpypgm2ram(msg,"Start 18F14K50 "); LCD_Disp_S(msg, PI_DSPLINE_1); strcpypgm2ram(msg,"Selected 100Koh"); LCD_Disp_S(msg, PI_DSPLINE_2); } // ******************** // 2.Wait // ******************** Wait_1mS(500); } //****************************************************************************************** // Function Name : check_range // Title : // Input : // Output : //****************************************************************************************** void check_range(void) { // 1.Charge capacitor TERM_BCF_B(TERM_SUP_C); // 2.Wait Wait_1mS(10); // 3.Check mode if((PORTB & 0b01000000)==0){ G_range=PI_RANGE_100K; }else{ G_range=PI_RANGE_100; } // 4.Release TERM_BSF_B(TERM_SUP_C); } //****************************************************************************************** // Function Name : anal_cap // Title : Analized capacitor // Input : // Output : //****************************************************************************************** void anal_cap(long *ret_data) { unsigned int adc_data; // 1.Charge capacitor TERM_BCF_B(TERM_SUP_C); // 2.Wait Wait_1mS(2000); // 3.Clear counter G_res_status=PI_NORMAL; G_int_counter=0; G_count_over=PI_NORMAL; // 4.Release TERM_BSF_B(TERM_SUP_C); // 5.Measure while(1){ GetADC(&adc_data); if(adc_data == 0){ *ret_data=(unsigned int)G_int_counter; break; } if(G_count_over!=PI_NORMAL){ *ret_data = 0; G_res_status=PI_OVER; break; } } } //****************************************************************************************** // Function Name : disp_data // Title : Display data // Input : // Output : //****************************************************************************************** void disp_data(long ret_data) { unsigned int tmp_i; float tmp_f; long tmp_l; unsigned char msg[17]; // 1.Clear display LCD_CLR(); // 2.Output data // (1)10,000uF range // if(G_range==PI_RANGE_100){ strcpypgm2ram(msg,"10,000uF range"); LCD_Disp_S(msg, PI_DSPLINE_1); // if(G_res_status!=PI_NORMAL){ strcpypgm2ram(msg,"********"); }else{ tmp_i=(unsigned int)(ret_data / 10 ) ; sprintf(msg, "%6d",tmp_i); msg[6]='.'; tmp_i = ret_data - (( ret_data / 10 ) * 10 ); sprintf(&msg[7], "%1d",tmp_i); } LCD_Disp_S(msg, PI_POS10); strcpypgm2ram(msg," [uF] "); LCD_Disp_S(msg, PI_POS11); // (2)10uF range // }else{ strcpypgm2ram(msg," 10uF range "); LCD_Disp_S(msg, PI_DSPLINE_1); if(G_res_status!=PI_NORMAL){ strcpypgm2ram(msg,"********"); }else{ tmp_i=(unsigned int)(ret_data / 10000 ) ; sprintf(msg, "%3d",tmp_i); msg[3]='.'; tmp_i = ret_data - (( ret_data / 10000 ) * 10000 ); sprintf(&msg[4], "%4.4d",tmp_i); } LCD_Disp_S(msg, PI_POS10); strcpypgm2ram(msg," [uF] "); LCD_Disp_S(msg, PI_POS11); } } //****************************************************************************************** // Function Name : Init_ADC // Title : Initialize A/D converter // Input : // Output : // Description : Analog Channel Select (RC2:AN6) //****************************************************************************************** void Init_ADC(void) { // ******************************************************* // 0.Define ANSEL(ANALOG SELECT REGISTER) and ANSELH(ANALOG SELECT REGISTER 2) // ANSEL[7] ANS7: RC3 Analog Select Control bit // 1 = Digital input buffer of RC3 is disabled // 0 = Digital input buffer of RC3 is enabled // ANSEL[6] ANS6: RC2 Analog Select Control bit // 1 = Digital input buffer of RC2 is disabled // 0 = Digital input buffer of RC2 is enabled // ANSEL[5] ANS5: RC1 Analog Select Control bit // 1 = Digital input buffer of RC1 is disabled // 0 = Digital input buffer of RC1 is enabled // ANSEL[4] ANS4: RC0 Analog Select Control bit // 1 = Digital input buffer of RC0 is disabled // 0 = Digital input buffer of RC0 is enabled // ANSEL[3] ANS3: RA4 Analog Select Control bit // 1 = Digital input buffer of RA4 is disabled // 0 = Digital input buffer of RA4 is enabled // ANSEL[2-0] Unimplemented: Read as ‘0’ // // ANSELH[7-4] Unimplemented: Read as ‘0’ // ANSELH[3] ANS11: RB5 Analog Select Control bit // 1 = Digital input buffer of RB5 is disabled // 0 = Digital input buffer of RB5 is enabled // ANSELH[2] ANS10: RB4 Analog Select Control bit // 1 = Digital input buffer of RB4 is disabled // 0 = Digital input buffer of RB4 is enabled // ANSELH[1] ANS9: RC7 Analog Select Control bit // 1 = Digital input buffer of RC7 is disabled // 0 = Digital input buffer of RC7 is enabled // ANSELH[0] ANS8: RC6 Analog Select Control bit // 1 = Digital input buffer of RC6 is disabled // 0 = Digital input buffer of RC6 is enabled // ******************************************************* ANSEL = 0b01000000; //Select ANS6 ANSELH = 0b00000000; // ******************************************************* // 1.Define ADCON0(A/D CONTROL REGISTER 0) Register // ADCON0(7-6) Unimplemented: Read as ‘0’ // ADCON0(5-2) CHS<3:0>: Analog Channel Select bits // 0000 = Reserved // 0001 = Reserved // 0010 = Reserved // 0011 = AN3 // 0100 = AN4 // 0101 = AN5 // 0110 = AN6 // 0111 = AN7 // 1000 = AN8 // 1001 = AN9 // 1010 = AN10 // 1011 = AN11 // 1100 = Reserved // 1101 = Reserved // 1110 = DAC // 1111 = FVR // ADCON0(1) GO/DONE: A/D Conversion Status bit // 1 = A/D conversion cycle in progress. Setting this bit starts an A/D conversion cycle. // This bit is automatically cleared by hardware when the A/D conversion has completed. // 0 = A/D conversion completed/not in progress // ADCON0(0) ADON: ADC Enable bit // 1 = ADC is enabled // 0 = ADC is disabled and consumes no operating current // ******************************************************* ADCON0 = 0b00011001; //Select AN6 // ******************************************************* // 2.Define ADCON1(A/D CONTROL REGISTER 1) Register // ADCON1(7-4) Unimplemented: Read as ‘0’ // ADCON1(3-2) PVCFG<1:0>: Positive Voltage Reference select bit // 00 = Positive voltage reference supplied internally by VDD. // 01 = Positive voltage reference supplied externally through VREF+ pin. // 10 = Positive voltage reference supplied internally through FVR. // 11 = Reserved. // ADCON1(1-0) NVCFG<1:0>: Negative Voltage Reference select bit // 00 = Positive voltage reference supplied internally by VSS. // 01 = Positive voltage reference supplied externally through VREF- pin. // 10 = Reserved. // 11 = Reserved. // ******************************************************* // ADCON1 = 0b00001000; //supplied FVR ADCON1 = 0b00000000; //supplied VDD // ******************************************************* // 3.Define ADCON2(A/D CONTROL REGISTER 2) Register // ADCON2(7) ADFM: A/D Conversion Result Format Select bit // 1 = Right justified // 0 = Left justified // ADCON2(6) Unimplemented: Read as ‘0’ // ADCON2(5-3) ACQT<2:0>: A/D Acquisition time select bits. Acquisition time is the duration that the A/D charge // holding capacitor remains connected to A/D channel from the instant the GO/DONE bit is set until // conversions begins. // 000 = 0(1) // 001 = 2 TAD // 010 = 4 TAD // 011 = 6 TAD // 100 = 8 TAD // 101 = 12 TAD // 110 = 16 TAD // 111 = 20 TAD // ADCON2(2-0) ADCS<2:0>: A/D Conversion Clock Select bits // 000 = FOSC/2 // 001 = FOSC/8 // 010 = FOSC/32 // 011 = FRC(1) (clock derived from a dedicated internal oscillator = 600 kHz nominal) // 100 = FOSC/4 // 101 = FOSC/16 // 110 = FOSC/64 // 111 = FRC(1) (clock derived from a dedicated internal oscillator = 600 kHz nominal) // ******************************************************* // ADCON2 = 0b10111110; // ADCON2 = 0b10001000; ADCON2 = 0b10111110; } //****************************************************************************************** // Function Name : IntFVR // Title : Initialized Fixed Voltage Reference // Input : // Output : //****************************************************************************************** void IntFVR(void) { // ******************************************************* // 1.Define REFCON0(REFERENCE CONTROL REGISTER 0) // REFCON0(7) FVR1EN: Fixed Voltage Reference 1 Enable bit // 0 = FVR is disabled // 1 = FVR is enabled !!! FVR ; 1.024V Fixed Reference Voltage // REFCON0(6) FVR1ST: Fixed Voltage Reference 1 Stable bit // 0 = FVR is not stable // 1 = FVR is stable // REFCON0(5-4) FVR1S<1:0>: Fixed Voltage Reference 1 Voltage Select bits // 00 = Reserved, do not use // 01 = 1.024V (x1) // 10 = 2.048V (x2) // 11 = 4.096V (x4) // REFCON0(bit 3-0) // Unimplemented: Read as ‘0’ // ******************************************************* REFCON0 = 0b11100000; } //****************************************************************************************** // Function Name : GetADC // Title : Get A/D converter // Input : // Output : // Description : //****************************************************************************************** void GetADC(unsigned int *adc_data) { unsigned char Dat_L,Dat_H; // ***************** // 1.Start ADC // ***************** // GO/DONE: A/D Conversion Status bit ADCON0 = ADCON0 | 0b00000010; // ***************** // 2.Get data // ***************** while((ADCON0 & 0b00000010)!=0); Dat_H = ADRESH; Dat_L = ADRESL; // ***************** // 3.Edit data // ***************** *adc_data = (unsigned int)Dat_H * (unsigned int)256 + (unsigned int)Dat_L; } //****************************************************************************************** // Function name : LCD_Disp_S // Title : LCD display String // Input : // Output : //****************************************************************************************** void LCD_Disp_S(unsigned char *in_msg, unsigned char addr) { // *********************************** // 1.表示位置の設定 // *********************************** LCD_set_addr(addr); // *********************************** // 2.LCDに表示する // *********************************** while(1){ if(*in_msg==0x00)break; LCD_Disp(*in_msg); in_msg++; } } //****************************************************************************************** // Function Name : LCD_Disp // Title : LCDに文字を表示する // Input : // Output : //****************************************************************************************** void LCD_Disp(unsigned char in_char) { // 上位4ビット LCD_SETH(in_char); BSF(LCD_RS); //RS mnop(PI_ENBL_N); //48MHz mode Only BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal mnop(PI_ENBL_N); //48MHz mode Only BCF(LCD_RS); //RS mnop(PI_ENBL_N); // 下位4ビット LCD_SETL(in_char); BSF(LCD_RS); //RS mnop(PI_ENBL_N); //48MHz mode Only BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal mnop(PI_ENBL_N); //48MHz mode Only BCF(LCD_RS); //RS mnop(PI_ENBL_N); Wait_1mS(10); } //****************************************************************************************** // Function Name : Init_LCD // Title : インストラクションによるLCDの初期化 // Input : // Output : //****************************************************************************************** void Init_LCD(void) { // ****************************** // 1.ファンクションセット // ****************************** // (1)15mS以上待つ Wait_1mS(100); // (2)ファンクションセット(1); LCD_SETH(0b00110000); //PORTB = 0b0011**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal // (3)4.1mS以上待つ Wait_1mS(10); // (4)ファンクションセット(2) LCD_SETH(0b00110000); //PORTB = 0b0011**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal // (5)100μS以上待つ Wait_1mS(10); // (6)ファンクションセット(3) LCD_SETH(0b00110000); //PORTB = 0b0011**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // ****************************** // 2.表示設定 // ****************************** // (1)ファンクションセット LCD_SETH(0b00100000); //PORTB = 0b0010**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 上位4ビット LCD_SETH(0b00100000); //PORTB = 0b0010**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b10000000); //PORTB = 0bnf****** n=1:2 Line, f=0:5*8 dots  BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (2)表示OFF // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b10000000); //PORTB = 0b1000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (3)表示ON // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b00010000); //PORTB = 0b0001**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (4)エントリーモードセット // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b01100000); //PORTB = 0b01I/DS**** I/D=1:Incriment, S=0 BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (5)表示ON // // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b00000000; BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b11000000); //PORTB = 0b11000000; BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // // (6)DD RAM アドレスセット(先頭表示位置を設定する) // 上位4ビット LCD_SETH(0b10000000); //PORTB = 0b1000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); } //****************************************************************************************** // Function Name : LCD_CLR // Title : LCDの画面クリア // Input : // Output : //****************************************************************************************** void LCD_CLR(void) { // 上位4ビット LCD_SETH(0b00000000); //PORTB = 0b0000**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETH(0b00010000); //PORTB = 0b0001**** BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); } //****************************************************************************************** // Function name : LCD_set_addr // Title : LCDのDD−RAM−ADRESSを設定する // Input : // Output : //****************************************************************************************** void LCD_set_addr(unsigned char addr) { unsigned char tmp_char; // 上位4ビット tmp_char = 0b10000000 | addr ; LCD_SETH(tmp_char); //PORTB = 0b1***---- BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); // 下位4ビット LCD_SETL(addr); //PORTB = 0b----**** BCF(LCD_RS); //RS BSF(LCD_E ); //Enable-Signal mnop(PI_ENBL_N); BCF(LCD_E); //Enable-Signal Wait_1mS(10); } //****************************************************************************************** // Function Name : LCD_SETH // Title : LCDのDB(7-4)にデータ設定 // Input : // Output : //****************************************************************************************** void LCD_SETH(unsigned char tmp_data) { PORTC = PORTC & 0b00001111; PORTC = PORTC | (tmp_data & 0b11110000); } //****************************************************************************************** // Function Name : LCDDBH // Title : LCDのDB(3-0)にデータ設定 // Input : // Output : //****************************************************************************************** void LCD_SETL(unsigned char tmp_data) { PORTC = PORTC & 0b00001111; PORTC = PORTC | ((tmp_data & 0b00001111) << 4); } //****************************************************************************************** // Function Name : BCF // Title : // Input : // Output : //****************************************************************************************** void BCF(unsigned int pos) { PORTC = PORTC & ~(1 << pos); } //****************************************************************************************** // Function Name : BSF // Title : // Input : // Output : //****************************************************************************************** void BSF(unsigned int pos) { PORTC = PORTC | (1 << pos); } //****************************************************************************************** // Function Name : TERM_BCF_B (PORTB) // Title : // Input : // Output : //****************************************************************************************** void TERM_BCF_B(unsigned int pos) { PORTB = PORTB & ~(1 << pos); } //****************************************************************************************** // Function Name : TERM_BSF_B (PORTB) // Title : // Input : // Output : //****************************************************************************************** void TERM_BSF_B(unsigned int pos) { PORTB = PORTB | (1 << pos); } //****************************************************************************************** // Function Name : mnop // Title : wait function // Input : // Output : //****************************************************************************************** void mnop(unsigned int no) { unsigned int lp; for(lp=0;lp 0d1200(Count) ==> 0x04B0(Count) // TMR1 = 0xFFFF - 0x04B0 ==> 0xFB4F // TMR0H = 0xFB; TMR0L = 0x4F; // ******************************************************* // 3.Timer // ******************************************************* INTCONbits.TMR0IF = 0; while(1){ if(INTCONbits.TMR0IF != 0 )break; } } //****************************************************************************************** // Function Name : Int_Timer1 ※Timer1 // Title : // Input : // Output : //****************************************************************************************** void Int_Timer1(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) TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // ******************************************************* T1CON = 0b10000001; // 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. // ******************************************************* // 0.1mS(10KHz) : 12MHz * 4(PLLON) / 4(PIC) / 1(Prescale) / 10000(10KHz) ==> 0d1200(Count) ==> 0x04B0(Count) // TMR1 = 0xFFFF - 0x04B0 ==> 0xFB4F // #define PII_100uS_H 0xFB #define PII_100uS_L 0x65 //Adjust TMR1H = PII_100uS_H; TMR1L = PII_100uS_L; } //****************************************************************************************** // Function Name : Timer3 ※Timer3 (OK) // Title : Wait 1[mS] // Input : // Output : //****************************************************************************************** void Timer3(void) { // ******************************************************* // 1.Define T3CON(TIMER3 CONTROL REGISTER) Register // T3CON(7) RD16: 16-bit Read/Write Mode Enable bit // 1 = Enables register Read/Write of Timer3 in one 16-bit operation // 0 = Enables register Read/Write of Timer3 in two 8-bit operations // T3CON(6) Unimplemented: Read as ‘0’ // T3CON(5-4) T3CKPS1:T3CKPS0: Timer3 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 // T3CON(3) T3CCP1: Timer3 and Timer1 to CCP1 Enable bits // 1 = Timer3 is the clock source for compare/capture of ECCP1 // 0 = Timer1 is the clock source for compare/capture of ECCP1 // T3CON(2) T3SYNC: Timer3 External Clock Input Synchronization Control bit // (Not usable if the system clock comes from Timer1/Timer3) // When TMR3CS = 1: // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input // When TMR3CS = 0: // This bit is ignored. Timer3 uses the internal clock when TMR3CS = 0. // T3CON(1) TMR3CS: Timer3 Clock Source Select bit // 1 = External clock input from Timer1 oscillator or T1CKI // (on the rising edge after the first falling edge) // 0 = Internal clock (FOSC/4) // T3CON(0) TMR3ON: Timer3 On bit // 1 = Enables Timer3 // 0 = Stops Timer3 // ******************************************************* T3CON = 0b10000001; //16bit // ******************************************************* // 2.Define TMR3 Register // ******************************************************* // TMR3 data ( OSC:12MHz, 4X PLLON, 1:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 12MHz * 4(PLLON) / 4(PIC) / 1(Prescale) / 1000(1000Hz)==> 0d12,000(Count) ==> 0x2EE0(Count) // TMR1 = 0xFFFF - 0x2EE0 ==> 0xD11F // TMR3H = 0xD1; TMR3L = 0x1F; // ******************************************************* // 3.Timer // ******************************************************* PIR2bits.TMR3IF = 0; while(1){ if(PIR2bits.TMR3IF != 0 )break; } } //****************************************************************************************** // 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) { // ******************* // 1.Reset Timer // ******************* PIR1bits.TMR1IF = 0; TMR1H = PII_100uS_H; TMR1L = PII_100uS_L; // ******************* // 2.Counter // ******************* if(G_int_counter