//**************************************************************** // SLAVE_PIC_UART_and_LCD(CPU: PIC24FJ64GA002) // // UART : 8 bit, non parity, 1 stop bit // Used U1ATX, U1ARX // // LCD unlook busy : not switch direction (write fixed) // // Condition: // 8MHz Internal RC oscillator, 4x PLL (8MHzx4= 32MHz) // Fcy=32MHz/2=16MHz, Tcy=62.5ns // // 2012/1/1: N.Ishii //**************************************************************** #include #include "uart.h" #include "My_LCD_Lib.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 ) //----------------------------------------------------------------------------------- #define CR 0x0d #define LF 0x0a #define STX 0x02 //------------------------------------------------------------------ /// message table char msg1[] = {"First Line\0"}; char msg2[] = {"Second Line\0"}; //------------------------------------------------------------------ int n = 0,i=0; char RX1[17],RX2[17]; char RX1_LCD[17],RX2_LCD[17]; int cmd; int LineMode = 1; //------------------------------------------------------------------------ void LCD() { cmd = ReadUART1(); switch(cmd) { case CR: LineMode = 1; n = 0; // Reset Char Send Counter break; case LF: LineMode = 2; n = 0; break; case STX: // Transmit Buffer for(i = 0; i <= 15; i++) { RX1_LCD[i] = RX1[i]; RX2_LCD[i] = RX2[i]; } RX1_LCD[16] = '\0'; RX2_LCD[16] = '\0'; // LCD Disp. lcd_inst_wr(DISP_CLEAR_CURSOR_HOME); lcd_chr_wr(RX1_LCD); lcd_inst_wr(0xc0); lcd_chr_wr(RX2_LCD); break; default: switch(LineMode) { case 1: RX1[n] = cmd; break; case 2: RX2[n] = cmd; break; default: break; } n++; break; } } //----------------------------------------------------------------------------------------------------- /// Main routine int main(void) { // CPU Clock Pre Scalere 1:1 CLKDIV = 0; // Set AD1PCFG: ANx Port is All Digital Pin AD1PCFG = 0xFFFF; // Set Port Direction TRISAbits.TRISA3 = 0; // RA3 is RED_LED output used debug LATAbits.LATA3 = 1; // RED_LED OFF // Initialize LCD lcd_init(); lcd_inst_wr(DISP_ON_CURSOR_OFF_BLINK_OFF); //------< 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' //----------------------------------------------------------------------------- // Initiale LCD Disp. lcd_chr_wr(msg1); // "First Line" lcd_inst_wr(0xc0); lcd_chr_wr(msg2); // "Second Line" while(1) { while(!DataRdyUART1()); // Wait Not Rx Ready LCD(); } return 0; }