Sample prog to test timer aspects

Dependencies:   TextLCD mbed-src

Testing mbed Timer

Committer:
eduardoG26
Date:
Mon Mar 30 15:33:35 2015 +0000
Revision:
2:a8c19b073684
Parent:
1:fa206fcadfad
bugfix: timer mask

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eduardoG26 0:a55017238349 1 /* Testing the overflow of Timer
eduardoG26 0:a55017238349 2 LCD on I2C bus, PCF8574
eduardoG26 0:a55017238349 3 */
eduardoG26 0:a55017238349 4 #include "mbed.h"
eduardoG26 0:a55017238349 5 #include "TextLCD.h"
eduardoG26 0:a55017238349 6
eduardoG26 0:a55017238349 7 Timer t;
eduardoG26 0:a55017238349 8
eduardoG26 0:a55017238349 9 I2C i2c_lcd(I2C_SDA,I2C_SCL); // SDA, SCL
eduardoG26 0:a55017238349 10
eduardoG26 0:a55017238349 11 TextLCD_I2C lcd(&i2c_lcd, 0x40, TextLCD::LCD16x2); // I2C bus, PCF8574 Slaveaddress, LCD Type
eduardoG26 0:a55017238349 12
eduardoG26 2:a8c19b073684 13 // Trick to change baudrate of stdout, SFord
eduardoG26 2:a8c19b073684 14 void baud(int baudrate)
eduardoG26 2:a8c19b073684 15 {
eduardoG26 2:a8c19b073684 16 Serial s(USBTX, USBRX);
eduardoG26 2:a8c19b073684 17 s.baud(baudrate);
eduardoG26 2:a8c19b073684 18 }
eduardoG26 2:a8c19b073684 19
eduardoG26 0:a55017238349 20 int main()
eduardoG26 0:a55017238349 21 {
eduardoG26 0:a55017238349 22 uint32_t Millis, LastMillis;
eduardoG26 2:a8c19b073684 23 baud(921600); // see function above
eduardoG26 0:a55017238349 24 i2c_lcd.frequency(400000); // 400KHz
eduardoG26 0:a55017238349 25 lcd.setCursor(lcd.CurOff_BlkOff); // Cursor off, Blink off
eduardoG26 0:a55017238349 26 lcd.cls();
eduardoG26 0:a55017238349 27 lcd.setBacklight(TextLCD::LightOn);
eduardoG26 0:a55017238349 28 lcd.locate(0,0); // Goto beginning of line = second par.
eduardoG26 0:a55017238349 29 lcd.printf("Testing Timer...\n");
eduardoG26 0:a55017238349 30 lcd.locate(0,1); // Goto beginning of line = second par.
eduardoG26 0:a55017238349 31 lcd.printf("...us overflow \n");
eduardoG26 2:a8c19b073684 32 printf("\nTesting Timer us overflow..\n");
eduardoG26 0:a55017238349 33
eduardoG26 0:a55017238349 34 t.reset();
eduardoG26 0:a55017238349 35 t.start();
eduardoG26 2:a8c19b073684 36
eduardoG26 2:a8c19b073684 37 #define TIMER_MASK (0x1000uL - 1)
eduardoG26 2:a8c19b073684 38
eduardoG26 2:a8c19b073684 39 Millis = t.read_ms() & TIMER_MASK;
eduardoG26 0:a55017238349 40 lcd.locate(0,1); // Goto beginning of line = second par.
eduardoG26 0:a55017238349 41 lcd.printf("Start M: %08d",Millis);
eduardoG26 0:a55017238349 42
eduardoG26 0:a55017238349 43 do {
eduardoG26 0:a55017238349 44 LastMillis = Millis;
eduardoG26 2:a8c19b073684 45 Millis = t.read_ms() & TIMER_MASK;
eduardoG26 0:a55017238349 46 } while(LastMillis <= Millis);
eduardoG26 0:a55017238349 47
eduardoG26 0:a55017238349 48 lcd.locate(0,0); // Goto beginning of line = second par.
eduardoG26 0:a55017238349 49 lcd.printf("Last M: %08d",LastMillis);
eduardoG26 0:a55017238349 50 lcd.locate(0,1);
eduardoG26 0:a55017238349 51 lcd.printf("Act. M: %08d",Millis);
eduardoG26 0:a55017238349 52
eduardoG26 2:a8c19b073684 53 printf("Last Millis: %08d\n",LastMillis);
eduardoG26 2:a8c19b073684 54 printf("Act. Millis: %08d\n",Millis);
eduardoG26 0:a55017238349 55 }