[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-chat] Endless loop in AVR?
From: |
Bob Paddock |
Subject: |
Re: [avr-chat] Endless loop in AVR? |
Date: |
Sat, 28 Jul 2007 09:07:55 -0400 |
User-agent: |
Opera Mail/9.10 (Win32) |
On Sat, 28 Jul 2007 08:24:35 -0400, Wojtek Dabrowski <address@hidden>
wrote:
Actually, no, I haven't touched microcontrollers before. I'm just used
to +5V meaning that there is a signal, and ground meaning there is no
signal
Many things in the world of Micros are active 'low'.
Thanks for the help, I'll try those things now - I hope this depleted my
repertiore of noob-questions for now :)
A tip for you: Don't think of signals as being +5 or Ground nor
think of them of being high or low. Think of them as being
Active/Asserted, or Inactive/Deasserted. In the long run
it makes porting code between different hardware architectures
much less painful. You can then write code that says
things like "AlarmLED_ASSERTED()" and you know that the LED
is on. Where if you say "AlarmLED_HIGH()" you don't
really know what that means without intimate knowledge
of the hardware, and goes down hill fast if the next
revision of the hardware inverts something.
Here is a simple example of a button that
lights up a LED, without ever directly using
the concept of +5/High or Ground/Low in the main line code:
File hardware.h, that contains anything to do with the ports,
all such port data is centralized in this one file:
/* Menu Push Button Input: */
#define MenuPB_BIT (5)
#define MenuPB _BV(MenuPB_BIT)
#define MenuPB_ASSERT() (BitClr( PORTE, MenuPB_BIT ))
#define MenuPB_DEASSERT() (BitSet( PORTE, MenuPB_BIT ))
#define MenuPB_TOGGLE() (BitToggle(PORTE, MenuPB_BIT ))
#define MenuPB_ASSERTED() (!BitTest( PINE, MenuPB_BIT ))
#define MenuPB_DEASSERTED() ( BitTest( PINE, MenuPB_BIT ))
/* LED Output: */
#define AlarmLED_BIT (7)
#define AlarmLED _BV(AlarmLED_BIT)
#define AlarmLED_ASSERT() (BitSet( PORTB, AlarmLED_BIT ))
#define AlarmLED_DEASSERT() (BitClr( PORTB, AlarmLED_BIT ))
#define AlarmLED_TOGGLE() (BitToggle(PORTB, AlarmLED_BIT ))
#define AlarmLED_ASSERTED() ( BitTest( PINB, AlarmLED_BIT ))
#define AlarmLED_DEASSERTED() (!BitTest( PINB, AlarmLED_BIT ))
File main.c:
#include <avr/io.h>
#include "hardware.h"
int main( void )
{
DDRB = AlarmLED;
for(;;)
{
if( MenuPB_ASSERTED() )
{
AlarmLED_ASSERT();
}
else
{
AlarmLED_DEASSERT();
}
}
}
/*
Code like that will be much clear to you if you ever
need to change the code a few years from now.
Consider how bad it would be if it said if( Button_LOW() ){ LED_LOW() }
and the schematic was long since lost...
See "Bit-level operations" in Bitops.How at
http://c.snippets.org/browser.php
to see what BitSet()/BitClr() et.al do.
*/
There are other ways to do it as there always are, I've just found
that is what works the best for me over the years.