Edward Stott / Mbed 2 deprecated IC-SWITCH

Dependencies:   Adafruit_GFX mbed

Committer:
estott
Date:
Fri Apr 08 11:35:29 2016 +0000
Revision:
1:5aebe57872ed
Parent:
0:775d3ce39c34
Child:
2:a5b7373c4644
Fixed typo in comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
estott 0:775d3ce39c34 1 #include "mbed.h"
estott 0:775d3ce39c34 2 #include "Adafruit_SSD1306.h"
estott 0:775d3ce39c34 3
estott 0:775d3ce39c34 4 //Switch input definition
estott 0:775d3ce39c34 5 #define SW_PIN p22
estott 0:775d3ce39c34 6
estott 0:775d3ce39c34 7 //Sampling period for the switch oscillator (us)
estott 0:775d3ce39c34 8 #define SW_PERIOD 20000
estott 0:775d3ce39c34 9
estott 0:775d3ce39c34 10 //Display interface pin definitions
estott 0:775d3ce39c34 11 #define D_MOSI_PIN p5
estott 0:775d3ce39c34 12 #define D_CLK_PIN p7
estott 0:775d3ce39c34 13 #define D_DC_PIN p8
estott 0:775d3ce39c34 14 #define D_RST_PIN p9
estott 0:775d3ce39c34 15 #define D_CS_PIN p10
estott 0:775d3ce39c34 16
estott 0:775d3ce39c34 17 //an SPI sub-class that sets up format and clock speed
estott 0:775d3ce39c34 18 class SPIPreInit : public SPI
estott 0:775d3ce39c34 19 {
estott 0:775d3ce39c34 20 public:
estott 0:775d3ce39c34 21 SPIPreInit(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk)
estott 0:775d3ce39c34 22 {
estott 0:775d3ce39c34 23 format(8,3);
estott 0:775d3ce39c34 24 frequency(2000000);
estott 0:775d3ce39c34 25 };
estott 0:775d3ce39c34 26 };
estott 0:775d3ce39c34 27
estott 0:775d3ce39c34 28 //Interrupt Service Routine prototypes (functions defined below)
estott 0:775d3ce39c34 29 void sedge();
estott 0:775d3ce39c34 30 void tout();
estott 0:775d3ce39c34 31
estott 0:775d3ce39c34 32 //Output for the alive LED
estott 0:775d3ce39c34 33 DigitalOut alive(LED1);
estott 0:775d3ce39c34 34
estott 0:775d3ce39c34 35 //External interrupt input from the switch oscillator
estott 0:775d3ce39c34 36 InterruptIn swin(SW_PIN);
estott 0:775d3ce39c34 37
estott 0:775d3ce39c34 38 //Switch sampling timer
estott 0:775d3ce39c34 39 Ticker swtimer;
estott 0:775d3ce39c34 40
estott 0:775d3ce39c34 41 //Registers for the switch counter, output register and update flag
estott 0:775d3ce39c34 42 uint16_t scounter=0,scount=0,update=0;
estott 0:775d3ce39c34 43
estott 0:775d3ce39c34 44 //Initialise SPI instance for communication with the display
estott 0:775d3ce39c34 45 SPIPreInit gSpi(D_MOSI_PIN,NC,D_CLK_PIN); //MOSI,MISO,CLK
estott 0:775d3ce39c34 46
estott 0:775d3ce39c34 47 //Initialise display driver instance
estott 0:775d3ce39c34 48 Adafruit_SSD1306_Spi gOled1(gSpi,D_DC_PIN,D_RST_PIN,D_CS_PIN,64,128); //SPI,DC,RST,CS,Height,Width
estott 0:775d3ce39c34 49
estott 0:775d3ce39c34 50 int main() {
estott 0:775d3ce39c34 51 //Initialisation
estott 0:775d3ce39c34 52
estott 0:775d3ce39c34 53 //Attach switch oscillator counter ISR to the switch input instance for a rising edge
estott 0:775d3ce39c34 54 swin.rise(&sedge);
estott 0:775d3ce39c34 55
estott 0:775d3ce39c34 56 //Attach switch sampling timer ISR to the timer instance with the required period
estott 0:775d3ce39c34 57 swtimer.attach_us(&tout, SW_PERIOD);
estott 0:775d3ce39c34 58
estott 0:775d3ce39c34 59 //Write some sample text
estott 0:775d3ce39c34 60 gOled1.printf("%ux%u OLED Display\r\n", gOled1.width(), gOled1.height());
estott 0:775d3ce39c34 61
estott 0:775d3ce39c34 62 //Main loop
estott 0:775d3ce39c34 63 while(1)
estott 0:775d3ce39c34 64 {
estott 0:775d3ce39c34 65 //Has the update flag been set?
estott 0:775d3ce39c34 66 if (update) {
estott 0:775d3ce39c34 67 //Clear the update flag
estott 0:775d3ce39c34 68 update = 0;
estott 0:775d3ce39c34 69
estott 0:775d3ce39c34 70 //Set text cursor
estott 0:775d3ce39c34 71 gOled1.setTextCursor(0,0);
estott 0:775d3ce39c34 72
estott 0:775d3ce39c34 73 //Write the latest switch osciallor count
estott 0:775d3ce39c34 74 gOled1.printf("\n%05u ",scount);
estott 0:775d3ce39c34 75
estott 0:775d3ce39c34 76 //Copy the display buffer to the display
estott 0:775d3ce39c34 77 gOled1.display();
estott 0:775d3ce39c34 78
estott 0:775d3ce39c34 79 //Toggle the alive LED
estott 0:775d3ce39c34 80 alive = !alive;
estott 0:775d3ce39c34 81 }
estott 0:775d3ce39c34 82
estott 0:775d3ce39c34 83
estott 0:775d3ce39c34 84 }
estott 0:775d3ce39c34 85 }
estott 0:775d3ce39c34 86
estott 0:775d3ce39c34 87
estott 1:5aebe57872ed 88 //Interrupt Service Routine for rising edge on the switch oscillator input
estott 0:775d3ce39c34 89 void sedge() {
estott 0:775d3ce39c34 90 //Increment the edge counter
estott 0:775d3ce39c34 91 scounter++;
estott 0:775d3ce39c34 92 }
estott 0:775d3ce39c34 93
estott 1:5aebe57872ed 94 //Interrupt Service Routine for the switch sampling timer
estott 0:775d3ce39c34 95 void tout() {
estott 0:775d3ce39c34 96 //Read the edge counter into the output register
estott 0:775d3ce39c34 97 scount = scounter;
estott 0:775d3ce39c34 98 //Reset the edge counter
estott 0:775d3ce39c34 99 scounter = 0;
estott 0:775d3ce39c34 100 //Trigger a display update in the main loop
estott 0:775d3ce39c34 101 update = 1;
estott 0:775d3ce39c34 102 }
estott 0:775d3ce39c34 103