SPI Master Test
Dependencies: mbed-rtos mbed st7565LCD
main.cpp@4:c2b67c69d048, 2016-10-04 (annotated)
- Committer:
- ryood
- Date:
- Tue Oct 04 01:26:14 2016 +0000
- Revision:
- 4:c2b67c69d048
- Parent:
- 3:2b2c6c0e9f1d
- Child:
- 5:915a9d45c969
remove back light pwm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:36cebc939c49 | 1 | #include "mbed.h" |
ryood | 0:36cebc939c49 | 2 | #include "rtos.h" |
ryood | 3:2b2c6c0e9f1d | 3 | #include "st7565LCD.h" |
ryood | 0:36cebc939c49 | 4 | |
ryood | 0:36cebc939c49 | 5 | #define SPI_SPEED (10000000) |
ryood | 0:36cebc939c49 | 6 | |
ryood | 0:36cebc939c49 | 7 | BusIn Switches(PA_0, PA_1, PA_4, PB_0, PC_1, PC_0); |
ryood | 0:36cebc939c49 | 8 | |
ryood | 0:36cebc939c49 | 9 | SPI SpiM(PA_7, PA_6, PA_5); // mosi, miso, sclk |
ryood | 0:36cebc939c49 | 10 | DigitalOut SpiMCs(PB_6); |
ryood | 0:36cebc939c49 | 11 | |
ryood | 1:74e13cd94576 | 12 | InterruptIn stepChangeInterrupt(PC_7); |
ryood | 1:74e13cd94576 | 13 | |
ryood | 3:2b2c6c0e9f1d | 14 | //ST7565(PinName mosi, PinName sclk, PinName cs, PinName rst, PinName a0); |
ryood | 3:2b2c6c0e9f1d | 15 | ST7565 gLCD(PB_15, PB_13, PB_12, PB_2, PB_1); |
ryood | 3:2b2c6c0e9f1d | 16 | |
ryood | 1:74e13cd94576 | 17 | volatile bool isStepChanged = false; |
ryood | 0:36cebc939c49 | 18 | uint8_t prevSendVal = 0x00; |
ryood | 0:36cebc939c49 | 19 | |
ryood | 1:74e13cd94576 | 20 | void setChangeStep() |
ryood | 1:74e13cd94576 | 21 | { |
ryood | 1:74e13cd94576 | 22 | isStepChanged = true; |
ryood | 1:74e13cd94576 | 23 | } |
ryood | 1:74e13cd94576 | 24 | |
ryood | 0:36cebc939c49 | 25 | int main() |
ryood | 0:36cebc939c49 | 26 | { |
ryood | 0:36cebc939c49 | 27 | printf("\r\n\nNucleo rtos SPI Master Test..\r\n"); |
ryood | 0:36cebc939c49 | 28 | |
ryood | 3:2b2c6c0e9f1d | 29 | // Setup LCD |
ryood | 0:36cebc939c49 | 30 | |
ryood | 3:2b2c6c0e9f1d | 31 | gLCD.begin(0x10); |
ryood | 3:2b2c6c0e9f1d | 32 | gLCD.drawstring(0, 0, "SPI Master Test"); |
ryood | 3:2b2c6c0e9f1d | 33 | gLCD.display(); |
ryood | 3:2b2c6c0e9f1d | 34 | |
ryood | 0:36cebc939c49 | 35 | // Setup Switches |
ryood | 0:36cebc939c49 | 36 | Switches.mode(PullUp); |
ryood | 0:36cebc939c49 | 37 | /* |
ryood | 0:36cebc939c49 | 38 | while(1) { |
ryood | 0:36cebc939c49 | 39 | printf("%x\r\n", ~Switches.read() &0x3f); |
ryood | 0:36cebc939c49 | 40 | Thread::wait(100); |
ryood | 0:36cebc939c49 | 41 | } |
ryood | 0:36cebc939c49 | 42 | */ |
ryood | 0:36cebc939c49 | 43 | |
ryood | 1:74e13cd94576 | 44 | // Setup Interrupt |
ryood | 1:74e13cd94576 | 45 | stepChangeInterrupt.fall(&setChangeStep); |
ryood | 1:74e13cd94576 | 46 | |
ryood | 0:36cebc939c49 | 47 | // Setup SPI |
ryood | 0:36cebc939c49 | 48 | SpiMCs = 1; |
ryood | 0:36cebc939c49 | 49 | SpiM.format(8, 0); |
ryood | 0:36cebc939c49 | 50 | SpiM.frequency(SPI_SPEED); |
ryood | 0:36cebc939c49 | 51 | |
ryood | 4:c2b67c69d048 | 52 | Thread::wait(1000); |
ryood | 4:c2b67c69d048 | 53 | //gLCD.clear(); |
ryood | 4:c2b67c69d048 | 54 | |
ryood | 0:36cebc939c49 | 55 | for (;;) { |
ryood | 0:36cebc939c49 | 56 | uint8_t sendVal = ~Switches.read(); |
ryood | 0:36cebc939c49 | 57 | |
ryood | 2:95204570426c | 58 | if (prevSendVal != sendVal || isStepChanged) { |
ryood | 0:36cebc939c49 | 59 | SpiMCs = 0; |
ryood | 2:95204570426c | 60 | uint8_t receivedVal = SpiM.write(sendVal); |
ryood | 0:36cebc939c49 | 61 | SpiMCs = 1; |
ryood | 0:36cebc939c49 | 62 | |
ryood | 0:36cebc939c49 | 63 | prevSendVal = sendVal; |
ryood | 2:95204570426c | 64 | |
ryood | 2:95204570426c | 65 | if (isStepChanged) { |
ryood | 4:c2b67c69d048 | 66 | char lineBuffer[20]; |
ryood | 3:2b2c6c0e9f1d | 67 | sprintf(lineBuffer, "Step: %02d", receivedVal); |
ryood | 4:c2b67c69d048 | 68 | gLCD.drawstring(0, 0, lineBuffer); |
ryood | 3:2b2c6c0e9f1d | 69 | gLCD.display(); |
ryood | 2:95204570426c | 70 | isStepChanged = false; |
ryood | 2:95204570426c | 71 | } |
ryood | 0:36cebc939c49 | 72 | } |
ryood | 0:36cebc939c49 | 73 | } |
ryood | 0:36cebc939c49 | 74 | } |