Hello, I am unable to interface the 24C16 in my 8051 board. Can anyone give me the code (in 'C')required for interfacing the 24C16 using any two port lines. Thanks, Mohit
Hello! I have write a program to write and read to/from AT24c04 serial ROM (89C52 interface with AT24C04). I am using Keil C to program. But i can read any data which i have write to AT24c04. I don't know exacterly why. My purpose is very simple. I listent from USART and get data from PC key board. If the character which i get from pc is 'w' or 'W' then i do get data from PC and write to AT24c04 until i get the 0x0D. Now if i get the 'r' or 'R' character from PC i read data which i have just write to AT24c04 and send back to PC. But i can't read any. Here is my code #include <REG52.h> unsigned char data DEVICEADD; sbit SDA = P1^0; sbit SCL = P1^1; int data c; char data wrom; char data rrom; void outchar(char data chr) { while(!TI) { } TI = 0; SBUF = chr; } void outstr(char* str) { while(*str != 0) { outchar(*str++); } } char i2c_start(void) { /* if(!SDA || !SCL) return 0; // i2c_start error: bus not free */ // Both bus lines are high, SDA = 0; // so set data line low to generate a start condition. SCL = 0; /* SDA = 1; SCL = 1; SDA = 0; SCL = 0; */ return 1; } void i2c_stop(void) { /* SDA = 0; SCL = 1; SDA = 1; */ unsigned char input_var; SCL = 1; SDA = 0; SCL = 1; SDA = 1; input_var = SDA; } // return ack value: 0 for fale; 1 for success char i2c_write(char data byte) { int data i; for(i = 0; i < 8 ; i++) { SDA = ((byte & 0x80) ? 1 : 0); SCL = 1; // Ensure that the data is stable during the high period. byte <<= 1; SCL = 0; } // Get ack SDA = 1; SCL = 1; i = SDA; //i = 500; SCL = 0; /* while(i-- && SDA) { } */ return !i; // Get data at end of cycle. } char i2c_writebyte(int data address, char data byte) { if(!i2c_start()) { outstr("i2c_start error: bus not free.\n"); return 0; } if(!i2c_write(DEVICEADD)) { i2c_stop(); i2c_start(); if(!i2c_write(DEVICEADD)) // Interface failed to acknowledge device address { i2c_stop(); outstr("Interface failed to acknowledge device address.\n"); return 0; } } if(!i2c_write(address >>8)) // Interface failed to acknowledge byteH address { i2c_stop(); outstr("Interface failed to acknowledge byteH address.\n"); return 0; } if(!i2c_write(address)) // Interface failed to acknowledge byteL address { i2c_stop(); outstr("Interface failed to acknowledge byteL address.\n"); return 0; } if(!i2c_write(byte)) // Interface failed to acknowledge data { i2c_stop(); outstr("Interface failed to acknowledge data.\n"); return 0; } i2c_stop(); return 1; // success } char i2c_read(char data ACK) { char index, byte; index = SDA; // Put data pin into read mode byte = 0x00; for(index = 0; index < 8; index++) // Send 8 bits to the I2C Bus { byte <<= 1; SCL = 1; // Clock the data into the I2C Bus byte |= SDA; // Input the data from the I2C Bus SCL = 0; } // Write ack bit SDA = ACK; SCL = 1; // Ensure that the data is stable during the high period. SCL = 0; return byte; } char i2c_readbyte(int data address) { char data byte; if(!i2c_start()) { outstr("i2c_start error: bus not free.\n"); return 0; } if(!i2c_write(DEVICEADD)) { i2c_stop(); i2c_start(); if(!i2c_write(DEVICEADD)) // Interface failed to acknowledge device address { i2c_stop(); outstr("Interface failed to acknowledge device address.\n"); return 0; } } // Transmit address to read from. if(!i2c_write(address >> 8)) // Interface failed to acknowledge byteH address { i2c_stop(); outstr("Interface failed to acknowledge byteH address. \n"); return 0; } if(!i2c_write(address)) // Interface failed to acknowledge byteL address { i2c_stop(); outstr("Interface failed to acknowledge byteL address. \n"); return 0; } // Now terminate write operation and do a read. if(!i2c_start()) { outstr("i2c_start error: bus not free.\n"); return 0; } // Transmit address to read from. if(!i2c_write(DEVICEADD | 1)) // Slave failed to ack read access { i2c_stop(); outstr("Slave failed to ack read access.\n"); return 0; } // Read from address and ack = 1. byte = i2c_read(1); i2c_stop(); return byte; } char get_char() { while(!RI) { } RI = 0; return SBUF; } void WriteData() { char data ch; ch = get_char(); while(ch != 0x0D) { i2c_writebyte(c++,ch); outchar(ch); ch = get_char(); } wrom = 0; ES = 1; } void ReadData() { char data index; char data input; for(index = 0; index < c; index++) { input = i2c_readbyte(index); if(input != 0) outchar(input); } c = 0; rrom = 0; ES = 1; } void main(void) { // Init serial SCON = 0x52; // Set timer1 mode and reload value TMOD &= ~0xF0; // clear timer 1 mode bits TMOD |= 0x20; // put timer 1 into MODE 2 8 bit auto reload TH1 = 0xFD; TR1 = 1; // start timer 1 TI = 0; ES = 1; // Enable Serial interrupt EA = 1; // Enable all interrupts c = 0; rrom = 0; wrom = 0; DEVICEADD = 0xA0; // 10100000B while(1) { if(wrom == 1) { outchar('W'); WriteData(); } if(rrom == 1) { outchar('R'); ReadData(); } /* count = get_char(); outchar(count); */ } } void serial (void) interrupt 4 using 3 { char data inp; if(RI) // Received data interrupt { RI = 0; inp = (char)SBUF; if((inp == 'w') || (inp == 'W')) { ES = 0; rrom = 0; c = 0; TI = 1; wrom = 1; } else if((inp == 'r') || (inp == 'R')) { ES = 0; wrom = 0; TI = 1; rrom = 1; } } else if(TI) // Transmitted data interrupt { TI = 0; } } Could you help me ?
I have just suceeded in connecting to a M24C08 from an Infineon C167. Two things.. 1 Your code shows two address bytes to the chip, the doc only shows one. 2 You must wait 5ms between a write and a read. the code that worked for me was.. fnI2C_SendStart(); fnI2C_SendByte(0xA0); //device address write mode fnI2C_SendByte(0); //lo address fnI2C_SendByte(45); //a number fnI2C_SendStop(); //wait for data line to go low //this is awful i know for (i=0;i<80;i++) // your 5ms delay here { fnDelay(127); } //read it from flash //random access read according to st fnI2C_SendStart(); fnI2C_SendByte(0xA0); //device address dummy write mode fnI2C_SendByte(0); fnI2C_SendStart(); fnI2C_SendByte(0xA1); // read i = fnI2C_ReceiveByteNA(); // fnI2C_SendStop(); ..........
Could you give me, your full source code, which you had test sucess. Thanks.
This is all the code for C167 to 24C08. You will need to sort out your own 'printf' statement. #define _MAIN_ /*********************************************************************/ /*********************************************************************/ #include <reg167.h> #include <stdio.h> #include <string.h> //I2C values #define INPUT DP2 = DP2 & 0xBFFF; //bit 14 of P2 used for I2C data line #define OUTPUT DP2 = DP2 | 0xC000; // bit 14 is ourput , lets do bit 15 here as well! #define SETCLOCK P2 = P2 | 0x8000; //bit 15 high #define CLRCLOCK P2 = P2 & 0x7FFF; //bit 15 low #define SETDATA P2 = P2 | 0x4000; //bit 14 high #define CLRDATA P2 = P2 & 0xBFFF; //bit 14 low #define BOTHHIGH P2 = P2 | 0xC000; //bits 14 and 15 high #define BOTHLOW P2 = P2 & 0x3FFF; #define RD 0x01; #define WR 0x00; /* global */ unsigned char delayfull; unsigned char delayhalf; unsigned char onebit[16]; unsigned char rx; /*** Executable Functions ***/ void fnDelay(unsigned char mydelay) { unsigned char uchCounter; for(uchCounter = 0; uchCounter < mydelay; uchCounter ++); //need to play with this } void fnI2C_SendStart() { OUTPUT SETDATA SETCLOCK fnDelay(delayhalf); CLRDATA fnDelay(delayhalf); CLRCLOCK fnDelay(delayhalf); } void fnI2C_SendStop() { OUTPUT BOTHLOW SETCLOCK fnDelay(delayhalf); SETDATA fnDelay(delayhalf); CLRCLOCK fnDelay(delayhalf); } //receive byte function /* Read Byte */ unsigned char fnI2C_ReceiveByte() { unsigned char uchBitCounter; unsigned char uchBitSelection; INPUT uchBitSelection=0x00; for(uchBitCounter=0;uchBitCounter<8;uchBitCounter++) { SETCLOCK fnDelay(delayfull); if((P2 & 0x4000) > 0) //looking for data on bit 14 uchBitSelection|=0x01; if(uchBitCounter<7) uchBitSelection<<=1; fnDelay(delayfull); CLRCLOCK fnDelay(delayfull); } OUTPUT CLRDATA fnDelay(delayfull); SETCLOCK fnDelay(delayfull); CLRCLOCK INPUT return uchBitSelection; } unsigned char fnI2C_ReceiveByteNA() { unsigned char uchBitCounter; unsigned char uchBitSelection; INPUT uchBitSelection=0x00; for(uchBitCounter=0;uchBitCounter<8;uchBitCounter++) { SETCLOCK fnDelay(delayfull); if((P2 & 0x4000) > 0) // is bit 14 held high then the data is a 1 uchBitSelection|=0x01; if(uchBitCounter<7) uchBitSelection<<=1; fnDelay(delayfull); CLRCLOCK fnDelay(delayfull); } SETCLOCK fnDelay(delayfull); CLRCLOCK return uchBitSelection; } /* Send Byte */ unsigned char fnI2C_SendByte(unsigned char uchByte) { unsigned char uchBitCounter; unsigned char uchBitSelection; OUTPUT uchBitSelection = 0x80; for(uchBitCounter = 0; uchBitCounter <8; uchBitCounter ++) { if(uchByte & uchBitSelection) SETDATA else CLRDATA uchBitSelection >>= 1; SETCLOCK fnDelay(delayfull); CLRCLOCK fnDelay(delayfull); CLRDATA } CLRDATA //august INPUT SETCLOCK fnDelay(delayfull); // should check if acknowledge is received uchBitSelection = _PTA.Byte; CLRCLOCK uchBitSelection ^= 0xFF; uchBitSelection &= 0x08; //20 SHOULD BE 08 fnDelay(delayfull); //return uchBitSelection; } /* Function Definitions */ // Main Function Entered From Reset void main (void) { unsigned char i; long x; delayfull = 8;//play with these to get the i2c repetition rate delayhalf = 4; DP2 = 0xFF00; P2 = P2 & 0x00FF; //clear top 8 bits IEN = 1; /* Enable Interrupts (for monitor) */ //write one byte to flash fnI2C_SendStart(); fnI2C_SendByte(0xA0); //device address write mode fnI2C_SendByte(0); //lo address fnI2C_SendByte(45); //a number fnI2C_SendStop(); //nasty - should check for something in the chip //wait for data line to go low for (i=0;i<80;i++) //worked at 100 and 80 and 60, failed at 40 { fnDelay(127); } //read it from flash //random access read according to st fnI2C_SendStart(); fnI2C_SendByte(0xA0); //device address write mode fnI2C_SendByte(0); fnI2C_SendStart(); fnI2C_SendByte(0xA1); // read i = fnI2C_ReceiveByteNA(); //NA?? fnI2C_SendStop(); printf("%d",i); /* Loop forever */ while(1) { } // end of while forever loop } //end of main .... good luck steve robinson
i want example code c51 for read and write 24c02 tank you!
i want example code c51 for read and write 24c02 once more: GIMME CODE!! Eventually all us old farts will pass and what are you young whippersnappers that have been given finished code every time gonna do then. You never learned to code your own and there will be nobody to give it to you. Try coding your own, and post the specific problems with your OWN code here and help will be forthcoming. ALSO: how do you propose that anyone can help you when you see no need to specify what uC you are using (specific derivative), not to mention that the 24c02 is NOT totally identical from manufacturer to manufacturer. Erik