• 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

    • Microsoft now allows you to tweak Visual Studio to new extremes by Usama Jawad Visual Studio 2026 recently netted a powerful feature that finally allows developers to review Git Pull Requests (PRs) directly in the integrated development environment (IDE), without needing to switch to the browser. However, that's not all that Microsoft introduced in the latest version, as it also offered a useful way to customize the IDE to their heart's content. Although Visual Studio 2026 already provides lots of stock themes based on Fluent Design principles, Microsoft understands that people still want to customize them further according to their preferences. The IDE now offers a new configuration page for themes under Tools > Options > Environment > Visual Experience > Theme colors. This allows you to set hex color codes for accent colors, hover states, and more, and apply them without requiring a restart. The Redmond tech giant further says that: Microsoft is also offering granular color tokens that allow you to customize various UX elements like the tab header without impacting the rest of the shell chrome. Your themes are also stored in JSON format in %LOCALAPPDATA%\Microsoft\VisualStudio\18.0_xxxxxxxx\ColorThemes, which basically enables you to share it with others or override existing themes. This is a major improvement in terms of the user experience powering these customization capabilities. This is because extensions were required to replace the theme, and it was not very easy to make minor adjustments. This theme configuration UX solves this problem and is available in Visual Studio 2026 version 18.7, available here.
    • AnyDesk 9.7.6 by Razvan Serea AnyDesk is a fast remote desktop system and enables users to access their data, images, videos and applications from anywhere and at any time, and also to share it with others. AnyDesk is the first remote desktop software that doesn't require you to think about what you can do. CAD, video editing or simply working comfortably with an office suite for hours are just a few examples. AnyDesk is designed for modern multi-core CPUs. Most of AnyDesk's image processing is done con­currently. This way, AnyDesk can utilize up to 90% of modern CPUs. AnyDesk works across multiple platforms and operating systems: Windows, Linux, Free BSD, Mac OS, iOS and Android. Just 7 megabytes - downloaded in a glimpse, sent via email, or fired up from your USB drive, AnyDesk will turn any desktop into your desktop in se­conds. No administrative privileges or installation needed. AnyDesk 9.7.6 changelog: Fixed Bugs Added validation feedback for passwords shorter than five characters Fixed an issue in AnyDesk One Meeting where the account name was used instead of the entered username after logging out Fixed crashes related to message editing and context menu interactions Fixed issues affecting message scrolling, text formatting, typing indicators, quoted messages, and community privacy settings Prevented users from editing automated system messages Corrected download status reporting, temporary file naming, and menu overlap issues affecting the Download History and Reactions pop-ups Other Changes Added chat list grouping settings Added keyboard navigation support (Page Up, Page Down, Home, and End) for the message feed Removed Chat language options Updated translations Download: AnyDesk 9.7.6 | 8.0 MB (Free for private use, paid upgrade available) Links: AnyDesk Home Page | Other platforms | Release History | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • It's interesting to hear that AI growth is not investor driven.
    • A few weeks ago, I had this same exact issue. Unfortunately, I didn't think of disabling the Secure Boot option in BIOS. I updated my BIOS to the latest version for my MB and it resolved the issue.
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      501
    2. 2
      +Edouard
      201
    3. 3
      PsYcHoKiLLa
      127
    4. 4
      Steven P.
      80
    5. 5
      ATLien_0
      76
  • Tell a friend

    Love Neowin? Tell a friend!