M tamaki
/
prgEx_stdLCD
mbed board program for Program Design Exercise use UART on USB, AD, tact SW, LED
Revision 0:d14a5eeef3d4, committed 2015-07-11
- Comitter:
- tamaki
- Date:
- Sat Jul 11 01:22:12 2015 +0000
- Commit message:
- Program Design Ex
Changed in this revision
diff -r 000000000000 -r d14a5eeef3d4 ACM1602NI.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ACM1602NI.cpp Sat Jul 11 01:22:12 2015 +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; +}
diff -r 000000000000 -r d14a5eeef3d4 ACM1602NI.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ACM1602NI.h Sat Jul 11 01:22:12 2015 +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 +
diff -r 000000000000 -r d14a5eeef3d4 TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Sat Jul 11 01:22:12 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#e4cb7ddee0d3
diff -r 000000000000 -r d14a5eeef3d4 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Jul 11 01:22:12 2015 +0000 @@ -0,0 +1,173 @@ +//program design practice +//mbed program +//use Standard LCD #define STD_LCD +//use I2C LCD #define I2C_LCD +//**need Inport TextLCD, ACM1602NI.cpp, ACM1602NI.h +// +#include "mbed.h" + +#define STD_LCD + +#ifdef STD_LCD + #include "TextLCD.h" + TextLCD lcd(p27,p28,p26,p29,p25,p30); // rs,e, d4,d5,d6,d7 +#endif +#ifdef I2C_LCD + #include "ACM1602NI.h" + ACM1602NI lcd(p28, p27); //sda scl +#endif + +Ticker flipper; +DigitalOut led1(p21); +DigitalOut led2(p22); +DigitalOut led3(p23); +DigitalOut led4(p24); +DigitalIn sw1(p14); +DigitalIn sw2(p15); +DigitalIn sw3(p16); +AnalogIn vr(p17); +Serial pc(USBTX, USBRX); // tx, rx + +unsigned char cnt; +char msg1[] = "S1:0 S2:0 S3:0"; +char msg2[] = " 511 1010"; +char msg3[] = " "; +char StBuf[5]; +char SendBuf[17]; +int flg_rdet, recv_cnt; +int vrad; + +void Cnv210(int val) +{ + unsigned char temp; + int i; + StBuf[0] = StBuf[1] = StBuf[2] = StBuf[3] = StBuf[4] = ' '; + i = 0; + do { + temp = val % 10; + StBuf[i++] = temp + '0'; + } while (( val /= 10 ) != 0 ); +} + +void rxRecieve() +{ + char ch; + + ch = pc.getc(); + if ( flg_rdet == 0 ) { + if ( ch == 'r' ) { + flg_rdet = 1; + recv_cnt = 5; + } + } + if ( flg_rdet == 1 ) { + switch ( recv_cnt ) { + case 0: + msg3[0] = ch; + recv_cnt = 1; + break; + case 1: + msg3[1] = ch; + recv_cnt = 2; + break; + case 2: + msg3[2] = ch; + recv_cnt =3; + break; + case 3: + msg3[3] = ch; + recv_cnt = 0; + flg_rdet = 0; + lcd.locate(12,1); + lcd.printf(msg3); + break; + default: + recv_cnt = 0; + break; + + } + } +} + +void flip() { + if((cnt & 0x8) == 0) led4= 0; + else led4 = 1; + if((cnt & 0x4) == 0) led3= 0; + else led3 = 1; + if((cnt & 0x2) == 0) led2= 0; + else led2 = 1; + if((cnt & 0x1) == 0) led1= 0; + else led1 = 1; + + if(!sw1 == 0) msg1[3] = '0'; + else msg1[3] = '1'; + if(!sw2 == 0) msg1[8] = '0'; + else msg1[8] = '1'; + if(!sw3 == 0) msg1[13] = '0'; + else msg1[13] = '1'; + + if(!led4 == 0) msg2[7] = '1'; + else msg2[7] = '0'; + if(!led3 == 0) msg2[8] = '1'; + else msg2[8] = '0'; + if(!led2 == 0) msg2[9] = '1'; + else msg2[9] = '0'; + if(!led1 == 0) msg2[10] = '1'; + else msg2[10] = '0'; + + vrad = (int)(vr.read() * 1023); + Cnv210(vrad); + msg2[0] = StBuf[4]; + msg2[1] = StBuf[3]; + msg2[2] = StBuf[2]; + msg2[3] = StBuf[1]; + msg2[4] = StBuf[0]; +; + + lcd.locate(0,0); + lcd.printf(msg1); + + lcd.locate(0,1); + lcd.printf(msg2); + SendBuf[2] = StBuf[4]; + SendBuf[3] = StBuf[3]; + SendBuf[4] = StBuf[2]; + SendBuf[5] = StBuf[1]; + SendBuf[6] = StBuf[0]; + + SendBuf[7] = msg2[10]; + SendBuf[8] = msg2[9]; + SendBuf[9] = msg2[8]; + SendBuf[10] = msg2[7]; + + SendBuf[11] = msg1[3]; + SendBuf[12] = msg1[8]; + SendBuf[13] = msg1[13]; + pc.printf(SendBuf); + cnt++; + if(cnt == 16) cnt = 0; +} + +int main() { + pc.baud(38400); + cnt = 0; + sw1.mode(PullUp); + sw2.mode(PullUp); + sw3.mode(PullUp); + lcd.cls(); + lcd.printf(msg1); + lcd.locate(0,1); + lcd.printf(msg2); + pc.printf("Serial Communication Start\n"); + SendBuf[0] = 'C'; + SendBuf[1] = 'S'; + SendBuf[14] = '\r'; + SendBuf[15] = '\n'; + SendBuf[16] = 0x00; + flg_rdet = 0; + recv_cnt = 0; + flipper.attach(&flip, 0.75); // the address of the function to be attached (flip) and the interval (2 seconds) + pc.attach(rxRecieve, Serial::RxIrq); + while (1) { + } +} \ No newline at end of file
diff -r 000000000000 -r d14a5eeef3d4 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Jul 11 01:22:12 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file