Vous êtes sur la page 1sur 6

Compatibilidad

WINAVR - MikroC
Programacion C
Escuela Militar de Ingenieria
Ing. Germn Pereira Muoz

Prototipos de Funcion(Cabeceras)
Las Funciones deben ser declaradas antes de su uso:
Una declaracion no es mas que cabecera de la funcion no la funcion en
si
Las declaraciones de Funcion(cabeceras) puden ser incluidas en una
archivo include

La definicion de la funcion es incluida en el cuerpo de la funcion

void function_name(int, float); //cabecera


Retorno de tipo es requerido; void significa que no retorna nada
Tipos de Argumentos (los nombres no son necesarios, pero se puede
incluir los nombres si se decea)

07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

Main
Cualquier programa C necesita una funcion
llamada main
Generalmente definida (no declarada)
int main(void){
int x;
//variable local
x = add(3, 9); //llamada a funcion
return 0;
}

El cuerpo de programa se encuentra entre


laves { }
Esta funcion tradicionalmente retorna 0
07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

Otras Funciones
Otras definiciones de funcion son
usualmente puestas por debajo de la funcion
main o en un archivo separado
int add(int a, int b){
return a+b;
}
Arguentos pasados
por valor a la
funcion
07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

Asignacion de Variables
uint8_t x = 3; //asignacion e inicializacion externa
int8_t loquesea(uint_8 a)//parametro local automatico
{
int_8 negOne = 0xFF;
// local automatico
static int8_t serial = 1; //local statico
return (serial++) + a + negOne;
}

Locales automaticos existen solo cuando dura la


llamada de la funcion
Los datos globales y static son creados e inicializados
una sola vez en el programa; estos retiene valores entre
llamadas de funciones
07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

Literales
Los enteros pueden ser expresados en varias bases
decimal: 29, -13, 0
octal: 0377
hexadecimal: 0x34F2
binario: 0b011011
Characteres
'a'
String
Es un Strig terminado en NUL almacenado en
un ARRAY"
07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

Arrays
Declaracion de ARRAYS
uint8_t edad[10];
int16_t temp[5];
int8_t ejemplo[] = {-1, 2, -5};

Si es posible los Arrays son Dinamicamente


asignados arrays, pero requieren una
funcion de asignacion de memoria
07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

Puertos de E/S
DDRx - Este registro determina la direccion (input/output)
de los pins en port[x]
A '0' bit en DDR pone al pin del puerto como entrada (input)
A '1' bit en DDR pone al pin del puerto como salida (output)
PORTx - Este registro contienen el estado para salida
de los pines en port[x]
A '0' bit pone al pin del puerto como salida=0 (LOW ~0V)
A '1' bit pone al pin del puerto como salida=1 (HIGH ~5V)
PINx - Este registro contiene el estado de entrada de los pines en port[x]
A '0' bit indica que el pin del puerto es = 0 (LOW ~0V)
A '1' bit indica que el pin del puerto es = 1 (HIGH ~5V)
La X puede ser reemplazada por A,B,C,D,E,F, o G dependiendo del puerto
designado.
07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

Manipulacin de Datos
WINAVR
Manipulacin de Bytes:

MikroC
Manipulacin de Bytes:

Configuracin de Puerto:

Configuracin de Puerto:

DDRD = 0xF0;
// Los 4 bits superiores como salida
// Los 4 bits inferiores como entrada

Lectura de Byte
dato = PINB;

Escritura de Byte
PORTB = dato;

Manipulacin de Bits:

TRISD = 0x0F;
// Los 4 bits superiores como salida
// Los 4 bits inferiores como entrada

Lectura de Byte
dato = PORTB;

Escritura de Byte
PORTB = dato;

Manipulacin de Bits:

sbi(DDRD, 7);
TRISD. F7=0;
// coloca el bit 7 como salida
// coloca el bit 7 como salida
sbi(PORTD, 0);
PORTD. F0=1;
// coloca el Bit 0 del PORTD en
// coloca el Bit 0 del PORTD en
1
1
cbi(PORTD, 4);
PORTD.F4=0;
07/22/10
// coloca el Bit 4 del PORTD
Ing. Germn
en 0 Pereira Muoz
// coloca el Bit 4 del PORTD en9 0
Programacion C para AVR

Puertos
PORT E/S y manipulacion de bit
PORTB = 0xFF;
GICR |= (1<<INT0);
PORTB &= ~((1<<5)|(1<<7));
PORTB &= ~(_BV(5)|_BV(7));
_BV(b) es un macro para (1<<b)
uint8_t pins = PIND;
07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

10

Testeando Bits
WINAVR

MikroC

if

if

(PIND & _BV(4))

(PORTD.F4==1)

//bit 4 en uno
}

//bit 4 en uno
}

if (~PIND & _BV(4))

if (PORTD.F4==0)

{
//bit 4 en cero
}

{
//bit 4 en cero
}

if (!(PIND & _BV(4)))

if (!(PORTD.F4==1))

{
//bit 4 en cero

//bit 4 en cero

}
07/22/10

}
Ing. Germn Pereira Muoz
Programacion C para AVR

11

Ejemplo de Programa
Display de 7 segmentos

RP1

U2

33
34
35
36
37
38
39
40

18

R
R
R
R
R
R
R
R

