//****************************************************************************************** // PIC18F14K50 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H22.09.01 // History : // Laungage : MPLAB C18 //****************************************************************************************** // (1)Description // 1)OSC ; Internal OSC (16MHz) // 2)Define I/O PORT // RA4(AN3) : Input // RC0〜RC7 : 7 Segment LED(a〜g,DP) // RB4〜RB7 : 1桁〜4桁 //****************************************************************************************** #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 FOSC = IRC //Oscillator Selection bits(Internal RC 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 Timer(void); void Wait(unsigned int); void InitOSC(void); void InitADC(void); void GetADC(unsigned int *); unsigned char SetFNT(unsigned char); void WatDSP(unsigned char, unsigned char, unsigned char , unsigned char ); void DEMO01(void); void DEMO02(void); // //Define parameter // #define P_K1 4 #define P_K2 5 #define P_K3 6 #define P_K4 7 // volatile unsigned char PC_priod = 0; //****************************************************************************************** // Program : // Title : // Input : // Output : //****************************************************************************************** void main(void) { unsigned int adc_data; unsigned long int adc_data_l; char tmp_data_s[5] ; // ***************************** // 1.Initialized Segment // ***************************** // (1)Define OSC InitOSC(); //Internal OSC 16MHz // (2)Define I/O port PORTB = 0x00; PORTC = 0xFF; TRISA = 0xFF; TRISB = 0b00001111; TRISC = 0b00000000; // (3)Initialize A/D Converter InitADC(); // ***************************** // 2.Program main // ***************************** // (1)Send Message DEMO01(); // (2)Main program while(1){ // 2-1)Get A/D_Converter_data GetADC(&adc_data); // 2-2)Convert data adc_data_l = adc_data ; adc_data_l = adc_data_l * (long int)2000 / (long int)1024 ; adc_data = adc_data_l; // 2-3)Convert string sprintf(tmp_data_s,"%4.4d",adc_data); // 2-4)Put data PC_priod = 3; WatDSP(tmp_data_s[0],tmp_data_s[1],tmp_data_s[2],tmp_data_s[3]); }; } //****************************************************************************************** // Function Name : DEMO01 // Title : Initialize Message // Input : // Output : // Description : //****************************************************************************************** void DEMO01(void) { WatDSP('P','i','c',' '); WatDSP('P','i','c',' '); WatDSP('P','i','c',' '); WatDSP('P','i','c',' '); WatDSP('1','8','F',' '); WatDSP('1','8','F',' '); WatDSP('1','8','F',' '); WatDSP('1','8','F',' '); } //****************************************************************************************** // Function Name : DEMO02 // Title : Auto counter // Input : // Output : // Description : //****************************************************************************************** void DEMO02(void) { unsigned char lp1,lp2,lp3,lp4; for(lp4=0;lp4<16;lp4++){ for(lp3=0;lp3<16;lp3++){ for(lp2=0;lp2<16;lp2++){ for(lp1=0;lp1<16;lp1++){ WatDSP(lp4,lp3,lp2,lp1); } } } } } //****************************************************************************************** // Function Name : InitOSC // Title : // Input : // Output : // Description : Internal 16MHz //****************************************************************************************** void InitOSC(void) { // ******************************************************* // 1.Define OSCCON(OSCILLATOR CONTROL REGISTER) // OSCCON(7) IDLEN: Idle Enable bit // 1 = Device enters Idle mode on SLEEP instruction // 0 = Device enters Sleep mode on SLEEP instruction // OSCCON(6-4) IRCF<2:0>: Internal Oscillator Frequency Select bits // 111 = 16 MHz // 110 = 8 MHz // 101 = 4 MHz // 100 = 2 MHz // 011 = 1 MHz(3) // 010 = 500 kHz // 001 = 250 kHz // 000 = 31 kHz(2) // OSCCON(3) OSTS: Oscillator Start-up Time-out Status bit(1) // 1 = Device is running from the clock defined by FOSC<2:0> of the CONFIG1 register // 0 = Device is running from the internal oscillator (HFINTOSC or LFINTOSC) // OSCCON(2) HFIOFS: HFINTOSC Frequency Stable bit // 1 = HFINTOSC frequency is stable // 0 = HFINTOSC frequency is not stable // OSCCON(1-0) SCS<1:0>: System Clock Select bits // 1x = Internal oscillator block // 01 = Secondary (Timer1) oscillator // 00 = Primary clock (determined by CONFIG1H[FOSC<3:0>]). // ******************************************************* OSCCON =0b11111111; //Internal 16MHz // ******************************************************* // 2.Define OSCCON2(OSCILLATOR CONTROL REGISTER 2) // OSCCON2(7-3) Unimplemented: Read as ‘0’ // OSCCON2(2) PRI_SD: Primary Oscillator Drive Circuit shutdown bit // 1 = Oscillator drive circuit on // 0 = Oscillator drive circuit off (zero power) // OSCCON2(1) HFIOFL: HFINTOSC Frequency Locked bit // 1 = HFINTOSC is in lock // 0 = HFINTOSC has not yet locked // OSCCON2(0) LFIOFS: LFINTOSC Frequency Stable bit // 1 = LFINTOSC is stable // 0 = LFINTOSC is not stable // ******************************************************* OSCCON2 =0b00000000; // ******************************************************* // 3.Define OSCTUNE(OSCILLATOR TUNING REGISTER) // OSCTUNE(7) INTSRC: Internal Oscillator Low-Frequency Source Select bit // 1 = 31.25 kHz device clock derived from 16 MHz HFINTOSC source (divide-by-512 enabled) // 0 = 31 kHz device clock derived directly from LFINTOSC internal oscillator // OSCTUNE(6) SPLLEN: Software Controlled Frequency Multiplier PLL bit // 1 = PLL enabled (for HFINTOSC 8 MHz only) // 0 = PLL disabled // OSCTUNE(5-0) TUN<5:0>: Frequency Tuning bits // 011111 = Maximum frequency // 011110 = // ・・・・ // 000001 = // 000000 = Oscillator module is running at the factory calibrated frequency. // 111111 = // ・・・・ // 100000 = Minimum frequency // ******************************************************* OSCTUNE = 0b00000000; } //****************************************************************************************** // Function Name : InitADC // Title : // Input : // Output : // Description : Analog Channel Select (AN10) //****************************************************************************************** void InitADC(void) { // ******************************************************* // 0.Define ANSEL(ANALOG SELECT REGISTER) and ANSELH(ANALOG SELECT REGISTER 2) // ******************************************************* // ==> See InitUART_function // ANSEL = 0b11111000; // ANSELH = 0b00001111; // ******************************************************* // 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 = 0b00001101; // ******************************************************* // 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 = 0b00000000; // ******************************************************* // 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; } //****************************************************************************************** // Function Name : GetADC // Title : // 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 = Dat_H * 256 + Dat_L; } //****************************************************************************************** // Function Name : // Title : // Input : // Output : //****************************************************************************************** void WatDSP(unsigned char in_char4, unsigned char in_char3, unsigned char in_char2, unsigned char in_char1) { unsigned int lp; unsigned char tmp_fnt; unsigned int timer = 1; for(lp=0;lp<50;lp++){ tmp_fnt = SetFNT(in_char4); if(PC_priod == 4) tmp_fnt = tmp_fnt & 0b01111111; PORTC = tmp_fnt ; PORTB = (1<: 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 T1CON = 0b10000001; // 16-bit Read/Write Mode, 1:1 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 // TMR1 data ( Internal OSC:16MHz, 1:8 Prescale value, 0.01[S];100Hz ) // 10mS(100Hz): 16MHz / 4(PIC) / 8(Prescale) / 100(100Hz)==> 0d5,000(Count) ==> 0x1388(Count) // TMR1 = 0xFFFF - 0x1388 ==> 0xEC77 // TMR1 data ( Internal OSC:16MHz, 1:1 Prescale value, 0.001[S];1000Hz ) // 10mS(1000Hz): 16MHz / 4(PIC) / 1(Prescale) / 1000(100Hz)==> 0d4,000(Count) ==> 0x0FA0(Count) // TMR1 = 0xFFFF - 0x0FA0 ==> 0xF05F // TMR1H = 0xF0; TMR1L = 0x5F; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR1IF = 0; while(1){ if(PIR1bits.TMR1IF != 0 )break; } }