Код: Выделить всё
#include <mega168.h>
#include <stdio.h>
#include <delay.h>
// I2C Bus functions
#asm
.equ __i2c_port=0x08 ;PORTC
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
#include <i2c.h>
#ifndef RXB8
#define RXB8 1
#endif
#ifndef TXB8
#define TXB8 0
#endif
#ifndef UPE
#define UPE 2
#endif
#ifndef DOR
#define DOR 3
#endif
#ifndef FE
#define FE 4
#endif
#ifndef UDRE
#define UDRE 5
#endif
#ifndef RXC
#define RXC 7
#endif
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
volatile unsigned int addr;
volatile unsigned char pageSize;
volatile bit page_stop_status;
void meny(){
putchar(13);
putchar(10);
printf("1.Read eeprom");
putchar(13);
putchar(10);
}
void page_stop()
{
i2c_stop();
delay_ms(30);
page_stop_status=1;
}
void addr_save(){ // запись счетчиа байт
i2c_start();
i2c_write(0xa0);
i2c_write(0);
i2c_write(0 & 0x00FF);
i2c_write((addr>>8) & 0xFF);
i2c_write((addr & 0xFF)); // адрес байта
i2c_write(pageSize); // номер байта в странице
page_stop();
}
void readaddr() { // чтение счетчиа байт
unsigned char hi_addr,lo_addr;
i2c_start(); // I2C start signal
i2c_write(0xA0); // send byte via I2C (device address + W)
i2c_write(0>>8);
i2c_write(0 & 0x00FF);
i2c_start(); // I2C signal repeated start
i2c_write(0xA1); // send byte (device address + R)
hi_addr = i2c_read(0); // Read the data (NO acknowledge)
i2c_start(); // I2C signal repeated start
i2c_write(0xA1); // send byte (device address + R)
lo_addr = i2c_read(0); // Read the data (NO acknowledge)
i2c_start(); // I2C signal repeated start
i2c_write(0xA1); // send byte (device address + R)
pageSize = i2c_read(0); // Read the data (NO acknowledge)
addr=hi_addr;
addr=(addr<<8); // собираем млатший и старший байт
addr=(addr | lo_addr);
page_stop();
}
void ReadMem(){ // читать память
unsigned int cnt;
unsigned int temp_addr;
readaddr();
if (addr==3) {putchar('E'); putchar('m'); putchar('t'); putchar('y');putchar(' '); putchar('M'); putchar('e'); putchar('m'); putchar('o');putchar('r'); putchar('y');
putchar(13);
putchar(10);
putchar(13);
putchar(10);}
else {
putchar('R'); putchar('e'); putchar('a'); putchar('d');putchar('i'); putchar('n'); putchar('g');
putchar(13);
putchar(10);
LED_ON;
temp_addr=3;
while (temp_addr<addr) {
i2c_start();
i2c_write(0xa0);
i2c_write(temp_addr>>8);
i2c_write(temp_addr & 0x00FF);
for(cnt=0;cnt<128;cnt++) {
i2c_start(); // I2C signal repeated start
i2c_write(0xA1); // send byte (device address + R)
putchar(i2c_read(0));
temp_addr++;
if (temp_addr==addr)
break;
} // Read the data (NO acknowledge)
page_stop();
}
page_stop();
putchar(13);
putchar(10);
LED_OFF;
putchar('O'); putchar('k');
}
putchar(13);
putchar(10);
meny();
}
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) {
if (data=='1') ReadMem();
}
}
void main(void)
{
PORTB=0xFF;
DDRB=0x00;
// Port C initialization
PORTC=0x7F;
DDRC=0x00;
// Port D initialization
PORTD=0xE0;
DDRD=0x00;
// External Interrupt(s) initialization
// INT0: On
EICRA=0x00;
EIMSK=0x00;
EIFR=0x00;
PCICR=0x00;
// Clock value: 31.250 kHz
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;
// Timer/Counter 1 initialization
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
ASSR=0x00;
TCCR2A=0x00;
TCCR2B=0x00;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x01;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x00;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;
// USART Baud Rate: 1200
UCSR0A=0x00;
UCSR0B=0x98;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;
// Analog Comparator: Off
ACSR=0x80;
ADCSRB=0x00;
DIDR1=0x00;
// ADC disabled
ADCSRA=0x00;
// SPI disabled
SPCR=0x00;
// TWI disabled
TWCR=0x00;
/* Prepare the sleep in power save mode*/
SMCR |= ((1<<SE) | (0<<SM2) | (0<<SM1) | (0<<SM0));
/* Enter sleep mode */
i2c_init(); // включаем память
delay_ms(100);
readaddr();
if (addr==65535) addr=pageSize=3;
addr_save();
page_stop();
meny();
#asm("sei")
#pragma optsize-
#asm("wdr")
WDTCSR=0x39;
WDTCSR=0x29;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
delay_ms(100);
while (1)
{
page_stop();
addr_save();
}
}


