Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Problem with I2C (ds1307) 18f4550 MPLAB C18

Status
Not open for further replies.

trungkstn

Newbie level 5
Joined
Nov 26, 2010
Messages
9
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,332
I use C18 compiler. I have some problem:
1. I2C debugger notice: error
2. Can't write ds1307

My code

Code:
/*
 * File:   sht10.c
 * Author: trung
 *
 * Created on April 30, 2012, 8:02 PM
 */
#define USE_OR_MASKS
#include <p18cxxx.h>
#include <GenericTypeDefs.h>
#include <usart.h>
#include <stdio.h>
#include <delays.h>
#include <math.h>
#include <i2c.h>

#pragma config FOSC=HS, FCMEN=ON, WDT=OFF, IESO=OFF, XINST=OFF, LVP=OFF

#define BAUD_RATE_GEN 31
#define FREQ 20000000
#define BITRATE 100000

BYTE add = 0x68;

void InitBoard(void);
void WireToDs1307(BYTE value);
BYTE Dec2Bcd(BYTE value);
BYTE Bcd2Dec(BYTE value);
void SetDateTimeToDs1307(BYTE day, BYTE month, BYTE year, BYTE hour, BYTE minute, BYTE second);
void GetDateTimeToDs1307(BYTE *day, BYTE *month, BYTE *year, BYTE *hour, BYTE *minute, BYTE *second);
void TransformTime(BYTE *day, BYTE *month, BYTE *year, BYTE *hour, BYTE *minute, BYTE *second);
void Delay(unsigned int n);

void low_isr(void);
void high_isr(void);

#pragma code low_vector=0x18
void interrupt_at_low_vector(void)
{
  _asm GOTO low_isr _endasm
}
#pragma code /* return to the default code section */

#pragma interruptlow low_isr
void low_isr (void)
{
}

#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
  _asm GOTO high_isr _endasm
}
#pragma code /* return to the default code section */

#pragma interruptlow high_isr
void high_isr (void)
{
}


BYTE Dec2Bcd(BYTE value)
{
    return (((value/10)<<4) + (value%10));
}

BYTE Bcd2Dec(BYTE value)
{
    return ((value>>4)*10 + (value&0x0F));
}

void Delay(unsigned int n)
{
    unsigned char i;
    for(i = 0; i < n; i++)
        Delay100TCYx(n);
		
}

void InitBoard(void)
{
    BYTE sync_mode, slew;
    OpenUSART(USART_TX_INT_OFF | USART_RX_INT_OFF | USART_ASYNCH_MODE | USART_EIGHT_BIT | USART_CONT_RX | USART_BRGH_LOW, BAUD_RATE_GEN);
    baudUSART(BAUD_8_BIT_RATE | BAUD_AUTO_OFF);

    CloseI2C();
    sync_mode = MASTER;
    slew = SLEW_OFF;
    OpenI2C(sync_mode, slew);
    SSPADD = (FREQ/BITRATE)/4-1;

}

void WireToDs1307(BYTE value)
{
    BYTE status, data;

    do{
        status = WriteI2C(value);
        if(status == -1)
        {
            data = SSPBUF;
            SSPCON1bits.WCOL=0;
        }
        printf("\r\n status: %d %d", value, status);
    }while(status!=0);
    
   while(SSPCON2bits.ACKSTAT); 
}

void SetDateTimeToDs1307(BYTE day, BYTE month, BYTE year, BYTE hour, BYTE minute, BYTE second)
{
    second = second & 0x7F;
    hour = hour & 0x3F;
    StartI2C();
    WireToDs1307(add << 1);
    WireToDs1307(0x00);
    WireToDs1307(Dec2Bcd(second));
    WireToDs1307(Dec2Bcd(minute));
    WireToDs1307(Dec2Bcd(hour));
    WireToDs1307(Dec2Bcd(day));
    WireToDs1307(Dec2Bcd(month));
    WireToDs1307(Dec2Bcd(year));
    //WireToDs1307(0x80);
    StopI2C();
}


void main(void) {
    unsigned char value[3];
    InitBoard();
    Delay(100);
    IdleI2C();
    StartI2C();
    WriteI2C(0xD0);
    WriteI2C(0x00);
    WriteI2C(0x00);
    StopI2C();
    
    while (1) {
    }
    
    return;
}
and some images

pic 1.pngpic 2.pngpic3.png
 

I don't know why and how but frankly speaking problem fixed. Thank a lot. I2C debugger has some wrong notice but ds1307 run perfectly.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top