sagar2993
Newbie level 3
controllerIC16F690
RTCS1307
It is working fine for Proteus but it dose not work for my actual hardware please advice
please check my code
RTCS1307
It is working fine for Proteus but it dose not work for my actual hardware please advice
please check my code
Code:
#include <xc.h>
#include "lcd.h"
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#define _XTAL_FREQ 8000000
#define LED PORTAbits.RA0
#define SDA PORTBbits.RB4
#define SCL PORTBbits.RB6
#define SADDR TRISBbits.TRISB4
char SEC,MIN,HOUR,DATE,MONTH,YEAR,DAY, SEC1;
int a,b,c,d,e,f,g,h;
char DAYS[8][4]={"SUN","MUN","TUE","WED","THU","FRI","SAT"};
char arr[7];
char time[8];
void START_I2C()
{
SDA=1;
SCL=1;
__delay_ms(20);
SDA=0;
__delay_ms(20);
SCL=0;
// LCD_POS(2,0x01);
// LCD_STRING("S");
}
void STOP_I2C()
{
SDA=0;
SCL=1;
SCL=1;
__delay_ms(20);
SDA=1;
// LCD_POS(1,0x08);
// LCD_STRING("p");
}
void WRITE_I2C_DATA(char d)
{
char i;
SCL=0;
SDA=0;
__delay_ms(20);
for(i=0;i<=7;i++)
{
if((d & 0x80) ==0x80)
{
SDA=1;
// LCD_POS(2,0x03);
//LCD_STRING("P");
}
else
{
SDA=0;
// LCD_POS(2,0x03);
// LCD_STRING("Q");
}
d=d<<1;
__delay_us(20);
SCL=1;
SCL=1;
__delay_us(20);
SCL=0;
__delay_us(20);
}
i=SDA;
// LCD_POS(2,0x05);
// LCD_STRING("S");
SCL=1;
__delay_us(20);
SCL=0;
__delay_us(20);
/* for(i=0;i<=7;i++)
{
SDA=((d&0x80)?1:0);
d=d<<1;
LCD_POS(2,0x05);
LCD_STRING("S");
__delay_us(20);
SCL=1;
SCL=1;
__delay_us(20);
SCL=0;
__delay_us(20);
}
i=SDA;
LCD_POS(1,0x03);
LCD_STRING("P");
SCL=1;
__delay_us(20);
SCL=0;
__delay_us(20);*/
}
void WRITE_I2C(char data,char caddr)
{
START_I2C();
WRITE_I2C_DATA(0xD0);
WRITE_I2C_DATA(data);
WRITE_I2C_DATA(caddr);
STOP_I2C();
}
void SET_CLOCK()
{
WRITE_I2C(0x00,0x00); //SEC
__delay_ms(2);
WRITE_I2C(0x01,0x58); //MIN
__delay_ms(2);
WRITE_I2C(0x02,0x16);//HOUR
__delay_ms(2);
WRITE_I2C(0x03,0x6);//DAY
__delay_ms(2);
WRITE_I2C(0x04,0x18);//DATE
__delay_ms(2);
WRITE_I2C(0x05,0x09);//MONTH
__delay_ms(2);
WRITE_I2C(0x06,0x15);//YEAR
__delay_ms(2);
}
char READ_I2C_DATA(void)
{
char i;
char j;
SADDR=1;
SCL=0;
j=0x00;
for(i=0;i<=7;i++)
{
// LCD_POS(2,0x01);
// LCD_STRING("s1");
j=j<<1;
// LCD_POS(2,0x01);
// LCD_DATA(j);
SCL=1;
j |=SDA;
SCL=0;
}
// LCD_POS(2,0x04);
// LCD_STRING("s2");
SADDR=0;
SDA=1;
SCL=1;
SCL=1;
SCL=0;
return j;
// LCD_POS(2,0x07);
// LCD_STRING("s3");
}
char READ_I2C(char daddr)
{
char s1;
START_I2C();
WRITE_I2C_DATA(0xD0);// write
//LCD_POS(2,0x07);
// LCD_STRING("S3");
WRITE_I2C_DATA(daddr);
// WRITE_I2C_DATA(0x07);
// WRITE_I2C_DATA(0x10);
START_I2C();
WRITE_I2C_DATA(0xD1);// read
s1= READ_I2C_DATA();
// LCD_POS(2,0x01);
// LCD_DATA(s1);
STOP_I2C();
return s1;
}
/*
char CONVERT(int fdata)
{
fdata=(fdata>>4);
return fdata;
}
char CONVERT1(int gdata)
{
gdata=(gdata>>4);
return gdata;
}
*/
int main(void)
{
long int s=0;
OSCCONbits.IRCF=0x7;
// char a=0x12;
ANSEL=0X00;
ANSELH=0X00;
TRISB=0x00;
TRISAbits.TRISA0=0x00;
TRISAbits.TRISA1=0x00;
TRISAbits.TRISA2=0x00;
TRISCbits.TRISC0=0x00;
TRISCbits.TRISC1=0x00;
TRISCbits.TRISC2=0x00;
TRISCbits.TRISC3=0x00;
// INTCONbits.INTF=0;
//__delay_us(20);
LCD_PRE_INIT();
__delay_us(200);
LCD_INIT();
__delay_us(10);
LCD_INIT();
__delay_us(10);
LCD_INIT();
__delay_us(10);
__delay_ms(200);
SET_CLOCK();
__delay_ms(10);
LCD_POS(1,0x01);
__delay_us(20);
LCD_CMD(0x80);
LCD_STRING("SAGAR");
LCD_CMD(0xE0);
while(1)
{
// s++;
SEC=READ_I2C(0x00);
// SEC1=CONVERT(SEC);
///SEC1=CONVERT1(SEC);
// LCD_POS(2,0x01);
// LCD_DATA(SEC);
MIN=READ_I2C(0x01);
// LCD_POS(2,0x03);
// LCD_DATA(MIN);
HOUR=READ_I2C(0x02);
// LCD_POS(2,0x05);
/// LCD_DATA(HOUR);
DAY=READ_I2C(0x03);
// LCD_POS(2,0x07);
// LCD_DATA(DAY);
/* DATE=READ_I2C(0x04);
MONTH=READ_I2C(0x05);
YEAR=READ_I2C(0x06);*/
LCD_CLEAR();
LCD_POS(1,0x01);
// LCD_DATA(a);
/* SEC=arr[0];
MIN=arr[1];
HOUR=arr[1];
time[6]=CONVERT(SEC);
time[7]=CONVERT1(SEC);
time[5]=':';
time[3]=CONVERT(MIN);
time[4]=CONVERT1(MIN);
time[2]=':';
time[1]=CONVERT(HOUR);
time[0]=CONVERT1(HOUR);
int i;
for(i=0;i<=7;i++)
{
LCD_DATA(time[i]);
}
*/
// HOUR=0x05;
// LCD_DATA(HOUR);
LCD_DATA(HOUR/0x10+0x30);
LCD_DATA(HOUR%0x10+0x30);
// __delay_ms(1);
// LCD_DATA(MIN=0x10);
LCD_DATA(':');
LCD_DATA(MIN/0x10+0x30);
LCD_DATA(MIN%0x10+0x30);
// __delay_ms(1);
LCD_DATA(':');
LCD_DATA(SEC/0x10+0x30);
LCD_DATA(SEC%0x10+0x30);
LCD_DATA(' ');
LCD_DATA(' ');
// __delay_ms(1);
// LCD_STRING(DAY[DAYS-1]);
// __delay_ms(1);
/* LCD_POS(2,0x01);
LCD_DATA(DATE/0x10+0x30);
LCD_DATA(DATE%0x10+0x30);
// LCD_STRING('');
LCD_DATA(' ');
LCD_STRING("/");
LCD_DATA(' ');
// __delay_ms(1);
LCD_DATA(MONTH/0x10+0x30);
LCD_DATA(MONTH%0x10+0x30);
LCD_DATA(' ');
LCD_STRING("/");
LCD_DATA(' ');
// __delay_ms(1);
LCD_DATA(YEAR/0x10+0x30);
LCD_DATA(YEAR%0x10+0x30);*/
// __delay_ms(1);
/* if(s>=20)
{
s=0;
}*/
}
LCD_TIME(40);
LCD_CLEAR();
}
Last edited by a moderator: