// Chapter 6 9S12C32 C programs // Jonathan W. Valvano, 2/07/07 // This software accompanies the book, // Embedded Microcomputer Systems: Real Time Interfacing, Second Edition // published by Thomson Engineering, 2006 // MC9S12C32, CodeWarrior C // PT3 input = external signal unsigned short Time; // incremented void Init(void){ asm sei // make atomic TIOS &=~0x08; // PT3 input capture DDRT &=~0x08; // PT3 is input TSCR1 = 0x80; // enable TCNT TSCR2 = 0x01; // 500ns clock TCTL4 = (TCTL4&0x3F)|0x40; TIE |= 0x08; // Arm IC3, rising TFLG1 = 0x08; // initially clear Time = 0; asm cli } void interrupt 11 IC3Han(void){ TFLG1 = 0x08; // acknowledge Time++; } // Program 6.2. Periodic interrupt using input capture and an external clock. // MC9S12C32, CodeWarrior C // PT1/IC1 input = external signal // rising edge to rising edge // resolution = 500ns // Range = 36 us to 32 ms, // no overflow checking unsigned short Period; // 500 ns units unsigned short First; // TCNT first edge unsigned char Done; // Set each rising void Init(void){ asm sei // make atomic TIOS &=~0x02; // PT1 input capture DDRT &=~0x02; // PT1 is input TSCR1 = 0x80; // enable TCNT TSCR2 = 0x01; // 500ns clock TCTL4 = (TCTL4&0xF3)|0x04; // rising First = TCNT; // first will be wrong Done = 0; // set on subsequent TFLG1 = 0x02; // Clear C1F TIE |= 0x02; // Arm IC1 asm cli } void interrupt 9 TC1handler(void){ Period = TC1-First; // 500ns resolution First = TC1; // Setup for next TFLG1 = 0x02; // ack by clearing C1F Done = 0xFF; } // Program 6.4. C language period measurement. // MC9S12C32, CodeWarrior C unsigned short MsPeriod,LsPeriod; unsigned short First; unsigned short Count; unsigned char Mode; void interrupt 16 TOhandler(void){ TFLG2 = 0x80; // ack Count++; if(Count==65535){ // 35 minutes MsPeriod=LsPeriod=65535; TIE=0x00; TSCR2=0x00; // Disarm Mode = 2; // done } } void interrupt 9 TIC1handler(void){ if(Mode==0){ // first edge First = TC1; Count=0; Mode=1; if(((TC1&0x8000)==0) &&(TFLG2&0x80)) Count--; } else{ // second edge if(((TC1&0x8000)==0) &&(TFLG2&0x80)) Count++; Mode = 2; // measurement done MsPeriod = Count; LsPeriod = TC1-First; if(TC1