.
Dependencies: SDHCFileSystem mbed
main.cpp@0:1596b8644523, 2012-11-19 (annotated)
- Committer:
- TickTock
- Date:
- Mon Nov 19 04:44:11 2012 +0000
- Revision:
- 0:1596b8644523
- Child:
- 1:dafb963c3c14
Initial revision - just getting hw up and running
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TickTock | 0:1596b8644523 | 1 | #include "mbed.h" |
TickTock | 0:1596b8644523 | 2 | #include "CAN.h" |
TickTock | 0:1596b8644523 | 3 | #include "TextLCD.h" |
TickTock | 0:1596b8644523 | 4 | #include "SDFileSystem.h" |
TickTock | 0:1596b8644523 | 5 | #include "DebounceIn.h" |
TickTock | 0:1596b8644523 | 6 | #include "log.h" |
TickTock | 0:1596b8644523 | 7 | #include "beep.h" |
TickTock | 0:1596b8644523 | 8 | |
TickTock | 0:1596b8644523 | 9 | //VP230 |
TickTock | 0:1596b8644523 | 10 | //1:D 8:RS |
TickTock | 0:1596b8644523 | 11 | //2:GND 7:CANH |
TickTock | 0:1596b8644523 | 12 | //3:VCC 6:CANL |
TickTock | 0:1596b8644523 | 13 | //4:R 5:Vref |
TickTock | 0:1596b8644523 | 14 | |
TickTock | 0:1596b8644523 | 15 | //LPC1768 |
TickTock | 0:1596b8644523 | 16 | //1: VSS |
TickTock | 0:1596b8644523 | 17 | //2: NC:VIN |
TickTock | 0:1596b8644523 | 18 | //3: NC:VB |
TickTock | 0:1596b8644523 | 19 | //4: NC:nR |
TickTock | 0:1596b8644523 | 20 | //5: SPI:MOSI --> 6:SDRAM:DI |
TickTock | 0:1596b8644523 | 21 | //6: SPI:MISO --> 2:SDRAM:DO |
TickTock | 0:1596b8644523 | 22 | //7: SPI:SCLK --> 4:SDRAM:SCLK |
TickTock | 0:1596b8644523 | 23 | //8: CS --> 7:SDRAM:CS |
TickTock | 0:1596b8644523 | 24 | //9: CAN1:TX --> 1:CAN1:D |
TickTock | 0:1596b8644523 | 25 | //10: CAN1:RX --> 4:CAN1:R |
TickTock | 0:1596b8644523 | 26 | //11: RS --> 4:LCD:RS |
TickTock | 0:1596b8644523 | 27 | //12: E --> 6:LCD:E |
TickTock | 0:1596b8644523 | 28 | //13: D4 --> 11:LCD:D4 |
TickTock | 0:1596b8644523 | 29 | //14: D5 --> 12:LCD:D5 |
TickTock | 0:1596b8644523 | 30 | //15: D6 --> 13:LCD:D6 |
TickTock | 0:1596b8644523 | 31 | //16: D7 --> 14:LCD:D7 |
TickTock | 0:1596b8644523 | 32 | //17: CD --> 1:SDRAM:CD |
TickTock | 0:1596b8644523 | 33 | //18: NC:Ain |
TickTock | 0:1596b8644523 | 34 | //19: PB2 |
TickTock | 0:1596b8644523 | 35 | //20: PB1 |
TickTock | 0:1596b8644523 | 36 | //21: Spkr+ |
TickTock | 0:1596b8644523 | 37 | //22: NC:pwm |
TickTock | 0:1596b8644523 | 38 | //23: NC:pwm |
TickTock | 0:1596b8644523 | 39 | //24: NC:pwm |
TickTock | 0:1596b8644523 | 40 | //25: NC:pwm |
TickTock | 0:1596b8644523 | 41 | //26: NC:pwm |
TickTock | 0:1596b8644523 | 42 | //27: CAN1:Sleep --> 8:CAN1:RS |
TickTock | 0:1596b8644523 | 43 | //28: CAN2:Sleep --> 8:CAN2:RS |
TickTock | 0:1596b8644523 | 44 | //29: CAN2:RX --> 4:CAN2:R |
TickTock | 0:1596b8644523 | 45 | //30: CAN2:TX --> 1:CAN2:D |
TickTock | 0:1596b8644523 | 46 | //31: NC:USB_D+ |
TickTock | 0:1596b8644523 | 47 | //32: NC:USB_D- |
TickTock | 0:1596b8644523 | 48 | //33: NC:Eth_TD+ |
TickTock | 0:1596b8644523 | 49 | //34: NC:Eth_TD- |
TickTock | 0:1596b8644523 | 50 | //35: NC:Eth_RD+ |
TickTock | 0:1596b8644523 | 51 | //36: NC:Eth_RD- |
TickTock | 0:1596b8644523 | 52 | //37: NC:IF+ |
TickTock | 0:1596b8644523 | 53 | //38: NC:IF- |
TickTock | 0:1596b8644523 | 54 | //39: NC:5Vout |
TickTock | 0:1596b8644523 | 55 | //40: VCC3.3 |
TickTock | 0:1596b8644523 | 56 | |
TickTock | 0:1596b8644523 | 57 | Beep buzzer(p21); |
TickTock | 0:1596b8644523 | 58 | DigitalIn CD( p17 ); |
TickTock | 0:1596b8644523 | 59 | DigitalIn PB1( p20 ); |
TickTock | 0:1596b8644523 | 60 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) |
TickTock | 0:1596b8644523 | 61 | TextLCD lcd(p11, p12, p13, p14, p15, p16); // rs, e, d0-d3 (RW to GND) |
TickTock | 0:1596b8644523 | 62 | Ticker ticker; |
TickTock | 0:1596b8644523 | 63 | DigitalOut led1(LED1); |
TickTock | 0:1596b8644523 | 64 | DigitalOut led2(LED2); |
TickTock | 0:1596b8644523 | 65 | DigitalOut led3(LED3); |
TickTock | 0:1596b8644523 | 66 | DigitalOut led4(LED4); |
TickTock | 0:1596b8644523 | 67 | CAN can1(p9, p10); // CAN1 uses pins 9 and 10 (tx, rx) |
TickTock | 0:1596b8644523 | 68 | CAN can2(p30, p29); // CAN2 uses pins 30 and 29 (tx, rx) |
TickTock | 0:1596b8644523 | 69 | bool LogCreated = false; |
TickTock | 0:1596b8644523 | 70 | char logMsg[64]; |
TickTock | 0:1596b8644523 | 71 | |
TickTock | 0:1596b8644523 | 72 | char counter = 0; |
TickTock | 0:1596b8644523 | 73 | //DigitalOut can1_SleepMode(p11); // Use pin 11 to control the sleep mode of can1 |
TickTock | 0:1596b8644523 | 74 | //DigitalOut can2_SleepMode(p28); // Use pin 28 to control the sleep mode of can2 |
TickTock | 0:1596b8644523 | 75 | Serial pc(USBTX, USBRX); // tx, rx default settings (9600 8N1) |
TickTock | 0:1596b8644523 | 76 | |
TickTock | 0:1596b8644523 | 77 | void Log (char *message) |
TickTock | 0:1596b8644523 | 78 | { |
TickTock | 0:1596b8644523 | 79 | FILE *file; |
TickTock | 0:1596b8644523 | 80 | |
TickTock | 0:1596b8644523 | 81 | if (!LogCreated) { |
TickTock | 0:1596b8644523 | 82 | file = fopen(LOGFILE, "w"); |
TickTock | 0:1596b8644523 | 83 | LogCreated = true; |
TickTock | 0:1596b8644523 | 84 | } |
TickTock | 0:1596b8644523 | 85 | else |
TickTock | 0:1596b8644523 | 86 | file = fopen(LOGFILE, "a"); |
TickTock | 0:1596b8644523 | 87 | |
TickTock | 0:1596b8644523 | 88 | if (file == NULL) { |
TickTock | 0:1596b8644523 | 89 | if (LogCreated) |
TickTock | 0:1596b8644523 | 90 | LogCreated = false; |
TickTock | 0:1596b8644523 | 91 | return; |
TickTock | 0:1596b8644523 | 92 | } |
TickTock | 0:1596b8644523 | 93 | else |
TickTock | 0:1596b8644523 | 94 | { |
TickTock | 0:1596b8644523 | 95 | fputs(message, file); |
TickTock | 0:1596b8644523 | 96 | //fprintf(file, message); |
TickTock | 0:1596b8644523 | 97 | fclose(file); |
TickTock | 0:1596b8644523 | 98 | } |
TickTock | 0:1596b8644523 | 99 | |
TickTock | 0:1596b8644523 | 100 | if (file) |
TickTock | 0:1596b8644523 | 101 | fclose(file); |
TickTock | 0:1596b8644523 | 102 | } |
TickTock | 0:1596b8644523 | 103 | |
TickTock | 0:1596b8644523 | 104 | |
TickTock | 0:1596b8644523 | 105 | void send() { |
TickTock | 0:1596b8644523 | 106 | if(can1.write(CANMessage(0x350, &counter, 1))) { |
TickTock | 0:1596b8644523 | 107 | counter++; |
TickTock | 0:1596b8644523 | 108 | pc.printf("Message %d sent\n", counter); |
TickTock | 0:1596b8644523 | 109 | lcd.printf("sent: %d \n", counter); |
TickTock | 0:1596b8644523 | 110 | if(LogCreated) { |
TickTock | 0:1596b8644523 | 111 | if((PB1==0)||(CD==0)){ |
TickTock | 0:1596b8644523 | 112 | LogCreated=false; |
TickTock | 0:1596b8644523 | 113 | pc.printf("Stopping log\n", counter); |
TickTock | 0:1596b8644523 | 114 | }else{ |
TickTock | 0:1596b8644523 | 115 | sprintf(logMsg, "sent: %d \n", counter); |
TickTock | 0:1596b8644523 | 116 | Log(logMsg); |
TickTock | 0:1596b8644523 | 117 | pc.printf("Message logged\n"); |
TickTock | 0:1596b8644523 | 118 | buzzer.beep(800,0.025); } |
TickTock | 0:1596b8644523 | 119 | } |
TickTock | 0:1596b8644523 | 120 | } |
TickTock | 0:1596b8644523 | 121 | led1 = !led1; |
TickTock | 0:1596b8644523 | 122 | } |
TickTock | 0:1596b8644523 | 123 | |
TickTock | 0:1596b8644523 | 124 | void recieve1() { |
TickTock | 0:1596b8644523 | 125 | static CANMessage msg1; |
TickTock | 0:1596b8644523 | 126 | can1.read(msg1); |
TickTock | 0:1596b8644523 | 127 | pc.printf("Can1 Message received: %d\n", msg1.data[0]); |
TickTock | 0:1596b8644523 | 128 | lcd.printf("Can1 rxd: %d\n", msg1.data[0]); |
TickTock | 0:1596b8644523 | 129 | led2 = !led2; |
TickTock | 0:1596b8644523 | 130 | } |
TickTock | 0:1596b8644523 | 131 | void recieve2() { |
TickTock | 0:1596b8644523 | 132 | static CANMessage msg2; |
TickTock | 0:1596b8644523 | 133 | can2.read(msg2); |
TickTock | 0:1596b8644523 | 134 | pc.printf("Can2 Message received: %d\n", msg2.data[0]); |
TickTock | 0:1596b8644523 | 135 | lcd.printf("Can2 rxd: %d\n", msg2.data[0]); |
TickTock | 0:1596b8644523 | 136 | led3 = !led3; |
TickTock | 0:1596b8644523 | 137 | } |
TickTock | 0:1596b8644523 | 138 | |
TickTock | 0:1596b8644523 | 139 | int main() { |
TickTock | 0:1596b8644523 | 140 | pc.baud(115200); // change serial interface to pc to 115200, 8N1 |
TickTock | 0:1596b8644523 | 141 | can1.frequency(100000); |
TickTock | 0:1596b8644523 | 142 | can2.frequency(100000); |
TickTock | 0:1596b8644523 | 143 | //can1_SleepMode = 0; // Turn off Sleep Mode |
TickTock | 0:1596b8644523 | 144 | //can2_SleepMode = 0; // Turn off Sleep Mode |
TickTock | 0:1596b8644523 | 145 | CD.mode(PullUp) ; //SDRAM Chip Detect |
TickTock | 0:1596b8644523 | 146 | PB1.mode(PullUp) ; //Pushbutton 1 |
TickTock | 0:1596b8644523 | 147 | ticker.attach(&send, 1); |
TickTock | 0:1596b8644523 | 148 | can1.attach(&recieve1); |
TickTock | 0:1596b8644523 | 149 | can2.attach(&recieve2); |
TickTock | 0:1596b8644523 | 150 | if(CD == 1) { |
TickTock | 0:1596b8644523 | 151 | pc.printf("Starting Can Log\n"); |
TickTock | 0:1596b8644523 | 152 | Log("Starting Can Log\n"); |
TickTock | 0:1596b8644523 | 153 | } |
TickTock | 0:1596b8644523 | 154 | else { |
TickTock | 0:1596b8644523 | 155 | pc.printf("No SDRAM Inserted.\n"); |
TickTock | 0:1596b8644523 | 156 | } |
TickTock | 0:1596b8644523 | 157 | } |