Difference between revisions of "MiniMexleIO"

From RoboWiki
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
also active low (i.e. LED lights when port is logic 0).
 
also active low (i.e. LED lights when port is logic 0).
  
[[Image:MiniMexleIO.png|center|thumb|200px|Connection of the LED and pushbutton on the MiniMEXLE board.]]
+
[[Image:MiniMexleIO.png|center|thumb|350px|Connection of the LED and pushbutton on the MiniMEXLE board.]]
  
 
And here is a short C-code for controlling them:
 
And here is a short C-code for controlling them:
  
 
<source lang="c">
 
<source lang="c">
#include <avr\io.h>
+
/* ************************************************************************** */
 +
/*                                                                            */
 +
/*  Monoliticke mikropocitace - Priklad 1.1                                  */
 +
/*                                                                            */
 +
/*  Program zasvieti/zhasne LED na PB2 podla stavu prepinaca na PC0          */
 +
/*                                                                            */
 +
/*  Autor: Richard Balogh <balogh@elf.stuba.sk>                              */
 +
/*  Historia:                                                                */
 +
/*            19.02.2006 prva pokusna verzia                                  */
 +
/*            05.11.2008 uprava pre MiniMEXLE na cviko ZSAR                  */
 +
/*  Prenositelnost:                                                          */
 +
/*                                                                            */
 +
/* ************************************************************************** */
 +
 
 +
#include <avr\io.h>         // spolu s -mmcu=atmega88 nahra spravny .h subor
 +
                            // ktory potrebujeme kvoli PORTx, PINx a DDRx
  
 
int main()
 
int main()
 +
{
  
 
/* ***************** Initialization ************************ */
 
/* ***************** Initialization ************************ */
  
/* Port settings for LED *********************************** */                  
+
/* Port settings for LED *********************************** */
 +
 
 
     DDRB = 0b00000100;    // bin: 0000 0100  1 = out, 0 = in
 
     DDRB = 0b00000100;    // bin: 0000 0100  1 = out, 0 = in
 
     PORTB = 0xFF;          // bin: 1111 1111  1 = LED off
 
     PORTB = 0xFF;          // bin: 1111 1111  1 = LED off
  
/* Port settings for Switch ******************************** */                  
+
/* Port settings for Switch ******************************** */
 +
 
 
     DDRC = 0b00000000;    // bin: 0000 0000  1 = out, 0 = in
 
     DDRC = 0b00000000;    // bin: 0000 0000  1 = out, 0 = in
 
     PORTC = 0xFF;          // bin: 1111 1111  1 = pull-up active
 
     PORTC = 0xFF;          // bin: 1111 1111  1 = pull-up active
  
{
 
  while (1) {
 
 
 
  
   if (PINC & 0b00000001) then
+
 
     PORTD = 0x01;
+
/* ***************** Main Endless Loop ********************** */
   else  
+
 
     PORTD = 0x01;
+
 
 +
  while (1) {              // toto rob stale dokola
 +
 
 +
   if (PINC & 0b00000001)   // bin:  PINC & 0000 0001 = sw1 OFF?
 +
     PORTB = 0x04;        // Lepsie takto
 +
// PORTB |= 0xff;    // bin  PORTD + 0010 0000 = nastav PD5, LED OFF
 +
   else       // t.j. ak sw1 ON:
 +
//        PORTD &= 0xff;    // bin: PORTD & 1101 1111 = vynuluj PD5, LED ON
 +
     PORTB = 0x00;
  
 
   }
 
   }
 +
  return (0);            // formalita, nikdy sem neprideme
 +
}
  
}
 
 
</source>
 
</source>
  

Latest revision as of 11:53, 5 November 2008

The usual "Hello, World!" program in the area of the single chip microcontrollers is to connect a pushbutton and LED and to control them.

We already have the pushbutton and the LED on the MiniMexle board - the pushbutton is connected to the pin PC0 and is active low, the LED is connected to PB2 and is also active low (i.e. LED lights when port is logic 0).

Connection of the LED and pushbutton on the MiniMEXLE board.

And here is a short C-code for controlling them:

/* ************************************************************************** */
/*                                                                            */
/*  Monoliticke mikropocitace - Priklad 1.1                                   */
/*                                                                            */
/*  Program zasvieti/zhasne LED na PB2 podla stavu prepinaca na PC0           */
/*                                                                            */
/*  Autor: Richard Balogh <balogh@elf.stuba.sk>                               */
/*  Historia:                                                                 */
/*            19.02.2006 prva pokusna verzia                                  */
/*            05.11.2008 uprava pre MiniMEXLE na cviko ZSAR                   */
/*  Prenositelnost:                                                           */
/*                                                                            */
/* ************************************************************************** */

#include <avr\io.h>         // spolu s -mmcu=atmega88 nahra spravny .h subor
                            // ktory potrebujeme kvoli PORTx, PINx a DDRx

int main()
{

/* ***************** Initialization ************************ */

/* Port settings for LED *********************************** */

     DDRB = 0b00000100;     // bin: 0000 0100  1 = out, 0 = in
    PORTB = 0xFF;           // bin: 1111 1111  1 = LED off

/* Port settings for Switch ******************************** */

     DDRC = 0b00000000;     // bin: 0000 0000  1 = out, 0 = in
    PORTC = 0xFF;           // bin: 1111 1111  1 = pull-up active



/* ***************** Main Endless Loop ********************** */


  while (1) {              // toto rob stale dokola

  if (PINC & 0b00000001)   // bin:  PINC & 0000 0001 = sw1 OFF?
     PORTB = 0x04;         // Lepsie takto
	 // PORTB |= 0xff;     // bin  PORTD + 0010 0000 = nastav PD5, LED OFF
  else       // t.j. ak sw1 ON:
//         PORTD &= 0xff;     // bin: PORTD & 1101 1111 = vynuluj PD5, LED ON
     PORTB = 0x00;

  }
   return (0);             // formalita, nikdy sem neprideme
}


Task:

The task is to control the on and off state of the LED with a single button. First press of the button will set LED on, second press off. Hint: You will need a memory to store the actual LED state to change it.