unsigned char I2c_read(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo) { int i; I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr)) { I2c_stop(); DPRINTK("Fail to write device addr\n"); return ERROR_CODE_READ_ADDR; } if(I2c_write_byte(sub_addr)) { I2c_stop(); DPRINTK("Fail to write sub addr\n"); return ERROR_CODE_READ_ADDR; } I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr+1)) { I2c_stop(); DPRINTK("Fail to write device addr+1\n"); return ERROR_CODE_READ_ADDR; } for(i = 0; i<ByteNo; i++) buff[i] = I2c_read_byte(); I2C_DELAY; I2C_DELAY_LONG; I2c_stop(); I2C_DELAY_LONG; return ERROR_CODE_TRUE; }
unsigned char I2c_read(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo) { int i; nack_flag = ByteNo - 1; I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr)) { I2c_stop(); printk("Fail to write device addr\n"); return ERROR_CODE_READ_ADDR; } if(I2c_write_byte(sub_addr)) { I2c_stop(); printk("Fail to write sub addr\n"); return ERROR_CODE_READ_ADDR; } #if 1 // printk("=========i2c_start===\n"); I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr+1)) { I2c_stop(); printk("Fail to write device addr+1\n"); return ERROR_CODE_READ_ADDR; } for(i = 0; i<ByteNo; i++) buff[i] = I2c_read_byte(i); I2C_DELAY; I2C_DELAY_LONG; I2c_stop(); I2C_DELAY_LONG; #endif printk("buff[0] = %d,buff[1] = %d\n",buff[0],buff[1]); return ERROR_CODE_TRUE; }
unsigned char I2c_write(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo) { int i; I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr)) { I2c_stop(); return ERROR_CODE_WRITE_ADDR; } if(I2c_write_byte(sub_addr)) { I2c_stop(); return ERROR_CODE_WRITE_ADDR; } for(i = 0; i<ByteNo; i++) { if(I2c_write_byte(buff[i])) { I2c_stop(); return ERROR_CODE_WRITE_DATA; } } I2C_DELAY; I2c_stop(); I2C_DELAY_LONG; return ERROR_CODE_TRUE; }
//========================================================================// // Leer EEPROM // //======================================================================// void leer_EEPROM(void) { uint8_t dat; if(RD_Mer_L==0xff) { RD_Mer_H++;} _delay_ms(3); I2c_start(); WR_EEPROM(DIR_WR);//Dirrecion de la memoria para escuribir en la memoria WR_EEPROM(RD_Mer_H);//Dirrecion Alta de la EEPROM WR_EEPROM(RD_Mer_L);//Dirrecion baja de la EEPROM I2c_start(); dat=Read_EEPROM(); Lcd_pixel(p_x,p_y,dat); Lcd_pixel(p_x,p_y,dat); Lcd_pixel(p_x,p_y,dat); PORTB=dat; if(p_y==132) { p_x++; p_y=0; } if(p_x==132) { p_x=0; } p_y++; RD_Mer_L++; TWCR=_BV(TWINT)|_BV(TWEN)|_BV(TWSTO); }
//===============================================================================// // Datos a la EEPROM // //=============================================================================// void Mad_EEPROM(uint8_t dat) { if(WR_Mer_L==0xFF) {WR_Mer_H++;} I2c_start(); WR_EEPROM(DIR_WR);//Se le dice a la EEprom que se le va a escribir WR_EEPROM(WR_Mer_H);//Dierrcion alta de la EEPROM WR_EEPROM(WR_Mer_L);//Dirrecion baja de la EEPROM WR_EEPROM(dat); WR_Mer_L++; TWCR=_BV(TWINT)|_BV(TWEN)|_BV(TWSTO); PORTB=dat; }