Test timer interrupt, generat flags with period of 100ms 500ms and 1000ms from kl25z :D

Dependencies:   mbed

Committer:
PedroS1989
Date:
Thu Mar 19 12:35:32 2015 +0000
Revision:
0:2ecfe9cc0e2b
Test interrupt timer 0 -> generate flags with period of 100ms 500ms and 1000ms ; ; This is good work for applications

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PedroS1989 0:2ecfe9cc0e2b 1 //AUTOR: Pedro Santos
PedroS1989 0:2ecfe9cc0e2b 2 // plainas_14@hotmail.com
PedroS1989 0:2ecfe9cc0e2b 3 //DATA: 19/13/2015
PedroS1989 0:2ecfe9cc0e2b 4 //NOTAS: http:
PedroS1989 0:2ecfe9cc0e2b 5 //CODIGO PARA: programa de teste para entradas e saidas da placa kl25Z
PedroS1989 0:2ecfe9cc0e2b 6
PedroS1989 0:2ecfe9cc0e2b 7 #include "mbed.h"
PedroS1989 0:2ecfe9cc0e2b 8
PedroS1989 0:2ecfe9cc0e2b 9 Serial pc(USBTX, USBRX); // Declare the serial port
PedroS1989 0:2ecfe9cc0e2b 10
PedroS1989 0:2ecfe9cc0e2b 11 //********************************************************************
PedroS1989 0:2ecfe9cc0e2b 12 //********************************************************************
PedroS1989 0:2ecfe9cc0e2b 13 //DEFINIÇÃO DE ENTRADAS E SAIDAS USADAS NO PROGRAMA
PedroS1989 0:2ecfe9cc0e2b 14 //SAIDAS DIGITAIS
PedroS1989 0:2ecfe9cc0e2b 15 DigitalOut led1 (PTD0);
PedroS1989 0:2ecfe9cc0e2b 16 DigitalOut led2 (PTD3);
PedroS1989 0:2ecfe9cc0e2b 17 DigitalOut led3 (PTD2);
PedroS1989 0:2ecfe9cc0e2b 18 DigitalOut led4 (PTE5);
PedroS1989 0:2ecfe9cc0e2b 19
PedroS1989 0:2ecfe9cc0e2b 20 DigitalOut led_r(LED1); // Define digital outputs for each LED color
PedroS1989 0:2ecfe9cc0e2b 21 DigitalOut led_g(LED2);
PedroS1989 0:2ecfe9cc0e2b 22 DigitalOut led_b(LED3);
PedroS1989 0:2ecfe9cc0e2b 23
PedroS1989 0:2ecfe9cc0e2b 24 //ENTRADAS DIGITAIS
PedroS1989 0:2ecfe9cc0e2b 25 DigitalIn tecla1 (PTE20);
PedroS1989 0:2ecfe9cc0e2b 26 DigitalIn tecla2 (PTE21);
PedroS1989 0:2ecfe9cc0e2b 27 DigitalIn tecla3 (PTE22);
PedroS1989 0:2ecfe9cc0e2b 28 DigitalIn tecla4 (PTE23);
PedroS1989 0:2ecfe9cc0e2b 29
PedroS1989 0:2ecfe9cc0e2b 30 int count_1000ms=0;
PedroS1989 0:2ecfe9cc0e2b 31 int count_500ms=0;
PedroS1989 0:2ecfe9cc0e2b 32 int count_100ms=0;
PedroS1989 0:2ecfe9cc0e2b 33
PedroS1989 0:2ecfe9cc0e2b 34 char f_100ms=0;
PedroS1989 0:2ecfe9cc0e2b 35 char f_500ms=0;
PedroS1989 0:2ecfe9cc0e2b 36 char f_1000ms=0;
PedroS1989 0:2ecfe9cc0e2b 37
PedroS1989 0:2ecfe9cc0e2b 38 //********************************************************************
PedroS1989 0:2ecfe9cc0e2b 39 //********************************************************************
PedroS1989 0:2ecfe9cc0e2b 40 void TPM0_IRQHandler(void){ //10ms interrupt
PedroS1989 0:2ecfe9cc0e2b 41
PedroS1989 0:2ecfe9cc0e2b 42 count_1000ms++;
PedroS1989 0:2ecfe9cc0e2b 43 count_500ms++;
PedroS1989 0:2ecfe9cc0e2b 44 count_100ms++;
PedroS1989 0:2ecfe9cc0e2b 45
PedroS1989 0:2ecfe9cc0e2b 46 if(count_100ms==10){
PedroS1989 0:2ecfe9cc0e2b 47 // led_b=!led_b;
PedroS1989 0:2ecfe9cc0e2b 48 led3=!led3;
PedroS1989 0:2ecfe9cc0e2b 49 count_100ms=0;
PedroS1989 0:2ecfe9cc0e2b 50 }
PedroS1989 0:2ecfe9cc0e2b 51
PedroS1989 0:2ecfe9cc0e2b 52 if(count_500ms==50){
PedroS1989 0:2ecfe9cc0e2b 53 // led_g=!led_g;
PedroS1989 0:2ecfe9cc0e2b 54 led2=!led2;
PedroS1989 0:2ecfe9cc0e2b 55 count_500ms=0;
PedroS1989 0:2ecfe9cc0e2b 56 }
PedroS1989 0:2ecfe9cc0e2b 57
PedroS1989 0:2ecfe9cc0e2b 58 if(count_1000ms==100){
PedroS1989 0:2ecfe9cc0e2b 59 // led_r=!led_r;
PedroS1989 0:2ecfe9cc0e2b 60 led1=!led1;
PedroS1989 0:2ecfe9cc0e2b 61 count_1000ms=0;
PedroS1989 0:2ecfe9cc0e2b 62 }
PedroS1989 0:2ecfe9cc0e2b 63
PedroS1989 0:2ecfe9cc0e2b 64
PedroS1989 0:2ecfe9cc0e2b 65 TPM0->SC= 0x000000cF; //Volta a defenir a cena
PedroS1989 0:2ecfe9cc0e2b 66 return;
PedroS1989 0:2ecfe9cc0e2b 67 }
PedroS1989 0:2ecfe9cc0e2b 68
PedroS1989 0:2ecfe9cc0e2b 69 //********************************************************************
PedroS1989 0:2ecfe9cc0e2b 70 //********************************************************************
PedroS1989 0:2ecfe9cc0e2b 71 void init_timer0(){
PedroS1989 0:2ecfe9cc0e2b 72
PedroS1989 0:2ecfe9cc0e2b 73 // TPM0->SC=0x2b;
PedroS1989 0:2ecfe9cc0e2b 74 // TPM0_DMA=0;//->DMA=0; //disable dma
PedroS1989 0:2ecfe9cc0e2b 75 // TPM0_SC->TOF=0; //flag de interruption
PedroS1989 0:2ecfe9cc0e2b 76 // TPM0_SC->TOIE=1; //enable interupt flag
PedroS1989 0:2ecfe9cc0e2b 77 // TPM0_SC->CPWMS=0; //enable interupt flag
PedroS1989 0:2ecfe9cc0e2b 78 // TPM0_SC->CMOD=1; //cmod register
PedroS1989 0:2ecfe9cc0e2b 79 // TPM0_SC->ps=8; //pre- escalar div pag 553 datasheet
PedroS1989 0:2ecfe9cc0e2b 80
PedroS1989 0:2ecfe9cc0e2b 81
PedroS1989 0:2ecfe9cc0e2b 82 // TMP0->CNT=100;
PedroS1989 0:2ecfe9cc0e2b 83 // TMP0->MOD=5000;
PedroS1989 0:2ecfe9cc0e2b 84
PedroS1989 0:2ecfe9cc0e2b 85 led_b=0;
PedroS1989 0:2ecfe9cc0e2b 86
PedroS1989 0:2ecfe9cc0e2b 87 wait(1);
PedroS1989 0:2ecfe9cc0e2b 88
PedroS1989 0:2ecfe9cc0e2b 89 led_b=!led_b;
PedroS1989 0:2ecfe9cc0e2b 90
PedroS1989 0:2ecfe9cc0e2b 91 SIM->SOPT2=(SIM->SOPT2 | (1UL<<24));
PedroS1989 0:2ecfe9cc0e2b 92 SIM->SCGC6=(SIM->SCGC6 | (1UL<<24));//enable TPM 0,1
PedroS1989 0:2ecfe9cc0e2b 93 //TPM0->SC=0x0;
PedroS1989 0:2ecfe9cc0e2b 94 //
PedroS1989 0:2ecfe9cc0e2b 95 //TPM0->CNT=200;
PedroS1989 0:2ecfe9cc0e2b 96 //TPM0->MOD|=4850;
PedroS1989 0:2ecfe9cc0e2b 97 //TPM0->SC|=0xEF;//0008
PedroS1989 0:2ecfe9cc0e2b 98
PedroS1989 0:2ecfe9cc0e2b 99
PedroS1989 0:2ecfe9cc0e2b 100 TPM0->SC=0x0;
PedroS1989 0:2ecfe9cc0e2b 101
PedroS1989 0:2ecfe9cc0e2b 102 TPM0->CNT=0x0;
PedroS1989 0:2ecfe9cc0e2b 103 TPM0->SC=0x00000040; //disable LPTPM
PedroS1989 0:2ecfe9cc0e2b 104 TPM0->MOD=3750; //max value = 1/(48mhz/128) -> 10^-3/ans =3750
PedroS1989 0:2ecfe9cc0e2b 105 TPM0->SC=0x000000cF;//enable LPTPM
PedroS1989 0:2ecfe9cc0e2b 106
PedroS1989 0:2ecfe9cc0e2b 107
PedroS1989 0:2ecfe9cc0e2b 108
PedroS1989 0:2ecfe9cc0e2b 109 }
PedroS1989 0:2ecfe9cc0e2b 110
PedroS1989 0:2ecfe9cc0e2b 111
PedroS1989 0:2ecfe9cc0e2b 112 //********************************************************************
PedroS1989 0:2ecfe9cc0e2b 113 //****************************** MAIN ********************************
PedroS1989 0:2ecfe9cc0e2b 114 //********************************************************************
PedroS1989 0:2ecfe9cc0e2b 115 int main()
PedroS1989 0:2ecfe9cc0e2b 116 {
PedroS1989 0:2ecfe9cc0e2b 117 led_r=1;
PedroS1989 0:2ecfe9cc0e2b 118 led_g=1;
PedroS1989 0:2ecfe9cc0e2b 119 led_b=1;
PedroS1989 0:2ecfe9cc0e2b 120 init_timer0();
PedroS1989 0:2ecfe9cc0e2b 121
PedroS1989 0:2ecfe9cc0e2b 122 pc.baud (38400);
PedroS1989 0:2ecfe9cc0e2b 123
PedroS1989 0:2ecfe9cc0e2b 124 NVIC_SetVector(TPM0_IRQn, (uint32_t)&TPM0_IRQHandler);
PedroS1989 0:2ecfe9cc0e2b 125 NVIC_SetPriority(TPM0_IRQn, 1);
PedroS1989 0:2ecfe9cc0e2b 126 NVIC_EnableIRQ(TPM0_IRQn);
PedroS1989 0:2ecfe9cc0e2b 127
PedroS1989 0:2ecfe9cc0e2b 128 //****************************** LOOP ********************************
PedroS1989 0:2ecfe9cc0e2b 129 while(1) {
PedroS1989 0:2ecfe9cc0e2b 130
PedroS1989 0:2ecfe9cc0e2b 131 led_b = 0;
PedroS1989 0:2ecfe9cc0e2b 132 wait(0.1); //Wait 100ms
PedroS1989 0:2ecfe9cc0e2b 133 led_b = 1;
PedroS1989 0:2ecfe9cc0e2b 134 wait(0.1); //Wait 100ms
PedroS1989 0:2ecfe9cc0e2b 135
PedroS1989 0:2ecfe9cc0e2b 136 /*
PedroS1989 0:2ecfe9cc0e2b 137 // pc.printf("%u \n\r",TPM0->CNT); // Send a status message
PedroS1989 0:2ecfe9cc0e2b 138
PedroS1989 0:2ecfe9cc0e2b 139 //testa tecla 1 e 2
PedroS1989 0:2ecfe9cc0e2b 140 if(tecla1==1 || tecla2==1)
PedroS1989 0:2ecfe9cc0e2b 141 led3=1;
PedroS1989 0:2ecfe9cc0e2b 142 else
PedroS1989 0:2ecfe9cc0e2b 143 led3=0;
PedroS1989 0:2ecfe9cc0e2b 144
PedroS1989 0:2ecfe9cc0e2b 145 //testa tecla 3 e 4
PedroS1989 0:2ecfe9cc0e2b 146 if(tecla3==1 || tecla4==1)
PedroS1989 0:2ecfe9cc0e2b 147 led4=1;
PedroS1989 0:2ecfe9cc0e2b 148 else
PedroS1989 0:2ecfe9cc0e2b 149 led4=0;
PedroS1989 0:2ecfe9cc0e2b 150 */
PedroS1989 0:2ecfe9cc0e2b 151
PedroS1989 0:2ecfe9cc0e2b 152 }
PedroS1989 0:2ecfe9cc0e2b 153 }