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 char sec,minu,hour,day,month,year,date; char m1,s1,h1,d1,dt,y1,mo1; void write() { // I2C_Init(100000); // initialize full master mode Lcd8_Out(1,1,"write"); I2C_Start(); // issue start signal I2C_Wr(0xd0); // address DS1307 I2C_Wr(0); // start from word at address (REG0) I2C_Wr(0x80); // write $80 to REG0. (pause counter + 0 sec) I2C_Wr(0); // write 0 to minutes word to (REG1) I2C_Wr(0x12); // write 12 to hours word (24-hours mode)(REG2) I2C_Wr(0x05); // write 5 - Thursday (REG3) I2C_Wr(0x25); // write 25 to date word (REG4) I2C_Wr(0x09); // write 9 (September) to month word (REG5) I2C_Wr(0x08); // write 08 to year word (REG6) I2C_Stop(); // issue stop signal I2C_Start(); // issue start signal I2C_Wr(0xD0); // address DS1307 I2C_Wr(0); // start from word at address 0 I2C_Wr(0); // write 0 to REG0 (enable counting + 0 sec) I2C_Stop(); } void read() { Lcd8_Out(1,1,"read"); I2C_Start(); I2C_wr(0xD0); I2C_wr(0); I2C_Repeated_Start(); I2C_wr(0xD1); sec=I2C_Rd(1); minu=I2C_Rd(1); hour=I2C_Rd(1); day=I2C_Rd(1); date=I2C_Rd(1); month=I2C_Rd(1); year=I2C_Rd(0); I2C_Stop(); } void display() { LCD_Out(1,1,"display"); s1=Bcd2Dec(sec); Lcd8_Chr(2,7,(s1/10 +48)); Lcd8_Chr(2,8,(s1%10 +48)); Lcd8_Out(2,6,":"); m1=Bcd2Dec(minu); Lcd8_Chr(2,4,(m1/10 +48)); Lcd8_Chr(2,5,(m1%10 +48)); Lcd8_Out(2,3,":"); h1=Bcd2Dec(hour); Lcd8_Chr(2,1,(h1/10 +48)); Lcd8_Chr(2,2,(h1%10 +48)); d1=Bcd2Dec(day); Lcd8_Chr(1,5,(d1/10 +48)); Lcd8_Chr(1,6,(d1%10 +48)); Lcd8_Out(1,11,"/"); dt=Bcd2Dec(date); Lcd8_Chr(1,9,(dt/10 +48)); Lcd8_Chr(1,10,(dt%10 +48)); mo1=Bcd2Dec(month); Lcd8_Chr(1,12,(mo1/10 +48)); Lcd8_Chr(1,13,(mo1%10 +48)); Lcd8_Out(1,14,"/"); y1=Bcd2Dec(year); Lcd8_Chr(1,15,(y1/10 +48)); Lcd8_Chr(1,16,(y1%10 +48)); } void main() { trisc.f4=0; trisc.f3=0; Lcd8_Config(&portc,&portb,0,2,1,7,6,5,4,3,2,1,0); Lcd8_Out(1,1,"start"); Delay_ms(1000); I2C_Init(100000); write(); while(1) { read(); display(); } }
Lcd8_Config(&portc,&portb,0,2,1,7,6,5,4,3,2,1,0);
i have only 7 days to sumbit my project and i am unable to do it, please somebody give me simulation on proteus 6.9 and code. please
If you're going to write the code yourself or work on the project, I recommend you use mikroC PRO for PIC instead of the older mikroC (if you use mikroC, that is). You can download it for free from here:
https://www.mikroe.com/mikroc/pic/
Hi! Tahmid
Is that mikroC v 8.1/8.2? Is it free?
Can you post your circuit. I will post the mikroC Pro code for DS1307 in 2 hrs. I have a working code using mikroC Pro
#include<htc.h>
#define _XTAL_FREQ 4000000 // defines the crystal used
__CONFIG(0X2F0A);
void ready()
{
TRISB7=1;
RB7 = 1;
RE0=0;
RE1=1;
while (RB7==1)
{
RE2=0;
RE2=1;
}
TRISB7=0;
}
//To send the COMMANDS for LCD
void command(unsigned char i)
{
ready();
PORTB=i;
RE0=0;
RE1=0;
RE2=1;
RE2=0;
}
//To send the DATA for LCD
void da_dis1(unsigned char i)
{
ready();
PORTB=i;
RE0=1;
RE1=0;
RE2=1;
RE2=0;
}
//to send i2c data
void rrr(unsigned char i)
{
unsigned char j;
//command(0xc8);
j=(i&0x0f)+'0';
i=i&0x7f;
i=(i>>4)+'0';
da_dis1(i);
da_dis1(j);
}
void lcd_init()
{
command(0x38);
command(0x0e);
command(0x01);
command(0x06);
command(0x80);
}
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
SMP=1;
SSPCON = 0x28; // SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1))
SSPADD = 0x0a; // 100Khz @ 4Mhz Fosc
}
void I2C1_Start()
{
SEN = 1; // Start condition enabled
while(SEN); // automatically cleared by hardware wait for start condition to finish
}
void I2C1_Stop()
{
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; // Acknowledge data bit, 1 = NAK
ACKEN = 1; // Ack data enabled
while(ACKEN); // wait for ack data to send on bus
}
void I2CWait(){
while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) ); // wait for any pending transfer
}
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;
RCEN = 1; // Enable data reception
while(!BF); // wait for buffer full
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
}
void main(){
/* Buffer where we will read/write our data */
unsigned char I2CData[] = {0x00, 0x40, 0x18, 0x03, 0x17, 0x10, 0x12, 0x10};
unsigned char i;
ADCON1 =0x06 ; // Changes PORTA and PORTE to digital
TRISA = 0x00 ; // Configure PORTA as output
TRISD = 0X00 ; // Configure PORTD as output
TRISB = 0x00 ; // Configure PORTB as output
TRISC = 0xff ; // Configure PORTC as input
TRISE = 0x00 ; // Configure PORTE as output
OPTION_REG=0X00; // to enble internal pull up register for port B
lcd_init();
I2CInit(); // Initialize I2C Port
//write Valves to RTC
I2C1_Start(); // Send Start condition
I2CSend(0xD0); // Send DS1307 slave address with write operation
I2CSend(0x00); //Send subaddress 0x00, we are writing to this location
// Loop to write 8 bytes //
for(i=0;i<8;i++)
I2CSend(I2CData[i]); //send I2C data one by one
I2C1_Stop(); //Send a stop condition - as transfer finishes
//reading data from DS1307
while(1)
{
I2C1_Start();
I2CSend(0xD0); // Send slave address with write
I2CSend(0x00); //Initialazing address pointer
I2CRestart(); //Send a repeated start, For to start reading
I2CSend(0xD1); // send slave address with read bit set
/* Loop to read 8 bytes from I2C slave */
for(i=7;i>0;i--) {
I2CData[i] = I2CRead(); //read a byte
if(i-1) I2CAck(); else I2CNak(); //ACK if its not the last byte to read if its the last byte then send a NAK
}
I2C1_Stop(); //Send stop
//display time
command(0x80);
rrr(I2CData[5]); //hour
da_dis1(':');
//command(0x83);
rrr(I2CData[6]); //minutes
da_dis1(':');
//command(0x86);
rrr(I2CData[7]); //Seconds
command(0xc0);
rrr(I2CData[3]); //Date
da_dis1('-');
rrr(I2CData[2]); //Month
da_dis1('-');
rrr(I2CData[1]); //year
}
}
#ifndef DS1307
#define DS1307 0xD0
#endif
// LCD module connections
sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB2_bit;
sbit LCD_D5 at LATB3_bit;
sbit LCD_D6 at LATB6_bit;
sbit LCD_D7 at LATB7_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
unsigned char num,flag;
char b;
char x;
int cnt;
unsigned int ch_cnt = 0;
char colon[] = ":";
unsigned char year1, month1, day1, hours1, mins2, seconds1;
char i;
unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];
int error, am_pm = 0;
char time_format[3];
int ihr;
char sihr[17], data2log[38], data2log1[23];
unsigned int m, log_data_cnt = 0;
void Zero_Fill(char *value) {
if (value[1] == 0) {
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}
void Write_Time() {
I2C2_Start(); // issue start signal
I2C2_Wr(DS1307); // address DS1307
I2C2_Wr(0); // start from word at address (REG0)
I2C2_Wr(0x80); // write $80 to REG0. (pause counter + 0 sec)
I2C2_Wr(0); // write 0 to minutes word to (REG1)
I2C2_Wr(0x17); // write 17 to hours word (24-hours mode)(REG2)
I2C2_Wr(0x02); // write 2 - Monday (REG3)
I2C2_Wr(0x04); // write 4 to date word (REG4)
I2C2_Wr(0x05); // write 5 (May) to month word (REG5)
I2C2_Wr(0x01); // write 01 to year word (REG6)
I2C2_Stop(); // issue stop signal
I2C2_Start(); // issue start signal
I2C2_Wr(0xD0); // address DS1307
I2C2_Wr(0); // start from word at address 0
I2C2_Wr(0); // write 0 to REG0 (enable counting + 0 sec)
I2C2_Stop(); // issue stop signal
}
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
I2C2_Start();
I2C2_Wr(DS1307);
I2C2_Wr(0);
I2C2_Repeated_Start();
I2C2_Wr(0xD1);
*sec =I2C2_Rd(1);
*min =I2C2_Rd(1);
*hr =I2C2_Rd(1);
*week_day =I2C2_Rd(1);
*day =I2C2_Rd(1);
*mn =I2C2_Rd(1);
*year =I2C2_Rd(0);
I2C2_Stop();
}
void Transform_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
*sec = ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day = ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
*year = ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
Lcd_Out(1,1,"Date:");
switch(week_day){
case 1: txt="Sun"; break;
case 2: txt="Mon"; break;
case 3: txt="Tue"; break;
case 4: txt="Wed"; break;
case 5: txt="Thu"; break;
case 6: txt="Fri"; break;
case 7: txt="Sat"; break;
}
Lcd_Out(1,7,txt);
Lcd_Chr(1,11,(day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1,12, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1,13,'/');
Lcd_Chr(1,14,(mn / 10) + 48);
Lcd_Chr(1,15,(mn % 10) + 48);
Lcd_Chr(1,16,'/');
Lcd_Out(1,17,"2");
Lcd_Out(1,18,"0");
Lcd_Chr(1,19, (year / 10) + 48); // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,20, (year % 10) + 48);
if(hr >= 12) {
Lcd_Out(2,7,"PM");
am_pm = 1;
}
else if(hr < 12) {
Lcd_Out(2,7,"AM");
am_pm = 0;
}
Lcd_Chr(2,11,(hr / 10) + 48);
Lcd_Chr(2,12,(hr % 10) + 48);
Lcd_Out(2,13,":");
Lcd_Chr(2,14,(min / 10) + 48);
Lcd_Chr(2,15,(min % 10) + 48);
Lcd_Out(2,16,":");
Lcd_Chr(2,17,(sec / 10) + 48);
Lcd_Chr(2,18,(sec % 10) + 48);
}
void main() {
TRISA = 0b00000011;
PORTA = 0b00000000;
LATA = 0b00000000;
TRISB = 0b00100000;
PORTB = 0b00000000;
LATB = 0b00000000;
TRISD = 0b00001111;
LATD = 0b00000000;
PORTD = 0b00000000;
TRISC = 0b00000000;
PORTC = 0b00000000;
LATC = 0b00000000;
TRISE = 0b111;
LATE = 0x00;
PORTE = 0x00;
ANCON0 = 0b00011100;
ANCON1 = 0b10011100;
ANCON1.PCFG11 = 1;
ADCON0 = 0b00000000;
//ADCON1 = 0b10110101;
ADCON1 = 0b10110101;
//CONFIG3H = 0b00001010;
CM1CON.CON = 0;
CM2CON.CON = 0;
cnt = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"RTC");
I2C2_Init(100000);
Delay_ms(3000);
Lcd_Cmd(_LCD_CLEAR);
txt = "Time:";
Lcd_Out(2,1,txt);
PORTC.F2 = 1;
while(1) {
//Write_Time();
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Display_Time(sec, min1, hr, week_day, day, mn, year);
}
}
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?