my implementation of mbed-like classes using the LPC1768 register access.

Dependents:   registers-example RedWireBridge

This is just to satisfy my curiosity on how the mbed libraries work. I put it here just in case others are too. Every time I learn how another internal register works, I'll keep it here to save myself from future coding headaches.

working

  • DigitalIn
  • DigitalOut
  • wait()

mostly working

  • Serial
  • Timer
  • Ticker
  • Timeout

Serial doesn't have all the methods that mbed had, but it works for me. (only UART0, so only over USB to the pc for now) Timer has the same limitations of mbed for default resolution (30 min limit), and if you start at the end of resolution and stop after it rolls back to 0, it doesn't take that into account. But I added the option to change resolution, so I can have longer timers.

For Ticker, I used a 100 microsecond timer instead of a 1 microsecond Timer, so the smallest interval in between function calls is 100 microseconds. (10KHz) However, this means that the maximum interval in between function calls is 59 hours. (untested)

The Timeout class, simply uses a Ticker, but then marks it as nonactive after the first function call. Automatically calls the detach() function when attaching it again, so no don't need to worry about it.

InOut.h

Committer:
elevatorguy
Date:
2013-01-03
Revision:
2:276fb0fe230c
Parent:
0:d2d9baa1a6d8

File content as of revision 2:276fb0fe230c:

#ifndef __INOUT_H_
#define __INOUT_H_

#include "LPC17xx.h"

//useful constants to define
#define PULLUP 0
#define PULLDOWN 1
#define PULLNONE 2
#define KEEPER 3
#define OUTPUT 1
#define INPUT 0
#define LED1 1
#define LED2 2
#define LED3 3
#define LED4 4
#define NULL 0

#define p5 5
#define p6 6
#define p7 7
#define p8 8
#define p9 9
#define p10 10
#define p11 11
#define p12 12
#define p13 13
#define p14 14
#define p15 15
#define p16 16
#define p17 17
#define p18 18
#define p19 19
#define p20 20
#define p21 21
#define p22 22
#define p23 23
#define p24 24
#define p25 25
#define p26 26
#define p27 27
#define p28 28
#define p29 29
#define p30 30

class InOut
{
public: // char type if value never goes above 256
    char port; //the port number (ports 0, 1 or 2)
    char bit; //the bit of the port
    void write(bool); //bool because it can only be high or low
    bool read(); //bool because it can only be high or low
    InOut(char, bool); //contructor for pins (led or mbed pin #, and 1 for output or 0 for input)
    InOut(char, char, bool); // port, bit and 1 for output / 0 for input. This is for the pins that the mbed doesn't use
    void setDirection(bool); // the constructor calls this method, but we can call it too if we need to
    void mode(char); // PULLUP, PULLDOWN, PULLNONE or KEEPER (char because really we are only using 2 bits)
    InOut& operator=(bool); // so we can do led1 = 0; instead of led1.write(0); just like the mbed library does
    operator bool(); // so we can do "led1" instead of "led1.read()" just like the mbed library does
    void output(); //calls setDirection, to set bit to output
    void input(); //calls setDirection, to set bit to input
};

#endif