Emtronics_sys
Newbie level 3
Hi ,I face a Problem in inter facing RTC, I wrote the code in C using the Hitech C compiler,I found that the My code works properly in simulation and doesnt work when freely run continously or programming mode,I use MPLAB IDE and ICD2 Debugger
The code I wrote is posted below, I wanted an Immediate help, So please find me some solution where the problem could be.. or check where i have made the mistake..
I simply displayed the data i get initially but the seconds is not updating.. and getting struck up... if that does then i can manage displaying the formated RTC data.. check my code and tell me a solution..
#include<htc.h>
#define I2C_START SEN=1
#define I2C_STOP PEN=1
#define REP_START RSEN=1
//RTC DATA INDEX
#define SEC 00
#define MIN 01
#define HRS 02
#define DAY 03
#define DATE 04
#define MONTH 05
#define YEAR 06
#define START_UP_SCREEN 01
//LCD
#define RS RE0 //Register select bit is connected PORTE.0 so PORTE.0 is defined as RS
#define LRW RE2 //Read or Write bit is connected PORTE.1 so PORTE.0 is defined as RW
#define EN RE1 //Enable bit is connected PORTE.2 so PORTE.0 is defined as RS
#define LCD PORTD //LCd Data pins are connected to the PORTD
void I2C_ISR();
void RTC_WRITE();
void RTC_READ();
void LCD_INIT(void);
void LCD_CMD(unsigned char cmd);
void DISP_DATA(const char *dat);
void DISP_CHAR(char dat);
void Delay(unsigned int );
void DISP_UPDATE();
bit ACCESS_RTC,SEND_ADDR_FLAG,RW_FLAG,REPTD_START,SEND_STOP,CHECK_ACK=0;
bit DATA_RCVD=0,DISP_TIME;
unsigned char DATE_TIME[7];
unsigned char RW_DAT;
unsigned char count;
unsigned int DELAY_COUNT;
unsigned char PROCESS_STAT;
const char company_name[]="Emtronics Systems";
void main()
{
unsigned int j;
//Port line initializations
PORTA=0x00;
TRISA=0x00;
ADCON1=0x06;
TRISE=0x00;
TRISD=0x00;
TRISC=0x7F;
RC7=0;
PORTA=0x08;
//while(1)
for(j=0;j<=10000;j++)
{
PORTA=0x08;
Delay(4);
PORTA=0x00;
Delay(1);
}
//DATE TIME INITIALIZATIONS
DATE_TIME[SEC]=0x00;
DATE_TIME[MIN]=0x55;
DATE_TIME[HRS]=0x67;
DATE_TIME[DAY]=0x02;
DATE_TIME[DATE]=0x12;
DATE_TIME[MONTH]=0x12;
DATE_TIME[YEAR]=0x11;
//DATE_TIME[7]=0x00;
DISP_TIME=1;
ACKSTAT = 1;
ACCESS_RTC=1;
LCD_INIT();
DISP_DATA(&company_name);
RTC_WRITE();
//OPTION= 0x07;
//INTCON=0x20;
PROCESS_STAT=0x01;
while(1)
{
I2C_ISR();
LCD_CMD(0xC1);
if(ACCESS_RTC)
DISP_UPDATE();
RTC_READ();
}
}
void DISP_UPDATE()
{
unsigned int i=0;
LCD_CMD(0xC0);
if(DISP_TIME)
{
if(PROCESS_STAT==START_UP_SCREEN)
{
do
{
DISP_CHAR(0x30+(0x0F&(DATE_TIME>>4)));
DISP_CHAR(0x30+(0x0F&DATE_TIME));
DISP_CHAR(':');
i++;
}while(i<=3);
}
}
}
/**********************************************************************************************************************************************
* LCD_INIT()
***********************************************************************************************************************************************
*Function arguments : Nil
*Functions Returns : Nil
*Function Description :This function will initialize the LCD by giving the basic Initialization commands to the LCD.
* It also specifies the 4 bit or 8bit operation through the commands given.
*
***********************************************************************************************************************************************/
void LCD_INIT()
{
LCD_CMD(0x38);
LCD_CMD(0x0E);
LCD_CMD(0x01);
LCD_CMD(0x06);
LCD_CMD(0x80);
}
/**********************************************************************************************************************************************
* LCD_CMD()
***********************************************************************************************************************************************
*Function arguments : the Command to be passed to the LCD is passed as an argument.
*Argument Type : unsigned character
*Functions Returns : Nil
*Return Type : Nil
*Function Description :This function will get an unsigned character as an argument and will transmit the argument to the LCD as a command
*
***********************************************************************************************************************************************/
void LCD_CMD(unsigned char cmd)
{
LRW=0;
LCD=cmd;
RS=0;
EN=1;
Delay(10);
EN=0;
}
/**********************************************************************************************************************************************
* DISP_DATA()
***********************************************************************************************************************************************
*Function arguments : starting address of the array of the data to be displayed
*Functions Returns :Nil
*Function Description :This Function will take the starting address of any array of string to be displayed on the LCD.
* Then it will transmit the array of data that are to be displayed one by one.
*
***********************************************************************************************************************************************/
void DISP_DATA(const char *dat)
{
do
{
DISP_CHAR(*dat++);
}while(*dat);
}
/**********************************************************************************************************************************************
* DISP_CHAR()
***********************************************************************************************************************************************
*Function arguments : Character data to be displayed.
*Functions Returns :Nil
*Function Description :This function will display the character data that is passed as an argument to this function
*
***********************************************************************************************************************************************/
void DISP_CHAR(char dat)
{
LRW=0;
LCD=dat;
RS=1;
EN=1;
Delay(10);
EN=0;
}
void Delay(unsigned int n)
{
unsigned int i,j;
for(i=0;i<=n;i++)
for(j=0;j<=10;j++);
}
/***************************************************************************************************
RTC_WRITE()
*****************************************************************************************************/
void RTC_WRITE()
{
if(ACCESS_RTC)
{ //IF ACCESS_RTC is not set, RTC already under use.
SSPSTAT=0b10000000;
SSPCON=0b00011000;
SSPADD=0x1F;
RW_DAT=0xFE;
RW_FLAG=0;
REPTD_START=0;
SSPIF=0;
ACKSTAT=0;
SSPIE=0;
SSPEN=1;
//SEN=1;
I2C_START;
//while(!SSPIF);
//while(RW);
//CHECK_ACK=1;
//ACKSTAT=0;
//SEND_ADDR_FLAG=1;
//SSPIF=0;
//SSPIE=1;
//SSPBUF=0b11010000;
//if(WCOL)
// {
// LCD_CMD(0xC0);
// DISP_DATA("I2C BUS ERROR");
// }
//while(!SSPIF);
//while(ACKSTAT);
//LCD_CMD(0xC0);
//DISP_CHAR('A');
ACCESS_RTC=0;
}
}
/***************************************************************************************************
RTC_READ()
*****************************************************************************************************/
void RTC_READ()
{
if(ACCESS_RTC)
{
SSPSTAT=0b10000000;
SSPCON=0b00011000;
SSPADD=0x1F;
RW_DAT=0xFE;
RW_FLAG=1;
SEND_ADDR_FLAG=1;
REPTD_START=0;
SSPIF=0;
ACKSTAT=0;
SSPIE=0;
SSPEN=1;
I2C_START;
ACCESS_RTC=0;
}
}
/***************************************************************************************************
I2C_ISR()
*****************************************************************************************************/
void I2C_ISR()
{
if(SSPIF)//&&SSPEN&&(SSPCON&0x0F==0x08))
{
//I2C INTERRUPT SERVICE ROUTINE
SSPIF=0;
if((START && !CHECK_ACK) || ACKSTAT)
{
CHECK_ACK=1;
ACKSTAT=0;
while(RW);
SSPBUF=0b11010001&RW_DAT;
// LCD_CMD(0xC0);
// DISP_CHAR('y');
if(!RW_FLAG)
{
SEND_ADDR_FLAG=1;
}
}
else if(!BF && DATA_RCVD)
{
DATE_TIME[count]=SSPBUF;
count++;
if(count==0x07)
{
count=0;
ACKDT=1;
ACKEN=1;
SEND_STOP=1;
DISP_TIME=1;
}
else
{
ACKDT=0;
ACKEN=1;
}
DATA_RCVD=0;
//BF=0;
}
else if(SEND_STOP)
{
PEN=1;
SEND_STOP=0;
}
else if(STOP)
{
// while(RW);
//SSPADD=0x00;
ACCESS_RTC=1;
SSPIE=0;
CHECK_ACK=0;
// SSPBUF=0x00;
SSPCON=0x00;
// DISP_CHAR('S');
}
else if((!ACKSTAT) && (CHECK_ACK))
{
while(RW);
if(SEND_ADDR_FLAG)
{
SSPBUF=0x00;//to be continued
count=0x00;
SEND_ADDR_FLAG=0;
//LCD_CMD(0xC0);
}
else if(!REPTD_START)
{
if(RW_FLAG==1)
{
RSEN=1;
RW_DAT=0xFF;
count=0;
REPTD_START=1;
CHECK_ACK=0;
}
else
{
if(count<0x07)
{
SSPBUF=DATE_TIME[count];
count++;
LCD_CMD(0xC0);
DISP_CHAR(count+0x30);
}
else
{
PEN=1;
CHECK_ACK=0;
}
}
}
else if(REPTD_START)
{
SSPCON2=0x08;
DATA_RCVD=1;
}
// ACKSTAT=1;
}
if(WCOL)
{
LCD_CMD(0xC0);
DISP_DATA("I2C BUS ERROR");
}
}
}
Thanking you,
Kannan
The code I wrote is posted below, I wanted an Immediate help, So please find me some solution where the problem could be.. or check where i have made the mistake..
I simply displayed the data i get initially but the seconds is not updating.. and getting struck up... if that does then i can manage displaying the formated RTC data.. check my code and tell me a solution..
#include<htc.h>
#define I2C_START SEN=1
#define I2C_STOP PEN=1
#define REP_START RSEN=1
//RTC DATA INDEX
#define SEC 00
#define MIN 01
#define HRS 02
#define DAY 03
#define DATE 04
#define MONTH 05
#define YEAR 06
#define START_UP_SCREEN 01
//LCD
#define RS RE0 //Register select bit is connected PORTE.0 so PORTE.0 is defined as RS
#define LRW RE2 //Read or Write bit is connected PORTE.1 so PORTE.0 is defined as RW
#define EN RE1 //Enable bit is connected PORTE.2 so PORTE.0 is defined as RS
#define LCD PORTD //LCd Data pins are connected to the PORTD
void I2C_ISR();
void RTC_WRITE();
void RTC_READ();
void LCD_INIT(void);
void LCD_CMD(unsigned char cmd);
void DISP_DATA(const char *dat);
void DISP_CHAR(char dat);
void Delay(unsigned int );
void DISP_UPDATE();
bit ACCESS_RTC,SEND_ADDR_FLAG,RW_FLAG,REPTD_START,SEND_STOP,CHECK_ACK=0;
bit DATA_RCVD=0,DISP_TIME;
unsigned char DATE_TIME[7];
unsigned char RW_DAT;
unsigned char count;
unsigned int DELAY_COUNT;
unsigned char PROCESS_STAT;
const char company_name[]="Emtronics Systems";
void main()
{
unsigned int j;
//Port line initializations
PORTA=0x00;
TRISA=0x00;
ADCON1=0x06;
TRISE=0x00;
TRISD=0x00;
TRISC=0x7F;
RC7=0;
PORTA=0x08;
//while(1)
for(j=0;j<=10000;j++)
{
PORTA=0x08;
Delay(4);
PORTA=0x00;
Delay(1);
}
//DATE TIME INITIALIZATIONS
DATE_TIME[SEC]=0x00;
DATE_TIME[MIN]=0x55;
DATE_TIME[HRS]=0x67;
DATE_TIME[DAY]=0x02;
DATE_TIME[DATE]=0x12;
DATE_TIME[MONTH]=0x12;
DATE_TIME[YEAR]=0x11;
//DATE_TIME[7]=0x00;
DISP_TIME=1;
ACKSTAT = 1;
ACCESS_RTC=1;
LCD_INIT();
DISP_DATA(&company_name);
RTC_WRITE();
//OPTION= 0x07;
//INTCON=0x20;
PROCESS_STAT=0x01;
while(1)
{
I2C_ISR();
LCD_CMD(0xC1);
if(ACCESS_RTC)
DISP_UPDATE();
RTC_READ();
}
}
void DISP_UPDATE()
{
unsigned int i=0;
LCD_CMD(0xC0);
if(DISP_TIME)
{
if(PROCESS_STAT==START_UP_SCREEN)
{
do
{
DISP_CHAR(0x30+(0x0F&(DATE_TIME>>4)));
DISP_CHAR(0x30+(0x0F&DATE_TIME));
DISP_CHAR(':');
i++;
}while(i<=3);
}
}
}
/**********************************************************************************************************************************************
* LCD_INIT()
***********************************************************************************************************************************************
*Function arguments : Nil
*Functions Returns : Nil
*Function Description :This function will initialize the LCD by giving the basic Initialization commands to the LCD.
* It also specifies the 4 bit or 8bit operation through the commands given.
*
***********************************************************************************************************************************************/
void LCD_INIT()
{
LCD_CMD(0x38);
LCD_CMD(0x0E);
LCD_CMD(0x01);
LCD_CMD(0x06);
LCD_CMD(0x80);
}
/**********************************************************************************************************************************************
* LCD_CMD()
***********************************************************************************************************************************************
*Function arguments : the Command to be passed to the LCD is passed as an argument.
*Argument Type : unsigned character
*Functions Returns : Nil
*Return Type : Nil
*Function Description :This function will get an unsigned character as an argument and will transmit the argument to the LCD as a command
*
***********************************************************************************************************************************************/
void LCD_CMD(unsigned char cmd)
{
LRW=0;
LCD=cmd;
RS=0;
EN=1;
Delay(10);
EN=0;
}
/**********************************************************************************************************************************************
* DISP_DATA()
***********************************************************************************************************************************************
*Function arguments : starting address of the array of the data to be displayed
*Functions Returns :Nil
*Function Description :This Function will take the starting address of any array of string to be displayed on the LCD.
* Then it will transmit the array of data that are to be displayed one by one.
*
***********************************************************************************************************************************************/
void DISP_DATA(const char *dat)
{
do
{
DISP_CHAR(*dat++);
}while(*dat);
}
/**********************************************************************************************************************************************
* DISP_CHAR()
***********************************************************************************************************************************************
*Function arguments : Character data to be displayed.
*Functions Returns :Nil
*Function Description :This function will display the character data that is passed as an argument to this function
*
***********************************************************************************************************************************************/
void DISP_CHAR(char dat)
{
LRW=0;
LCD=dat;
RS=1;
EN=1;
Delay(10);
EN=0;
}
void Delay(unsigned int n)
{
unsigned int i,j;
for(i=0;i<=n;i++)
for(j=0;j<=10;j++);
}
/***************************************************************************************************
RTC_WRITE()
*****************************************************************************************************/
void RTC_WRITE()
{
if(ACCESS_RTC)
{ //IF ACCESS_RTC is not set, RTC already under use.
SSPSTAT=0b10000000;
SSPCON=0b00011000;
SSPADD=0x1F;
RW_DAT=0xFE;
RW_FLAG=0;
REPTD_START=0;
SSPIF=0;
ACKSTAT=0;
SSPIE=0;
SSPEN=1;
//SEN=1;
I2C_START;
//while(!SSPIF);
//while(RW);
//CHECK_ACK=1;
//ACKSTAT=0;
//SEND_ADDR_FLAG=1;
//SSPIF=0;
//SSPIE=1;
//SSPBUF=0b11010000;
//if(WCOL)
// {
// LCD_CMD(0xC0);
// DISP_DATA("I2C BUS ERROR");
// }
//while(!SSPIF);
//while(ACKSTAT);
//LCD_CMD(0xC0);
//DISP_CHAR('A');
ACCESS_RTC=0;
}
}
/***************************************************************************************************
RTC_READ()
*****************************************************************************************************/
void RTC_READ()
{
if(ACCESS_RTC)
{
SSPSTAT=0b10000000;
SSPCON=0b00011000;
SSPADD=0x1F;
RW_DAT=0xFE;
RW_FLAG=1;
SEND_ADDR_FLAG=1;
REPTD_START=0;
SSPIF=0;
ACKSTAT=0;
SSPIE=0;
SSPEN=1;
I2C_START;
ACCESS_RTC=0;
}
}
/***************************************************************************************************
I2C_ISR()
*****************************************************************************************************/
void I2C_ISR()
{
if(SSPIF)//&&SSPEN&&(SSPCON&0x0F==0x08))
{
//I2C INTERRUPT SERVICE ROUTINE
SSPIF=0;
if((START && !CHECK_ACK) || ACKSTAT)
{
CHECK_ACK=1;
ACKSTAT=0;
while(RW);
SSPBUF=0b11010001&RW_DAT;
// LCD_CMD(0xC0);
// DISP_CHAR('y');
if(!RW_FLAG)
{
SEND_ADDR_FLAG=1;
}
}
else if(!BF && DATA_RCVD)
{
DATE_TIME[count]=SSPBUF;
count++;
if(count==0x07)
{
count=0;
ACKDT=1;
ACKEN=1;
SEND_STOP=1;
DISP_TIME=1;
}
else
{
ACKDT=0;
ACKEN=1;
}
DATA_RCVD=0;
//BF=0;
}
else if(SEND_STOP)
{
PEN=1;
SEND_STOP=0;
}
else if(STOP)
{
// while(RW);
//SSPADD=0x00;
ACCESS_RTC=1;
SSPIE=0;
CHECK_ACK=0;
// SSPBUF=0x00;
SSPCON=0x00;
// DISP_CHAR('S');
}
else if((!ACKSTAT) && (CHECK_ACK))
{
while(RW);
if(SEND_ADDR_FLAG)
{
SSPBUF=0x00;//to be continued
count=0x00;
SEND_ADDR_FLAG=0;
//LCD_CMD(0xC0);
}
else if(!REPTD_START)
{
if(RW_FLAG==1)
{
RSEN=1;
RW_DAT=0xFF;
count=0;
REPTD_START=1;
CHECK_ACK=0;
}
else
{
if(count<0x07)
{
SSPBUF=DATE_TIME[count];
count++;
LCD_CMD(0xC0);
DISP_CHAR(count+0x30);
}
else
{
PEN=1;
CHECK_ACK=0;
}
}
}
else if(REPTD_START)
{
SSPCON2=0x08;
DATA_RCVD=1;
}
// ACKSTAT=1;
}
if(WCOL)
{
LCD_CMD(0xC0);
DISP_DATA("I2C BUS ERROR");
}
}
}
Thanking you,
Kannan