I'm tearing my hair out over a simple for loop!
Why does this code work: - void clsLCD(void)
-
{ // Erase the LCD DDRAM and set cursor home (0,0)
-
char line;
-
for (line=0; line<6; line++) // 6 lines of LCD DDRAM
-
{clLLCD(line);} // Erase each in turn
-
}
And this code not work? - void clsLCD(void)
-
{ // Erase the LCD DDRAM and set cursor home (0,0)
-
char line;
-
for (line=5; line>=0; line--) // 6 lines of LCD DDRAM
-
{clLLCD(line);} // Erase each in turn
-
}
The only change is the direction of the loop, up or down.
15 2247
the thing that stands out just by looking at it is that you have "line" defined as a character, but in the for loop, you use it like it's an integer. Why? Are you doing that intentionally? That's the problem.
IF you need the ascii representation ( the 'char' ), then after you pass the integer value to whatever function you're passing it to, THEN convert it to char form.
You're basically getting lucky with the first one.
A char is an 8 bit unsigned integer value, used a lot in PIC programs to save space. When you only have 300 bytes of RAM, it matters!
-
#include<stdio.h>
-
int main()
-
{
-
char line;
-
for (line=0; line<6; line++)
-
printf("Hi\n");
-
for (line=5; line>=0; line--)
-
printf("\nhello");
-
getch();
-
return 0;
-
}
-
Hi and Hello got printed 6 times. No problem
To Markus,
No idea, the program goes into hyperspace.
From other replies it seems there is nothing wrong with the code, will investigate further...
This is not a single for loop problem. Check out LCD's datasheet. Is it allowed to begin LCD clear from line 5 to line 0? Maybe you should set cursor every time you decrease the line? In increment this is done automatically by hardware.
Furthermore there is no "hyperspace". You have a debugger, watch window, memory window, registers window, so use them! But above all, read the datasheet!
Yes, what is hyperspace? Is it only LCD that stops working until reboot, or the MCU itself hangs and can even blink a LED after that?
I've done some more tests, it keeps going round and round the loop. I've realised why: the variable is defined as unsigned, so when it gets to 0 it becomes 255. D'OH!
A char is an 8 bit unsigned integer value
A falsehood. A char may be signed.
Avoid char. Use unsigned char or signed char instead.
Banfa 9,065
Expert Mod 8TB
Not always a good idea, for instance if you are planning on passing the variable or a pointer to the variable to a standard library function then you should use char. Explicitly using unsigned or signed char makes the code non-portable, specifically to platforms that use the alternate sign as default.
But if you are just using it as a 1 byte integer variable then I agree specify signed or unsigned.
Normally I use: - typedef unsigned short int byte;
But I was trying to make it simple for the example I was having a problem with.
Banfa 9,065
Expert Mod 8TB
Erm that is quite an unusual definition as although there is no standard defined definition of a byte I think most people would expect it to be the smallest addressable memory unit which is defined by the standard as a char. In fact the standard specifically states that the systems memory must be addressable as a contiguous series of char, although it makes no requirement on the number of bits in the char.
Anyway on many (most? or maybe all common ones) systems short has a size of 2 char, not the smallest addressable unit.
This is a PIC microcontroller. A few KB of ROM, and a few hundred bytes of paged RAM. Technically the ROM (where constants are stored) is about 12-14 bits wide, but a char and a short are 8 bits. I just prefer to keep char for when I mean an actual character.
Banfa 9,065
Expert Mod 8TB
Well then it would work for your current platform but that would be a portability issue if for-instance you ever chose/had to move to a more powerful PIC. I am fairly sure the PICs I have used had 2 byte shorts and ints and 4 byte longs (and included the long short type which was 3 bytes).
The point I am trying to make is what you have done works for your current platform, however semantically it is not correct, you are relying on the fact that short and char have the same size which can easily change from platform to platform.
If your byte type has the semantic meaning of the smallest addressable memory unit then it should be based on a built-in type that is defined to also have that meaning.
In general what your have done, written code based on the specific implementation of the platform rather than the definitions from the standard is extremely common and probably one of the biggest causes of code bein un-portable.
However at least in your case as a worst case scenario you would need to change a single typedef to get everything working again.
I guess that
typedef unsigned char byte;
would be better for portability, but the rest of the code relies on built-in routines from the specific compiler, and a particular type of chip, so I don't think this is a major problem.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Gabriel F. Alcober |
last post by:
Hi! There goes a newbie trouble:
for i in range(0, len(subject)):
if subject in preps:
psubject.append(noun_syn_parser(subject))
subject =
Since the last line eliminates some elements of...
|
by: John Edwards |
last post by:
Hello,
I have sort of a newbie question. I'm trying to optimize a loop by
breaking it into two
passes.
Example:
for(i = 0; i < max; i++)
{
|
by: Daniel Mark |
last post by:
Hello all:
I am using PIL to draw some graphics and I need to draw some texts
in vertical direction rather than the default left-to-right horizontal
direction.
Is there anyway I could do...
|
by: Candace |
last post by:
I have to write a program to find all Pythagorean triples for a right
triangle. I know that the squares of two sides of the triangle must equal
the square of the third (longest) side. However, I...
|
by: JodiPhillips |
last post by:
Hello All,
I'm trying to limit the number of attempts a user has to log into an MS Access 2003 database, but am having very little success.
My current code for log in is as follows (and thanks...
|
by: Jim |
last post by:
I'm a newbie so I'm probably doing something simple and dumb but I just
can't get Blogger to accept a JavaScript for/next loop.
Here's what I've tried
<Script Language='JavaScript>
{
for...
|
by: icarus |
last post by:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.
The user is supposed to enter q or any other non-character to exit the
program.
Problem with this...
|
by: Pivot_Tables |
last post by:
Hi,
I have created a recursive SQL Query in DB2 and it works fine until
some point in the tree where the data gets into infinite loop. Below
are some sample data from my relationship table.
...
|
by: bhavyagupt |
last post by:
i 'm not able to use the accordion menu code in for loop . can anyone solve ma problem.
code.......
slider.js------> java script
var slider=function(){
var array=; var speed=10; var...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |