mbed base board 2016 Check program check for SD card read&write (3 switches, 6 leds, I2C LCD, SD card)
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
Revision 1:a41c274f95e2, committed 2016-10-17
- Comitter:
- tamaki
- Date:
- Mon Oct 17 00:20:38 2016 +0000
- Parent:
- 0:bdbd3d6fc5d5
- Child:
- 2:0ee1c0236a11
- Commit message:
- mbed board check program for SD card(read, write)
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ACM1602NI.cpp Mon Oct 17 00:20:38 2016 +0000 @@ -0,0 +1,130 @@ +/* An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01 + * Copyright 2013, Takuo WATANABE (wtakuo) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "ACM1602NI.h" + +#define I2C_SUCCESS 0 +#define I2C_FAILURE 1 + +ACM1602NI::ACM1602NI(PinName sda, PinName scl) : _i2c(sda, scl) +{ + init(); +} + +ACM1602NI::ACM1602NI(I2C &i2c) : _i2c(i2c) +{ + init(); +} + +void ACM1602NI::init() +{ + writeCommand(0x01); + wait_ms(i2c_command_wait_ms); + writeCommand(0x38); + wait_ms(i2c_command_wait_ms); + writeCommand(0x0f); + wait_ms(i2c_command_wait_ms); + writeCommand(0x06); + wait_ms(i2c_command_wait_ms); + locate(0, 0); +} + +int ACM1602NI::writeBytes(const char *data, int length, bool repeated) +{ + wait_us(i2c_bit_wait_us); + _i2c.start(); + for (int i = 0; i < length; i++) { + wait_us(i2c_bit_wait_us); + if (_i2c.write(data[i]) != 1) { + wait_us(i2c_bit_wait_us); + _i2c.stop(); + return I2C_FAILURE; + } + } + if (!repeated) { + wait_us(i2c_bit_wait_us); + _i2c.stop(); + } + return I2C_SUCCESS; +} + +void ACM1602NI::character(int column, int row, int c) +{ + writeCommand(address(column, row)); + writeData(c); +} + +void ACM1602NI::cls() +{ + writeCommand(0x01); + wait_ms(i2c_command_wait_ms); + locate(0, 0); +} + +void ACM1602NI::locate(int column, int row) +{ + _column = column; + _row = row; +} + +int ACM1602NI::_putc(int value) +{ + if (value == '\n') { + _column = 0; + _row = (_row + 1) % rows(); + } else { + character(_column, _row, value); + _column++; + if (_column >= columns()) { + _column = 0; + _row = (_row + 1) % rows(); + } + } + return value; +} + +int ACM1602NI::_getc() +{ + return -1; +} + +void ACM1602NI::writeCommand(int command) +{ + char bs[3] = { i2c_addr, 0x00, command }; + writeBytes(bs, 3); +} + +void ACM1602NI::writeData(int data) +{ + char bs[3] = { i2c_addr, 0x80, data }; + writeBytes(bs, 3); +} + +int ACM1602NI::address(int column, int row) +{ + return 0x80 + row * 0x40 + column; +} + +int ACM1602NI::columns() +{ + return display_columns; +} + +int ACM1602NI::rows() +{ + return display_rows; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ACM1602NI.h Mon Oct 17 00:20:38 2016 +0000 @@ -0,0 +1,95 @@ +/* An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01 + * Copyright 2013, Takuo WATANABE (wtakuo) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ACM1602NI_H +#define ACM1602NI_H + +#include "mbed.h" + +/** An I2C text LCD library for Displaytronic ACM1602NI-FLW-FBW-M01. + * The device does not work with default I2C library due to its slow I2C responce. + * This library adds some extra waits so that the device can answer to the I2C commands. + * The interface is basically the same as TextLCD by Simon Ford. + * + * @code + * #include "mbed.h" + * #include "ACM1602NI.h" + * + * // p9: sda, p10: scl + * ACM1602NI lcd(p9, p10); + * + * int main() { + * lcd.printf("Hello, World!\n"); + * lcd.printf("ACM1602NI\n"); + * } + * @endcode + */ +class ACM1602NI : public Stream +{ +public: + /** Create an ACM1602NI object connected to the specified I2C pins. + * + * @param sda The I2C data pin + * @param scl The I2C clock pin + */ + ACM1602NI(PinName sda, PinName scl); + + /** Create an ACM1602NI object connected to the specified I2C port. + * + * @param i2c The I2C port to connect to + */ + ACM1602NI(I2C &i2c); + + /** Initialize the device and clear screen + */ + void init(); + + /** Locate to a screen column and row + * + * @param column The horizontal position from the left, indexed from 0 + * @param row The vertical position from the top, indexed from 0 + */ + void locate(int column, int row); + + /** Clear the screen and locate to 0,0 */ + void cls(); + + int rows(); + int columns(); + +protected: + virtual int _putc(int value); + virtual int _getc(); + + int address(int column, int raw); + void character(int column, int row, int c); + int writeBytes(const char *data, int length, bool repeated = false); + void writeCommand(int command); + void writeData(int data); + + static const int i2c_addr = 0x50 << 1; + static const int i2c_bit_wait_us = 20; + static const int i2c_command_wait_ms = 4; + + static const int display_columns = 16; + static const int display_rows = 2; + + I2C _i2c; + int _column, _row; +}; + +#endif +
--- a/SDFileSystem.lib Fri Dec 07 11:25:01 2012 +0000 +++ b/SDFileSystem.lib Mon Oct 17 00:20:38 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4 +http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#8db0d3b02cec
--- a/main.cpp Fri Dec 07 11:25:01 2012 +0000 +++ b/main.cpp Mon Oct 17 00:20:38 2016 +0000 @@ -1,19 +1,274 @@ +#define CPP1 0 +#define C1 1 + + +#if CPP1 #include "mbed.h" #include "SDFileSystem.h" - +#include "ACM1602NI.h" +DigitalOut led1(p21); +DigitalOut led2(p22); +DigitalOut led3(p23); +DigitalOut led4(p24); +DigitalOut led5(p25); +DigitalOut led6(p26); +//DigitalIn sw1(p14); +//DigitalIn sw2(p15); +//DigitalIn sw3(p16); +AnalogIn vr(p17); + +FILE *fp; SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board - -int main() { - printf("Hello World!\n"); - - mkdir("/sd/mydir", 0777); +ACM1602NI lcd(p28, p27); //sda scl + +class cChkSw +{ +public: + int st[3]; + int counter[3];//past counter + cChkSw(PinName, PinName, PinName); + DigitalIn *sw1; + DigitalIn *sw2; + DigitalIn *sw3; + void init(); + void chksw(); - FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); - if(fp == NULL) { - error("Could not open file for write\n"); +protected: +}; + +void cChkSw::init() +{ +} + +cChkSw::cChkSw(PinName s1, PinName s2, PinName s3) +{ + sw1 = new DigitalIn(s1); + sw2 = new DigitalIn(s2); + sw3 = new DigitalIn(s3); + sw1->mode(PullUp); + sw2->mode(PullUp); + sw3->mode(PullUp); +} + +void cChkSw::chksw() +//switch status 0:off, 1:posEdge, 2:on, 3:negEdge +{ + static int ssw[3], psw[3], cnt[3], tcnt[3]; + int i; + psw[0] = sw1->read(); + psw[1] = sw2->read(); + psw[2] = sw3->read(); +//switch status 0:off, 1:posEdge, 2:on, 3:negEdge + for(i = 0; i < 3; i++){ + if((ssw[i] == 0)&&(psw[i] == 0)){ + st[i] = 2;//on + tcnt[i] ++; + counter[i] = tcnt[i]; + } + if((ssw[i] == 0)&&(psw[i] == 1)){ + st[i] = 3;//negEdge + cnt[i] = tcnt[i]; + tcnt[i] = 0; + counter[i] = tcnt[i]; + } + if((ssw[i] == 1)&&(psw[i] == 1)){ + st[i] = 0;//off + tcnt[i] ++; + } + if((ssw[i] == 1)&&(psw[i] == 0)){ + st[i] = 1;//posEdge + cnt[i] = tcnt[i]; + tcnt[i] = 0; + counter[i] = tcnt[i]; + } + ssw[i] = psw[i]; + } +} + +int main() { + char str[80]; + + + lcd.cls(); + lcd.locate(0,0); + lcd.printf("SD Card chkpgm"); + lcd.locate(0,1); + +// mkdir("/sd/mydir", 0777); + led1=led2=led3=led4=led5=led6 = 0; + int counter = 0; + + cChkSw cs(p14,p15,p16); + while(1){ + cs.chksw(); + led6 = 1; + + if(cs.st[0] == 1){ + led1=led2=led3=led4=led5=led6 = 0; + led4 = 1; + counter++; + FILE *fp = fopen("/sd/sdtest.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + led2 = 1; + }else{ + fprintf(fp, "SDcard#%d\n", counter); + fclose(fp); + lcd.locate(0,1); + lcd.printf(" "); + lcd.locate(0,1); + lcd.printf("counter = %d", counter); + led1 = 1; + } + } + if(cs.st[1] == 1){ + led1=led2=led3=led4=led5=led6 = 0; + led5 = 1; + fp = fopen("/sd/sdtest.txt", "r"); + if(fp == NULL) { + error("Could not open file for write\n\r"); + led2 = 1; + }else{ + fscanf(fp, "%s\n", str);//read from SD card + lcd.locate(0,1); + lcd.printf(" ");//output string to LCD + lcd.locate(0,1); + lcd.printf("text = %s", str);//output string to USB serial + fclose(fp); + led1 = 1; + } + } + if(cs.st[2] == 1){ + led1=led2=led3=led4=led5=led6 = 0; + led6 = 1; + counter = 0; + lcd.locate(0,1); + lcd.printf(" "); + } } - fprintf(fp, "Hello fun SD Card World!"); - fclose(fp); +} +#endif + +#if C1 +// +// +// +#include "mbed.h" +#include "SDFileSystem.h" +#include "ACM1602NI.h" + +DigitalOut led1(p21); +DigitalOut led2(p22); +DigitalOut led3(p23); +DigitalOut led4(p24); +DigitalOut led5(p25); +DigitalOut led6(p26); +DigitalIn sw1(p14); +DigitalIn sw2(p15); +DigitalIn sw3(p16); +AnalogIn vr(p17); + +FILE *fp; +SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board +ACM1602NI lcd(p28, p27); //sda scl + +//switch status 0:off, 1:posEdge, 2:on, 3:negEdge +int sw_st[3]; +int sw_cnt[3];//past counter + +void chksw(void) +{ + static int ssw[3], psw[3], cnt[3], tcnt[3]; + int i; + psw[0] = sw1; + psw[1] = sw2; + psw[2] = sw3; +//switch status 0:off, 1:posEdge, 2:on, 3:negEdge + for(i = 0; i < 3; i++){ + if((ssw[i] == 0)&&(psw[i] == 0)){ + sw_st[i] = 2;//on + tcnt[i] ++; + sw_cnt[i] = tcnt[i]; + } + if((ssw[i] == 0)&&(psw[i] == 1)){ + sw_st[i] = 3;//negEdge + cnt[i] = tcnt[i]; + tcnt[i] = 0; + sw_cnt[i] = tcnt[i]; + } + if((ssw[i] == 1)&&(psw[i] == 1)){ + sw_st[i] = 0;//off + tcnt[i] ++; + } + if((ssw[i] == 1)&&(psw[i] == 0)){ + sw_st[i] = 1;//posEdge + cnt[i] = tcnt[i]; + tcnt[i] = 0; + sw_cnt[i] = tcnt[i]; + } + ssw[i] = psw[i]; + } +} + +int main() { + char str[80]; + + + lcd.cls(); + lcd.locate(0,0); + lcd.printf("SD Card chkpgm"); + lcd.locate(0,1); - printf("Goodbye World!\n"); +// mkdir("/sd/mydir", 0777); + led1=led2=led3=led4=led5=led6 = 0; + int counter = 0; + + while(1){ + chksw(); + led6 = 1; + + if(sw_st[0] == 1){ + led1=led2=led3=led4=led5=led6 = 0; + led4 = 1; + counter++; + FILE *fp = fopen("/sd/sdtest.txt", "w"); + if(fp == NULL) { + error("Could not open file for write\n"); + led2 = 1; + }else{ + fprintf(fp, "SDcard#%d\n", counter); + fclose(fp); + lcd.locate(0,1); + lcd.printf(" "); + lcd.locate(0,1); + lcd.printf("counter = %d", counter); + led1 = 1; + } + } + if(sw_st[1] == 1){ + led1=led2=led3=led4=led5=led6 = 0; + led5 = 1; + fp = fopen("/sd/sdtest.txt", "r"); + if(fp == NULL) { + error("Could not open file for write\n\r"); + led2 = 1; + }else{ + fscanf(fp, "%s\n", str);//read from SD card + lcd.locate(0,1); + lcd.printf(" ");//output string to LCD + lcd.locate(0,1); + lcd.printf("text = %s", str);//output string to USB serial + fclose(fp); + led1 = 1; + } + } + if(sw_st[2] == 1){ + led1=led2=led3=led4=led5=led6 = 0; + led6 = 1; + counter = 0; + lcd.locate(0,1); + lcd.printf(" "); + } + } } +#endif
--- a/mbed.bld Fri Dec 07 11:25:01 2012 +0000 +++ b/mbed.bld Mon Oct 17 00:20:38 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/63cdd78b2dc1 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/25aea2a3f4e3 \ No newline at end of file