En esta sección veremos como realizar distintas prácticas con los perifericos del timer del MC9S08QE128, este microcrontrolador tiene un reloj de tiempo real, que puede funcionar con un cristal de 32.768KHz, para que de el segundo exacto, la tarjeta DEMOQE ya tiene incluido este cristal, ahora veremos como realizar un reloj con la tarjeta.
1 |
#include <hidef.h> /* for EnableInterrupts macro */ |
En esta práctica vemos como utilizar el PWM, no esta probada esta practica en la tarjeta, favor de revisar su funcionamiento, saludos
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#include <hidef.h> /* for EnableInterrupts macro */ #include "derivative.h" /* include peripheral declarations */ #define TPM_INPUT_CLK 20000 //8MHz 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 TPM_Init(void) { TPM3SC = 0x02; return; } char TPM_Config(unsigned char Freq, char Duty) { unsigned int Mod = TPM_INPUT_CLK; unsigned long Cnt = 0x00; if(Freq > 256){ return 0; }else{ Mod /= Freq; TPM3MOD = Mod; TPM3C0SC = 0x24; Cnt = (unsigned long)(Mod * Duty); Cnt /= 100; TPM3C0V = (int)Cnt; return 1; } } void TPM_Start(void) { TPM3CNT = 0; TPM3SC |= 0x0C; //bus clock, divided by 8 return; } void TPM_Stop(void) { char Dummy; Dummy = TPM3SC; TPM3SC &= 0x67; //bus clock, divided by 8 return; } void main(void) { EnableInterrupts; /* enable interrupts */ /* include your code here */ Init_Config_Clock(); TPM_Init(); TPM_Config(10,50); TPM_Start(); for(;;) { } /* loop forever */ /* please make sure that you never leave main */ } |














