Nesse exemplo mostro como acessar o RTC MCP7940.

Segue o código:

[sections] [section title=”Código Fonte”]

#define MCP7940_Adr     0xDE    // ENDEREÇO
#define I2C_READ  1
#define I2C_WRITE 0

void write_MCP7940(unsigned short address,unsigned short w_data)
{
GIE_bit = 0; //desabilita interrupção
I2C1_Start(); // issue I2C start signal
I2C1_Wr(MCP7940_Adr | I2C_WRITE); // send byte via I2C (device address + W)
I2C1_Wr(address); // send byte (address of MCP7940 location)
I2C1_Wr(w_data); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal
GIE_bit = 1; //habilita interrupção
}

unsigned short read_MCP7940(unsigned short address)
{
unsigned char dado;
GIE_bit = 0; //desabilita interrupção
I2C1_Start();
I2C1_Wr(MCP7940_Adr | I2C_WRITE); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 –> 0xD0
I2C1_Wr(address);
I2C1_Start();
I2C1_Wr(MCP7940_Adr | I2C_READ); //0x68 followed by 1 –> 0xD1
dado=I2C1_Rd(0);
I2C1_Stop();
GIE_bit = 1; //habilita interrupção
return(dado);
}

void read_data()
{
GIE_bit = 0; //desabilita interrupção
sec = read_MCP7940(0)&0x7f; // read second
minute = read_MCP7940(1)&0x7F; // read minute
hour = read_MCP7940(2); // read hour
day = read_MCP7940(3); // read day
date = read_MCP7940(4); // read date
month = read_MCP7940(5); // read month
year = read_MCP7940(6); // read year
GIE_bit = 1; //habilita interrupção
}

[/section] [/sections]
Obrigado e até o próximo post.

DATEK Tecnologia Eletrônica
[PIC][MIKROC] Acessando o RTC MCP7940
Tags:                     

Deixe uma resposta

%d blogueiros gostam disto: