//**************************************************************** // RS232C_CODE_GENERETOR // // Condition: // 8MHz Internal RC oscillator, 4x PLL (8MHzx4= 32MHz) // Fcy=32MHz/2=16MHz, Tcy=62.5ns // // CPU: PIC24FJ64GA002 // // 2012/4/23: N.Ishii //**************************************************************** #include #include "uart.h" /// Set Configuration Word 1 _CONFIG1 ( JTAGEN_OFF & // JTAG Port: OFF GCP_OFF & // Code Protect:@OFF GWRP_OFF & // Write ProtectFOFF BKBUG_OFF & // Background Debug: OFF COE_OFF & // Clip On Emulation: OFF ICS_PGx1 & // Select ICD Pin: EMUC/EMUD-> PGC1/PGD1 For Common Use FWDTEN_OFF // WDT: OFF ) /// Set Configuration Word 2 _CONFIG2 ( IESO_OFF & // 2 Speed Start Up: OFF FNOSC_FRCPLL & // 8MHz Internal RC oscillator, 4x PLL-> 8MHzx4=32MHz // FNOSC_FRC & // Non PLL: 8MHz(Fcy=4MHz) FCKSM_CSDCMD & // Change Clock Control: OFF Clock Monitor: OFF OSCIOFNC_ON & // OSCO/RA3 function: Used RA3 IOL1WAY_OFF & // RP Register Protection: Unlimited Writes To RP Registers I2C1SEL_PRI & // I2C1 pins Select: Use Primary I2C1 pins POSCMOD_NONE // Oscillator Selection: Primary disabled ) //------------------------------------------------------------------------------------- /// Wait Vol Table unsigned int wait_vol[] = {2000,2000,2000,2000,2000,2000,2500,9500, 1500,1500,2500,5500,4500,8500,7500,6500, 2500,2500,5500,4500,4500,9500,5500,5500, 4500,2500,2500,3500,2500,2500,2500 }; //------------------------------------------------------------------------------------ char TX_ON_flag = 0; unsigned char CODE; //----------------------------------------------------------------------------------- // Delay Subrutin void Waitx1ms(int x) // Td = 1mS * x { int i,j; for(i = 0 ; i < x ; ++i) { for(j = 0 ; j < 2620 ; ++j) asm("clrwdt"); //Td=1mS } } void Waitx1us(int x) // Td = 1uS * x { int i; for(i = 0 ; i < x ; ++i) { /// Td=1000nS(125nS x 8) asm("clrwdt"); //Td=125nS(62.5nS x 2) asm("clrwdt"); asm("clrwdt"); asm("clrwdt"); asm("clrwdt"); asm("clrwdt"); asm("clrwdt"); asm("clrwdt"); asm("clrwdt"); asm("clrwdt"); } } //----------------------------------------------------------------------------------------------------------- /// Switch Change Notification Interrupts void __attribute__((interrupt, no_auto_psv)) _CNInterrupt(void) { Waitx1ms(1); if (!PORTBbits.RB0) { // TX_START_SW ON ? TX_ON_flag = 1; } IFS1bits.CNIF = 0; // Clead CNIF Flag } //---------------------------------------------------------------------------------------------------------------- /// Main routine int main(void) { unsigned char i; // CPU Clock Pre Scalere 1:1 CLKDIV = 0; // Set AD1PCFG: ANx Port is All Digital Pin AD1PCFG = 0xFFFF; /// Set LED & SW Port Direction TRISAbits.TRISA3 = 0; // RA3 is RED_LED output TRISAbits.TRISA4 = 0; // RA4 is GREEN_LED output TRISB = 0xFFFF; // RB0(TX_START_SW),RB1(SW2), RB8-RB15(DIP_SW_B0-B7) is input // Set Pull Up CNPU1 = 0xF830; // Pull Up Port is RB0(SW1), RB1(SW2), RB11-RB15(DIP_SW_B2-B7) CNPU2 = 0x0061; // Pull Up Port is RB8-RB10(DIP_SW_B0-B1) /// Set Switch Change Notification Interrupts CNEN1 = 0x0030; // Valid TX_START_SW, SW2 // IPC4bits.CNIP = 6; // The Order of Priority: CN INT=6, (T1 INT=5) IEC1bits.CNIE = 1; // Enable CN INT //------< Initialize RS232C>-------------------------------------------------- /// Set RX Input Port TRISBbits.TRISB3 = 1; // RB3 is RX input /// UART1 Pin Mapping RPINR18bits.U1RXR = 3; // UART1 RX to RP3 RPOR1bits.RP2R = 3; // UART1 TX to RP2(Function No=3 -> U1TX) /// Initialize UART1:@9600bps, 8Bit, Non Parity, Non Control Flow U1BRG = 103; // 9600bps@Fcy=16MHz:Value of U1BRG= (16M/16x9600)-1 // U1BRG = 51; // 19200bps@Fcy=16MHz:Value of U1BRG= (16M/16x19200)-1 U1MODE = 0b1000100000000000; // UART1 Mode Set: UARTEN,RTSMD= '1' Other Bit= '0' U1STA = 0b0000010000000000; // UART1 Status Set:UTXEN= '1' Other Bit= '0' //----------------------------------------------------------------------------- // Initialize LED LATAbits.LATA3 = 1; // RED_LED OFF LATAbits.LATA4 = 1; // GREEN_LED OFF //------------------------------------------------------------------------------------- /// main loop while(1) { if (TX_ON_flag) { CODE = (unsigned char)((~PORTB & 0xFF00) >> 8); // Read 8BIT_DIP_SW if (CODE != 0xFF) { putcUART1(CODE); // Set Code and TX UART LATAbits.LATA3 = 0; // RED_LED ON Waitx1ms(100); } else { // Demo LATAbits.LATA3 = 0; // RED_LED ON for (i = 1; i <= 31; ++i) { putcUART1(i); Waitx1ms(wait_vol[i-1]); } putcUART1(0x6F); // 'o'cmmand: 8va putcUART1(12); // Standerd Chime Sound Waitx1ms(5500); putcUART1(0x4F); // 'O'cmmand: Retern to normal putcUART1(12); // Standerd Chime Sound Waitx1ms(5500); } TX_ON_flag = 0; } else LATAbits.LATA3 = 1; // RED_LED OFF } }