viernes, 3 de septiembre de 2010

Delay

Para poner un delay en el código para el micro de freescale en el compilador de CodeWarrior solamente debes de poner un for que de ciclos a lo wey para que cuente muchas veces hasta que te de el tiempo que quiere, según el prof. esta es la única forma de hecalro pero mi equipo y yo creemos que se puede hacer de otra forma, con alguna función de la librería de CodeWarrior que por cierto no encontramos.

CONTEO DE 4 BITS CON INTERRUPCIONES USANDO IFS

#include /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */

#define ON 0
#define OFF 1
#define LED1 PTFD_PTFD0
#define LED2 PTFD_PTFD1
#define LED3 PTFD_PTFD3
#define LED4 PTFD_PTFD4
#define SW1 PTGD_PTGD0
#define SW2 PTGD_PTGD1

void escribeLEDs(char dato);

unsigned char x;

void main(void){
EnableInterrupts;
SOPT_COPE=0;

PTFDD_PTFDD0=1; //INICIALIZA PUERTO PARA LED
PTFDD_PTFDD1=1;
PTFDD_PTFDD4=1;
PTFDD_PTFDD5=1;
PTFDS_PTFDS0=1;
PTFDS_PTFDS1=1;
PTFDS_PTFDS4=1;
PTFDS_PTFDS5=1;


PTGDD_PTGDD0=0; //INICIALIZA PUERTO PARA SW
PTGDD_PTGDD1=0;
PTGPE_PTGPE0=1;
PTGPE_PTGPE1=1;

/*Inicializa KBI SW1 SW2*/
KBISC_KBIMOD=0; //SOLO TRANSICION
KBIPE_KBIPE0=1; //HABILITA PINES KBI
KBIPE_KBIPE1=1;
KBISC_KBIE=1; //HABILITA INTERRUPCION
x=0;
for(;;)
{ }
}

void interrupt 26 isrkbi(void){
if(SW1==ON)
{
if(x==15)
x=15;
else
x++;
}
if(SW2==ON)
{
if(x==0)
x=0;
else
x--;
}
KBISC_KBACK = 1; //Confirma haber recibido y atendido la interrupción
escribeLEDs(x);
}

void escribeLEDs(char dato){
char aux1,aux2;
aux1=PTFD & 0xCC;
aux2=dato<<2;
aux2=aux2&0x30 | dato&0x03;
aux1|=aux2;
PTFD=aux1^0x33;
}

CONTEO DE 4 BITS CON INTERRUPCIONES USANDO SWITCH

Aumento y decremento de numero binario en 4 LEDs por interrupciones.

Ejemplo en clase de Encendido y apago de LED por intrrupciones.

#include
#include “derivative.h”

#define ON 0
#define OFF 1
#define LED1 PTFD_PTFD0
#define LED2 PTFD_PTFD1
#define LED3 PTFD_PTFD2
#define LED4 PTFD_PTFD3

#define SW1
#define SW2

unsigned int x; //Declaro variable global x

void main(void) {
Enable_Interrupts; /*enable interrupts */
SOPT_COPE = 0;

PTFDD_PTFDD0 = 1; // Inicializa puerto para LED
PTFDS_PTFDS0 = 1;
PTFDD_PTFDD1 = 1; // Inicializa puerto para LED
PTFDS_PTFDS1 = 1;
PTFDD_PTFDD4 = 1; // Inicializa puerto para LED
PTFDS_PTFDS4 = 1;
PTFDD_PTFDD5 = 1; // Inicializa puerto para LED
PTFDS_PTFDS5 = 1;


PTGDD_PTGDD0 = 0; // Iinicializa puerto para SW
PTGDD_PTGDD1 = 0;
PTGDD_PTGPE0 = 1;
PTGDD_PTGPE1 = 1;

//Inicializa KBI enSW1 y SW2
KBISC_KBIMOD = 0; // Solo transición
KBIPE_KBIPE0 = 1; //Habilita pines de KBI
KBIPE_KBIPE1 = 1;
KBISC_KBIE = 1; // Habilita interrupciónn de KBI

x=0; //Declado que x empiece con valor de 0 antes de entrar al loop.

For(;;) {
// __RESET_WATCHDOG(); /*feeds the dog*/
} /*loop forever*/
/* please make sure that you never leave main */
}

