//****************************************************************************************** // 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) : HOLD SWITCH // PORTB(6) : no connect // PORTC(2)AN6 : FPM-02PG // //****************************************************************************************** #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 PI_NORMAL 0 #define PI_FAILURE 1 // #define PI_ENBL_N 10 // #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 PI_ADCMODE_0 0 #define PI_ADCMODE_1 1 #define PI_ADCMODE_2 2 // //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 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 Hold_data(unsigned int); // volatile unsigned char G_ADC_MODE = PI_ADCMODE_0; // PI_ADCMODE_0 - PI_ADCMODE_2 //volatile unsigned char G_ADC_MODE = PI_ADCMODE_1; // PI_ADCMODE_0 - PI_ADCMODE_2 //volatile unsigned char G_ADC_MODE = PI_ADCMODE_2; // PI_ADCMODE_0 - PI_ADCMODE_2 void main(void) { unsigned int adc_data; unsigned int def_data; unsigned int res_data; // ************************************* // 1.Initialize Segment // ************************************* // (1)Define I/O port PORTA = 0b00000000; PORTB = 0b00000000; PORTC = 0b00000000; TRISA = 0b11111111; TRISB = 0b11111111; 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)Start message Demo01(); // ************************************* // 2.Program Main // ************************************* // (0)Default data GetADC(&def_data); while(1){ // (1)Analized GetADC(&adc_data); if(def_data: 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’ // ******************************************************* switch(G_ADC_MODE){ case PI_ADCMODE_0: REFCON0 = 0b11010000; break; case PI_ADCMODE_1: REFCON0 = 0b11100000; break; default: REFCON0 = 0b11110000; break; } } //****************************************************************************************** // 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 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; } }