timer0 stopwatch

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 
00006 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2);
00007 Serial pc (USBTX, USBRX);
00008 DigitalOut myled(LED1);
00009 
00010 // global vars
00011 int t [6];
00012 int run_status = -1;
00013 
00014 // functions
00015 void update_display();
00016 void update_timer();
00017 void init_timer0();
00018 
00019 int main() {
00020     
00021     // init timer and display
00022     for (int i=0;i<6;i++) t[i]=0;
00023     update_display();
00024 
00025     // init timer
00026     init_timer0();
00027     char input = 'z';
00028     
00029     while(1) {
00030         
00031         if ( input == 's') { 
00032             run_status = 1;
00033             LPC_TIM0->TCR |= 1;
00034         }
00035         if ( input == 'p') {
00036             run_status = 2;
00037             LPC_TIM0->TCR &= ~1;
00038         }
00039         if ( input == 'r') {
00040             if (run_status == 2) {
00041                 for (int i=0; i<6; i++) t[i]=0;
00042                 update_display();
00043             }
00044         }
00045         
00046         input = pc.getc();
00047    
00048     } 
00049 }
00050 
00051 void update_display() {
00052     lcd.printf("%d%d:%d%d:%d%d\n\n",t[5],t[4],t[3],t[2],t[1],t[0]);
00053 }
00054 
00055 void update_timer() {
00056 // signal = 1 : start
00057 // signal = 2 : stop
00058 
00059     if (run_status == 1) {
00060         
00061         // update m0
00062         t[0]++;
00063 
00064         // update m1
00065         if (t[0] >= 10) {
00066             t[0] = 0;
00067             t[1]++;
00068 
00069             // update S0
00070             if (t[1] >= 10) {
00071                 t[1] = 0;
00072                 t[2]++;
00073 
00074                 // update S1
00075                 if (t[2] >= 10) {
00076                     t[2] = 0;
00077                     t[3]++;
00078 
00079                     // update M0
00080                     if (t[3] >= 6) {
00081                         t[3]=0;
00082                         t[4]++;
00083 
00084                         // update M1
00085                         if (t[4] >= 10) {
00086                             t[4]=0;
00087                             t[5]++;
00088                         }
00089                     }
00090                 }
00091             }
00092         }
00093 
00094         // update display
00095         update_display();
00096     }        
00097     
00098     // reset interrupt
00099     LPC_TIM0->IR |= 1 << 0;
00100 }
00101 
00102 void init_timer0() {
00103 
00104     //power up TIMER0 (PCONP[1])
00105     LPC_SC->PCONP |= 1 << 1;
00106 
00107     // reset and set TIMER0 to timer mode
00108     LPC_TIM0->TCR = 0x2;
00109     LPC_TIM0->CTCR = 0x0;
00110 
00111     // set no prescaler
00112     LPC_TIM0->PR = 0;
00113 
00114     // calculate period (1 interrupt every 1/100 second)
00115     uint32_t period = 240000;
00116 
00117     // set match register and enable interrupt
00118     LPC_TIM0->MR0 = period;
00119     LPC_TIM0->MCR |= 1 << 0;    // interrupt on match
00120     LPC_TIM0->MCR |= 1 << 1;    // reset on match
00121 
00122     // enable the vector in the interrupt controller
00123     NVIC_SetVector(TIMER0_IRQn, (uint32_t)&update_timer);
00124     NVIC_EnableIRQ(TIMER0_IRQn);
00125 
00126     // start the timer
00127     LPC_TIM0->TCR = 1;
00128 }