void interrupt 26 isrkbi(void) { //26 corresponde a la isr para KBI

if(SW1 == ON) {
switch(x) {

case 0:
x++;
LED1 = OFF;
LED2 = OFF;
LED3 = OFF;
LED4 = OFF;
break;

case 1:
x++;
LED1 = ON;
LED2 = OFF;
LED3 = OFF;
LED4 = OFF;
break;

case 2:
x++;
LED1 = OFF;
LED2 = ON;
LED3 = OFF;
LED4 = OFF;
break;

case 3:
x++;
LED1 = ON;
LED2 = ON;
LED3 = OFF;
LED4 = OFF;
break;

case 4:
x++;
LED1 = OFF;
LED2 = OFF;
LED3 = ON;
LED4 = OFF;
break;

case 5:
x++;
LED1 = ON;
LED2 = OFF;
LED3 = ON;
LED4 = OFF;
break;

case 6:
x++;
LED1 = OFF;
LED2 = ON;
LED3 = ON;
LED4 = OFF;
break;

case 7:
x++;
LED1 = ON;
LED2 = ON;
LED3 = ON;
LED4 = OFF;
break;

case 8:
x++;
LED1 = OFF;
LED2 = OFF;
LED3 = OFF;
LED4 = ON;
break;

case 9:
x++;
LED1 = ON;
LED2 = OFF;
LED3 = OFF;
LED4 = ON;
break;
case 10:
x++;
LED1 = OFF;
LED2 = ON;
LED3 = OFF;
LED4 = ON;
break;

case 11:
x++;
LED1 = ON;
LED2 = ON;
LED3 = OFF;
LED4 = ON;
break;

case 12:
x++;
LED1 = OFF;
LED2 = OFF;
LED3 = ON;
LED4 = ON;
break;

case 13:
x++;
LED1 = ON;
LED2 = OFF;
LED3 = ON;
LED4 = ON;
break;

case 14:
x++;
LED1 = OFF;
LED2 = ON;
LED3 = ON;
LED4 = ON;
break;

case 15:
x++;
LED1 = ON;
LED2 = ON;
LED3 = ON;
LED4 = ON;
break;

default:
x=0;
LED1 = OFF;
LED2 = OFF;
LED3 = OFF;
LED4 = OFF;
break;

}
KBISC_KBACK = 1; //Confirma haber recibido y atendido la interrupciónn.
}

If(SW2 == ON) {
switch(x) {

case 0:
x--;
LED1 = OFF;
LED2 = OFF;
LED3 = OFF;
LED4 = OFF;
break;

case 1:
x--;
LED1 = ON;
LED2 = OFF;
LED3 = OFF;
LED4 = OFF;
break;

case 2:
x--;
LED1 = OFF;
LED2 = ON;
LED3 = OFF;
LED4 = OFF;
break;

case 3:
x--;
LED1 = ON;
LED2 = ON;
LED3 = OFF;
LED4 = OFF;
break;

case 4:
x--;
LED1 = OFF;
LED2 = OFF;
LED3 = ON;
LED4 = OFF;
break;

case 5:
x--;
LED1 = ON;
LED2 = OFF;
LED3 = ON;
LED4 = OFF;
break;

case 6:
x--;
LED1 = OFF;
LED2 = ON;
LED3 = ON;
LED4 = OFF;
break;

case 7:
x--;
LED1 = ON;
LED2 = ON;
LED3 = ON;
LED4 = OFF;
break;

case 8:
x--;
LED1 = OFF;
LED2 = OFF;
LED3 = OFF;
LED4 = ON;
break;

case 9:
x--;
LED1 = ON;
LED2 = OFF;
LED3 = OFF;
LED4 = ON;
break;
case 10:
x--;
LED1 = OFF;
LED2 = ON;
LED3 = OFF;
LED4 = ON;
break;

case 11:
x--;
LED1 = ON;
LED2 = ON;
LED3 = OFF;
LED4 = ON;
break;

case 12:
x--;
LED1 = OFF;
LED2 = OFF;
LED3 = ON;
LED4 = ON;
break;

case 13:
x--;
LED1 = ON;
LED2 = OFF;
LED3 = ON;
LED4 = ON;
break;

case 14:
x--;
LED1 = OFF;
LED2 = ON;
LED3 = ON;
LED4 = ON;
break;

case 15:
x--;
LED1 = ON;
LED2 = ON;
LED3 = ON;
LED4 = ON;
break;

default:
x=0;
LED1 = OFF;
LED2 = OFF;
LED3 = OFF;
LED4 = OFF;
break;

}

}
KBISC_KBACK = 1; //Confirma haber recibido y atendido la interrupciónn.
}

}


//Nota: Para cambair el modo de transición para que cambie con solo dejar apretado el push button en vez de tener que apretarlo cada vez se hace cambiando la linea:

KBISC_KBIMOD = 0; // Solo transición

Por:

KBISC_KBIMOD = 1; // Continuo

Encendido y Apagao de 1 LED por interrupciones.

Ejemplo en clase de Encendido y apago de LED por intrrupciones.

#include
#include “derivative.h”

#define ON 0
#define OFF 1
#define LED1 PTFD_PTFD 0
#define SW1
#define SW2

void main(void) {
Enable_Interrupts; /*enable interrupts */
SOPT_COPE = 0;

PTFDD_PTFDD0 = 1; // Inicializa puerto para LED
PTFDS_PTFDS0 = 1;

PTGDD_PTGDD0 = 0; // Iinicializa puerto para SW
PTGDD_PTGDD1 = 0;
PTGDD_PTGPE0 = 1;
PTGDD_PTGPE1 = 1;

//Inicializa KBI enSW1 y SW2
KBISC_KBIMOD = 0; // Solo transición
KBIPE_KBIPE0 = 1; //Habilita pines de KBI
KBIPE_KBIPE1 = 1;
KBISC_KBIE = 1; // Habilita interrupciónn de KBI

For(;;) {
// __RESET_WATCHDOG(); /*feeds the dog*/
} /*loop forever*/
/* please make sure that you never leave main */
}

void interrupt 26 isrkbi(void) { //26 corresponde a la isr para KBI
if(SW1 == ON)
LED1 = ON;
If(SW2 == ON)
LED1 == OFF;
KBISC_KBACK = 1; //Confirma haber recibido y atendido la interrupciónn.
}