//****************************************************************************************** // PIC32MX250F128B Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H26.06.20 // History : // Laungage : MPLAB C32 //****************************************************************************************** // Description // (1)OSC : Internal OSC PLL 40MHz // (2)UART Propaty : 9600BPS // (3)Assign UART1 Pin : RX(PB13),TX(PB15) Genaral I/O Port // (4)Assign UART2 Pin : RX(PB11),TX(PB14) This terminal is used to communicate with the APT3011F4 // (5)RB9 Pin : This pin is used to reset the APT3011F4 // (6)A/D Converter : AN0 Genaral I/O Port //****************************************************************************************** //#include #include // //Define configration // #pragma config PMDL1WAY = OFF //Peripheral Module Disable Configuration:Allow multiple reconfigurations #pragma config IOL1WAY = OFF //Peripheral Pin Select Configuration:Allow multiple reconfigurations #pragma config FUSBIDIO = OFF //USB USID Selection:Controlled by Port Function #pragma config FVBUSONIO = OFF //USB VBUS ON Selection:Controlled by Port Function //#pragma config FPLLIDIV= DIV_1 //PLL Input Divider:1x Divider (DIV_1,DIV_2,DIV_3,DIV_4,DIV_5,DIV_6,DIV_10,DIV_12) #pragma config FPLLIDIV = DIV_2 //#pragma config FPLLMUL = MUL_15 //PLL Multiplier:15x Multiplier(MUL_15,MUL_16,MUL_17,MUL_18,MUL_19,MUL_20,MUL_21,MUL_24) #pragma config FPLLMUL = MUL_20 #pragma config UPLLIDIV = DIV_1 //USB PLL Input Divider #pragma config UPLLEN = OFF //USB PLL Enable //#pragma config FPLLODIV= DIV_1 //System PLL Output Clock Divider:PLL Divide by 1(DIV_1,DIV_2,DIV_4,DIV_8,DIV_16,DIV_32,DIV_64,DIV_256) #pragma config FPLLODIV = DIV_2 //#pragma config FNOSC = FRC //Oscillator Selection Bits:Fast RC Osc (FRC) (FRC,FRCPLL,PRI,PRIPLL,SOSC,LPRC,FRCDIV16,FRCDIV) #pragma config FNOSC = FRCPLL #pragma config FSOSCEN = OFF //Secondary Oscillator Enable:Disabled #pragma config IESO = OFF //Internal/External Switch Over:Disabled #pragma config POSCMOD = OFF //Primary Oscillator Configuration:Primary osc disabled (EC,XT,HS,OFF) #pragma config OSCIOFNC = OFF //CLKO Output Signal Active on the OSCO Pin::Disabled #pragma config FPBDIV = DIV_1 //Peripheral Clock Divisor:Pb_Clk is Sys_Clk/1 (DIV_1,DIV_2,DIV_4,DIV_8) #pragma config FCKSM = CSDCMD //Clock Switching and Monitor Selection:Clock Switch Disable, FSCM Disabled (CSECME,CSECMD,CSDCMD) #pragma config WDTPS = PS1 //Watchdog Timer Postscaler:1:1 (PS1,,,,PS1048576) #pragma config WINDIS = OFF //Watchdog Timer Window Enable:Watchdog Timer is in Non-Window Mode #pragma config FWDTEN = OFF //Watchdog Timer Enable:WDT Disabled (SWDTEN Bit Controls) #pragma config FWDTWINSZ = WINSZ_50//Watchdog Timer Window Size:Window Size is 50% (WINSZ_75,WINSZ_50,WINSZ_37,WISZ_25) #pragma config JTAGEN = OFF //JTAG Enable:JTAG Disabled #pragma config ICESEL = RESERVED//ICE/ICD Comm Channel Select:Reserved (RESERVED,ICS_PGx3,ICS_PGx2,ICS_PGx1) #pragma config PWP = OFF //Program Flash Write Protect: #pragma config BWP = OFF //Boot Flash Write Protect bit: #pragma config CP = OFF //Code Protect: // //Define function // void InitUART1(void); void InitUART2(void); void SendUART1(unsigned char); void SendUART2(unsigned char); void RecUART1(unsigned char *); void RecUART2(unsigned char *); void SendUART1Str(unsigned char *); void SendUART2Str(unsigned char *); void cnv_data(unsigned char *,unsigned int); void wat(unsigned int); void Timer(void); void InitADC(void); void Get_data(unsigned int *); void Demo001(void); void Play(char); void Play_s(char *); // //Define Parameter // #define PI_LINE_MAX 20 int main(void) { unsigned int adc_data; unsigned char S_adc_data[PI_LINE_MAX]; // ******************* // 1.Initialize // ******************* // (1)Define PortB TRISB = 0xFFFFFDFF; // 32bit LATB = 0x00000000; // 32bit PORTB = 0x00000000; // 32bit // (2)Define ANSEL (All Degital Port) ANSELA = 0; ANSELB = 0; // (3)Assign UART1 Pin (Rx1:RP13,Tx1:RP15) // Rx:U1RX==>RPB13 U1RXR = 0b0011; // Tx:RPB15R(PORTB15)==>U1TX RPB15R = 0b0001; // (4)Assign UART2 Pin (Rx2:RP11,Tx2:RP14) // Rx:U2RX==>RPB11 U2RXR = 0b0011; // Tx:RPB14R(PORTB14)==>U2TX RPB14R = 0b0010; // (5)Initialize UART1 // InitUART1(); // (6)Initialize UART2 InitUART2(); // (7)Initialize A/D Converter InitADC(); // (8)Put Message // SendUART1Str(" PIC32MX250F128B Connected OK> "); // ******************* // 2.Program Main // ******************* // (1)Reset APT3011F4 PORTB = 0x00000000; wat01((unsigned int)1); PORTB = 0x00000200; // (2)Program main while(1){ // (2-1)Get A/D-Conveter data Get_data(&adc_data); // (2-2)Convert data format cnv_data(&S_adc_data[0],adc_data); // (2-3)Put data Play_s(&S_adc_data[0]); // (2-4)Title SendUART2Str("miriboruto"); SendUART2(0x0d); // (2-5)Wait wat01((unsigned int)10); } } //****************************************************************************************** // Function Name : Demo001 // Title : // Input : // Output : //****************************************************************************************** void Demo001(void) { // SendUART2Str("#J"); SendUART2(0x0d); wat01((unsigned int)10); // SendUART2Str("#K"); SendUART2(0x0d); wat01((unsigned int)10); // SendUART2Str("ohayougozaimasu"); SendUART2(0x0d); wat01((unsigned int)40); // SendUART2Str("konnichiwa"); SendUART2(0x0d); wat01((unsigned int)30); // SendUART2Str("konbanwa"); SendUART2(0x0d); wat01((unsigned int)30); } //****************************************************************************************** // Function Name : Play_s // Title : // Input : // Output : //****************************************************************************************** void Play_s(char put_s[]) { unsigned char lp = 0; while(put_s[lp] != 0){ Play(put_s[lp]); lp++; } } //****************************************************************************************** // Function Name : Play // Title : // Input : // Output : //****************************************************************************************** void Play(char put_c) { switch (put_c) { case '0': SendUART2Str("zero"); SendUART2(0x0d); break; case '1': SendUART2Str("ichi"); SendUART2(0x0d); break; case '2': SendUART2Str("nii"); SendUART2(0x0d); break; case '3': SendUART2Str("san"); SendUART2(0x0d); break; case '4': SendUART2Str("yon"); SendUART2(0x0d); break; case '5': SendUART2Str("gou"); SendUART2(0x0d); break; case '6': SendUART2Str("roku"); SendUART2(0x0d); break; case '7': SendUART2Str("nana"); SendUART2(0x0d); break; case '8': SendUART2Str("hachi"); SendUART2(0x0d); break; case '9': SendUART2Str("kyuu"); SendUART2(0x0d); break; case '.': SendUART2Str("ten"); SendUART2(0x0d); break; default : break; } wat01((unsigned int)10); } //****************************************************************************************** // Function Name : InitADC // Title : Initialized A/D Converter // Input : // Output : //****************************************************************************************** void InitADC(void) { // ******************************* // 0.Set ANSEL bits // ******************************* ANSELAbits.ANSA0 = 1; //AN0: Analog PORT // ******************************* // 1.AD1CON1: ADC CONTROL REGISTER 1 // AD1CON1[31-16] Unimplemented: Read as e0f // AD1CON1[15] ON: ADC Operating Mode bit(1) // 1 = ADC module is operating // 0 = ADC module is not operating // AD1CON1[14] Unimplemented: Read as e0f // AD1CON1[13] SIDL: Stop in Idle Mode bit // 1 = Discontinue module operation when device enters Idle mode // 0 = Continue module operation in Idle mode // AD1CON1[12-11] Unimplemented: Read as e0f // AD1CON1[10-8] FORM<2:0>: Data Output Format bits // 011 = Signed Fractional 16-bit (DOUT = 0000 0000 0000 0000 sddd dddd dd00 0000) // 010 = Fractional 16-bit (DOUT = 0000 0000 0000 0000 dddd dddd dd00 0000) // 001 = Signed Integer 16-bit (DOUT = 0000 0000 0000 0000 ssss sssd dddd dddd) // 000 = Integer 16-bit (DOUT = 0000 0000 0000 0000 0000 00dd dddd dddd) // 111 = Signed Fractional 32-bit (DOUT = sddd dddd dd00 0000 0000 0000 0000) // 110 = Fractional 32-bit (DOUT = dddd dddd dd00 0000 0000 0000 0000 0000) // 101 = Signed Integer 32-bit (DOUT = ssss ssss ssss ssss ssss sssd dddd dddd) // 100 = Integer 32-bit (DOUT = 0000 0000 0000 0000 0000 00dd dddd dddd) // AD1CON1[7-5] SSRC<2:0>: Conversion Trigger Source Select bits // 111 = Internal counter ends sampling and starts conversion (auto convert) // 110 = Reserved // 101 = Reserved // 100 = Reserved // 011 = CTMU ends sampling and starts conversion // 010 = Timer 3 period match ends sampling and starts conversion // 001 = Active transition on INT0 pin ends sampling and starts conversion // 000 = Clearing SAMP bit ends sampling and starts conversion // AD1CON1[4] CLRASAM: Stop Conversion Sequence bit (when the first ADC interrupt is generated) // 1 = Stop conversions when the first ADC interrupt is generated. Hardware clears the ASAM bit when the // ADC interrupt is generated. // 0 = Normal operation, buffer contents will be overwritten by the next conversion sequence // AD1CON1[3] Unimplemented: Read as e0f // AD1CON1[2] ASAM: ADC Sample Auto-Start bit // 1 = Sampling begins immediately after last conversion completes; SAMP bit is automatically set. // 0 = Sampling begins when SAMP bit is set // AD1CON1[1] SAMP: ADC Sample Enable bit(2) // 1 = The ADC sample and hold amplifier is sampling // 0 = The ADC sample/hold amplifier is holding // When ASAM = 0, writing e1f to this bit starts sampling. // When SSRC = 000, writing e0f to this bit will end sampling and start conversion. // AD1CON1[0] DONE: Analog-to-Digital Conversion Status bit(3) // 1 = Analog-to-digital conversion is done // 0 = Analog-to-digital conversion is not done or has not started // Clearing this bit will not affect any operation in progress. // ******************************* AD1CON1bits.ON = 1; AD1CON1bits.SIDL = 0; AD1CON1bits.FORM = 0b000; AD1CON1bits.SSRC = 0b111; AD1CON1bits.CLRASAM = 0; AD1CON1bits.ASAM = 0; AD1CON1bits.SAMP = 0; AD1CON1bits.DONE = 0; // ******************************* // 2.AD1CON2: ADC CONTROL REGISTER 2 // AD1CON2[31-16] Unimplemented: Read as e0f // AD1CON2[15-13] VCFG<2:0>: Voltage Reference Configuration bits // VREFH VREFL // 000 AVDD AVss // 001 External VREF+ pin AVSS // 010 AVDD External VREF- pin // 011 External VREF+ pin External VREF- pin // 1xx AVDD AVSS // AD1CON2[12] OFFCAL: Input Offset Calibration Mode Select bit // 1 = Enable Offset Calibration mode // Positive and negative inputs of the sample and hold amplifier are connected to VREFL // 0 = Disable Offset Calibration mode // The inputs to the sample and hold amplifier are controlled by AD1CHS or AD1CSSL // AD1CON2[11] Unimplemented: Read as e0f // AD1CON2[10] CSCNA: Input Scan Select bit // 1 = Scan inputs // 0 = Do not scan inputs // AD1CON2[9-8] Unimplemented: Read as e0f // AD1CON2[7] BUFS: Buffer Fill Status bit // Only valid when BUFM = 1. // 1 = ADC is currently filling buffer 0x8-0xF, user should access data in 0x0-0x7 // 0 = ADC is currently filling buffer 0x0-0x7, user should access data in 0x8-0xF // AD1CON2[6] Unimplemented: Read as e0f // AD1CON2[5-2] SMPI<3:0>: Sample/Convert Sequences Per Interrupt Selection bits // 1111 = Interrupts at the completion of conversion for each 16th sample/convert sequence // 1110 = Interrupts at the completion of conversion for each 15th sample/convert sequence // **** // 0001 = Interrupts at the completion of conversion for each 2nd sample/convert sequence // 0000 = Interrupts at the completion of conversion for each sample/convert sequence // AD1CON2[1] BUFM: ADC Result Buffer Mode Select bit // 1 = Buffer configured as two 8-word buffers, ADC1BUF7-ADC1BUF0, ADC1BUFF-ADCBUF8 // 0 = Buffer configured as one 16-word buffer ADC1BUFF-ADC1BUF0 // AD1CON2[0] ALTS: Alternate Input Sample Mode Select bit // 1 = Uses Sample A input multiplexer settings for first sample, then alternates between Sample B and // Sample A input multiplexer settings for all subsequent samples // 0 = Always use Sample A input multiplexer settings // ******************************* AD1CON2bits.VCFG = 0b000; AD1CON2bits.OFFCAL = 0; AD1CON2bits.CSCNA = 0; AD1CON2bits.BUFS = 0; AD1CON2bits.SMPI = 0b0000; AD1CON2bits.BUFM = 0; AD1CON2bits.ALTS = 0; // ******************************* // 3.AD1CON3: ADC CONTROL REGISTER 3 // AD1CON3[31-16] Unimplemented: Read as e0f // AD1CON3[15] ADRC: ADC Conversion Clock Source bit // 1 = Clock derived from FRC // 0 = Clock derived from Peripheral Bus Clock (PBCLK) // AD1CON3[14-13] Unimplemented: Read as e0f // AD1CON3[12-8] SAMC<4:0>: Auto-Sample Time bits(1) // 11111 = 31 TAD // * // * // * // 00001 =1 TAD // 00000 =0 TAD (Not allowed) // AD1CON3[7-0] ADCS<7:0>: ADC Conversion Clock Select bits(2) //@@@@@11111111 =TPBEE2E(ADCS<7:0> + 1) = 512ETPB = TAD // * // * // * // 00000001 =TPBE2E(ADCS<7:0> + 1) = 4ETPB = TAD // 00000000 =TPBE2E(ADCS<7:0> + 1) = 2ETPB = TAD // ******************************* AD1CON3bits.ADRC = 1; AD1CON3bits.SAMC = 0b11111; AD1CON3bits.ADCS = 0b11111111; // ******************************* // 4.AD1CHS: ADC INPUT SELECT REGISTER // AD1CHS[31] CH0NB: Negative Input Select bit for Sample B // 1 = Channel 0 negative input is AN1 // 0 = Channel 0 negative input is VREFL // AD1CHS[30-28] Unimplemented: Read as e0f // AD1CHS[27-24] CH0SB<3:0>: Positive Input Select bits for Sample B // 1111 = Channel 0 positive input is Open(1) // 1110 = Channel 0 positive input is IVREF(2) // 1101 = Channel 0 positive input is CTMU temperature sensor (CTMUT)(3) // 1100 = Channel 0 positive input is AN12(4) // E // E // E // 0001 = Channel 0 positive input is AN1 // 0000 = Channel 0 positive input is AN0 // AD1CHS[23] CH0NA: Negative Input Select bit for Sample A Multiplexer Setting(2) // 1 = Channel 0 negative input is AN1 // 0 = Channel 0 negative input is VREFL // AD1CHS[22-20] Unimplemented: Read as e0f // AD1CHS[19-16] CH0SA<3:0>: Positive Input Select bits for Sample A Multiplexer Setting // 1111 = Channel 0 positive input is Open(1) // 1110 = Channel 0 positive input is IVREF(2) // 1101 = Channel 0 positive input is CTMU temperature (CTMUT)(3) // 1100 = Channel 0 positive input is AN12(4) // E // E // E // 0001 = Channel 0 positive input is AN1 // 0000 = Channel 0 positive input is AN0 // AD1CHS[15-0] Unimplemented: Read as e0f // ******************************* AD1CHSbits.CH0NB = 0; AD1CHSbits.CH0SB = 0b0000; AD1CHSbits.CH0NA = 0; AD1CHSbits.CH0SA = 0b0000; // ******************************* // 5.AD1CSSL: ADC INPUT SCAN SELECT REGISTER // AD1CSSL[31-16] Unimplemented: Read as e0f // AD1CSSL[15-0] CSSL<15:0>: ADC Input Pin Scan Selection bits(1,2) // 1 = Select ANx for input scan // 0 = Skip ANx for input scan // Note 1: CSSL = ANx, where x = 0-12; CSSL13 selects CTMU input for scan; CSSL14 selects IVREF for scan; // CSSL15 selects VSS for scan. // 2: On devices with less than 13 analog inputs, all CSSLx bits can be selected; however, inputs selected for // scan without a corresponding input on the device will convert to VREFL. // ******************************* AD1CSSLbits.CSSL0 = 0; AD1CSSLbits.CSSL1 = 0; AD1CSSLbits.CSSL2 = 0; AD1CSSLbits.CSSL3 = 0; AD1CSSLbits.CSSL4 = 0; AD1CSSLbits.CSSL5 = 0; AD1CSSLbits.CSSL6 = 0; AD1CSSLbits.CSSL7 = 0; AD1CSSLbits.CSSL8 = 0; AD1CSSLbits.CSSL9 = 0; AD1CSSLbits.CSSL10 = 0; AD1CSSLbits.CSSL11 = 0; AD1CSSLbits.CSSL12 = 0; AD1CSSLbits.CSSL13 = 0; AD1CSSLbits.CSSL14 = 0; AD1CSSLbits.CSSL15 = 0; } //****************************************************************************************** // Function Name : Get_data // Title : Get A/D-Converter-Data // Input : // Output : //****************************************************************************************** void Get_data(unsigned int *adc_data) { AD1CON1bits.SAMP=1; while(!IFS0bits.AD1IF); *adc_data = (unsigned int)ADC1BUF0; } //****************************************************************************************** // Function Name : cnv_data // Title : Convert data format // Input : // Output : //****************************************************************************************** void cnv_data(unsigned char out_data[], unsigned int in_data) { in_data = in_data * 3300 / 1023 ; sprintf(out_data,"%8d",in_data); // strcat(out_data, "[mV] "); } //****************************************************************************************** // Function Name : InitUART1 // Title : Initialized UART1 // Input : // Output : //****************************************************************************************** void InitUART1(void) { // ************************** // (1)Define UxMODE: UARTx MODE REGISTER // UxMODE[31-16] Unimplemented: Read as e0f // UxMODE[15] ON: UARTx Enable bit(1) // 1 = UARTx is enabled. UARTx pins are controlled by UARTx as defined by UEN<1:0> and UTXEN control bits // 0 = UARTx is disabled. All UARTx pins are controlled by corresponding bits in the PORTx, TRISx and LATx // registers; UARTx power consumption is minimal // UxMODE[14] Unimplemented: Read as e0f // UxMODE[13] SIDL: Stop in Idle Mode bit // 1 = Discontinue operation when device enters Idle mode // 0 = Continue operation in Idle mode // UxMODE[12] IREN: IrDA Encoder and Decoder Enable bit // 1 = IrDA is enabled // 0 = IrDA is disabled // UxMODE[11] RTSMD: Mode Selection for UxRTS Pin bit // 1 = UxRTS pin is in Simplex mode // 0 = UxRTS pin is in Flow Control mode // UxMODE[10] Unimplemented: Read as e0f // UxMODE[9-8] UEN<1:0>: UARTx Enable bits // 11 = UxTX, UxRX and UxBCLK pins are enabled and used; UxCTS pin is controlled by corresponding bits // in the PORTx register // 10 = UxTX, UxRX, UxCTS and UxRTS pins are enabled and used // 01 = UxTX, UxRX and UxRTS pins are enabled and used; UxCTS pin is controlled by corresponding bits // in the PORTx register // 00 = UxTX and UxRX pins are enabled and used; UxCTS and UxRTS/UxBCLK pins are controlled by // corresponding bits in the PORTx register // UxMODE[7] WAKE: Enable Wake-up on Start bit Detect During Sleep Mode bit // 1 = Wake-up enabled // 0 = Wake-up disabled // UxMODE[6] LPBACK: UARTx Loopback Mode Select bit // 1 = Loopback mode is enabled // 0 = Loopback mode is disabled // UxMODE[5] ABAUD: Auto-Baud Enable bit // 1 = Enable baud rate measurement on the next character - requires reception of Sync character (0x55); // cleared by hardware upon completion // 0 = Baud rate measurement disabled or completed // UxMODE[4] RXINV: Receive Polarity Inversion bit // 1 = UxRX Idle state is e0f // 0 = UxRX Idle state is e1f // UxMODE[3] BRGH: High Baud Rate Enable bit // 1 = High-Speed mode - 4x baud clock enabled // 0 = Standard Speed mode - 16x baud clock enabled // UxMODE[2-1] PDSEL<1:0>: Parity and Data Selection bits // 11 = 9-bit data, no parity // 10 = 8-bit data, odd parity // 01 = 8-bit data, even parity // 00 = 8-bit data, no parity // UxMODE[0] STSEL: Stop Selection bit // 1 = 2 Stop bits // 0 = 1 Stop bit // ************************** // U1MODEbits.ON = 1; U1MODEbits.ON = 0; U1MODEbits.SIDL = 0; U1MODEbits.IREN = 0; U1MODEbits.RTSMD = 0; U1MODEbits.UEN0 = 0; U1MODEbits.UEN1 = 0; U1MODEbits.WAKE = 0; U1MODEbits.LPBACK = 0; U1MODEbits.ABAUD = 0; U1MODEbits.RXINV = 0; U1MODEbits.BRGH = 0; U1MODEbits.PDSEL1 = 0; U1MODEbits.PDSEL0 = 0; U1MODEbits.STSEL = 0; // ************************** // (2)Define U1BRG // BAUDRATE(Register)=SYSCLK/16/BAUDRATE-1 // ************************** U1BRG = 259; // BAUDRATE = 40MHz/16/9600-1 = 0d259 9600BPS(40MHz) // ************************** // (3)Define UxSTA: UARTx STATUS AND CONTROL REGISTER // UxSTA[31-25] Unimplemented: Read as e0f // UxSTA[24] ADM_EN: Automatic Address Detect Mode Enable bit // 1 = Automatic Address Detect mode is enabled // 0 = Automatic Address Detect mode is disabled // UxSTA[23-16] ADDR<7:0>: Automatic Address Mask bits // When the ADM_EN bit is e1f, this value defines the address character to use for automatic address detection. // UxSTA[15-14] UTXISEL<1:0>: TX Interrupt Mode Selection bits // 11 = Reserved, do not use // 10 = Interrupt is generated and asserted while the transmit buffer is empty // 01 = Interrupt is generated and asserted when all characters have been transmitted // 00 = Interrupt is generated and asserted while the transmit buffer contains at least one empty space // UxSTA[13] UTXINV: Transmit Polarity Inversion bit // If IrDA mode is disabled (i.e., IREN (UxMODE<12>) is e0f): // 1 = UxTX Idle state is e0f // 0 = UxTX Idle state is e1f // If IrDA mode is enabled (i.e., IREN (UxMODE<12>) is e1f): // 1 = IrDA encoded UxTX Idle state is e1f // 0 = IrDA encoded UxTX Idle state is e0f // UxSTA[12] URXEN: Receiver Enable bit // 1 = UARTx receiver is enabled. UxRX pin is controlled by UARTx (if ON = 1) // 0 = UARTx receiver is disabled. UxRX pin is ignored by the UARTx module. UxRX pin is controlled by port. // UxSTA[11] UTXBRK: Transmit Break bit // 1 = Send Break on next transmission. Start bit followed by twelve e0f bits, followed by Stop bit; // cleared by hardware upon completion // 0 = Break transmission is disabled or completed // UxSTA[10] UTXEN: Transmit Enable bit // 1 = UARTx transmitter is enabled. UxTX pin is controlled by UARTx (if ON = 1) // 0 = UARTx transmitter is disabled. Any pending transmission is aborted and buffer is reset. // UxTX pin is controlled by port. // UxSTA[9] UTXBF: Transmit Buffer Full Status bit (read-only) // 1 = Transmit buffer is full // 0 = Transmit buffer is not full, at least one more character can be written // UxSTA[8] TRMT: Transmit Shift Register is Empty bit (read-only) // 1 = Transmit shift register is empty and transmit buffer is empty (the last transmission has completed) // 0 = Transmit shift register is not empty, a transmission is in progress or queued in the transmit buffer // UxSTA[7-6] URXISEL<1:0>: Receive Interrupt Mode Selection bit // 11 = Reserved; do not use // 10 = Interrupt flag bit is asserted while receive buffer is 3/4 or more full (i.e., has 6 or more data characters) // 01 = Interrupt flag bit is asserted while receive buffer is 1/2 or more full (i.e., has 4 or more data characters) // 00 = Interrupt flag bit is asserted while receive buffer is not empty (i.e., has at least 1 data character) // UxSTA[5] ADDEN: Address Character Detect bit (bit 8 of received data = 1) // 1 = Address Detect mode is enabled. If 9-bit mode is not selected, this control bit has no effect // 0 = Address Detect mode is disabled // UxSTA[4] RIDLE: Receiver Idle bit (read-only) // 1 = Receiver is Idle // 0 = Data is being received // UxSTA[3] PERR: Parity Error Status bit (read-only) // 1 = Parity error has been detected for the current character // 0 = Parity error has not been detected // UxSTA[2] FERR: Framing Error Status bit (read-only) // 1 = Framing error has been detected for the current character // 0 = Framing error has not been detected // UxSTA[1] OERR: Receive Buffer Overrun Error Status bit. // This bit is set in hardware and can only be cleared (= 0) in software. // Clearing a previously set OERR bit resets the receiver buffer and RSR to empty state. // 1 = Receive buffer has overflowed // 0 = Receive buffer has not overflowed // UxSTA[0] URXDA: Receive Buffer Data Available bit (read-only) // 1 = Receive buffer has data, at least one more character can be read // 0 = Receive buffer is empty // ************************** U1STAbits.ADM_EN = 0; U1STAbits.UTXISEL1 = 0; U1STAbits.UTXISEL0 = 0; U1STAbits.UTXINV = 0; U1STAbits.URXEN = 0; U1STAbits.UTXBRK = 0; U1STAbits.UTXEN = 0; U1STAbits.URXISEL0 = 0; U1STAbits.URXISEL1 = 0; U1STAbits.ADDEN = 0; U1STAbits.OERR = 0; // ************************** // (4)Start UART // ************************** U1MODEbits.ON = 1; U1STAbits.UTXEN = 1; U1STAbits.URXEN = 1; } //****************************************************************************************** // Function Name : InitUART2 // Title : Initialized UART2 // Input : // Output : //****************************************************************************************** void InitUART2(void) { // ************************** // (1)Define UxMODE: UARTx MODE REGISTER // UxMODE[31-16] Unimplemented: Read as e0f // UxMODE[15] ON: UARTx Enable bit(1) // 1 = UARTx is enabled. UARTx pins are controlled by UARTx as defined by UEN<1:0> and UTXEN control bits // 0 = UARTx is disabled. All UARTx pins are controlled by corresponding bits in the PORTx, TRISx and LATx // registers; UARTx power consumption is minimal // UxMODE[14] Unimplemented: Read as e0f // UxMODE[13] SIDL: Stop in Idle Mode bit // 1 = Discontinue operation when device enters Idle mode // 0 = Continue operation in Idle mode // UxMODE[12] IREN: IrDA Encoder and Decoder Enable bit // 1 = IrDA is enabled // 0 = IrDA is disabled // UxMODE[11] RTSMD: Mode Selection for UxRTS Pin bit // 1 = UxRTS pin is in Simplex mode // 0 = UxRTS pin is in Flow Control mode // UxMODE[10] Unimplemented: Read as e0f // UxMODE[9-8] UEN<1:0>: UARTx Enable bits // 11 = UxTX, UxRX and UxBCLK pins are enabled and used; UxCTS pin is controlled by corresponding bits // in the PORTx register // 10 = UxTX, UxRX, UxCTS and UxRTS pins are enabled and used // 01 = UxTX, UxRX and UxRTS pins are enabled and used; UxCTS pin is controlled by corresponding bits // in the PORTx register // 00 = UxTX and UxRX pins are enabled and used; UxCTS and UxRTS/UxBCLK pins are controlled by // corresponding bits in the PORTx register // UxMODE[7] WAKE: Enable Wake-up on Start bit Detect During Sleep Mode bit // 1 = Wake-up enabled // 0 = Wake-up disabled // UxMODE[6] LPBACK: UARTx Loopback Mode Select bit // 1 = Loopback mode is enabled // 0 = Loopback mode is disabled // UxMODE[5] ABAUD: Auto-Baud Enable bit // 1 = Enable baud rate measurement on the next character - requires reception of Sync character (0x55); // cleared by hardware upon completion // 0 = Baud rate measurement disabled or completed // UxMODE[4] RXINV: Receive Polarity Inversion bit // 1 = UxRX Idle state is e0f // 0 = UxRX Idle state is e1f // UxMODE[3] BRGH: High Baud Rate Enable bit // 1 = High-Speed mode - 4x baud clock enabled // 0 = Standard Speed mode - 16x baud clock enabled // UxMODE[2-1] PDSEL<1:0>: Parity and Data Selection bits // 11 = 9-bit data, no parity // 10 = 8-bit data, odd parity // 01 = 8-bit data, even parity // 00 = 8-bit data, no parity // UxMODE[0] STSEL: Stop Selection bit // 1 = 2 Stop bits // 0 = 1 Stop bit // ************************** // U2MODEbits.ON = 1; U2MODEbits.ON = 0; U2MODEbits.SIDL = 0; U2MODEbits.IREN = 0; U2MODEbits.RTSMD = 0; U2MODEbits.UEN0 = 0; U2MODEbits.UEN1 = 0; U2MODEbits.WAKE = 0; U2MODEbits.LPBACK = 0; U2MODEbits.ABAUD = 0; U2MODEbits.RXINV = 0; U2MODEbits.BRGH = 0; U2MODEbits.PDSEL1 = 0; U2MODEbits.PDSEL0 = 0; U2MODEbits.STSEL = 0; // ************************** // (2)Define U2BRG // BAUDRATE(Register)=SYSCLK/16/BAUDRATE-1 // ************************** U2BRG = 259; // BAUDRATE = 40MHz/16/9600-1 = 0d259 9600BPS(40MHz) // ************************** // (3)Define UxSTA: UARTx STATUS AND CONTROL REGISTER // UxSTA[31-25] Unimplemented: Read as e0f // UxSTA[24] ADM_EN: Automatic Address Detect Mode Enable bit // 1 = Automatic Address Detect mode is enabled // 0 = Automatic Address Detect mode is disabled // UxSTA[23-16] ADDR<7:0>: Automatic Address Mask bits // When the ADM_EN bit is e1f, this value defines the address character to use for automatic address detection. // UxSTA[15-14] UTXISEL<1:0>: TX Interrupt Mode Selection bits // 11 = Reserved, do not use // 10 = Interrupt is generated and asserted while the transmit buffer is empty // 01 = Interrupt is generated and asserted when all characters have been transmitted // 00 = Interrupt is generated and asserted while the transmit buffer contains at least one empty space // UxSTA[13] UTXINV: Transmit Polarity Inversion bit // If IrDA mode is disabled (i.e., IREN (UxMODE<12>) is e0f): // 1 = UxTX Idle state is e0f // 0 = UxTX Idle state is e1f // If IrDA mode is enabled (i.e., IREN (UxMODE<12>) is e1f): // 1 = IrDA encoded UxTX Idle state is e1f // 0 = IrDA encoded UxTX Idle state is e0f // UxSTA[12] URXEN: Receiver Enable bit // 1 = UARTx receiver is enabled. UxRX pin is controlled by UARTx (if ON = 1) // 0 = UARTx receiver is disabled. UxRX pin is ignored by the UARTx module. UxRX pin is controlled by port. // UxSTA[11] UTXBRK: Transmit Break bit // 1 = Send Break on next transmission. Start bit followed by twelve e0f bits, followed by Stop bit; // cleared by hardware upon completion // 0 = Break transmission is disabled or completed // UxSTA[10] UTXEN: Transmit Enable bit // 1 = UARTx transmitter is enabled. UxTX pin is controlled by UARTx (if ON = 1) // 0 = UARTx transmitter is disabled. Any pending transmission is aborted and buffer is reset. // UxTX pin is controlled by port. // UxSTA[9] UTXBF: Transmit Buffer Full Status bit (read-only) // 1 = Transmit buffer is full // 0 = Transmit buffer is not full, at least one more character can be written // UxSTA[8] TRMT: Transmit Shift Register is Empty bit (read-only) // 1 = Transmit shift register is empty and transmit buffer is empty (the last transmission has completed) // 0 = Transmit shift register is not empty, a transmission is in progress or queued in the transmit buffer // UxSTA[7-6] URXISEL<1:0>: Receive Interrupt Mode Selection bit // 11 = Reserved; do not use // 10 = Interrupt flag bit is asserted while receive buffer is 3/4 or more full (i.e., has 6 or more data characters) // 01 = Interrupt flag bit is asserted while receive buffer is 1/2 or more full (i.e., has 4 or more data characters) // 00 = Interrupt flag bit is asserted while receive buffer is not empty (i.e., has at least 1 data character) // UxSTA[5] ADDEN: Address Character Detect bit (bit 8 of received data = 1) // 1 = Address Detect mode is enabled. If 9-bit mode is not selected, this control bit has no effect // 0 = Address Detect mode is disabled // UxSTA[4] RIDLE: Receiver Idle bit (read-only) // 1 = Receiver is Idle // 0 = Data is being received // UxSTA[3] PERR: Parity Error Status bit (read-only) // 1 = Parity error has been detected for the current character // 0 = Parity error has not been detected // UxSTA[2] FERR: Framing Error Status bit (read-only) // 1 = Framing error has been detected for the current character // 0 = Framing error has not been detected // UxSTA[1] OERR: Receive Buffer Overrun Error Status bit. // This bit is set in hardware and can only be cleared (= 0) in software. // Clearing a previously set OERR bit resets the receiver buffer and RSR to empty state. // 1 = Receive buffer has overflowed // 0 = Receive buffer has not overflowed // UxSTA[0] URXDA: Receive Buffer Data Available bit (read-only) // 1 = Receive buffer has data, at least one more character can be read // 0 = Receive buffer is empty // ************************** U2STAbits.ADM_EN = 0; U2STAbits.UTXISEL1 = 0; U2STAbits.UTXISEL0 = 0; U2STAbits.UTXINV = 0; U2STAbits.URXEN = 0; U2STAbits.UTXBRK = 0; U2STAbits.UTXEN = 0; U2STAbits.URXISEL0 = 0; U2STAbits.URXISEL1 = 0; U2STAbits.ADDEN = 0; U2STAbits.OERR = 0; // ************************** // (4)Start UART // ************************** U2MODEbits.ON = 1; U2STAbits.UTXEN = 1; U2STAbits.URXEN = 1; } //****************************************************************************************** // Function Name : SendUART1Str // Title : Send UART1 Strings // Input : // Output : //****************************************************************************************** void SendUART1Str(unsigned char in_s[]) { unsigned char lp = 0; while(in_s[lp] != 0){ SendUART1(in_s[lp]); lp++; } } //****************************************************************************************** // Function Name : SendUART2Str // Title : Send UART2 Strings // Input : // Output : //****************************************************************************************** void SendUART2Str(unsigned char in_s[]) { unsigned char lp = 0; while(in_s[lp] != 0){ SendUART2(in_s[lp]); lp++; } } //****************************************************************************************** // Function Name : SendUART1 // Title : Send UART1 // Input : // Output : //****************************************************************************************** void SendUART1(unsigned char in_c) { while(U1STAbits.UTXBF != 0); U1TXREG = in_c; } //****************************************************************************************** // Function Name : SendUART2 // Title : Send UART2 // Input : // Output : //****************************************************************************************** void SendUART2(unsigned char in_c) { while(U2STAbits.UTXBF != 0); U2TXREG = in_c; } //****************************************************************************************** // Function Name : RecUART1 // Title : Recive UART1 // Input : // Output : //****************************************************************************************** void RecUART1(unsigned char *out_c) { while(U1STAbits.URXDA == 0); *out_c = U1RXREG; } //****************************************************************************************** // Function Name : RecUART2 // Title : Recive UART2 // Input : // Output : //****************************************************************************************** void RecUART2(unsigned char *out_c) { while(U2STAbits.URXDA == 0); *out_c = U2RXREG; } //****************************************************************************************** // Function Name : wat01 // Title : // Input : 0.1[S] * n // Output : //****************************************************************************************** void wat01(unsigned int no) { unsigned int lp; for(lp=0;lp