473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

For loop direction (newbie)

I'm tearing my hair out over a simple for loop!

Why does this code work:

Expand|Select|Wrap|Line Numbers
  1. void clsLCD(void)
  2. { // Erase the LCD DDRAM and set cursor home (0,0)
  3.     char line;
  4.     for (line=0; line<6; line++) // 6 lines of LCD DDRAM
  5.         {clLLCD(line);}          // Erase each in turn
  6. }
And this code not work?

Expand|Select|Wrap|Line Numbers
  1. void clsLCD(void)
  2. { // Erase the LCD DDRAM and set cursor home (0,0)
  3.     char line;
  4.     for (line=5; line>=0; line--) // 6 lines of LCD DDRAM
  5.         {clLLCD(line);}          // Erase each in turn
  6. }
The only change is the direction of the loop, up or down.
Dec 30 '09 #1
15 2247
Markus
6,050 Expert 4TB
Define "does not work".
Dec 30 '09 #2
manontheedge
175 100+
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.
Dec 30 '09 #3
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!
Dec 30 '09 #4
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3.  {
  4.                 char line;
  5.                         for (line=0; line<6; line++)
  6.                                 printf("Hi\n");
  7.                         for (line=5; line>=0; line--)
  8.                                 printf("\nhello");
  9.                 getch();
  10.                 return 0;
  11.  }
  12.  
Hi and Hello got printed 6 times. No problem
Dec 30 '09 #5
To Markus,

No idea, the program goes into hyperspace.

From other replies it seems there is nothing wrong with the code, will investigate further...
Dec 30 '09 #6
alexis4
113 100+
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!
Dec 30 '09 #7
newb16
687 512MB
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?
Dec 30 '09 #8
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!
Dec 31 '09 #9
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 31 '09 #10
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.
Jan 1 '10 #11
Normally I use:

Expand|Select|Wrap|Line Numbers
  1. typedef unsigned short int byte;
But I was trying to make it simple for the example I was having a problem with.
Jan 1 '10 #12
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.
Jan 1 '10 #13
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.
Jan 1 '10 #14
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.
Jan 1 '10 #15
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.
Jan 1 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

9
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...
5
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++) {
2
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...
5
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...
3
JodiPhillips
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...
6
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...
11
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...
8
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. ...
3
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...
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.