zakir_cool
Junior Member level 3
Hi...i am trying a C code for RTC using PIC...I am not getting the exact thing,which i want to, So can any body suggest me..that what mistake i have done..the main thing is in 8051 my program is running fine. bt here i dont know when the ack and Nack should send...i gone throgh the data sheet of DS1307.i did't got it..
This is the main file..
This is RTC file...
And this I2C file...
This is the main file..
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 #include <htc.h> #include "i2c.c" #include "rtc.c" #include "lcd.c" #include "header.h" //clock[]={seconds,minutes,hours,day_of_week,date,month,year}; unsigned char I2CData[] = {0x00, 0x15, 0x11, 0x01, 0x25, 0x01, 0x09, 0x12}; unsigned char *s[]={"SUN","MON","TUE","WED","THU","FRI","SAT"}; unsigned char add = 0, d; void main() { TRISB =0x00; // Configure PORTB as output TRISD=0x00; TRISC=0x00; init_lcd(); I2CInit(); //DS1307 I2C is running at 100KHz while(add<=6) //update real time clock { write_ds1307(add,I2CData[add]); add++; } while(1) { d=read_ds1307(0x02);//read hour write_lcd((d/16)+48); write_lcd((d%16)+48); delay_ms(10); write_lcd(':'); d=read_ds1307(0x01); // read minute; write_lcd((d/16)+48); write_lcd((d%16)+48); delay_ms(10); write_lcd(':'); d=read_ds1307(0x00); // read second write_lcd((d/16)+48); write_lcd((d%16)+48); delay_ms(10); write_lcd(' '); display_lcd(s[read_ds1307(0x03)]); // read day delay_ms(10); cmd_lcd(0xc0); d=read_ds1307(0x04); // read date write_lcd((d/16)+48); write_lcd((d%16)+48); delay_ms(10); write_lcd('/'); d=read_ds1307(0x05); // read month write_lcd((d/16)+48); write_lcd((d%16)+48); delay_ms(10); write_lcd('/'); write_lcd('2'); write_lcd('0'); d=read_ds1307(0x06); // read year write_lcd((d/16)+48); write_lcd((d%16)+48); delay_ms(75); cmd_lcd(0x01); } }
This is RTC file...
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include "header.h" unsigned char read_ds1307(unsigned char address) { unsigned char data; I2CStart(); I2CSend(0xd0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0 I2CAck(); I2CSend(address); I2CAck(); I2CRestart(); I2CSend(0xd1); //0x68 followed by 1 --> 0xD1 I2CAck(); data=I2CRead(); I2CNak(); I2CStop(); delay_ms(10); return(data); } void write_ds1307(unsigned char address,unsigned char w_data) { I2CStart(); // issue I2C start signal //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0 I2CSend(0xD0); // send byte via I2C (device address + W) I2CAck(); I2CSend(address); // send byte (address of DS1307 location) I2CAck(); I2CSend(w_data); // send data (data to be written) I2CAck(); I2CStop(); // issue I2C stop signal delay_ms(10); }
And this I2C file...
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 #include "header.h" void I2CInit() { 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 */ SSPCON = 0x28; /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */ SSPADD = 0x28; /* 100Khz @ 4Mhz Fosc */ } /* Function: I2CStart Return: Arguments: Description: Send a start condition on I2C Bus */ void I2CStart() { I2CWait(); SEN = 1; /* Start condition enabled */ while(SEN); /* automatically cleared by hardware */ /* wait for start condition to finish */ } /* Function: I2CStop Return: Arguments: Description: Send a stop condition on I2C Bus */ void I2CStop() { I2CWait(); PEN = 1; /* Stop condition enabled */ while(PEN); /* PEN automatically cleared by hardware */ /* Wait for stop condition to finish */ } /* Function: I2CRestart Return: Arguments: Description: Sends a repeated start condition on I2C Bus */ void I2CRestart() { I2CWait(); RSEN = 1; /* Repeated start enabled */ while(RSEN); /* wait for condition to finish */ } /* Function: I2CAck Return: Arguments: Description: Generates acknowledge for a transfer */ void I2CAck(){ ACKDT = 0; /* Acknowledge data bit, 0 = ACK */ ACKEN = 1; /* Ack data enabled */ while(ACKEN); /* wait for ack data to send on bus */ } /* Function: I2CNck Return: Arguments: Description: Generates Not-acknowledge for a transfer */ void I2CNak(){ ACKDT = 1; /* Acknowledge data bit, 1 = NAK */ ACKEN = 1; /* Ack data enabled */ while(ACKEN); /* wait for ack data to send on bus */ } /* Function: I2CWait Return: Arguments: Description: wait for transfer to finish */ void I2CWait(){ while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) ); /* wait for any pending transfer */ } /* Function: I2CSend Return: Arguments: dat - 8-bit data to be sent on bus data can be either address/data byte Description: Send 8-bit data on I2C bus */ void I2CSend(unsigned char dat) { //display_lcd("It's done here"); SSPBUF = dat; /* Move data to SSPBUF */ while(BF); /* wait till complete data is sent from buffer */ I2CWait(); /* wait for any pending transfer */ } /* Function: I2CRead Return: 8-bit data read from I2C bus Arguments: Description: read 8-bit data from I2C bus */ unsigned char I2CRead(void){ unsigned char temp; /* Reception works if transfer is initiated in read mode */ RCEN = 1; /* Enable data reception */ while(!BF); /* wait for buffer full */ //BF = 1; temp = SSPBUF; /* Read serial buffer and store in temp register */ I2CWait(); /* wait to check any pending transfer */ return temp; /* Return the read data from bus */ }
Last edited by a moderator: