Las entradas y salidas digitales son los primeros periféricos que veremos, como su nombre lo indica, son entradas digitales (3.3V) y salidas (3.3V).
La primera practicas que veremos, será hacer parpadear los leds que se encuentran en el puerto C, del C0 al C5
1 |
#include <hidef.h> /* for EnableInterrupts macro */ |
En la segunda práctica veremos como utlizar los puertos de entrada que encuentran en los pines PTA2, PTA3, PTD2, PTD3 conectados a unos push button:
1 |
#include <hidef.h> /* for EnableInterrupts macro */ |
En esta practica, veremos como utilizar las interrupciones de teclado, llamadas KBI, el programa realiza lo mismo que el primero, con la excepción que, al presionar PTA2 y PTA3, cambiará el valor del retardo, provocando que visualmente se vea mas lento o rapido el parpader, esto se realiza al cambiar el valor de la variable delay_val, la cual determina hasta que valor el ciclo for, en la función delay, se cierra.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#include <hidef.h> /* for EnableInterrupts macro */ #include "derivative.h" /* include peripheral declarations */ unsigned int delay_val=60000; //variable de retardo void Init_Config_Clock(void){ /* Common initialization of the write once registers */ /* SOPT1: COPE=0,COPT=1,STOPE=0,RSTOPE=0,BKGDPE=1,RSTPE=0 */ SOPT1 = 0x42; /* SPMSC1: LVDF=0,LVDACK=0,LVDIE=0,LVDRE=1,LVDSE=1,LVDE=1,BGBE=0 */ SPMSC1 = 0x1C; /* SPMSC2: LPR=0,LPRS=0,LPWUI=0,PPDF=0,PPDACK=0,PPDE=1,PPDC=0 */ SPMSC2 = 0x02; /* SPMSC3: LVDV=0,LVWV=0,LVWIE=0 */ SPMSC3 &= (unsigned char)~0x38; /* System clock initialization */ if (*(unsigned char*far)0xFFAF != 0xFF) { /* Test if the device trim value is stored on the specified address */ ICSTRM = *(unsigned char*far)0xFFAF; /* Initialize ICSTRM register from a non volatile memory */ ICSSC = (unsigned char)((*(unsigned char*far)0xFFAE) & (unsigned char)0x01); /* Initialize ICSSC register from a non volatile memory */ } /* ICSC1: CLKS=0,RDIV=0,IREFS=0,IRCLKEN=0,IREFSTEN=0 */ ICSC1 = 0x00; /* Initialization of the ICS control register 1 */ /* ICSC2: BDIV=0,RANGE=0,HGO=0,LP=0,EREFS=1,ERCLKEN=0,EREFSTEN=0 */ ICSC2 = 0x04; /* Initialization of the ICS control register 2 */ while(!ICSSC_OSCINIT) { /* Wait until the initialization of the external crystal oscillator is completed */ } /* ICSSC: DRST_DRS=0,DMX32=0 */ ICSSC &= (unsigned char)~0xE0; /* Initialization of the ICS status and control */ while((ICSSC & 0xC0) != 0x00) { /* Wait until the FLL switches to Low range DCO mode */ } /* SCGC1: TPM3=1,TPM2=1,TPM1=1,ADC=1,IIC2=1,IIC1=1,SCI2=1,SCI1=1 */ SCGC1 = 0xFF; /* SCGC2: DBG=1,FLS=1,IRQ=1,KBI=1,ACMP=1,RTC=1,SPI2=1,SPI1=1 */ SCGC2 = 0xFF; } void delay(void){ unsigned int i; for (i=0; i<delay_val;i++); } void interrupt VectorNumber_Vkeyboard KBI_isr(){ KBI1SC_KBACK=1; //limpiamos la bandera para salir de la interrupcion if(!PTAD_PTAD2){ //preguntamos si el pin A2 esta en 0, si es asi, entonces hace la sentencia delay_val=30000; } if(!PTAD_PTAD3){ //preguntamos si el pin A3 esta en 0, si es asi, entonces hace la sentencia delay_val=60000; } } void main(void) { EnableInterrupts; /* enable interrupts */ /* include your code here */ Init_Config_Clock(); //configuramos el reloj para que trabaje a 8 MHz con un cristal de 32.768KHz PTCDD= 0x3F; //configuramos como salida el puerto C los pines C0 a C5, para salida son 1, para entrada 0 PTCD=0xFF; PTADD= 0x00; //configuramos el puerto A como entrada PTAPE=0x0C; //habilitamos las resistencias Pullup en A2 y A3 KBI1PE= 0x0C; //habilitamos los pines A3 y A2 como interrupciones de KBI KBI1SC= 0x02; //habilitamos las interrupciones en KBI for(;;) { PTCD=0x3F; delay(); PTCD=0x00; delay(); } /* loop forever */ /* please make sure that you never leave main */ } |














