• 0

Question

Right, I don't know if I'm being stupid or what but I'm reading the time from an RTC chip via I2C and storing it in an array, and then processing it as below... For some reason, Data[8] shows the seconds fine but Data[0] always shows 0, but I can't really see why? Am I doing something wrong or is this a bug?

while (i < 7)
{
IdleI2C();
Data[i] = ReadI2C();
++i;
}

Data[8] = Data[0] & 0b01111111;
Data[0] = Data[0] & 0b01111111; //Seconds
Data[1] = Data[1] & 0b01111111; //Minutes
Data[2] = Data[2] & 0b01111111; //Hours
Data[3] = Data[3] & 0b00000111; //Days
Data[4] = Data[4] & 0b00111111; //Date
Data[5] = Data[5] & 0b00011111; //Month
Data[6] = Data[6] & 0b00011111; //Year
Data[7] = Data[7] & 0b00011111; //Control

Data[0] = ((Data[0] & 0b01110000)>>4)*10 + (Data[0] & 0b00001111);
Data[1] = ((Data[1] & 0b01110000)>>4)*10 + (Data[1] & 0b00001111);
if ((Data[2] & 0b01000000) == 0b01000000)
{
    //24 hour
    Data[2] = ((Data[2] & 0b00110000)>>4)*10 + (Data[2] & 0b00001111);
}
else
{
    //12 hour
    Data[2] = ((Data[2] & 0b00010000)>>4)*10 + (Data[2] & 0b00001111);
}
Data[4] = ((Data[4] & 0b00010000)>>4)*10 + (Data[4] & 0b00001111);
Data[5] = ((Data[5] & 0b00010000)>>4)*10 + (Data[5] & 0b00001111);
Data[6] = ((Data[6] & 0b11110000)>>4)*10 + (Data[6] & 0b00001111);
Data[8] = ((Data[8] & 0b01110000)>>4)*10 + (Data[8] & 0b00001111);

sprintf(Bob, "%d%c", Data[2], 128);
LCDWriteString(&Bob);
sprintf(Bob, ":%d%c", Data[1], 128);
LCDWriteString(&Bob);
sprintf(Bob, ":%d%c", Data[0], 128); //Always shows 0
LCDWriteString(&Bob);
sprintf(Bob, ":%d%c", Data[8], 128); //Shows seconds correctly
LCDWriteString(&Bob);
sprintf(Bob, ":%d%c", Data[0], 128); //Again, always shows 0
LCDWriteString(&Bob);
LCDPosition(2); //Goes to 2nd line
sprintf(Bob, " %d%c", Data[3], 128);
LCDWriteString(&Bob);
sprintf(Bob, ":%d%c", Data[4], 128);
LCDWriteString(&Bob);
sprintf(Bob, ":%d%c", Data[5], 128);
LCDWriteString(&Bob);
sprintf(Bob, ":%d%c", Data[6], 128);
LCDWriteString(&Bob);
sprintf(Bob, ",%d%c", Data[8], 128);
LCDWriteString(&Bob);
LCDPosition(1); //Goes back to first line

Link to comment
https://www.neowin.net/forum/topic/1140794-mplabx-xc16-bug/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Yet another bug, this program seems like complete and utter shitware. It's noted for being a C compile, right, I'd call it a BASIC interpretor that ****s up as soon as you throw anything mediocre at it.

DayNumToDay(Data[3], &Bob);
sprintf(Bob, "%s%c", Bob, 128);
LCDWriteString(&Bob);

Works, outputs 'Sun'. The LCDWriteString() routine outputs each character until it finds 128, I was trying with sizeof but it appears that MPLAB doesn't like it for some stupid pathetic reason or other.

DayNumToDay(Data[3], &Bob);
sprintf(Bob, " %s%c", Bob, 128);
LCDWriteString(&Bob);

Doesn't, it messes up the LCD completely and I have to put an 'if (i < 8)...' inside the LCDWriteString otherwise (I presume) it endlessly prints out spaces and never gets to the 128 character. Now, I have absolutely NO IDEA WHAT the addition of a SINGLE SPACE character would do this.

Bob is defined as unsigned char Bob[10];

The only thing I can think is that after the 4th byte it skips to a completely different memory location, which is a complete and utter ****ing joke.

So please someone, enlighten me as to how I'm going wrong or if it's just better to dump this (so far) pathetic 4gb waste of complete tosh into the trashcan and be rid of it forever?

  • 0

I'm unsure of why the code in your first post does not work. It looks like you do the same things to Data[8] that you do to Data[0]. Try simulating the code on your computer using a known-good compiler (like GCC or Clang) by creating a ReadI2C() that gives you some output you would expect from your RTC chip. It will probably also be fairly easy to replace LCDWriteString() with puts() for the purpose of your simulation too. If it still doesn't do what you expect, you can at least run it through GDB to figure out why. If the simulation works right then it might be a compiler bug; you can submit a bug report to Microchip using their ticket system at http://support.microchip.com. Submitting the code to reproduce the bug (and the probably the simulation too) will probably get you a faster response.

As for your second problem, I have also encountered many bugs with sprintf() and family in Microchip's compilers (HI-TECH C for the PIC18F and XC16 for the PIC24F). One I remember in particular caused my program to freeze indefinitely (crash?) every time I tried to printf() a float using "%f". However every time I tried to output the same variable using "%x" it worked. The other floating point format specifiers are not supported by Microchip's printf() (I.E. "%F", "%e", "%E", "%g", "%G"). Although XC16 is based on GCC, its standard library is not based on glibc - its Microchip proprietary - and therefore has many bugs. I recommend that you report the bug to Microchip, including code necessary to reproduce it.

This topic is now closed to further replies.
  • Posts

    • Also, Guru3D.com always has some pros and experts lurking in the comments.
    • If anyone is wondering, it's 30fps at 4K
    • This 4K webcam from Acer is now only $59.99 by Taras Buria Those looking for an affordable, high-resolution webcam from a reputable manufacturer can check out the latest deal from Acer, which puts its 4K webcam at a solid price. Thanks to a 14% discount, you can buy this all-metal 4K webcam from Acer for just $59.99. Despite the affordable price tag, the webcam has a pretty large 1/2" CMOS sensor manufactured by Sony. Apart from its size, it supports additional conveniences, such as high dynamic range and phase-detection autofocus (PDAF). Plus, the camera ensures you do not have to buy additional audio equipment, as it comes with two built-in high-sensitivity microphones, a built-in background noise filter, and the ability to pick up your voice from up to 2.5 meters away. Acer is not cheaping out on materials, and the webcam is enclosed in a more premium metal case, which is also good at heat dissipation (high-resolution cameras can get quite hot). And to make sure no one is peeping at you when the camera is in use, there is a magnetic cover also made of metal. Additional conveniences include an LED status indicator and a built-in mount that lets you place a camera on a tripod. Acer 4K Webcam for PC/Mac with All-Metal Unibody Sculpted - $59.99 | 14% off Good to know This Amazon deal is U.S.-specific and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited, or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • Conversation Starter
      NovaEdgeX earned a badge
      Conversation Starter
    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      +Edouard
      185
    3. 3
      PsYcHoKiLLa
      107
    4. 4
      Steven P.
      87
    5. 5
      ATLien_0
      67
  • Tell a friend

    Love Neowin? Tell a friend!