PA3040
Advanced Member level 3
- Joined
- Aug 1, 2011
- Messages
- 883
- Helped
- 43
- Reputation
- 88
- Reaction score
- 43
- Trophy points
- 1,308
- Activity points
- 6,936
void I2CWait(){
while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) ); /* wait for any pending transfer */
}
void I2CInit(void){
TRISC3 = 1; /* SDA and SCL as input pin */
TRISC4 = 1; /* these pins can be configured either i/p or o/p */
SSPSTAT |= 0x80; /* Slew rate disabled */
SSPCON1 = 0x28; /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
SSPADD = 0x28; /* 100Khz @ 4Mhz Fosc */
}
void I2CStart(){
I2CWait();
SEN = 1;
while(SEN);
}
void I2CStop(){
PEN = 1; /* Stop condition enabled */
while(PEN); /* Wait for stop condition to finish */
/* PEN automatically cleared by hardware */
}
void I2CRestart(){
RSEN = 1; /* Repeated start enabled */
while(RSEN); /* wait for condition to finish */
}
void I2CAck(){
ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
ACKEN = 1; /* Ack data enabled */
while(ACKEN); /* wait for ack data to send on bus */
}
void I2CNak(){
ACKDT = 1; /* Not acknowledge data bit, 1 = NAK */
ACKEN = 1; /* Not Ack data enabled */
while(ACKEN); /* wait for Not ack data to send on bus */
}
void I2CAck_status(){
ACKSTAT = 1;
while(ACKSTAT); /* wait for Not ack data to send on bus */
}
void I2CSend(unsigned char dat){
SSPBUF = dat; /* Move data to SSPBUF */
while(BF); /* wait till complete data is sent from buffer */
I2CWait(); /* wait for any pending transfer */
}
unsigned char I2CRead(void){
unsigned char temp;
I2CWait(); /* Reception works if transfer is initiated in read mode */
RCEN = 1; /* Enable data reception */
while(!BF); /* wait for buffer full */
temp = SSPBUF; /* Read serial buffer and store in temp register */
/* wait to check any pending transfer */
}
what is the Oscillator frequency you are using with PIC18f45XX
as Value of SSPADD will be changed as the Baud rate generator roll over occurs here differently
- - - Updated - - -
what is the Oscillator frequency you are using with PIC18f45XX
as Value of SSPADD will be changed as the Baud rate generator roll over occurs here differently
#include <htc.h>
#define _XTAL_FREQ 4000000 // 4 MHz clock
#include <delays.h>
#pragma config OSC = XT
#pragma config OSCS = ON
#pragma config WDT = OFF
#pragma config LVP = OFF
#define LCD_EN RB3
#define LCD_RS RB5
#define LCD_RW RB4
#define LCD_DATA PORTD
void lcddata(unsigned char value);
void display();
void lcd_init();
unsigned char i;
char menu_name [5][10] = {"SET Time" , "SET Alarm" , "Alarm ON " , "Alarm OFF" , "Menu "};
unsigned char second = 0x55;
unsigned char minute = 0x59;
unsigned char hour = 0x23;
unsigned char day = 2;
unsigned char date = 20;
unsigned char month = 12;
unsigned char year = 13;
unsigned char secondalarm , minutealarm , houralarm , dayalarm , datealarm , monthalarm , yearalarm ;
char weekdayname[8][4] = {"day","Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",};
char MonthName[13][4] = {" ","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",};
char sub_menu_name[8][11]= { "Set Second","Set Minute" ,"Set Hour ","Set Date ","Set Month ","Set Year ","Set Day "};
unsigned char ampm[]= {'A','M','P','M'};
unsigned char tmp;
unsigned char i;
unsigned char k;
unsigned char x =26;
unsigned char flag = 0;
bit inc;
bit dec;
bit set = 0;
bit inc_dec;
bit alarm_off;
bit alarm_on;
bit set_alarm = 0;
bit set_time = 0;
bit menu_bit = 0;
void I2Cwrite();
void I2Cread();
void I2CInit(void);
void I2CStart(void);
void I2CStop(void);
void I2CStart(void);
void I2CRestart(void);
void I2CAck();
void I2CNak();
void I2CAck_status();
void I2CSend(unsigned char dat);
unsigned char I2CRead(void);
void rtc_tx_init ();
void rtc_to_rs232();
void rs232_to_rtc();
void printWeekDayNames();
void AMPM ();
void Menu();
void rx_int_init();
void interrupt isr ();
void Adjust_up();
void Adjust_down();
unsigned char decToBcd(unsigned char val);
unsigned char bcdToDec(unsigned char val);
void printDec(unsigned char val);
void Transform_Time();
void Display_Time();
void bcdToascii(unsigned char value);
void setalarm();
void I2CWait(){
while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) ); /* wait for any pending transfer */
}
void I2CInit(void){
TRISC3 = 1; /* SDA and SCL as input pin */
TRISC4 = 1; /* these pins can be configured either i/p or o/p */
SSPSTAT |= 0b10000000; //0X80 /* Slew rate disabled */
SSPCON1 = 0b00101000; //0x28; //0X28 /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
SSPCON2 = 0x00;
SSPADD = 9; /* 0X28100Khz @ 4Mhz Fosc */
}
void I2CStart(){
I2CWait();
SEN = 1;
while(SEN);
}
void I2CStop(){
PEN = 1; /* Stop condition enabled */
while(PEN); /* Wait for stop condition to finish */
/* PEN automatically cleared by hardware */
}
void I2CRestart(){
RSEN = 1; /* Repeated start enabled */
while(RSEN); /* wait for condition to finish */
}
void I2CAck(){
ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
ACKEN = 1; /* Ack data enabled */
while(ACKEN); /* wait for ack data to send on bus */
}
void I2CNak(){
ACKDT = 1; /* Not acknowledge data bit, 1 = NAK */
ACKEN = 1; /* Not Ack data enabled */
while(ACKEN); /* wait for Not ack data to send on bus */
}
void I2CAck_status(){
ACKSTAT = 1;
while(ACKSTAT); /* wait for Not ack data to send on bus */
}
void I2CSend(unsigned char dat){
SSPBUF = dat; /* Move data to SSPBUF */
while(BF); /* wait till complete data is sent from buffer */
I2CWait(); /* wait for any pending transfer */
}
unsigned char I2CRead(void){
unsigned char temp;
I2CWait(); /* Reception works if transfer is initiated in read mode */
RCEN = 1; /* Enable data reception */
while(!BF); /* wait for buffer full */
temp = SSPBUF; /* Read serial buffer and store in temp register */
/* wait to check any pending transfer */
}
void lcddata(unsigned char value){
LCD_DATA = value;
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 1;
__delay_ms (1);
LCD_EN = 0;
}
void lcdcmd(unsigned char value){
LCD_DATA = value;
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 1;
__delay_ms (1);
LCD_EN = 0;
}
void main(){
TRISD = 0x00;
TRISB = 0x00;
I2CInit();
lcd_init();
I2Cwrite();
while(1){
I2Cread();
Transform_Time();
Display_Time();
}
}
void printWeekDayNames(){
unsigned char j =0;
for(j=0;j<3;j++){
lcddata(weekdayname[day][j]);
}
}
//Convert normal decimal numbers to binary coded decimal
unsigned char decToBcd(unsigned char val){
return ( (val/10*16) + (val%10) );
}
//Convert binary coded decimal to normal decimal numbers
unsigned char bcdToDec(unsigned char val){
return ( (val/16*10) + (val%16) );
}
void lcd_init(){
LCD_EN =0;
__delay_ms(150);
lcdcmd(0x38);
__delay_ms(175);
lcdcmd(0x0e);
__delay_ms(15);
lcdcmd(0x01);
__delay_ms(10);
lcdcmd(0x06);
__delay_ms(10);
lcdcmd(0x80);
__delay_ms(10);
lcdcmd(0x0c);
__delay_ms(10);
}
void I2Cwrite(){
I2CStart(); /* Send Start condition */
I2CSend(0xD0); /* Send DS1307 slave address with write operation */
I2CAck_status();
I2CSend(0x00); /*Send subaddress 0x00,we are writing to this location */
I2CAck_status();
I2CSend(0x80); //write $80 to REG0. (pause counter + 0 sec)
I2CAck_status();
I2CSend(minute); //SetTime[1]
I2CAck_status();
I2CSend(hour); //12 hour mode 11 PM
I2CAck_status();
I2CSend(day); //write day of week
I2CAck_status();
I2CSend(date); // write date 31
I2CAck_status();
I2CSend(month); // write month dec
I2CAck_status();
I2CSend(year);
I2CAck_status();
I2CRestart();
I2CSend(0xD0); /* Send DS1307 slave address with write operation */
I2CAck_status();
I2CSend(0x00);
I2CAck_status();
I2CSend(second);
I2CAck_status();
I2CStop();
}
void I2Cread(){
I2CStart();
I2CSend(0xD0); /* Send slave address with write */
I2CAck_status();
I2CSend(0x00); /* Send a repeated start, after a dummy write to start reading */
I2CAck_status();
I2CRestart();
I2CSend(0xD1); /* send slave address with read bit set */
I2CAck_status();
second = I2CRead();
I2CAck();
minute = I2CRead();
I2CAck();
hour = I2CRead();
I2CAck();
day = I2CRead();
I2CAck();
date = I2CRead();
I2CAck();
month = I2CRead();
I2CAck();
year = I2CRead();
I2CNak();
I2CStop(); /* Send stop */
}
void Display_Time() {
lcdcmd(0x80);
printWeekDayNames();
lcddata(' ');
lcddata(' ');
lcddata ((hour / 10) + 48);
lcddata ((hour % 10) + 48);
lcddata(':');
lcddata ((minute / 10) + 48);
lcddata ((minute % 10) + 48);
lcddata(':');
lcddata ((second / 10) + 48);
lcddata ((second % 10) + 48);
lcdcmd(0xc1);
lcddata((date / 10) + 48); // Print tens digit of day variable
lcddata((date % 10) + 48); // Print oness digit of day variable
lcddata(' ');
for (i=0;i<3;i++){
lcddata(MonthName[month][i]);
}
lcddata(' ');
lcddata('2');
lcddata('0');
lcddata((year / 10) + 48);
lcddata((year % 10) + 48);
x--;
}
void Transform_Time() {
second = ((second & 0xF0) >> 4)*10 + (second & 0x0F); // Transform seconds
minute = ((minute & 0xF0) >> 4)*10 + (minute & 0x0F); // Transform months
hour = ((hour & 0xF0) >> 4)*10 + (hour & 0x0F); // Transform hours
date = ((date & 0xF0) >> 4)*10 + (date & 0x0F); // Transform hours
day =(day & 0x07);
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); // Transform month
year = ((year & 0x10) >> 4)*10 + (year & 0x0F); // Transform month
// day = ((day & 0xf0) >> 4)*10 + (day & 0x07); // Transform day****
}
Zip and post all Hi-Tech C project files and also Proteus file.
Ok. Zip and post Proteus 7.x format file.
If you are using your own i2c functions then why have you included i2c.h file? You should not include i2c.h file. It is Hi-Tech C library file for i2c. If you want to use library file then remove your i2c functions.
Edit: As you are using PIC18F you have to use LATx for LCD. You have to connect 3V battery to RTC.
have to use LATx for LCD.
#define LCD_EN LATB3
#define LCD_RS LATB5
#define LCD_RW LATB4
#define LCD_DATA LATD
You have to connect 3V battery to RTC
unsigned char I2CRead(){
unsigned char temp;
I2CWait();
RCEN = 1;
while(!BF);
temp = SSPBUF;
return SSPBUF;
}
unsigned char I2CRead(){
unsigned char temp;
I2CWait();
RCEN = 1;
while(!BF);
temp = SSPBUF;
}
Show how do you use the result of both result function..
void I2Cread(){
I2CStart();
I2CSend(0xD0);
I2CAck_status();
I2CSend(0x00);
I2CAck_status();
I2CRestart();
I2CSend(0xD1);
I2CAck_status();
second = I2CRead();
I2CAck();
minute = I2CRead();
I2CAck();
hour = I2CRead();
I2CAck();
day = I2CRead();
I2CAck();
date = I2CRead();
I2CAck();
month = I2CRead();
I2CAck();
year = I2CRead();
I2CNak();
I2CStop();
}
Show in both cases the compiling report .
Clean: Deleted file "C:\PIC\PIC18\pic18.p1".
Clean: Deleted file "C:\PIC\PIC18\pic18.c.cof".
Clean: Deleted file "C:\PIC\PIC18\pic18.c.hex".
Clean: Deleted file "C:\PIC\PIC18\pic18.c.sym".
Clean: Deleted file "C:\PIC\PIC18\pic18.c.map".
Clean: Deleted file "C:\PIC\PIC18\pic18.c.hxl".
Clean: Deleted file "C:\PIC\PIC18\startup.lst".
Clean: Deleted file "C:\PIC\PIC18\startup.rlf".
Clean Warning: File "C:\PIC\PIC18\doprnt.p1" doesn't exist.
Clean Warning: File "C:\PIC\PIC18\doprnt.pre" doesn't exist.
Clean: Deleted file "C:\PIC\PIC18\pic18.c.obj".
Clean: Deleted file "C:\PIC\PIC18\pic18.c.lst".
Clean: Deleted file "C:\PIC\PIC18\pic18.c.rlf".
Clean: Deleted file "C:\PIC\PIC18\pic18.c.sdb".
Clean: Done.
Build C:\PIC\PIC18\pic18.c for device 18F452
Using driver C:\Program Files\HI-TECH Software\PICC-18\9.80\bin\picc18.exe
Executing: "C:\Program Files\HI-TECH Software\PICC-18\9.80\bin\picc18.exe" --pass1 C:\PIC\PIC18\pic18.c -q --chip=18F452 -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Warning [340] C:\PIC\PIC18\pic18.c; 27.96 string not terminated by null character
Executing: "C:\Program Files\HI-TECH Software\PICC-18\9.80\bin\picc18.exe" -opic18.c.cof -mpic18.c.map --summary=default --output=default pic18.p1 --chip=18F452 -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
(1273) Omniscient Code Generation not available in Lite mode (warning)
HI-TECH C Compiler for PIC18 MCUs (Lite Mode) V9.80
Copyright (C) 2011 Microchip Technology Inc.
Advisory[1233] Employing 18F452 errata work-arounds:
Advisory[1234] * Address 4000h boundary
Advisory[1234] * Faulty table reads at -40 degrees C
Advisory[1234] * GOTO instruction at reset vector must be preceded by NOP
Advisory[1234] * Peripherals can misbehave if BSR = 15
Advisory[1234] * DAW instruction may improperly clear CARRY bit
Advisory[1234] * Specifically disable interrupt sources during tblwt instruction
Advisory[1234] * Writes to flash must target opposite side of 4000h boundary
Memory Summary:
Program space used 1548h ( 5448) of 8000h bytes ( 16.6%)
Data space used 11Ch ( 284) of 600h bytes ( 18.5%)
Configuration bits used 3h ( 3) of 7h words ( 42.9%)
EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)
ID Location space used 0h ( 0) of 8h nibbles ( 0.0%)
Running this compiler in PRO mode, with Omniscient Code Generation enabled,
often produces code which is 60% smaller and at least 400% faster than in
Lite mode. The HI-TECH C PRO compiler output for this code could be
3217 bytes smaller and run 4 times faster.
See http://www.microchip.com for more information.
Loaded C:\PIC\PIC18\pic18.c.cof.
********** Build successful! **********
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.p1".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.cof".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.hex".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.sym".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.map".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.hxl".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\startup.lst".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\startup.rlf".
Clean Warning: File "C:\PIC\I2C+LCD + USART_sec\doprnt.p1" doesn't exist.
Clean Warning: File "C:\PIC\I2C+LCD + USART_sec\doprnt.pre" doesn't exist.
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.obj".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.lst".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.rlf".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.sdb".
Clean: Deleted file "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.mcs".
Clean: Done.
Build C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c for device 16F877A
Using driver C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe
Executing: "C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 "C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c" -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Warning [340] C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c; 22.96 string not terminated by null character
Warning [343] C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c; 129.1 implicit return at end of non-void function
Executing: "C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" "-oI2C+LCD + USART.c.cof" "-mI2C+LCD + USART.c.map" --summary=default --output=default "I2C+LCD + USART.p1" --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.83
Copyright (C) 2011 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)
Memory Summary:
Program space used 1052h ( 4178) of 2000h words ( 51.0%)
Data space used 111h ( 273) of 170h bytes ( 74.2%)
EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)
Configuration bits used 1h ( 1) of 1h word (100.0%)
ID Location space used 0h ( 0) of 4h bytes ( 0.0%)
Running this compiler in PRO mode, with Omniscient Code Generation enabled,
produces code which is typically 40% smaller than in Lite mode.
The HI-TECH C PRO compiler output for this code could be 1671 words smaller.
See http://microchip.htsoft.com/portal/pic_pro for more information.
Loaded C:\PIC\I2C+LCD + USART_sec\I2C+LCD + USART.c.cof.
********** Build successful! **********
temp variable is declared as local inside the function
unsigned char I2CRead(){
unsigned char temp;
I2CWait();
RCEN = 1;
while(!BF);
temp = SSPBUF;
return temp;
}
maybe because MCU register are global variable.
I think in the matter of 16F, To transfer the value from SSPBUF to temp register ( Local veritable ) it should goes through W register. that mean the value exist in both W register and temp register. in this situation without "return " syntax, the W register value can transfer to any register any ware in the hardware level. What do you think?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?