[PIC] PIC18F46K22- configuration problem with I2C - PCF8583

Status
Not open for further replies.

Karol Plewka

Newbie level 1
Joined
Dec 10, 2014
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
8
Hi everyone

I am trying to configurate I2C interface between PCF8583 and PIC18F46K22, but for some reason it is not working and I can't seem to find the problem, especially since it all looks fine to me.
I use 16Mhz external oscilator, 2 pull-up resistor (4.7k) on SDA and SCL line, everything else is standard. That error is probably caused by the way I configured register but at this point I am not quite sure. Below theres my code, has anyone got an idea what could possibly be wrong?

Code:
#include <xc.h>
#include <p18f46k22.h>
#include <stdio.h>
#include <stdlib.h>
#include "bit_settings.h"
#include "my_delays.h"
#include "hd44780.h"
#include "KL.h"
#include <ctype.h>
#include <stdint.h>
#include <i2c.h>

// definitions
#define _XTAL_FREQ 16000000
#define PCF8583_WRITE_ADDRESS 0xA0
#define PCF8583_READ_ADDRESS 0xA1

// Register addresses
#define PCF8583_CTRL_STATUS_REG 0x00
#define PCF8583_100S_REG 0x01
#define PCF8583_SECONDS_REG 0x02
#define PCF8583_MINUTES_REG 0x03
#define PCF8583_HOURS_REG 0x04
#define PCF8583_DATE_REG 0x05
#define PCF8583_MONTHS_REG 0x06
#define PCF8583_TIMER_REG 0x07

#define PCF8583_ALARM_CONTROL_REG 0x08
#define PCF8583_ALARM_100S_REG 0x09
#define PCF8583_ALARM_SECS_REG 0x0A
#define PCF8583_ALARM_MINS_REG 0x0B
#define PCF8583_ALARM_HOURS_REG 0x0C
#define PCF8583_ALARM_DATE_REG 0x0D
#define PCF8583_ALARM_MONTHS_REG 0x0E
#define PCF8583_ALARM_TIMER_REG 0x0F

// Use the first NVRAM address for the year byte.
#define PCF8583_YEAR_REG 0x10


// Commands for the Control/Status register.
#define PCF8583_START_COUNTING 0x00
#define PCF8583_STOP_COUNTING 0x80

#if defined (I2C_IO_V6)
#define I2C2_SCL TRISDbits.TRISD0
#define I2C2_SDA TRISDbits.TRISD1
#endif

unsigned char seconds=0x00;
char bufa[5];

void Read_Time(void)
{
// read seconds
StartI2C2();
while(SSP2CON2bits.SEN);
WriteI2C2(PCF8583_WRITE_ADDRESS); // Address and Write Flag
WriteI2C2(PCF8583_SECONDS_REG); // Start from seconds memory location
RestartI2C2(); // At this moment master transmitter becomes master receiver
while(SSP2CON2bits.RSEN); // and PCF8583 slave receiver becomes slave transmitter
WriteI2C2(PCF8583_READ_ADDRESS); // Address and Read Flag
seconds=ReadI2C2(); // read seconds reg
NotAckI2C2();
StopI2C2();
while(SSPCON2bits.PEN);
}
void main(void)
{

ANSELB = 0; ANSELD=0; ANSELC = 0; ANSELE = 0;
PORTB = 0; PORTD = 0; PORTE = 0; PORTC=0;
TRISB = 0b00000001; TRISD = 0b00000011; TRISE = 0;
TRISC = 0b11110000;
SSPADD = 0x39;
ADCON1 = 0x0F;

OpenI2C2(MASTER, SLEW_ON);
Lcd_Init();
Lcd_Cmd(LCD_CLEAR);
Lcd_Cmd(LCD_CURSOR_OFF);
while(1)
{
Lcd_Cmd(LCD_CLEAR);
Delay_ms(100);
Read_Time();
sprintf(bufa,"%1d", seconds);
Lcd_Out(1,1,bufa);
}
}

Thanks in advance,
Karol
 

Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…