From: Jean-Paul <tch...@fr...> - 2005-08-30 20:05:23
|
Hi, PCF8574 come in two flavors with two different fixed addresses PCF85474 =3D 0 1 0 0 A2 A1 A0 RW =3D 0x40 PCF8574A =3D 0 1 1 1 A2 A1 A0 RW =3D 0x70 It looks like you are using a PCF8574 with the address of a PCF85474_A_ and pins A2=3D0 A1=3D1 A0=3D1 Check and try again On Tue, 30 Aug 2005 10:57:35 -0700 (PDT), Vikram Tamboli =20 <vik...@ya...> wrote: > Hi > > Actually I am trying to interface an 16*4 LCD to > AT89c51 using Pcf8574-I2C-io chip. > > But have problem in making simple io to work. > > I tried the following code > #include <stdio.h> > #include <AT89X51.H> > #include <intrins.h> > > #define CPUCLKHZ 11059200 > #define DELAY_CPU_CLOCK CPUCLKHZ / 12 // 8051 > clock rate (X1 mode) > #define DELAY_CONST 9.114584e-5 // Delay > routine constant > #define DELAY_MULTPLR (unsigned char)(DELAY_CONST * > DELAY_CPU_CLOCK) > > void delay_ms(volatile unsigned int count) > { > for(count *=3D DELAY_MULTPLR; count > 0; count--); > } > //#define SCL P3_6 // I2C Clock > //#define SDA P3_5 // I2C Data > #define SDA P2_0 // I2C Clock > #define SCL P2_1 // I2C Data > > #define nop _nop_ () > > #define XIO_ID 0x76 // Device identifier of > the I/O-Expander (PCF8574) > //#define XIO_ID_LCD 0x40 > //#define XIO_ID_OUTPUT 0x41 > //#define XIO_ID_INPUT 0x42 > //#define XIO_ID_EXTRA 0x43 > #define READ 0x01 // Alias for read > command bit > #define WRITE 0x00 // Alias for write > command bit > > void i2c_delay(void) > { > EA =3D 0; // Disable all interrupts for a accurate > timing > // Valid for fq =3D 24MHz -> Tcycle =3D 500ns -> 10 x > nop > nop; > nop; > nop; > nop; > nop; > nop; > nop; > nop; > nop; > nop; > EA =3D 1; // Enable all interrupts > return; > } > > void i2c_start(void) > { > EA =3D 0; // Disable all interrupts for a > accurate timing > // Force defined state of SCL and SDA > SCL =3D 1; // Release SCL > SDA =3D 1; // Release SDA > i2c_delay(); // Force 5=B5s-delay > // Generate START condition > SDA =3D 0; > i2c_delay(); // Force 5=B5s-delay > SCL =3D 0; > EA =3D 1; // Enable all interrupts > // I2C Busy > return; > } > > void i2c_stop(void) > { > EA =3D 0; // Disable all interrupts for a > accurate timing > // Generate STOP condition > SDA =3D 0; > i2c_delay(); // Force 5=B5s-delay > SCL =3D 1; // Release SCL > i2c_delay(); // Force 5=B5s-delay > SDA =3D 1; // Release SDA > EA =3D 1; // Enable all interrupts > // I2C Idle > return; > } > > void i2c_ack(void) > { > EA =3D 0; // Disable all interrupts for a > accurate timing > // Generate ACK > SDA =3D 0; > SCL =3D 1; // Release SCL > while(SCL =3D=3D 0); // Synchronize clock > i2c_delay(); // Force 5=B5s-delay > SCL =3D 0; // Force a clock cycle > EA =3D 1; // Enable all interrupts > return; > } > > void i2c_nack(void) > { > EA =3D 0; // Disable all interrupts for a > accurate timing > // Generate NACK > SDA =3D 1; // Release SDA > SCL =3D 1; // Release SCL > while(SCL =3D=3D 0); // Synchronize clock > i2c_delay(); // Force 5=B5s-delay > SCL =3D 0; // Force a clock cycle > EA =3D 1; // Enable all interrupts > return; > } > > unsigned char i2c_check(void) > { > EA =3D 0; // Disable all interrupts for a > accurate timing > SDA =3D 1; // Release SDA > SCL =3D 1; // Release SCL > while(SCL =3D=3D 0); // Synchronize clock > i2c_delay(); // Force 5=B5s-delay > if (SDA) // SDA is high > { > SCL =3D 0; // Force a clock cycle > EA =3D 1; // Enable all interrupts > return(1); // No acknowledge from Slave > } > SCL =3D 0; // Force a clock cycle > EA =3D 1; // Enable all interrupts > return(0); // Acknowledge from Slave > } > > void errormsg(unsigned char ascii) > { > putchar('=A7'); > putchar(ascii); > puts("-error occured !"); > putchar(0x0A); > putchar(0x0D); > putchar(0x07); > > return; > } > > > unsigned char i2c_write(unsigned char value) > { > idata unsigned char counter =3D 0; // Bitcounter > EA =3D 0; // Disable all > interrupts for a accurate timing > for ( counter =3D 0; counter < 8; counter++ ) > { > SDA =3D (bit)((value&0x80) >> 7); // Set SDA to value > of MSB > SCL =3D 1; // Release SCL > while(SCL =3D=3D 0); // Synchronize clock > i2c_delay(); // Force 5=B5s-delay > SCL =3D 0; // Force a clock > cycle > value <<=3D 1; // Prepare next bit > for transmission > } > EA =3D 1; // Enable all > interrupts > // Generate a 9. clock cycle and check ACK from SLAVE > // Return the result > return(i2c_check()); > } > > unsigned char i2c_read(void) > { > idata unsigned char result =3D 0; // Returnvalue > with read I2C byte > idata unsigned char counter =3D 0; // Bitcounter > EA =3D 0; // Disable all interrupts > for a accurate timing > for ( counter =3D 0; counter < 8; counter++ ) > { > SDA =3D 1; // Release SDA > SCL =3D 1; // Release SCL > while(SCL =3D=3D 0); // Synchronize clock > i2c_delay(); // Force 5=B5s-delay > result <<=3D 1; // Shift left the result > if (SDA) > result |=3D 0x01; // Set actual SDA state to LSB > SCL =3D 0; // Force a clock cycle > i2c_delay(); // Force 5=B5s-delay > } > EA =3D 1; // Enable all interrupts > // ACK or NACK from MASTER must be generated outside > this routine > return(result); > } > > void XIO_write(unsigned char content) > { > > i2c_start(); // Generate START condition > > // Send SLAVE adress with write request > if ( i2c_write(XIO_ID|WRITE) ) > { > errormsg('K'); > return; > } > > // Send content byte > if ( i2c_write(content) ) > { > errormsg('L'); > return; > } > > i2c_stop(); // Generate STOP condition > > return; > } > > unsigned char XIO_read(void) > { > unsigned char result; > > i2c_start(); // Generate START condition > > // Send SLAVE adress with write request > if ( i2c_write(XIO_ID|WRITE) ) > { > errormsg('K'); > return(0); > } > > // Set all pins high to be used as inputs > if ( i2c_write(0xFF) ) > { > errormsg('L'); > return(0); > } > > i2c_stop(); // Generate STOP condition > > i2c_start(); // Generate START condition > > // Send SLAVE adress with read request > if ( i2c_write(XIO_ID|READ) ) > { > errormsg('M'); > return(0); > } > > // Read port > result =3D i2c_read(); > > i2c_stop(); // Generate STOP condition > > return(result); > } > > > void main () > { > SCON =3D 0x52; > TMOD |=3D 0x20; > PCON |=3D 0x80; > TH1 =3D TL1 =3D 250; > TR1 =3D 1; > while(1) > { > delay_ms(90000); > delay_ms(90000); > delay_ms(90000); > XIO_write(0x00); > XIO_write(0x00); > delay_ms(90000); > delay_ms(90000); > delay_ms(90000); > XIO_write(0xFF); > XIO_write(0xFF); > P0 =3D ~P0; > P1 =3D ~P1; > P2 =3D ~P2; > puts("Hello World"); > } > } > but was getting error K error ocured. > dono where i am wrong. > > > Thanks in advance > > > Vikram > > > --- Gernot Fink <g....@gm...> wrote: > >> On Wed, 03 Aug 2005, you wrote: >> > hi all >> > >> > can any one provide the initialize details for >> 16*4 >> > LCD >> > I have connected the Lcd to port 2 in 4bit mode >> > >> > P2.0 <----> D4 >> > P2.1 <----> D5 >> > P2.2 <----> D6 >> > P2.3 <----> D7 >> > P2.4 <----> EN >> > P2.5 <----> A0 (0 means command, 1 means >> data) >> > >> >> Hallo, >> >> the delays are calculated for 9.216 MHZ. >> There are 2 delays, a long for cmds<4 and a short. >> This programm is so small and dirty to fit in a >> at89c2051 (on port1) >> >> #define lcd_putc(i) lcd_out(i) >> bit lcddata; >> >> void lcd_out(unsigned char c) >> { >> >> P2&=3D0x80; >> P2_5=3Dlcddata; >> P2|=3D(c>>4); >> P2_4=3D1; >> P2_4=3D0; >> P2&=3D0xc0; >> P2|=3Dc&15; >> P2_4=3D1; >> P2_4=3D0; >> >> P2_5=3D0; >> P2|=3D0x1f; >> >> if(lcddata=3D=3D0&&c<4)_asm >> mov a,#225 >> 00001$: >> nop >> nop >> nop >> nop >> dec a >> jnz 00001$ >> _endasm; >> _asm >> mov a,#12 >> 00100$: >> dec ACC >> jnz 00100$ >> _endasm; >> } >> >> code unsigned char >> lcdinitstring[]=3D{0x28,0x28,12,6,1,0}; >> void lcd_cmd(unsigned char c) >> { >> lcddata=3D0; >> lcd_out(c); >> lcddata=3D1; >> } >> void lcd_init(void) >> { >> lcddata=3D0; >> lcd_putc(0x28); >> lcd_puts(lcdinitstring); >> lcddata=3D1; >> } >> void lcd_puts(unsigned char code *c) >> { >> while (*c) >> { >> lcd_putc(*(c++)); >> } >> } >> void lcd_goto(unsigned char c) >> { >> lcddata=3D0; >> lcd_out(c+128); >> lcddata=3D1; >> } >> >> >> >> -- >> MFG Gernot >> >> >> > ------------------------------------------------------- >> SF.Net email is Sponsored by the Better Software >> Conference & EXPO >> September 19-22, 2005 * San Francisco, CA * >> Development Lifecycle Practices >> Agile & Plan-Driven Development * Managing Projects >> & Teams * Testing & QA >> Security * Process Improvement & Measurement * >> http://www.sqe.com/bsce5sf >> _______________________________________________ >> Sdcc-user mailing list >> Sdc...@li... >> > https://lists.sourceforge.net/lists/listinfo/sdcc-user >> > > > > =09 > ____________________________________________________ > Start your day with Yahoo! - make it your home page > http://www.yahoo.com/r/hs > > > ------------------------------------------------------- > SF.Net email is Sponsored by the Better Software Conference & EXPO > September 19-22, 2005 * San Francisco, CA * Development Lifecycle =20 > Practices > Agile & Plan-Driven Development * Managing Projects & Teams * Testing &= =20 > QA > Security * Process Improvement & Measurement * http://www.sqe.com/bsce5= sf > _______________________________________________ > Sdcc-user mailing list > Sdc...@li... > https://lists.sourceforge.net/lists/listinfo/sdcc-user > --=20 NEVER jump into a LOOP! |