en las instrucciones con operandos inmediatos el destino slo puede ser un registro del segundo grupo (r16..r31) Addi r5, 4 Addi r20, 4 ; INVALIDO!!! ; VALIDO!!!
Control de CPU
Instrucciones aritmtico-lgicas
Add Adiw Inc Mul Fmul Mulsu Tst .. rd, rs ; rd = rd + rs rdl, W ; rdh:rdl = rdh:rdl + W rd rd, rs rd, rs rd, rs rd ; rd = rd + 1 ; R1:R0 = rd * rs ; R1:R0 = (rd * rs) << 1 ; R1:R0 = rd * rs ; rd = rd rd
Instrucciones de salto
Rjmp Ijmp Jmp Cpse Sbrc Sbrs Breq Call Ret k rd, rs rs, b rs, b k k k ; PC = PC + k + 1 ; PC = PC + Z ; PC = k ; if (rd==rs) PC = PC + (2 else 3) ; if (rs(b)==0) PC = PC + (2 else 3) ; if (rs(b)==1) PC = PC + (2 else 3) ; if (Z==1) PC = PC + k + 1 ; push PC , PC = k ; pop PC
rd, Y+q ; rd = (Y+q) rd, k ; rd = (k) ; R0 = (Z) ; rd = (Z) ; (Z) = R1:R0 ; (Z) = rs+1:rs ; rd = P
; Out
P, rs
Tratamiento de bits
Sbi Bset Bst Sec Sei Lsl Clt . rd P, b s rd, b ; IO(P,b) = 1 ; SREG(s) = 1 ; T = rd(b) ;C=1 ;I=1 ; rd(n+1) = rd(n) , rd(0) = 0 ;T=0 ; Set ; Cbi ; Bclr ; Bld ; Clc ; Cli P, b s rd, s
Programando en C
Hints importantes en un cdigo C: Archivos include importantes: #include <avr/io.h> ; Definiciones de IO especficas para el dispositivo en uso
El archivo incluye: avr\common.h avr\sfr_defs.h avr\portpins.h avr\version.h ; descripcin de registros comunes a todos los AVR ; definicin de registros especiales
Programando en C
Hints importantes en un cdigo C: La rutina principal //Funcin principal int main(void) { //Declaracin e inicializaciones //Ciclo infinito while(1){ //Cdigo del programa prinicpal } }
NOTAR QUE: Un programa C siempre arranca ejecutando la rutina main Un programa en assembler siempre arranca ejecutando la interrupcin 0 [Reset]
Programando en C
La rutina principal
#include <avr\io.h> #include <util\delay.h> int main(void) { //Puerto D como salida DDRD = 0xFF; while (1){ _delay_ms(150); portD(0xE4); portD(0xE8); portD(0xD8); portD(0xD4); } } .include "m8def.inc" .org 0x0000 rjmp Temp1 Temp2 Temp3 Step = RESET = r16 = r17 = r18 r20 ;Reset Handle .def .def .def .def
; genera un delay de 256*256 unidades de tiempo Delay: Loop1: ldi Temp2,0 Loop2: inc Temp2 breq Loop1End rjmp Loop2 Loop1End: inc Temp1 breq Loop1End rjmp Loop1 Loop1End: ret RESET: ldi Temp1, low(RAMEND) out SPL, Temp1 ldi Temp1, high(RAMEND) out SPH, Temp1 ldi Temp1, 0xff out DDRD, Temp1 ldi Temp1, 0 Loop0: ldi Temp3, 55
Programando en C
Las interrupciones
#include <avr/io.h> #include <avr/interrupt.h> unsigned int incomingByte = 0x00; #define CALC_BAUDRATE(baudrate) F_CPU/16/((baudrate)-1) #define USART_BAUDRATE 51 void USART_Init(){ //Set baud rate UBRRL=(unsigned char)USART_BAUDRATE; //low byte UBRRH=(unsigned char)(USART_BAUDRATE>>8); //high byte //Data format: asynchronous,no parity, 1 stop bit, 8 bit size UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(0<<UCSZ2) |(1<<UCSZ1)|(1<<UCSZ0); //Enable Receiver and Interrupt on receive complete UCSRB=(1<<RXEN)|(1<<RXCIE); }
Programando en C
Las interrupciones
void USART_Tx( unsigned int data ){ /* Wait for empty transmit buffer */ while ( !( UCSRA & (1<<UDRE)) ){} /* Put data into buffer, sends the data */ UDR = data; } ISR(USART_RXC_vect) { incomingByte = UDR; } int main(void){ USART_Init(); ENABLE_IRQ; USART_Tx('O'); USART_Tx('K'); USART_Tx(':'); for(;;){} }