DS1307... again

General discussion on mikroC PRO for AVR.
Author
Message
xmaspako
Posts: 29
Joined: 03 Jan 2015 19:17

Re: DS1307... again

#16 Post by xmaspako » 12 Feb 2015 20:11

Oh... great!

So finally we can consider that issue... solved!

Another question: do you think that with the following modification in I2C1_Rd() routine:

Code: Select all

while (!PIR1.SSPIF || count >= 10000) cont++;
can I solve that problem?

I read the errata you kindly suggest me, but I don't know how to implement such modification.

Thank you very much... and sorry if I'm not good enough :oops: I'm e beginner in PIC programming :wink:
______
If you can imagine it, you can do it!

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: DS1307... again

#17 Post by marina.petrovic » 13 Feb 2015 13:46

Hi,

You can try to write your own I2C1_Rd()routine which you will use instead existing routine.
In your routine, you can try to add this adjustments which my colleague suggested on the forum topic.

Unfortunately, as I don't have the same hardware which you use so I am not able to test this adjustment and know whether it can help you with your problem.

Best regards,
Marina

xmaspako
Posts: 29
Joined: 03 Jan 2015 19:17

Re: DS1307... again

#18 Post by xmaspako » 17 Feb 2015 10:06

Unluckily, in spite of these modifications, it froze again :(

The modified read function for I2C that I implemented is the following:

Code: Select all

unsigned short I2C1_Rd2(unsigned short ack) {
  unsigned short tmp;
  unsigned count=0;
  PIR1.SSPIF = 0;
  SSPCON2.RCEN = 1;
  //while (!PIR1.SSPIF) ;        // --- here you have the problem
  while (!PIR1.SSPIF || count >= 10000) count++;

  tmp = SSPBUF;
  if (ack == 0)
    SSPCON2.ACKDT = 1;
  else
    SSPCON2.ACKDT = 0;

  PIR1.SSPIF = 0;
  SSPCON2.ACKEN = 1;
  //while (!PIR1.SSPIF) ;           // --- here you have the problem
  while (!PIR1.SSPIF || count >= 10000) count++;

  return tmp;
}
At this point, where am I doing wrong?
:?
______
If you can imagine it, you can do it!

xmaspako
Posts: 29
Joined: 03 Jan 2015 19:17

Re: DS1307... again

#19 Post by xmaspako » 17 Feb 2015 14:12

I'm very stupid... the error is on count flag for exiting condition:
...
while (!PIR1.SSPIF || count >= 10000) count++; //this is wrong!!!!
...
while (!PIR1.SSPIF && count <= 10000) count++; //this is the right way ;)
...
Very sorry :oops:
______
If you can imagine it, you can do it!

BrownGray
Posts: 6
Joined: 18 May 2015 12:10

Re: DS1307... again

#20 Post by BrownGray » 18 May 2015 18:45

Code: Select all

// Lcd pinout settings
sbit LCD_RS at PORTB1_bit;
sbit LCD_EN at PORTB2_bit;
sbit LCD_D7 at PORTC0_bit;
sbit LCD_D6 at PORTC1_bit;
sbit LCD_D5 at PORTC2_bit;
sbit LCD_D4 at PORTC3_bit;

// Pin direction
sbit LCD_RS_Direction at DDB1_bit;
sbit LCD_EN_Direction at DDB2_bit;
sbit LCD_D7_Direction at DDC0_bit;
sbit LCD_D6_Direction at DDC1_bit;
sbit LCD_D5_Direction at DDC2_bit;
sbit LCD_D4_Direction at DDC3_bit;

char saniye,dakika,saat,haftanin_gunu,ayin_gunu,ay,yil,derece;
void write(){
 saniye = 0x00;
 dakika = 0x51;
 saat = 0x63;
 haftanin_gunu = 0x00;
 ayin_gunu = 0x18;
 ay = 0x06;
 yil = 0x15;
TWI_Start();   // start
TWI_Write(0xD0); // Adress Write
TWI_Write(0); // 0h'dan başla
TWI_Write(saniye);
TWI_Write(dakika);
TWI_Write(saat);
TWI_Write(haftanin_gunu);
TWI_Write(ayin_gunu);
TWI_Write(ay);
TWI_Write(yil);
TWI_Stop();
}
void oku(){
TWI_Start();   // start
TWI_Write(0xD0); // Adress Write
TWI_Write(0x00); // 0h'dan başla
TWI_Start();
TWI_Write(0xD1);
saniye=TWI_Read(1); //0h 00-59
dakika=TWI_Read(1);    //1h   00-59
saat=TWI_Read(1);      //2h   00-12
haftanin_gunu=TWI_Read(1); // 3h      00-31
ayin_gunu=TWI_Read(1); // 4h      00-07
ay=TWI_Read(1);    // 5h      00-12
yil=TWI_Read(0);   // 6h      00-99 // WARNING It must be 0 if it is 1 FREEZE TIME
TWI_Stop();
}
void donusum(){
saniye = ((saniye & 0xF0) >>4)*10 + (saniye & 0x0F);
dakika = ((dakika & 0xF0) >>4)*10 + (dakika & 0x0F);
if(saat.b5 ==0) lcd_out(2,10,"AM");
else{
lcd_out(2,10,"PM");
}
saat = ((saat & 0x10)>>4)*10 + (saat & 0x0F);
ayin_gunu = ((ayin_gunu & 0x30)>>4)*10 + (ayin_gunu & 0x0F);
ay = ((ay & 0x10)>>4)*10 + (ay & 0x0F);
yil = ((yil & 0xF0)>>4)*10 + (yil & 0x0F);
}
void lcd_yaz(){
lcd_chr(1,2, (ayin_gunu / 10)+ 48);
lcd_chr(1,3, (ayin_gunu % 10) +48);
lcd_chr(1,4,'.');
lcd_chr(1,5,(ay /10)+48);
lcd_chr(1,6,(ay % 10)+48);
lcd_chr(1,7,'.');
lcd_chr(1,8,'2');
lcd_chr(1,9,'0');
lcd_chr(1,10,(yil / 10)+48);
lcd_chr(1,11,(yil % 10)+48);
lcd_chr(2,1,(saat / 10)+48);
lcd_chr(2,2,(saat % 10)+48);
lcd_chr(2,3,':');
lcd_chr(2,4,(dakika/10)+48);
lcd_chr(2,5,(dakika % 10)+48);
lcd_chr(2,6,':');
lcd_chr(2,7,(saniye / 10)+48);
lcd_chr(2,8,(saniye % 10)+48);
}
void main() {
 Lcd_Init();          // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR);  // ekran temizle
 Lcd_Cmd(_LCD_CURSOR_OFF);  //cursoru kapat
 TWI_Init(100000);   //100 kHz
 while(1){
 oku();
 donusum();
 lcd_yaz();
 switch(haftanin_gunu){
 case 0: lcd_out(1,13,"Pzt"); break;
 case 1: lcd_out(1,13,"Sal"); break;
 case 2: lcd_out(1,13,"Car"); break;
 case 3: lcd_out(1,13,"Per"); break;
 case 4: lcd_out(1,13,"Cum"); break;
 case 5: lcd_out(1,13,"Cts"); break;
 case 6: lcd_out(1,13,"Pzr"); break;
 }}}
U can try this code.
İf it will work ,i will tell you in english.

Post Reply

Return to “mikroC PRO for AVR General”