Hey. I'm working on a project involving a LCD screen and an ATTiny48, and decided to write a sort of splash screen to display on the LCD before moving into the main program code. Just for fun/practice. This follows from a previous post I made here:
https://www.eevblog.com/forum/microcontrollers/c-code-problem/Before I started this afternoon the code worked fine and as expected, but after writing a few lines for my splash screen, it breaks. The splash displays as expected but once it moves onto the main program code it doesn't quite function properly. The .c file is attached, and the snippet that I've added and that appears to break it is this:
/* Boot splash screen */
lcd_init(LCD_DISP_ON); // Initialize LCD to 4-bit mode
lcd_clrscr(); // Clear display
const char bat[8] = " Bat "; // Splash screen text, top line
// const char box[8] = " Box "; // Splash screen text, bottom line
char buffer[8]; // Buffer string to manipulate bat and box
int8_t i = 3;
int8_t j = 4;
for (int8_t h=4; h>=0; h--)
{
memset(buffer, '\0', sizeof(buffer)); // Reset/erase buffer string
// strncpy(buffer, bat, 8); // Copy bat into buffer for manipulation
buffer[i] = '<'; // Set '<' in buffer string
for (int8_t k=i-1; k>=0; k--)
{
buffer[k] = ' '; // And set all preceding elements to ' '
}
buffer[j] = '>'; // Set '>' in buffer string
for (int8_t k=j+1; k<=7; k++)
{
buffer[k] = ' '; // And set all proceeding elements to ' '
}
lcd_gotoxy(0,0); // Go to first line
lcd_puts(buffer); // Put string to display
_delay_ms(1000);
lcd_clrscr(); // Clear display
i--; // Decrement to move to next stage of splash screen
j++; // Increment to move to next stage of splash screen
}
Basically this just displays '<' and '>' moving from the middle of the LCD to the sides with the text 'Bat' appearing in this middle as they move outwards. This works fine but once it moves onto the main program code somehow my variable 'enc_count' is being incremented over and over until it reaches the upper limit that I set for it. This should be increment/decrement when I turn a rotary encoder, but somehow the program is doing it by itself.
I've narrowed it down to the strncpy line. If I remove that line the program code works as normal (but of course the splash screen doesn't really work). Based on what I learned from the previous thread linked to above, is my use of strncpy perhaps causing stack overflow or something?
As with sprintf is it better not to use strcpy in my application (i.e. with limited memory in my ATTiny48)?
Any help or ideas would be appreciated.
Cheers.