[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] Using PORTB with variables, PORTB = var
From: |
Matthew MacClary |
Subject: |
Re: [avr-gcc-list] Using PORTB with variables, PORTB = var |
Date: |
Sat, 22 Apr 2006 09:20:45 -0700 |
User-agent: |
Mutt/1.4.1i |
On Sat, Apr 22, 2006 at 07:29:10PM +1000, address@hidden wrote:
> This example doesn't, and I don't know why... I know it's specific
> to C, so that's why I'm asking here...
When asking a question like this, it would be useful for you to
describe exactly what you are seeing and what you expected to
see. Here is my interpretation of what you are probably seeing: in
less than half a second, a lit LED shifts across your output port and
then nothing else ever happens again.
> I would like to keep the functions if possible, I'm thinking it has
> something to do with scope perhaps?
You can do what you seem to try to be doing using functions. The
only scope problem I see is using a globally scoped variable
unnecessarily...
> I am new to C by the way, if it isn't evident already (:
Great, you can learn the right way to do things from the start
without fighting bad habits!
Here is how I would implement your program. I apologize in advance
since I don't have a board available to try the code on. I hope that
the main ideas are apparent, even if I didn't squash all the bugs.
-Matt
void blink(volatile uint8_t * port, uint8_t mask) {
*port = ~ mask; // LEDs on
sleep(500); // milliseconds
*port = 255; // LEDs off
}
void change(uint8_t * var) {
*var <<= 1;
if (0 == *var) {
*var = 1;
}
}
int main() {
uint8_t led_mask = 1;
DDRB = 255; // PORTB as outputs
PORTB= 255; // LEDs off
while (1) {
blink(PORTB, led_mask);
change(&led_mask);
}
return 0;
}
/* Changes
1) Don't use global variables (a common exception to this rule is to
communicate with an interrupt routine). [In my team you would lose
a hand for doing this.]
2) Don't assign a 16 bit signed integer value to an 8 bit unsigned
location (the port). [This would cost you your other hand ;-) ]
3) Don't write non-reentrant functions, that depend on and modify
global data.
4) Don't blink the LEDs faster than you can see them.
5) Don't keep shifting a variable the same way forever, since it will
end up all zeros.
6) Do keep programming AVRs in C!!
*/
- [avr-gcc-list] Using PORTB with variables, PORTB = var, unauthorized, 2006/04/22
- Re: [avr-gcc-list] Using PORTB with variables, PORTB = var, Abdul Malik Khan, 2006/04/22
- Re: [avr-gcc-list] Using PORTB with variables, PORTB = var, Lars Noschinski, 2006/04/22
- Re: [avr-gcc-list] Using PORTB with variables, PORTB = var, unauthorized, 2006/04/22
- Re: [avr-gcc-list] Using PORTB with variables, PORTB = var, Steve Meliza, 2006/04/22
- Re: [avr-gcc-list] Using PORTB with variables, PORTB = var, Kevin Cozens, 2006/04/22
- Re: [avr-gcc-list] Using PORTB with variables, PORTB = var, Ned Konz, 2006/04/22