test
Dependencies: EthernetInterface TextLCD mbed-rtos mbed
main.cpp@0:2b3659d0a56e, 2013-01-22 (annotated)
- Committer:
- y_notsu
- Date:
- Tue Jan 22 20:46:08 2013 +0000
- Revision:
- 0:2b3659d0a56e
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
y_notsu | 0:2b3659d0a56e | 1 | #include "mbed.h" |
y_notsu | 0:2b3659d0a56e | 2 | #include "TextLCD.h" |
y_notsu | 0:2b3659d0a56e | 3 | #include "EthernetInterface.h" |
y_notsu | 0:2b3659d0a56e | 4 | #include "HTTPServer.h" |
y_notsu | 0:2b3659d0a56e | 5 | |
y_notsu | 0:2b3659d0a56e | 6 | //EthernetNetIf eth; |
y_notsu | 0:2b3659d0a56e | 7 | HTTPServer svr; |
y_notsu | 0:2b3659d0a56e | 8 | DigitalOut myled(LED1); |
y_notsu | 0:2b3659d0a56e | 9 | TextLCD lcd(p24,p26,p27,p28,p29,p30); //rs,e,d4~d7 |
y_notsu | 0:2b3659d0a56e | 10 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
y_notsu | 0:2b3659d0a56e | 11 | DigitalOut cs(p8); |
y_notsu | 0:2b3659d0a56e | 12 | |
y_notsu | 0:2b3659d0a56e | 13 | Serial pc(USBTX, USBRX); // tx, rx |
y_notsu | 0:2b3659d0a56e | 14 | |
y_notsu | 0:2b3659d0a56e | 15 | int main() { |
y_notsu | 0:2b3659d0a56e | 16 | pc.printf("Startup...\n"); |
y_notsu | 0:2b3659d0a56e | 17 | lcd.printf("Startup"); |
y_notsu | 0:2b3659d0a56e | 18 | |
y_notsu | 0:2b3659d0a56e | 19 | EthernetInterface eth; |
y_notsu | 0:2b3659d0a56e | 20 | eth.init(); //Use DHCP |
y_notsu | 0:2b3659d0a56e | 21 | eth.connect(); |
y_notsu | 0:2b3659d0a56e | 22 | printf("IP Address is %s\n", eth.getIPAddress()); |
y_notsu | 0:2b3659d0a56e | 23 | |
y_notsu | 0:2b3659d0a56e | 24 | TCPSocketConnection sock; |
y_notsu | 0:2b3659d0a56e | 25 | sock.connect("mbed.org", 80); |
y_notsu | 0:2b3659d0a56e | 26 | |
y_notsu | 0:2b3659d0a56e | 27 | char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; |
y_notsu | 0:2b3659d0a56e | 28 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
y_notsu | 0:2b3659d0a56e | 29 | |
y_notsu | 0:2b3659d0a56e | 30 | char buffer[300]; |
y_notsu | 0:2b3659d0a56e | 31 | int ret; |
y_notsu | 0:2b3659d0a56e | 32 | while (true) { |
y_notsu | 0:2b3659d0a56e | 33 | ret = sock.receive(buffer, sizeof(buffer)-1); |
y_notsu | 0:2b3659d0a56e | 34 | if (ret <= 0) |
y_notsu | 0:2b3659d0a56e | 35 | break; |
y_notsu | 0:2b3659d0a56e | 36 | buffer[ret] = '\0'; |
y_notsu | 0:2b3659d0a56e | 37 | printf("Received %d chars from server:\n%s\n", ret, buffer); |
y_notsu | 0:2b3659d0a56e | 38 | } |
y_notsu | 0:2b3659d0a56e | 39 | |
y_notsu | 0:2b3659d0a56e | 40 | |
y_notsu | 0:2b3659d0a56e | 41 | |
y_notsu | 0:2b3659d0a56e | 42 | sock.close(); |
y_notsu | 0:2b3659d0a56e | 43 | |
y_notsu | 0:2b3659d0a56e | 44 | eth.disconnect(); |
y_notsu | 0:2b3659d0a56e | 45 | |
y_notsu | 0:2b3659d0a56e | 46 | pc.printf("Listening...\n"); |
y_notsu | 0:2b3659d0a56e | 47 | |
y_notsu | 0:2b3659d0a56e | 48 | lcd.locate(0,1); |
y_notsu | 0:2b3659d0a56e | 49 | // Setup the spi for 8 bit data, high steady state clock, |
y_notsu | 0:2b3659d0a56e | 50 | // second edge capture, with a 1MHz clock rate |
y_notsu | 0:2b3659d0a56e | 51 | spi.format(16,3); |
y_notsu | 0:2b3659d0a56e | 52 | spi.frequency(1000000); |
y_notsu | 0:2b3659d0a56e | 53 | |
y_notsu | 0:2b3659d0a56e | 54 | // Select the device by seting chip select low |
y_notsu | 0:2b3659d0a56e | 55 | cs = 0; |
y_notsu | 0:2b3659d0a56e | 56 | |
y_notsu | 0:2b3659d0a56e | 57 | // Send 0x8f, the command to read the WHOAMI register |
y_notsu | 0:2b3659d0a56e | 58 | spi.write(0x0000); |
y_notsu | 0:2b3659d0a56e | 59 | |
y_notsu | 0:2b3659d0a56e | 60 | // Send a dummy byte to receive the contents of the WHOAMI register |
y_notsu | 0:2b3659d0a56e | 61 | int whoami = spi.write(0x0000); |
y_notsu | 0:2b3659d0a56e | 62 | pc.printf("WHOAMI register = 0x%X\n", whoami); |
y_notsu | 0:2b3659d0a56e | 63 | lcd.printf("0x%X\n",whoami); |
y_notsu | 0:2b3659d0a56e | 64 | // Deselect the device |
y_notsu | 0:2b3659d0a56e | 65 | cs = 1; |
y_notsu | 0:2b3659d0a56e | 66 | while(1) |
y_notsu | 0:2b3659d0a56e | 67 | { |
y_notsu | 0:2b3659d0a56e | 68 | |
y_notsu | 0:2b3659d0a56e | 69 | // Select the device by seting chip select low |
y_notsu | 0:2b3659d0a56e | 70 | cs = 0; |
y_notsu | 0:2b3659d0a56e | 71 | |
y_notsu | 0:2b3659d0a56e | 72 | // Send 0x8f, the command to read the WHOAMI register |
y_notsu | 0:2b3659d0a56e | 73 | //spi.write(0x0000); |
y_notsu | 0:2b3659d0a56e | 74 | |
y_notsu | 0:2b3659d0a56e | 75 | // Send a dummy byte to receive the contents of the WHOAMI register |
y_notsu | 0:2b3659d0a56e | 76 | int outtemp = spi.write(0x0000); |
y_notsu | 0:2b3659d0a56e | 77 | int intemp = spi.write(0x0000); |
y_notsu | 0:2b3659d0a56e | 78 | pc.printf("outtemp = 0x%X\n", outtemp); |
y_notsu | 0:2b3659d0a56e | 79 | lcd.locate(0,0); |
y_notsu | 0:2b3659d0a56e | 80 | lcd.printf("Out:0x%X\n",outtemp); |
y_notsu | 0:2b3659d0a56e | 81 | pc.printf("intemp = 0x%X\n", intemp); |
y_notsu | 0:2b3659d0a56e | 82 | lcd.locate(0,1); |
y_notsu | 0:2b3659d0a56e | 83 | lcd.printf("In :0x%X",intemp); |
y_notsu | 0:2b3659d0a56e | 84 | // Deselect the device |
y_notsu | 0:2b3659d0a56e | 85 | cs = 1; |
y_notsu | 0:2b3659d0a56e | 86 | float disp; |
y_notsu | 0:2b3659d0a56e | 87 | if((outtemp& 0x8000) == 0){ // 0℃以上 |
y_notsu | 0:2b3659d0a56e | 88 | disp = (outtemp >> 4) * 0.0625; |
y_notsu | 0:2b3659d0a56e | 89 | } else { // 0℃未満 |
y_notsu | 0:2b3659d0a56e | 90 | disp = (((0xffff - outtemp) >> 4) + 1) * -0.0625; |
y_notsu | 0:2b3659d0a56e | 91 | } |
y_notsu | 0:2b3659d0a56e | 92 | pc.printf("Out:%4.2f[deg]\n",disp); |
y_notsu | 0:2b3659d0a56e | 93 | |
y_notsu | 0:2b3659d0a56e | 94 | if((intemp& 0x8000) == 0){ // 0℃以上 |
y_notsu | 0:2b3659d0a56e | 95 | disp = (intemp>> 4) * 0.0625; |
y_notsu | 0:2b3659d0a56e | 96 | } else { // 0℃未満 |
y_notsu | 0:2b3659d0a56e | 97 | disp = (((0xffff - intemp) >> 4) + 1) * -0.0625; |
y_notsu | 0:2b3659d0a56e | 98 | } |
y_notsu | 0:2b3659d0a56e | 99 | pc.printf("In:%4.2f[deg]\n",disp); |
y_notsu | 0:2b3659d0a56e | 100 | wait(10); |
y_notsu | 0:2b3659d0a56e | 101 | } |
y_notsu | 0:2b3659d0a56e | 102 | } |