U3
A 0 /A N 0
A 1 /A N 1
A 2 /A N 2 /V R E F - /C V R E F
A 3 /A N 3 /V R E F +
A 4 /T 0 C K I/C 1 O U T /R C V
A 5 /A N 4 /S S /L V D IN /C 2 O U T
A 6 /O S C 2 /C L K O
S C 1 /C L K I
B
B
B
B
B
B
B
B

0 /A
1 /A
2 /A
3 /A
4 /A
5 /K
6 /K
7 /K

N
N
N
N
N
B
B
B

1 2 / I N T 0 / F L T 0 / S D I/ S D A
1 0 /IN T 1 /S C K /S C L
8 /IN T 2 /V M O
9 /C C P 2 /V P O
1 1 /K B I0 /C S S P P
I1 /P G M
I2 /P G C
I3 /P G D

VUSB
P IC 1 8 F 4 5 5 0

07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

R C 0 /T 1 O S O /T 1 C K I
R C 1 / T 1 O S I / C C P 2 /U O E
R C 2 /C C P 1 /P 1 A
R C 4 / D -/ V M
R C 5 /D + /V P
R C 6 /T X /C K
R C 7 / R X / D T /S D O

R D 0 /S P P 0
R D 1 /S P P 1
R D 2 /S P P 2
R D 3 /S P P 3
R D 4 /S P P 4
R D 5 /S P P 5 /P 1 B
R D 6 /S P P 6 /P 1 C
R D 7 /S P P 7 /P 1 D
R E 0/A N 5 /C K 1 S P
R E 1/A N 6 /C K 2 S P
R E 2 /A N 7 /O E S P
R E 3 /M C L R / V P

P
P
P
P

15
16
17
23
24
25
26

RC
RC
RC
RC

0
1
2
3

19
20
21
22
27
28
29
30

RD
RD
RD
RD
RD
RD
RD
RD

0
1
2
3
4
5
6
7

8
9
10
1

R
R
R
R

C0
C1
C2
C3

4
3
2
1

5
6
7
8
ON

CRYSTAL

R
R
R
R
R
R
R
O

OFF

X1

2
3
4
5
6
7
14
13

9
8
7
6
5
4
3
2

R E S P A C K -8

D IP S W _ 4

R
R
R
R
R
R
R

D
D
D
D
D
D
D

0
1
2
3
4
5
6

R9
10k

12

Programa Ejemplo: disp7seg.c


WINAVR
MikroC

07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

13

Programa Ejemplo iotest.c


archivo: global.h
#ifndef GLOBAL_H
#define GLOBAL_H
// global AVRLIB defines
#include "avrlibdefs.h"
// global AVRLIB types definitions
#include "avrlibtypes.h"
// project/system dependent defines
// CPU clock speed
#define F_CPU
16000000
//#define F_CPU
14745000
//#define F_CPU
8000000
//#define F_CPU
7372800
//#define F_CPU
4000000
//#define F_CPU
3686400

// 16MHz processor
// 14.745MHz processor
// 8MHz processor
// 7.37MHz processor
// 4MHz processor
// 3.69MHz processor

#define CYCLES_PER_US ((F_CPU+500000)/1000000) // cpu cycles per


microsecond
#endif
07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

14

########### change this lines according to your project ##################


#put the name of the target mcu here (at90s8515, at90s8535, attiny22, atmega603 etc.)
MCU = atmega16
#put the name of the target file here (without extension)
TRG = disp7seg
#put your C sourcefiles here
SRC = $(TRG).c
#put additional assembler source file here
ASRC =
#additional libraries and object files to link
LIB =
#additional includes to compile
INC =
#assembler flags
ASFLAGS = -Wa, -gstabs
#compiler flags
CPFLAGS = -g -Os -Wall -Wstrict-prototypes -I$(AVRLIB) -Wa,-ahlms=$(<:.c=.lst)
#linker flags
LDFLAGS = -Wl,-Map=$(TRG).map,--cref
########### you should not need to change the following line #############
include $(AVRLIB)/make/avrproj_make
buffer.o : buffer.c
buffer.h
uart.o
: uart.c
uart.h
global.h
uart2.o : uart2.c
uart2.h
global.h
rprintf.o : rprintf.c
rprintf.h
a2d.o
: a2d.c
a2d.h
Ing. Germn Pereira Muoz
timer.o : timer.c
timer.h
global.h
Programacion C para AVR
pulse.o : pulse.c
pulse.h
timer.h
global.h
07/22/10
lcd.o
: lcd.c
lcd.h
global.h
15

Programa Ejemplo
disp7seg.c
archivo: make

Programa Ejemplo
disp7seg.c
Cabecera
//----- Archivos Include --------------------------------------------------------#include <avr/io.h>
// include I/O definiciones (nombres : port, pin , etc)
#include <avr/signal.h>
// include nombres "signal" (nombres
interrupcion)
#include <avr/interrupt.h> // include soporte para interrupciones
#include "global.h" // include configuraciones globales
#include <avr/delay.h>

07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

16

Tarea Laboratorio 1
Realizar el diseo circuital para:
Matriz de led
Motor paso a paso

07/22/10

Ing. Germn Pereira Muoz


Programacion C para AVR

17

Vous aimerez peut-être aussi