//****************************************************************************************** // dsPIC33FJ256GP506 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H21.04.10 // History : // Laungage : MPLAB C30 //****************************************************************************************** // (1)Description // OSC : 79.23MHz (7.37MHz[IntOSC]/2*43/2) //****************************************************************************************** #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); void InitUART(void); void Timer(unsigned int); void SendUART(char); void SendUARTStr(char *); void RecUART(char *); void InitADC(void); void GetADC(int *); int main(void) { int adc_data; long int tmp_data_l; char s_data[10]; // ******************* // 1.Initialize // ******************* // (0)Define OSC 79.23MHz(7.37MHz/2*43(M43)/2) CLKDIV = 0b0000000000000000; // PLLFBD = 0x001E; //M32 PLLFBD = 0x0029; //M43(M43-2=>0d41=>0x29) // (1)Set All Digital Port AD1PCFGL = 0xffff; AD1PCFGH = 0xffff; // (2)Define PortB TRISB = 0xFFFF; // LATB = 0x0000; PORTB = 0x0000; // (3)Initialize UART InitUART(); // (4)Initialize A/D Converter InitADC(); // ***************************** // 2.Program Main // ***************************** // (1)Start Message SendUARTStr("dsPIC33FJ256GP506 Connected OK!! > "); // (2)Program loop while(1){ // (2-1)Get A/D Converter GetADC(&adc_data); // (2-2)Convert data-format tmp_data_l = adc_data; // tmp_data_l = tmp_data_l * 3226 / 1000; // 10Bit : 0d3300 / 0b1111111111(0d1023) => 3.226 tmp_data_l = tmp_data_l * 8059 / 10000; // 12Bit : 0d3300 / 0b111111111111(0d4095) => 0.8059 adc_data = tmp_data_l; sprintf(s_data,"%4.4d",adc_data); // (2-3)Print AD-data SendUARTStr(s_data); SendUARTStr("[mV] "); // (2-4)Wait(1S) Timer(10); } } //****************************************************************************************** // Function Name : SendUARTStr // Title : Send UART Strings // Input : // Output : //****************************************************************************************** void SendUARTStr(char in_s[]) { unsigned int lp = 0; while(in_s[lp] != 0){ SendUART(in_s[lp]); lp++; } } //****************************************************************************************** // Function Name : InitUART // Title : Initialize UART // Input : // Output : //****************************************************************************************** void InitUART(void) { // ************************** // (1)Define U1MODE // ************************** U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func U1MODEbits.USIDL = 0; // Bit13 Continue in Idle U1MODEbits.IREN = 0; // Bit12 No IR translation U1MODEbits.RTSMD = 0; // Bit11 Simplex Mode U1MODEbits.UEN0 = 0; // Bits8,9 TX,RX enabled, CTS,RTS not U1MODEbits.UEN1 = 0; U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here) U1MODEbits.LPBACK = 0; // Bit6 No Loop Back U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55') // U1MODEbits.RXINV = 0; // Bit4 IdleState = 1 U1MODEbits.URXINV = 0; U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period U1MODEbits.PDSEL1 = 0; // Bits1,2 8bit, No Parity U1MODEbits.PDSEL0 = 0; U1MODEbits.STSEL = 0; // Bit0 One Stop Bit // ************************** // (2)Define U1BRG (16Bit;U1BRGL+U1BRGH) // BAUDRATE(Register)=SYSCLK/32/BAUDRATE-1 // ************************** // U1BRG = 25; // BAUDRATE = 8MHz/32/9600-1 = 0d25 9600BPS(8MHz) // U1BRG = 23; // BAUDRATE = 7.37MHz/32/9600-1 = 0d23 9600BPS(7.37MHz) U1BRG = 257; // BAUDRATE = 79.23MHz/32/9600-1 = 0d257 9600BPS(79.23MHz) // ************************** // (3)Define U1STA // ************************** U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!) U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15 U1STAbits.UTXBRK = 0; //Bit11 Disabled U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit* U1STAbits.TRMT = 0; //Bit8 *Read Only bit* U1STAbits.URXISEL0 = 0; //Bits6,7 Int. on character recieved U1STAbits.URXISEL1 = 0; U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit* U1STAbits.PERR = 0; //Bit3 *Read Only Bit* U1STAbits.FERR = 0; //Bit2 *Read Only Bit* U1STAbits.OERR = 0; //Bit1 *Read Only Bit* U1STAbits.URXDA = 0; //Bit0 *Read Only Bit* U1MODEbits.UARTEN = 1; // And turn the peripheral on U1STAbits.UTXEN = 1; } //****************************************************************************************** // Function Name : SendUART // Title : Send UART // Input : // Output : //****************************************************************************************** void SendUART(char in_c) { while(U1STAbits.UTXBF != 0); U1TXREG = in_c; } //****************************************************************************************** // Function Name : RecUART // Title : Recive UART // Input : // Output : //****************************************************************************************** void RecUART(char *out_c) { while(U1STAbits.URXDA == 0); *out_c = U1RXREG; } //****************************************************************************************** // Function Name : Timer // Title : wait function // Input : Count value (100mS) // Output : // Description : Internal OSC //****************************************************************************************** void Timer(unsigned int S01) { unsigned int lp; // *********************************************** // 1.Define T2CON Register // *********************************************** // T2CON = 0b1000000000100000; // 1/64,16Bit T2CON = 0b1000000000110000; // 1/256,16Bit // *********************************************** // 2.Set PR2-Register // *********************************************** // PR2 = 0x186A; // 10Hz (0x186a;0d6250) //FRC 8MHz 8MHz/2/64/10 // PR2 = 0x167D; // 10Hz (0x167D;0d5758) //FRC 7.37MHz 7.37MHz/2/64/10 PR2 = 0x3C72; // 10Hz (0x3C72;0d15474) //FRC 79.23MHz 79.23MHz/2/256/10 // *********************************************** // 3.Timer // *********************************************** TMR2 = 0; for(lp=0;lp) AD1PCFGLbits.PCFG0 = 0; //PCFG(15-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; // AD1CHS0bits.CH0SA0 = 0; //Channel 0 positive input is AN0 AD1CHS0bits.CH0SA1 = 0; //Channel 0 positive input is AN0 AD1CHS0bits.CH0SA2 = 0; //Channel 0 positive input is AN0 AD1CHS0bits.CH0SA3 = 0; //Channel 0 positive input is AN0 // 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 : GetADC // Title : Get A/D Converter Value // Input : // Output : //****************************************************************************************** void GetADC(int *dat) { AD1CON1bits.SAMP=1; IFS0bits.AD1IF = 0; while(IFS0bits.AD1IF==0); *dat = (unsigned int)ADC1BUF0; }