Operácie

Program AVR03

Zo stránky SensorWiki

Verzia tlače už nie je podporovaná a môže obsahovať chyby pri vykresľovaní. Prosím aktualizujte záložky vo svojom prehliadači a použite predvolenú funkciu pre tlač v prehliadači.
//*****************************************************************************
// File Name	: basiciotest.c
// 
// Title		: example usage of basic input and output functions on the AVR
// Revision		: 1.0
// Notes		:
// Target MCU	: Atmel AVR series
// Editor Tabs	: 4
// 
// Revision History:
// When			Who			Description of change
// -----------	-----------	-----------------------
// 18-Nov-2005	balogh		Created the program
//*****************************************************************************

 
//----- Include Files ---------------------------------------------------------
#include <avr/io.h>		      // include I/O definitions (port names, pin names, etc)
#include <avr/signal.h>	      // include "signal" names (interrupt names)
#include <avr/interrupt.h>	  // include interrupt support

#include "global.h"		      // include our global settings

#define LED1	PINB0	      // .equ 	LED1 	=PINB0
#define LED2	PINB1	      // .equ 	LED2 	=PINB1

// PINB0 je v atmega16.h je zadefinovany ako cislo  0
// Takze ma zmysel 	 sbi(PORTB,LED0);

// Naopak, toto nema zmysel, dosadi sa 3.
// #define SW1 	PINB3	
// Spravne ma byt:
#define SW1 	 bit_is_clear(PINB, 3)  // .equ	SW1	=PINB3	
#define SW2 	 bit_is_clear(PINB, 4)  // .equ	SW2	=PINB4	


//----- Zaciatok programu ------------------------------------------------------------
int main(void)
{
	// All AVR processors have I/O ports which each contain up to 8
	// user-controllable pins.  From a hardware perspective, these I/O pins
	// are each an actual physical pin coming out of the processor chip.
	// The voltage on the pins can be sensed or controlled via software,
	// hence their designation as Input/Output pins.

	// While I/O pins are actual wires in the real world, they also exist
	// inside the processor as special memory locations called registers.
	// The software-controlled contents of these registers is what
	// determines the state and operation of I/O pins and I/O ports.

	// Since AVR processors deal naturally with 8 bits at a time, I/O pins
	// are grouped into sets of 8 to form I/O ports.  Three registers
	// are assigned to each I/O port to control the function and state of
	// that port's pins.  The registers are 8-bits wide, and each bit (#0-7)
	// determines the operation of the corresponding number pin (pin 0-7).
	// The three registers are:

	// DDRx  - this register determines the direction (input/output) of the pins on port[x]
	//			A '0' bit in the DDR makes that port pin act as input
	//			A '1' bit in the DDR makes that port pin act as output

	// PORTx - this register contains the output state of the pins on port[x]
	//			A '0' bit makes the port pin output a LOW  (~0V)
	//			A '1' bit makes the port pin output a HIGH (~5V)

	// PINx  - this register contains the input state of the pins on port[x]
	//			A '0' bit indicates that the port pin is LOW  (at ~0V)
	//			A '1' bit indicates that the port pin is HIGH (at ~5V)

	// The x should be replaced with A,B,C,D,E,F, or G depending on the
	// desired port.  Note that not all AVR processors have the same set
	// or number of ports.  Consult the datasheet for your specific processor
	// to find out which ports it has.

	// in the AVR-GCC C language, ports can be accessed using two kinds of
	// commands:
	// inb() and outb()		-	in-byte and out-byte
	// cbi() and sbi()		-	clear-bit and set-bit

	// inb() and outb() should be used when you intend to read or write
	// several bits of a register at once.  Here are some examples:

	
  unsigned char TMP;	               // .def 	TMP 	=r16
  // Vystup na port ma obratene poradie: outp(hodnota, port);
  // Pri pouziti AVRlib je mozne pouzit "logickejsiu" outb:
  // outb(DDRB, 0x18);	               // set some of port B pins to input
  // okrem toho v C sa neda pouzit binarna hodnota, len oct, dec a hex
  outb(DDRB,0x018);          // ldi TMP,0b00011000	;nastav vstupy a vystupy
							 // out DDRB,TMP
  outb(PORTB,0x0F0);		 //	ldi TMP,0x0F0		; zhasni LED
							 //	out PORTB,TMP		; zapis na port

do					         // 	rjmp LOOP		; dokola
{						
  if (SW1)					// 	sbis	PINB,SW1	; obskoc ak je stlacene tlacitko
    {cbi(PORTB,LED1);		// 	rcall LED1_ON
	 sbi(PORTB,LED2);
	}
  if (SW2)					// 	sbis	PINB,SW2	; obskoc ak je stlacene tlacitko
    {
     cbi(PORTB,LED2);		// 	rcall LED2_ON
     sbi(PORTB,LED1);
	 }
} while (1);
	
	return 0;
}