Witam
Mam tu taj fragment kodu do obslugi i2c zestrony p.Petera Fleury, no i probuje wykryc mojego pcf. Mam juz skompilowany kod testowy ktory go wykorzystje - takze uklad dziala, oto kod nad ktorym pracuje
=======i2c.h==========
void i2c_init(void)
{
/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
TWSR = 0; /* no prescaler */
TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
}/* i2c_init */
/*************************************************************************
Issues a start condition and sends address and transfer direction.
return 0 = device accessible, 1= failed to access device
*************************************************************************/
unsigned char i2c_start(unsigned char address)
{
uint8_t twst;
// send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
// send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed and ACK/NACK has been received
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) )
return 1;
return 0;
}/* i2c_start */
=========main==========
#include <avr/io.h>
#include "i2c.h"
#include "usart.h"
#include <avr/interrupt.h>
#include <avr/signal.h>
#define PCFADDRESS 0xA0
SIGNAL (SIG_INTERRUPT0)
{
usart_puts("przerwanie\r");
}
int main()
{
unsigned char odczyt = 0x00;
usart_init(103);
i2c_init();
usart_puts("poczatek programu do ii2\r");
odczyt = i2c_start(PCFADDRESS+0x01);
if(odczyt)
{
usart_puts("urzadzenie padlo\r");
}
else
{
usart_puts("urzadzenie ok\r");
}
odczyt = i2c_start(PCFADDRESS);
if(odczyt)
{
usart_puts("urzadzenie padlo\r");
}
else
{
usart_puts("urzadzenie ok\r");
}
i2c_stop();
while(1);
return 0;
}
===================
niestety w konsoli tylko napis urzadzenie padlo, w czym jest blad?
tak w ogole chodzi mi o taka rzecz zeby np co sekunde czy co minute pcf generowal mi przerwanie - chce zrobic prosty zegarek - moze ktos juz cos takiego robil?
pozdrawiam
Mam tu taj fragment kodu do obslugi i2c zestrony p.Petera Fleury, no i probuje wykryc mojego pcf. Mam juz skompilowany kod testowy ktory go wykorzystje - takze uklad dziala, oto kod nad ktorym pracuje
=======i2c.h==========
void i2c_init(void)
{
/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
TWSR = 0; /* no prescaler */
TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
}/* i2c_init */
/*************************************************************************
Issues a start condition and sends address and transfer direction.
return 0 = device accessible, 1= failed to access device
*************************************************************************/
unsigned char i2c_start(unsigned char address)
{
uint8_t twst;
// send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
// send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed and ACK/NACK has been received
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) )
return 1;
return 0;
}/* i2c_start */
=========main==========
#include <avr/io.h>
#include "i2c.h"
#include "usart.h"
#include <avr/interrupt.h>
#include <avr/signal.h>
#define PCFADDRESS 0xA0
SIGNAL (SIG_INTERRUPT0)
{
usart_puts("przerwanie\r");
}
int main()
{
unsigned char odczyt = 0x00;
usart_init(103);
i2c_init();
usart_puts("poczatek programu do ii2\r");
odczyt = i2c_start(PCFADDRESS+0x01);
if(odczyt)
{
usart_puts("urzadzenie padlo\r");
}
else
{
usart_puts("urzadzenie ok\r");
}
odczyt = i2c_start(PCFADDRESS);
if(odczyt)
{
usart_puts("urzadzenie padlo\r");
}
else
{
usart_puts("urzadzenie ok\r");
}
i2c_stop();
while(1);
return 0;
}
===================
niestety w konsoli tylko napis urzadzenie padlo, w czym jest blad?
tak w ogole chodzi mi o taka rzecz zeby np co sekunde czy co minute pcf generowal mi przerwanie - chce zrobic prosty zegarek - moze ktos juz cos takiego robil?
pozdrawiam