trungkstn
Newbie level 5
I use C18 compiler. I have some problem:
1. I2C debugger notice: error
2. Can't write ds1307
My code
and some images
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;
}