/***************************************************** * I2C_LCD_TEST * * Used PIC24FJ64GA002 I2C2 Module * Condition: * 8MHz Internal RC oscillator, 4x PLL (8MHzx4= 32MHz) * Fcy=32MHz/2=16MHz, Tcy=62.5ns * * 2012/10/13: N.Ishii ******************************************************/ #include // 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 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 POSCMOD_NONE // Oscillator Selection: Primary disabled ) //----------------------------------------------------------------------------------- /// Message Table unsigned char Msg[17] = "Counter = xxxxx "; //----------------------------------------------------------------------------------- /// Prototype void itostring(char digit, unsigned int data, char *buffer); void lcd_init(void); void lcd_cmd(unsigned char cmd); void lcd_str(char* ptr); void lcd_icon(unsigned char num, unsigned char onoff); void Waitx1ms(int x); //----------------------------------------------------------------------------------- /// Main routine int main(void) { unsigned int Count, icon; unsigned char Flag; // 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 TRISAbits.TRISA4 = 0; // RA4 is GREEN_LED output TRISBbits.TRISB2 = 1; // RB2 is SDA2 (Hi) TRISBbits.TRISB3 = 1; // RB3 is SCL2 (Hi) LATAbits.LATA3 = 1; // RED LED OFF LATAbits.LATA4 = 1; // GREEN LED OFF // Initialize I2C2 I2C2BRG = 0x9E; // 100kHz I2C2CON = 0x8000; // Enable I2C2 // Initialize I2C_LCD lcd_init(); icon = 0; Flag = 1; Count = 0; /**** Main Loop *****/ while(1) { LATAbits.LATA4 = ~LATAbits.LATA4; // ALT GREEN LED lcd_cmd(0x80); // Cursor Home (First Line) lcd_str("Hello! LCD Test!"); itostring(5, Count++, Msg+10); lcd_cmd(0xC0); // Cursor Home (Secand Line) lcd_str(Msg); lcd_icon(icon, Flag); icon++; if(icon >= 14){ icon = 0; Flag ^= 1; } Waitx1ms(500); } } /*************************************** * Convert Integer -> ASCII ****************************************/ void itostring(char digit, unsigned int data, char *buffer) { char i; buffer += digit; // To Last of Strings for(i=digit; i>0; i--) { // From LS. Digit To MS. Digit buffer--; *buffer = (data % 10) + '0'; // This Digit Value-> Ascii Convert ('0'=0x30)-> Store Buffer data = data / 10; // Digit-1 } }