//****************************************************************************************** // dsPIC33FJ256GP506 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H22.11.17 // History : // Laungage : MPLAB C30 //****************************************************************************************** // (1)Description // OSC: Internal OSC 7.37MHz * PLL ==> 79.23MHz //****************************************************************************************** #include "string.h" #include "stdio.h" #include "ctype.h" #include "math.h" #include "stdlib.h" #include "time.h" #include "p33FJ256GP506.h" //_FBS(RBS_NO_BOOT_RAM & BSS_NO_BOOT_CODE & BWRP_WRPROTECT_OFF); //_FSS(RSS_NO_RAM & SSS_NO_SEC_CODE & SWRP_WRPROTECT_OFF); //_FGS(GSS_OFF & GCP_OFF & GWRP_OFF ); //_FOSCSEL(FNOSC_FRC & IESO_OFF); _FOSCSEL(FNOSC_FRCPLL & IESO_OFF); _FOSC(FCKSM_CSECMD & OSCIOFNC_ON & POSCMD_NONE ); //_FWDT(FWDTEN_OFF & WINDIS_OFF & WDTPRE_PR32 & WDTPOST_PS1); //_FPOR(FPWRT_PWR1); //_FICD(BKBUG_ON & COE_ON & JTAGEN_ON & ICS_PGD1); _FWDT(FWDTEN_OFF); // //Define function // void Demo01(void); void DSP7SEG( unsigned char *, unsigned int, unsigned int ); void DSP7SEG_i(unsigned int, unsigned int, unsigned int); void InitADC(void); void Get_adc_data(unsigned int *); unsigned int SetIND(unsigned int ); unsigned char SetFNT(unsigned char); void Wait_1mS(unsigned int); void Init_Timer2(void); // //Define parameter // volatile unsigned int G_DISP_int; volatile unsigned int G_INTCNT=0; int main(void) { // ******************* // 1.Initialize // ******************* // (1)Define OSC 79.23MHz(7.37MHz/2*43(M43)/2) CLKDIV = 0b0000000000000000; // PLLFBD = 0x001E; //M32 PLLFBD = 0x0029; //M43(M43-2=>0d41=>0x29) // (2)Define Port TRISB = 0x0000; LATB = 0x0000; PORTB = 0x0000; TRISC = 0xFFFF; // (3)Initialized A/D Converter InitADC(); // (4)Initialized Timer2 Init_Timer2(); // (5)Demo Demo01(); // ******************* // 2.Program Main // ******************* while(1){ DSP7SEG_i(G_DISP_int,100, 3 ); } } //****************************************************************************************** // Function Name : Demo01 // Title : // Input : // Output : //****************************************************************************************** void Demo01(void) { unsigned char demo_str_1[] = {'P','i','c',' '}; unsigned char demo_str_2[] = {'-','-','3','3'}; DSP7SEG(demo_str_1, 2000, 9); DSP7SEG(demo_str_2, 2000, 9); } //****************************************************************************************** // Function Name : DSP7SEG_i // Title : // Input : data, n[mS] , comma(0-3,9[nodisplay]) // Output : // Description : //****************************************************************************************** void DSP7SEG_i(unsigned int int_data, unsigned int disp_time, unsigned int clm) { unsigned char tmp_data_s[5]; sprintf(tmp_data_s,"%4.4d",int_data); DSP7SEG(tmp_data_s, disp_time, clm); } //****************************************************************************************** // Function Name : DSP7SEG // Title : // Input : data, n[mS] , comma(0-3,9[nodisplay]) // Output : // Description : //****************************************************************************************** void DSP7SEG(unsigned char disp_char[], unsigned int disp_time, unsigned int clm) { unsigned int lp,lp_1; unsigned int time_lp; unsigned int tmp_clm; // *************** // 1.Initialize // *************** time_lp = disp_time / 4; tmp_clm = 3 - clm; // *************** // 2.Display // *************** for(lp_1=0;lp_1) AD1PCFGLbits.PCFG0 = 1; //PCFG(17-0) 1:Digital mode, 0:Analog mode AD1PCFGLbits.PCFG1 = 1; AD1PCFGLbits.PCFG2 = 1; AD1PCFGLbits.PCFG3 = 1; AD1PCFGLbits.PCFG4 = 1; AD1PCFGLbits.PCFG5 = 1; AD1PCFGLbits.PCFG6 = 1; AD1PCFGLbits.PCFG7 = 1; AD1PCFGLbits.PCFG8 = 1; AD1PCFGLbits.PCFG9 = 1; AD1PCFGLbits.PCFG10 = 1; AD1PCFGLbits.PCFG11 = 1; AD1PCFGLbits.PCFG12 = 1; AD1PCFGLbits.PCFG13 = 1; AD1PCFGLbits.PCFG14 = 1; AD1PCFGLbits.PCFG15 = 1; AD1PCFGHbits.PCFG16 = 0; AD1PCFGHbits.PCFG17 = 1; // AD1CHS0bits.CH0SA0 = 0; //Channel 0 positive input is AN16 AD1CHS0bits.CH0SA1 = 0; //Channel 0 positive input is AN16 AD1CHS0bits.CH0SA2 = 0; //Channel 0 positive input is AN16 AD1CHS0bits.CH0SA3 = 0; //Channel 0 positive input is AN16 AD1CHS0bits.CH0SA4 = 1; //Channel 0 positive input is AN16 // AD1CHS0bits.CH0NB = 0; //Channel 0 negative input is VREF // AD1CHS123bits.CH123NA0 = 0; //CH1, CH2, CH3 negative input is VREF- AD1CHS123bits.CH123NA1 = 0; //CH1, CH2, CH3 negative input is VREF- AD1CHS123bits.CH123SA = 0; //0 = CH1 positive input is AN0, CH2 positive input is AN1, CH3 positive input is AN2 // AD1CSSH = 0x00; //ADC1 PORT CONFIGURATION REGISTER HIGH AD1CSSL = 0x01; //ADC1 PORT CONFIGURATION REGISTER LOW // b) Select voltage reference source to match expected range on analog inputs(AD1CON2<15:13>) AD1CON2bits.VCFG = 0; //VR+:AVDD , VR-:AVSS // c) Select the analog conversion clock to match desired data rate with processor clock (AD1CON3<7:0>) AD1CON3bits.ADCS = 0; //TCY/2 // d) Select the appropriate sample/conversion sequence (AD1CON1<7:5> and AD1CON3<12:8>) AD1CON1bits.SSRC = 0x7;//Internal counter ends sampling and starts conversion (auto-convert) AD1CON3bits.SAMC = 31; //31 TAD AD1CON3bits.ADCS = 0b111111; //ADC Conversion Clock Select bits (TCY (ADCS<7:0> + 1) = 64 TCY = TAD) // e) Select how conversion results are presented in the buffer (AD1CON1<9:8>) AD1CON1bits.FORM = 0; //Integer (0000 00dd dddd dddd) // e) 10-bit or 12-bit Operation Mode bit (AD1CON1<10>) AD1CON1bits.AD12B = 1; //1 = 12-bit, 1-channel ADC operation // AD1CON1bits.AD12B = 0; //0 = 10-bit, 4-channel ADC operation // f) Select interrupt rate (AD1CON2<5:2>) AD1CON2bits.SMPI = 0; //Interrupts at the completion of conversion for each sample/convert sequence // g) Turn on A/D module (AD1CON1<15>) AD1CON1bits.ADON = 1; // Turn on the A/D converter // ******************************* // 2. Configure A/D interrupt (if required) // ******************************* // a) Clear the AD1IF bit // IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit // b) Select A/D interrupt priority // IEC0bits.AD1IE = 1; // Enable A/D interrupt } //****************************************************************************************** // Function Name : Get_adc_data // Title : Get A/D-Converter-Data // Input : // Output : //****************************************************************************************** void Get_adc_data(unsigned int *adc_data) { unsigned long int tmp_data; AD1CON1bits.SAMP=1; IFS0bits.AD1IF = 0; while(IFS0bits.AD1IF==0); tmp_data = (unsigned int)ADC1BUF0 ; tmp_data = tmp_data * 3300 / 4095 ; *adc_data = (unsigned int)tmp_data ; } //****************************************************************************************** // Function Name : Wait_1mS // Title : Wait 1[mS] * n // Input : // Output : // Description : Timer_4 //****************************************************************************************** void Wait_1mS(unsigned int no) { unsigned int lp; // *********************************************** // 1.Define T2CON Register // *********************************************** T4CON = 0b1000000000000000; // 1/1, 16Bit // T4CON = 0b1000000000100000; // 1/64, 16Bit // T4CON = 0b1000000000110000; // 1/256,16Bit // *********************************************** // 2.Set PR2-Register // *********************************************** // PR4 = 0x186A; // 10Hz (0x186a;0d6250) //FRC 8MHz 8MHz/2/64/10 // PR4 = 0x167D; // 10Hz (0x167D;0d5758) //FRC 7.37MHz 7.37MHz/2/64/10 // PR4 = 0x3C72; // 10Hz (0x3C72;0d15474) //FRC 79.23MHz 79.23MHz/2/256/10 PR4 = 0x9ABF; // 1000Hz (0x9ABF;0d39615) //FRC 79.23MHz 79.23MHz/2/1/1000 // *********************************************** // 3.Timer // *********************************************** // TMR4 = 0; // *********************************************** // 3.Timer // *********************************************** for(lp=0;lp 15625) PR2 < 32767 // PR2 = 12500; // 5Hz (8MHz/64/2/5 => 12500) PR2 < 32767 // PR2 = 14394; // 1Hz (7.37MHz/256/2 => 14394) PR2 < 32767 // PR2 = 11516; // 5Hz (7.37MHz/64/2/5 => 12500) PR2 < 32767 PR2 = 15475; // 10Hz (79.23MHz/256/2/10 => 15475) PR2 < 32767 // *********************************************** // 3.Define Interrupt // *********************************************** // (1)SR: ALU STATUS REGISTER (IN CPU) SRbits.IPL = 1; // (2)Interrupt(Define Timer2) IPC1bits.T2IP = 2; //T2IP: Timer2 Interrupt Priority bits (Max 7) IFS0bits.T2IF = 0; //T2IF: Timer2 Interrupt Flag Status bit : 0 = Interrupt request has not occurred IEC0bits.T2IE = 1; //T2IE: Timer2 Interrupt Enable bit : 1 = Interrupt request enabled } //****************************************************************************************** // Function Name : // Title : Interrupt TIMER2 // Input : // Output : //****************************************************************************************** void __attribute__((interrupt, auto_psv)) _T2Interrupt(void) { unsigned int adc_data; // ******************************** // 1.Clear the Timer2 interrupt Flag // ******************************** IFS0bits.T2IF = 0; //T2IF: Timer2 Interrupt Flag Status bit : 0 = Interrupt request has not occurred // ******************************** // 2.Set data // ******************************** if(G_INTCNT<5){ G_INTCNT++; }else{ Get_adc_data(&adc_data); G_DISP_int = adc_data; G_INTCNT=0; } }