[an error occurred while processing this directive]
|
i2c.h
#ifndef I2C_H
#define I2C_H
extern void put_start_bit(void);
extern void put_stop_bit(void);
extern char i2c_put_byte(char data);
extern char i2c_get_byte(void);
extern char i2c_get_ack(void);
extern char i2c_put_ack(char ack);
#endif
//***************************************
i2c.asm
MODULE i2cio
PUBLIC put_start_bit
PUBLIC put_stop_bit
PUBLIC i2c_put_byte
PUBLIC i2c_get_byte
PUBLIC i2c_get_ack
PUBLIC i2c_put_ack
RSEG CODE:CODE
#if defined(__ATmega162__)
PORTC EQU 0x15
DDRC EQU 0x14
PINC EQU 0x13
SDA_PIN EQU 0x13
SDA_BIT EQU 0
set_scl MACRO
sbi PORTC,1
ENDM
clr_scl MACRO
cbi PORTC,1
ENDM
set_sda MACRO
cbi DDRC,0
ENDM
clr_sda MACRO
sbi DDRC,0
ENDM
#elif defined(__ATmega48__)
PORTC EQU 0x08
DDRC EQU 0x07
PINC EQU 0x06
SDA_PIN EQU 0x06
SDA_BIT EQU 4
set_scl MACRO
sbi PORTC,5
ENDM
clr_scl MACRO
cbi PORTC,5
ENDM
set_sda MACRO
cbi DDRC,4
ENDM
clr_sda MACRO
sbi DDRC,4
ENDM
#else
#error This file, should only be compiled with "--cpu" set
#endif
i2c_pause:
// ldi r19,10
//i2c_pause0:
ldi r18,10
i2c_pause1:
dec r18
brne i2c_pause1
// dec r19
// brne i2c_pause0
ret
put_start_bit:
set_sda
rcall i2c_pause
set_scl
rcall i2c_pause
clr_sda
rcall i2c_pause
clr_scl
rcall i2c_pause
ret
put_stop_bit:
clr_sda
clr_scl
rcall i2c_pause
set_scl
rcall i2c_pause
set_sda
rcall i2c_pause
ret
i2c_put_byte:
ldi r17,8
rcall i2c_put_byte1
ldi r16,0
ldi r17,1
rjmp i2c_get_byte1
i2c_put_byte1:
clr_sda
lsl r16
brcc i2c_put_byte11
set_sda
i2c_put_byte11:
rcall i2c_pause
set_scl
rcall i2c_pause
rcall i2c_pause
clr_scl
rcall i2c_pause
i2c_put_byte2:
dec r17
brne i2c_put_byte1
set_sda
ret
i2c_get_ack:
ldi r16,0
ldi r17,1
set_sda
rjmp i2c_get_byte1
i2c_put_ack:
ldi r17,1
rjmp i2c_put_byte1
i2c_get_byte:
ldi r16,0
ldi r17,8
i2c_get_byte1:
lsl r16
rcall i2c_pause
set_scl
rcall i2c_pause
sbic SDA_PIN,SDA_BIT
ori r16,1
rcall i2c_pause
clr_scl
rcall i2c_pause
dec r17
brne i2c_get_byte1
ret
ENDMOD
END
//***************************************
24C64.h
#ifndef M24C64_H
#define M24C64_H
extern char write_str(char* str, unsigned int addr, unsigned char size);
extern char read_str(char* str, unsigned int addr, unsigned char size);
#endif
//***************************************
24C64.c
#include "24C64.h" char write_str(char* str, unsigned int addr, unsigned char size) WP=0; if(i2c_put_byte(*str++)) addr++; char read_str(char* str, unsigned int addr, unsigned char size) if(!set_addr(addr)) put_start_bit();
E-mail:
info@telesys.ru
#include "I2C.h"
#include
#include
#if defined(__ATmega162__)
#define WP PORTC_Bit2
#elif defined(__ATmega48__)
#define WP PORTC_Bit3
#else
#error This file, should only be compiled with "--cpu" set
#endif
char set_addr(unsigned int addr)
{
unsigned char ct = 5;
set_addr1:
__watchdog_reset();
put_start_bit();
if(!i2c_put_byte(0xA0))
{
if(!i2c_put_byte((char)(addr >> 8)))
{
if(!i2c_put_byte((char)addr))return 1;
}
}
else
{
if(--ct)
{
put_stop_bit();
__delay_cycles(40000);
goto set_addr1;
}
}
put_stop_bit();
return 0;
}
{
write_str1:
if(!set_addr(addr))
{
put_stop_bit();
WP=1;
return 0;
}
write_str2:
{
put_stop_bit();
WP=1;
return 0;
}
if(--size == 0)goto write_str3;
if(((char)addr & 0x1F) == 0)
{
put_stop_bit();
__delay_cycles(40000);
goto write_str1;
}
goto write_str2;
write_str3:
put_stop_bit();
WP=1;
return 1;
}
{
{
put_stop_bit();
return 0;
}
if(i2c_put_byte(0xA1))
{
put_stop_bit();
return 0;
}
while(1)
{
*str++ = i2c_get_byte();
if((--size) == 0)
{
i2c_put_ack(0x80);
put_stop_bit();
return 1;
}
i2c_put_ack(0);
}
}
Ответы