Team pumpkin pie. Tokai Univ., MicroMouse
Embed:
(wiki syntax)
Show/hide line numbers
i2clcd.cpp
00001 #include "i2clcd.hpp" 00002 #include <mbed.h> 00003 #include <cstdio> 00004 #include <cstdarg> 00005 00006 #define ADDRESS (0x7c) 00007 00008 #define POS2ADDRESS(c, r) ((r*0x40)+(c)) 00009 #define DEFAULT_CONTRAST (0x20) 00010 00011 #define TYPE_COMMAND (0x00) 00012 #define TYPE_DATA (0x40) 00013 00014 #define CMD_FUNCTION_SET_NORMAL (0x38) 00015 #define CMD_FUNCTION_SET_EXTENDED (0x39) 00016 #define CMD_INTERNAL_OSC (0x14) 00017 #define CMD_CONTRAST1 (0x70) 00018 #define CMD_CONTRAST2 (0x5c) 00019 #define CMD_FOLLOWER_CTRL (0x60) 00020 #define CMD_DISPLAY_ON (0x0c) 00021 #define CMD_SET_CGRAM (0x40) 00022 #define CMD_RETURN_HOME (0x02) 00023 #define CMD_CLEAR_DISPLAY (0x01) 00024 #define CMD_ENTRY_MODE_SET (0x04) 00025 00026 LCD::LCD(PinName sda, PinName scl) : _i2c(sda, scl) { 00027 _i2c.frequency(400*1000); 00028 reset(); 00029 } 00030 00031 LCD::LCD(I2C& i2c) : _i2c(i2c) { 00032 reset(); 00033 } 00034 00035 void LCD::reset() { 00036 wait(0.015); 00037 write_cmd(CMD_FUNCTION_SET_NORMAL); 00038 write_cmd(CMD_FUNCTION_SET_EXTENDED); 00039 write_cmd(CMD_INTERNAL_OSC); 00040 write_cmd(CMD_CONTRAST1 | (DEFAULT_CONTRAST & 0x0f)); 00041 write_cmd(CMD_CONTRAST2 | ((DEFAULT_CONTRAST>>4) & 0x03)); 00042 write_cmd(CMD_FOLLOWER_CTRL | 0x08/*Fon*/ | 0x04 /*Rab2*/); 00043 00044 wait(0.3); 00045 write_cmd(CMD_FUNCTION_SET_NORMAL); 00046 write_cmd(CMD_DISPLAY_ON); 00047 00048 clear(); 00049 } 00050 00051 void LCD::write(char data) { 00052 char cmd[2] = {TYPE_DATA, data}; 00053 _i2c.write(ADDRESS, cmd, 2); 00054 } 00055 00056 void LCD::write_cmd(char data) { 00057 char cmd[2] = {TYPE_COMMAND, data}; 00058 _i2c.write(ADDRESS, cmd, 2); 00059 } 00060 00061 LCD LCD::locate(int c, int r) { 00062 write_cmd(0x80 | POS2ADDRESS(c, r)); 00063 return *this; 00064 } 00065 00066 LCD LCD::home() { 00067 write_cmd(CMD_RETURN_HOME); 00068 wait(0.00164); 00069 return *this; 00070 } 00071 00072 void LCD::clear() { 00073 write_cmd(CMD_CLEAR_DISPLAY); wait(0.00164); 00074 locate(0, 0); 00075 } 00076 00077 void LCD::put(char ch) { 00078 write(ch); 00079 } 00080 00081 void LCD::puts(const char *str) { 00082 while (*str) write(*str++); 00083 } 00084 00085 #ifdef I2C_LCD_CONFIG_ENABLE_PRINTF 00086 void LCD::printf(const char *fmt, ...) { 00087 char buf[32] = {'\0'}; 00088 va_list args; 00089 va_start(args, fmt); 00090 vsprintf(buf, fmt, args); 00091 va_end(args); 00092 puts(buf); 00093 } 00094 #endif 00095 #ifdef I2C_LCD_CONFIG_ENABLE_CONTRAST 00096 void LCD::contrast(uint8_t contrast) { 00097 write_cmd(CMD_FUNCTION_SET_EXTENDED); 00098 write_cmd(CMD_CONTRAST1 | (contrast & 0x0f)); 00099 write_cmd(CMD_CONTRAST2 | ((contrast>>4) & 0x03)); 00100 write_cmd(CMD_FUNCTION_SET_NORMAL); 00101 } 00102 void LCD::resetContrast() { 00103 contrast(DEFAULT_CONTRAST); 00104 } 00105 #endif
Generated on Mon Oct 28 2024 09:21:32 by
1.7.2