Operácie

AVR Vstupy a výstupy v C: Rozdiel medzi revíziami

Z SensorWiki

Riadok 1: Riadok 1:
 
 
== Vstupy a výstupy na MiniMEXLE doske. ==
 
== Vstupy a výstupy na MiniMEXLE doske. ==
  
Riadok 19: Riadok 18:
  
 
<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()
 
{
 
  
/* ***************** Initialization ************************ */
+
#define SW1 0
 +
#define SW2 1
 +
#define SW3 2
 +
#define SW4 3
  
/* Port settings for LED *********************************** */
+
#define RedLED 5
  
    DDRB = 0b00000100;    // bin: 0000 0100  1 = out, 0 = in
+
int main(void) {
    PORTB = 0xFF;          // bin: 1111 1111  1 = LED off
 
  
/* Port settings for Switch ******************************** */
+
  /* *********************** Init device MiniMEXLE ******************************** */
  
    DDRC = 0b00000000;     // bin: 0000 0000 1 = out, 0 = in
+
    DDRB = 0b00000100;       // PORTB LED on PB2 is output, PB6, 7 IN!!!
    PORTC = 0xFF;           // bin: 1111 1111  1 = pull-up active
+
  PORTB = 0b00000100;       // LED Active low, LED off, No pull-ups
  
 +
    DDRC = 0b00100000;      // PORTC LED on PC5  is output, SW1-4 are inputs
 +
  PORTC = 0b00001111;      // LED Active high, LED off, pull-ups ON
  
 +
    DDRD = 0b10000000;      // PORTD bit 7 is output, pull-ups OFF
 +
  PORTD = 0b00000000;      // PORTD bit PD7 is switch common ground
  
/* ***************** Main Endless Loop ********************** */
 
  
 +
for (;;) {
 +
      if( !( PINC & 0b00000001) ) {
 +
  // if( !( PINC & (1<<0) ) )    {       
 +
  // if( bit_is_clear(PINC, 0) ) {
 +
                PORTB &= 0b11111011;    // Yellow LED ON
 +
                while (!( PINC&0x01) );  // Wait until not released
 +
                }
 +
       
 +
  if(bit_is_clear(PINC, 1)){
 +
                PORTB |= 0b00000100;    // Yellow LED OFF
 +
                loop_until_bit_is_set(PINC, 1);
 +
                }
  
  while (1) {              // toto rob stale dokola
+
          if(bit_is_clear(PINC, SW3)){
 +
                  PORTC |= 0b00100000;    // Red LED ON
 +
// PORTC |= _BV(PC5);
 +
// PORTC |= (1<<RedLED);
 +
// PORTC |= (1<<5);
 +
                loop_until_bit_is_set(PINC, SW3);
 +
                }
  
  if (PINC & 0b00000001)   // bin:  PINC & 0000 0001 = sw1 OFF?
+
          if(bit_is_clear(PINC, SW4)) {
    PORTB = 0x04;        // Lepsie takto
+
                  PORTC &= 0b11011111;    // Red LED OFF
// PORTB |= 0xff;    // bin  PORTD + 0010 0000 = nastav PD5, LED OFF
+
// PORTC &= ~(_BV(PC5));
  else      // t.j. ak sw1 ON:
+
// PORTC &= ~(1<<RedLED);
//         PORTD &= 0xff;     // bin: PORTD & 1101 1111 = vynuluj PD5, LED ON
+
// PORTC &= ~(1<<5);
    PORTB = 0x00;
+
                loop_until_bit_is_set(PINC, SW4);
 +
                }
 +
          }
  
  }
 
  return (0);            // formalita, nikdy sem neprideme
 
 
}
 
}
 
 
</source>
 
</source>
  
  
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.
 
  
 
[[Category:AVR]][[Category:CAD_RS]]
 
[[Category:AVR]][[Category:CAD_RS]]

Verzia zo dňa a času 13:48, 26. november 2008

Vstupy a výstupy na MiniMEXLE doske.

Obvykle sa pri programovaní jednočipových mikropočítačov začína programom na ovládanie jedného vstupu a výstupu, napr. tlačítko a LED. Je to taký "Hello, World!" program pre vnorené systémy.


Na doske MiniMEXLE máme dve LED diódy a štryi tlačítka.

Connection of the LED and pushbutton on the MiniMEXLE board.

Tlačítka S1 až S4 sú pripojené k pinom PC0 až PC3 a sú aktívne v nule, žltá LED je pripojená k PB2 a je tiež aktívna v nule (t.j. LED svieti, keď je na výstupe log. 0). Druhá, červená LED dióda je aktívna v jednotke a je pripojená na pin PC5.


Nasledovný C-program demonštruje možnosti prístupu k binárnym vstupom a výstupom rôznymi prostriedkami jazyka avr-gcc.

#include <avr\io.h>

#define SW1 0
#define SW2 1
#define SW3 2
#define SW4 3

#define RedLED 5

int main(void) {

  /* *********************** Init device MiniMEXLE ******************************** */

    DDRB = 0b00000100;       // PORTB LED on PB2  is output, PB6, 7 IN!!!
   PORTB = 0b00000100;       // LED Active low, LED off, No pull-ups

    DDRC = 0b00100000;       // PORTC LED on PC5  is output, SW1-4 are inputs
   PORTC = 0b00001111;       // LED Active high, LED off, pull-ups ON

    DDRD = 0b10000000;       // PORTD bit 7 is output, pull-ups OFF
   PORTD = 0b00000000;       // PORTD bit PD7 is switch common ground


for (;;) {
		      if( !( PINC & 0b00000001) ) {
		   // if( !( PINC & (1<<0) ) )    {         
		   // if( bit_is_clear(PINC, 0) ) {
                PORTB &= 0b11111011;     // Yellow LED ON
                while (!( PINC&0x01) );  // Wait until not released 
                }
        
		   if(bit_is_clear(PINC, 1)){
                PORTB |= 0b00000100;     // Yellow LED OFF
                loop_until_bit_is_set(PINC, 1);
                }

           if(bit_is_clear(PINC, SW3)){
                   PORTC |= 0b00100000;     // Red LED ON
				// PORTC |= _BV(PC5);
				// PORTC |= (1<<RedLED);
				// PORTC |= (1<<5);
                loop_until_bit_is_set(PINC, SW3);
                }

           if(bit_is_clear(PINC, SW4)) {
                   PORTC &= 0b11011111;     // Red LED OFF
				// PORTC &= ~(_BV(PC5));
				// PORTC &= ~(1<<RedLED);
				// PORTC &= ~(1<<5);
                loop_until_bit_is_set(PINC, SW4);
                }
          }

}