Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "LPC17xx.h" 00003 #include "TextLCD.h" 00004 00005 // declare hardware you're going to use, mostly copied from example code in previous assignments / Dagens piazza post 00006 volatile unsigned short timer_count; 00007 Serial pc(USBTX, USBRX); 00008 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7 00009 00010 // declare constants used for counting 00011 int running = 0; 00012 char mins_tens = '0'; 00013 char mins_ones = '0'; 00014 char seconds_tens = '0'; 00015 char seconds_ones = '0'; 00016 char ms_tens = '0'; 00017 char ms_ones = '0'; 00018 00019 00020 // hardware interrupt handler, adapted from code in piazza post by Dagaen 00021 extern "C" void TIMER0_IRQHandler (void) 00022 { 00023 if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt, proceed 00024 { 00025 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag 00026 timer_count++; //increment timer_count 00027 } 00028 } 00029 00030 // init the hardware interrupt (timer0), adapted same as above 00031 void timer0_init(void) 00032 { 00033 LPC_SC->PCONP |=1<1; //timer0 power on 00034 LPC_SC-> PCLKSEL0 |= 1 << 2; // set timer clock to CCLCK nondivided (1 clock cycle = 1 increment) 00035 LPC_TIM0->MR0 = 1000000; //100mhz clock cycle, 1 cycle = 10ns, 10ms = 10 000 000 ns = 1M cycles 00036 LPC_TIM0->MCR = 3; //interrupt and reset control 00037 //3 = Interrupt & reset timer0 on match (111) sets all three bits 00038 NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt 00039 } 00040 00041 // update the display with the global timer values 00042 void update_display(void){ 00043 // initialize the display, start at 0, paused 00044 // first MM 00045 lcd.locate(0,0); 00046 lcd.putc(mins_tens); 00047 lcd.locate(1,0); 00048 lcd.putc(mins_ones); 00049 // then : 00050 lcd.locate(2,0); 00051 lcd.putc(':'); 00052 // then SS 00053 lcd.locate(3,0); 00054 lcd.putc(seconds_tens); 00055 lcd.locate(4,0); 00056 lcd.putc(seconds_ones); 00057 // then : 00058 lcd.locate(5,0); 00059 lcd.putc(':'); 00060 // then mS 00061 lcd.locate(6,0); 00062 lcd.putc(ms_tens); 00063 lcd.locate(7,0); 00064 lcd.putc(ms_ones); 00065 } 00066 00067 // reset global values to 0 00068 void reset(void){ 00069 mins_tens = '0'; 00070 mins_ones = '0'; 00071 seconds_tens = '0'; 00072 seconds_ones = '0'; 00073 ms_tens = '0'; 00074 ms_ones = '0'; 00075 } 00076 00077 // hw interrupt callback, deal with the keyboard input from PC 00078 void kb_interrupt() { 00079 00080 // get the char, put it on the PC command line 00081 char a = pc.getc(); 00082 pc.putc(a); 00083 // if the char is S, make sure TCR's relevant bit is set 00084 if(a == 's'){ 00085 // start timer 00086 running = 1; 00087 LPC_TIM0->TCR |= 1 << 0; 00088 // if the char is p, clear TCR, pausing the timer 00089 }else if (a == 'p'){ 00090 LPC_TIM0->TCR = 0; 00091 running = 0; 00092 // if the char is r, reset the timer, potentially update display independent of clk interrupt if stopped 00093 }else if (a == 'r'){ 00094 reset(); 00095 if(!running){ 00096 update_display(); 00097 } 00098 }else{ 00099 // do nothing for invalid char 00100 } 00101 } 00102 00103 00104 int main (void) 00105 { 00106 00107 // connect the serial device (PC keybd) to the interrupt 00108 pc.attach(&kb_interrupt); 00109 00110 // reset the counters, display the original 00:00:00 state 00111 reset(); 00112 update_display(); 00113 00114 00115 //init vars, start timer 00116 timer_count = 0; 00117 timer0_init(); 00118 00119 // main program loop 00120 while(true){ 00121 // on 10ms interrupt 00122 if(timer_count){ 00123 // set timer count to 0 every 10ms 00124 timer_count = 0; 00125 00126 // count up on 10ms interrupt, make sure counter updates properly 00127 00128 // update ms ones 00129 int msones_carry_flag = 0; 00130 if(ms_ones == '9'){ 00131 ms_ones = '0'; 00132 msones_carry_flag = 1; 00133 }else{ 00134 ms_ones++; 00135 } 00136 00137 // update ms tens 00138 int mstens_carry_flag = 0; 00139 if(msones_carry_flag){ 00140 if(ms_tens == '9'){ 00141 ms_tens = '0'; 00142 mstens_carry_flag = 1; 00143 }else{ 00144 ms_tens++; 00145 } 00146 } 00147 00148 // update sec ones 00149 int seconds_ones_carry_flag = 0; 00150 if(mstens_carry_flag){ 00151 if(seconds_ones == '9'){ 00152 seconds_ones = '0'; 00153 seconds_ones_carry_flag = 1; 00154 }else{ 00155 seconds_ones++; 00156 } 00157 } 00158 00159 // update sec tens 00160 int seconds_tens_carry_flag = 0; 00161 if(seconds_ones_carry_flag){ 00162 if(seconds_tens == '5'){ 00163 seconds_tens = '0'; 00164 seconds_tens_carry_flag = 1; 00165 }else{ 00166 seconds_tens++; 00167 } 00168 } 00169 00170 // update mins ones 00171 int mins_ones_carry_flag = 0; 00172 if(seconds_tens_carry_flag){ 00173 if(mins_ones == '9'){ 00174 mins_ones = '0'; 00175 mins_ones_carry_flag = 1; 00176 }else{ 00177 mins_ones++; 00178 } 00179 } 00180 00181 // update mins tens 00182 if(mins_ones_carry_flag){ 00183 if(mins_ones == '9'){ 00184 mins_ones = '0'; 00185 mins_ones_carry_flag = 1; 00186 }else{ 00187 mins_ones++; 00188 } 00189 } 00190 00191 // update display with new vals 00192 update_display(); 00193 00194 } 00195 00196 } 00197 }
Generated on Sat Jul 16 2022 01:58:53 by
1.7.2