Hi all. I'm fairly new to the AVR world so please bare with me. I've spent a long time looking for solutions to my problem, which has resulted in me spotting various things wrong with my code which I've fixed, but alas it's still not quite right. I really don't know what's going on so it's very difficult to search for an answer to an undefined problem!
Basically I'm writing what should be a simple program to read the output from a rotary encoder, increment/decrement a counter and display the counter on a LCD screen. I'm using Atmel Studio and an ATTiny2313. This forms part of a wider project I'm working on but that's not relevant. For the LCD display I'm using a HD77480 library that I found here:
I've used this before in other code and it's worked fine.
For the rotary encoder part, I've written a function myself, which is most likely causing trouble but I just can't see how. The function reads the current phase from the encoder and compares against the previous phase in order to determine CW or CCW motion. The comments explain a bit more, but essentially if the new phase is a valid move it will (should) update the enc_status variable to indicate a CW or CCW move, if not a valid move it should simply ignore it and exit the function. I'm sure it could be written far more efficiently but hey, I'm just getting started so this will do for me. The function returns enc_status that is either 0 (no change), 1 (CW motion) or -1 (CCW motion).
The main function then calls this function and increments/decrements a variable depending on the result. This variable of type int is then converted to a character string, using itoa(), in order to be passed to the lcd_puts() function in the LCD library that writes to the LCD display. I've used this very code (different variable names) previously and it has worked fine.
My issue is that there seems to be some interference between the LCD library functions and either the itoa() function and/or my encoder function. The reason I suspect this is because I'm seeing some strange results on the LCD display. As you can see in my code I have the LCD set to display "Counter:" on the first line and simply "0" on the second line. However after programming to the MCU the LCD screen initially just displays a "-1" on the bottom line. And then if I start to rotate the encoder the number on the LCD increases and decreases, even though the encoder variable isn't being sent there! It should just read "Counter:" and "0". So clearly somehow the encoder variable is somehow getting to the LCD, but I have no idea how. The number also doesn't behave how it should (if it were programmed to display!). It increments and decrements in 10's not 1's and once it goes to double figures for some reason the least significant digit appears again in the top line.
If I comment out the line that calls my encoder function, the LCD screen displays what I've put in the lcd_puts() functions. If I comment out the itoa() line and the line above, it also displays correctly.
Having just power cycled it again (for the nth time) this time the "Counter:" and "0" indeed did appear initially! And then the number would again increment/decrement according to the encoder. After another power cycle it's back to just displaying "-1". It has to be something to do with the function I've written. With the exact same hardware I've had the LCD screen behaving perfectly in the past, but with the addition of the encoder it's all gone hay wire.
Code attached. Very confused. Any help would be greatly appreciated.