NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Knight rider-ish code, with a few questions
September 04, 2010 by sporkalicious |
So i wrote up a simple little "knight rider" code after doing the Led blink guide, and though successful it raised a few questions, first here's my code
Now for the questions 1, Is there way to optimize the cycling of LEDs beyond what i have alreayd done here, and short of trying to run a 2x3 array? 2, Would something along the lines of;
be an acceptable way to set PC0-PC5 as outputs? I only ask because in the Port C data direction register (DDRC) table on page 88 of the MCU datasheet, the bits appear to be sequential, I ask this more out of curiosity and a lock of desire to accidently screw something up just trying it, than a desire to use some bizarre hard to understand method of setting pins to output mode. |
---|---|
September 04, 2010 by sporkalicious |
also to clarify by optimizing it i mean something along the lines of
If something along these lines would work, how would i go about defining the PC"X" in an acceptable manner? |
September 04, 2010 by Rick_S |
You have the basic idea, but the easiest way I've found when scanning an single LED back and forth is to toggle the enitire port rather than the this on/that off method. Highly simplified code might look more like this: (Modified borrowed code from an web source...)
In your code, there would have been a few problems but you are definately on the right track. 1st, in C, you have to declare each variable with a type Instead of
You would need something like
Here you have the right idea, but you don't need the PC"X" just x. Such as..
Would be
The same would be true here
Would become
In c, the equal sign is for assignment so if you say this
the if will be true because y is assigned zero not checked against it. If you want to compare y to zero you have to use a double equal sign like this:
The same would be true for the rest of your if statements below.
Would become
One other thing, don't forget parentesis and semicolons in the appropriate places.. I hope that helps a bit... Rick |
September 04, 2010 by sporkalicious |
Okay first off, Rick, you are my hero bow =P Thank you for taking the time to help me out, I'm horrible with C but learning quickly(my only prior programming experience is with a TI-83 calculator, lol) Thanks to your help i rewrote the code as follows;
and it works wonderfully, I did not use your initial suggestion of a code block simply beacuse I do not exactly understand it, and I simply refuse to use something until I understand it =P, which brings me to my next question. Line 5, and line 10(the for statements) of your code example are a bit confusing to me, after a bit of reading about for loops I think i may have a basic grasp, but I'm not sure, so if it's okay with you I'd like to run by what i think it's attempting to do, and how i could use it in my quick sample project
what i think this is saying is to start the loop when x=1 (which would require actually initially defining x as 1 to work if I'm right) and keep looping as long as x<128, and for each pass of the loop xis multiplied by 2? my bigest question here is what would the first output value of x be?
this one really throws me off though, i understand that once x=128 it does this loop instead which would halve x intil it is equalt or less than 1, what i really don't understand is the halving operation itself. theoreticly though applying this to my project should follow along the lines of
Please explain/correct me if I'm wrong, and thanks again for your help. |
September 04, 2010 by sporkalicious |
okay so after trying my attempt I was abit confused by the results, until i realized i had made a binary counter similar to one of the binary led clocks.. now i think i understand a bit more.. modified your code to create the following(128 would cycle 8 leds i'm only using 6)
what i still don't understand is the "-" before the equals sign in the second for statement, Having way too much fun with this now.. should be asleep.. lol |
September 05, 2010 by hevans (NerdKits Staff) |
Hi sporkalicious, The - sign is part of the -= operator which works just like the much more common += operator. These operators are just shorthand that first apply to operator to the variable, and then set the operator equal to the result. So doing
is the same as doing
In this case
would expand to
For this particular example, it turns out that the - is completely irrelevant, since the right side of the equation simplifies to x/2. In other words subtracting half from a number is the same as just dividing the number by two. Hope that makes sense! Humberto |
September 16, 2010 by rdalton |
I would also like to toss a thank you out to Rich. That example was well explained and detailed line by line. sporkalicious: I do the same thing you do. I won't use a block of code I don't understand. Unfortunately, I don't know C at all so I'm starting from ground zero. If you come across any C examples for the micro-controller like Rich posted, please post them here. There are some good ones in the manual but I need more details sometimes. Thanks. (sporkalicious: if you don't mind, i'm going to steal your code and try to understand it. :) |
Please log in to post a reply.
Did you know that you can use printf and scanf functions to talk to your computer from your USB NerdKit? Learn more...
|