Realtime clock

General discussion on mikroC.
Post Reply
Author
Message
ascomm
Posts: 129
Joined: 30 Mar 2005 18:28
Location: Finland

Realtime clock

#1 Post by ascomm » 08 Jun 2005 19:48

I tried to convert realtime clock project (Pascal/ Basic) to C, but with no luck. Only garbage is printed to LCD. What could be wrong?
I think it is someting to do with these lines:

Code: Select all

   min = ((min && 0xF0) >> 4)*10 + (min && 0x0F);
   hour = ((hour && 0xF0) >> 4)*10 + (hour && 0x0F);


Whole program code:

Code: Select all

void gettime()
{
    unsigned short sekunti, min, hour, vuosi, vkpv, sek;
    char *txt;
    I2C_Start();                        //generate start
    I2C_Wr(0xA0);                       //write addres of PCF8583
    I2C_Wr(2);                          //select second register
    I2C_Repeated_Start();               //generate repeated start
    I2C_Wr(0xA1);                       //write address for reading info
    sekunti = I2C_Rd(1);                //read seconds
    while (I2C_Is_Idle() == 0) asm nop;
    min = I2C_Rd(1);               //read minutes
    while (I2C_Is_Idle() == 0) asm nop;
    hour = I2C_Rd(1);                  //read hours
    while (I2C_Is_Idle() == 0) asm nop;
    vuosi = I2C_Rd(1);                  //read year and days
    while (I2C_Is_Idle() == 0) asm nop;
    vkpv = I2C_Rd(0);                   //read weekday and month
    while (I2C_Is_Idle() == 0) asm nop;
    I2C_Stop();                         //generate stop


   min = ((min && 0xF0) >> 4)*10 + (min && 0x0F);
   hour = ((hour && 0xF0) >> 4)*10 + (hour && 0x0F);

   byteToStr(hour,txt);
   LCD_Out(2,1,tunti);
   byteToStr(min,txt);
   LCD_Out(2,6,minuutti);
   //byteToStr(sekunti,txt);
   //LCD_Out(2,10,sekunti);

}




void main(){

  int sekunti, minuutti, tunti, paiva, kuukausi, vuosi, vkpv;
  TRISA = 0;
  TRISB = 0;                // PORTB asetetaan lähdöksi
  TRISC = 0;                // PORTC asetetaan lähdöksi
  Lcd_Init(&PORTB);         // Alustetaan LCD
  Lcd_Cmd(Lcd_CLEAR);       // Tyhjennetään näyttö
  Lcd_Cmd(Lcd_CURSOR_OFF);  // Kursori pois
  Lcd_Out(1, 1, "I2C Kello");      // Tulostetaan näyttöön, 1. rivi, 1. sarake
  Lcd_Cmd(Lcd_RETURN_HOME); // Kursori kotiin
  Delay_ms(1000);

  I2C_Init(100000);

  sekunti = 40;
  minuutti = 15;
  tunti = 11;
  paiva = 19;
  kuukausi = 12;

  sekunti = Dec2Bcd(sekunti);              //seconds
  minuutti = Dec2Bcd(minuutti);            //minuts
  tunti = Dec2Bcd(tunti);                  //hours
  paiva = Dec2Bcd(paiva);                  //days
  kuukausi = Dec2Bcd(kuukausi);            //months

  I2C_Start();                            //generate start
  I2C_Wr(0xA0);                       //write address
  I2C_Wr(0);                          //select control register
  I2C_Wr(8);                          //set year and day bit for masking
  I2C_Stop();                             //generate stop

  delay_ms(100);

  I2C_Start();                            //generate start
  I2C_Wr(0xA0);                        //write mode
  I2C_Wr(2);                          //select seconds Register
  I2C_Wr(sekunti);                          //write seconds
  I2C_Wr(minuutti);                          //write minuts
  I2C_Wr(tunti);                          //write hours
  I2C_Wr(paiva);                          //write days
  I2C_Wr(kuukausi);                      //write months
  I2C_Stop();

  while (1 == 1)
  {

    gettime();
    delay_ms(200);

  }

}


LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#2 Post by LGR » 08 Jun 2005 21:07

This first thing that I would do is confirm that the I2C communication is working correctly using something other than the LCD (USART, maybe). I would also verify that the LCD conversion routines work using numbers from a constant array. Test it in pieces.
If you know what you're doing, you're not learning anything.

ascomm
Posts: 129
Joined: 30 Mar 2005 18:28
Location: Finland

#3 Post by ascomm » 09 Jun 2005 06:09

LGR wrote:This first thing that I would do is confirm that the I2C communication is working correctly using something other than the LCD (USART, maybe). I would also verify that the LCD conversion routines work using numbers from a constant array. Test it in pieces.
I2C is workin correctly. I tested Pascal Project ant that works. Also writing to clock works fine.
If I write, say, 12:30:50 to time on on the clock, what is the output what I get ?

User avatar
rajkovic
mikroElektronika team
Posts: 694
Joined: 16 Aug 2004 12:40

Re: Realtime clock

#4 Post by rajkovic » 09 Jun 2005 09:07

ascomm wrote: I think it is someting to do with these lines:

Code: Select all

   min = ((min && 0xF0) >> 4)*10 + (min && 0x0F);
   hour = ((hour && 0xF0) >> 4)*10 + (hour && 0x0F);

you are probably right about location of problem because

Code: Select all

&&
is logical and operator
and I assume that you wanted bitwise and operator which is

Code: Select all

&
for details about these operators look in help

so your code should be

Code: Select all

   min = ((min & 0xF0) >> 4)*10 + (min & 0x0F);
   hour = ((hour & 0xF0) >> 4)*10 + (hour & 0x0F);
[/b]

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: Realtime clock

#5 Post by pizon » 09 Jun 2005 09:43

There's also issue regarding the newly introduced righthand alignment of all the xxToStr() functions, which requires additional operation (trimming spaces from the left) to be performed. You can take a look about this in the RTC example (with software I2C) that comes with mikroC.
pizon

Post Reply

Return to “mikroC General”