Skip to content

Instantly share code, notes, and snippets.

@nanase
Created January 15, 2020 23:40
Show Gist options
  • Save nanase/d1ecbfeb7f7c2dabcf5c3c3e74286711 to your computer and use it in GitHub Desktop.
Save nanase/d1ecbfeb7f7c2dabcf5c3c3e74286711 to your computer and use it in GitHub Desktop.
AQM1602Y on Arduino MKR ZERO (experiments)
#include "aqm1602.h"
AQM1602::AQM1602(uint8_t i2cAddress, uint8_t voltage, uint8_t contrast) {
this->i2cAddress = i2cAddress;
this->contrast = contrast;
this->voltage = voltage;
this->displayControl = 0x00;
// ACM series = 0x80
// AQM series = 0x40
this->controlBit = i2cAddress == 0x50 ? 0x80 : 0x40;
}
uint8_t AQM1602::writeCommand(uint8_t command) {
Wire.beginTransmission(this->i2cAddress);
Wire.write(0x00);
Wire.write(command);
return Wire.endTransmission();
}
uint8_t AQM1602::writeData(uint8_t data) {
Wire.beginTransmission(this->i2cAddress);
Wire.write(this->controlBit);
Wire.write(data);
return Wire.endTransmission();
}
void AQM1602::initialize() {
Wire.begin();
delay(40);
// required?
this->writeCommand(0x00);
delayMicroseconds(30);
this->writeCommand(0x38);
delayMicroseconds(30);
this->writeCommand(0x39);
delayMicroseconds(30);
this->writeCommand(0x14);
delayMicroseconds(30);
this->setContrast(this->contrast);
this->writeCommand(0x6C);
delay(200);
this->writeCommand(0x39);
delayMicroseconds(30);
this->clear();
this->writeCommand(0x0C);
delayMicroseconds(30);
}
void AQM1602::clear() {
this->writeCommand(0x01);
delay(2);
}
void AQM1602::setContrast(uint8_t contrast) {
this->contrast = contrast;
uint8_t contrast = (0x70 | (this->contrast & 0x0F));
uint8_t bon = this->voltage == AQM1602_VOLTAGE_5V ? 0x00 : 0x04;
uint8_t powercommand = (0x50 | (bon | (this->contrast >> 4)));
this->writeCommand(contrast);
delayMicroseconds(30);
this->writeCommand(powercommand);
delayMicroseconds(30);
}
void AQM1602::setCursor(uint8_t column, uint8_t row) {
uint8_t ca = (column + row * 0x40) | (0x80);
this->writeCommand(ca);
delayMicroseconds(30);
}
void AQM1602::moveToHome() {
this->writeCommand(0x02);
delay(2);
}
void AQM1602::scrollToLeft() {
this->writeCommand(0x18);
delayMicroseconds(30);
}
void AQM1602::scrollToRight() {
this->writeCommand(0x1C);
delayMicroseconds(30);
}
void AQM1602::showCursor();
void AQM1602::hideCursor();
void AQM1602::enableBlink();
void AQM1602::disableBlink();
void AQM1602::turnOnDisplay() {
this->displayControl |= 0x04;
this->writeCommand(0x08 | this->displayControl);
delayMicroseconds(30);
}
void AQM1602::turnOffDisplay() {
this->displayControl = 0x
}
// void AQM1602::createCharactor(uint8_t number, uint8_t* data);
void AQM1602::print(String text) {
int length = text.length();
for (int i = 0; i < length; i++) {
this->writeData(text.charAt(i));
delayMicroseconds(30);
}
}
#pragma once
#include "Arduino.h"
#include "Wire.h"
#define AQM1602_I2C_ADDRESS 0x3e
#define AQM1602_VOLTAGE_5V 5
#define AQM1602_VOLTAGE_3V3 3
#define AQM1602_DEFAULT_CONTRAST (0b00110000)
class AQM1602 {
public:
AQM1602(uint8_t i2cAddress, uint8_t voltage, uint8_t contrast);
uint8_t writeCommand(uint8_t command);
uint8_t writeData(uint8_t data);
void initialize();
void clear();
void setContrast(uint8_t contrast);
void setCursor(uint8_t column, uint8_t row);
void moveToHome();
void scrollToLeft();
void scrollToRight();
void showCursor();
void hideCursor();
void enableBlink();
void disableBlink();
void turnOnDisplay();
void turnOffDisplay();
void createCharactor(uint8_t number, uint8_t* data);
void print(String text);
private:
uint8_t i2cAddress;
uint8_t contrast;
uint8_t voltage;
uint8_t controlBit;
uint8_t displayControl;
};
#include "aqm1602.h"
#define LCD_RST_PIN 3
AQM1602 lcd(AQM1602_I2C_ADDRESS, AQM1602_VOLTAGE_3V3, AQM1602_DEFAULT_CONTRAST);
volatile bool state = false;
uint8_t ledState = LOW;
uint32_t count = 0;
void interrupt0() {
state = !state;
}
void setup() {
pinMode(LCD_RST_PIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LCD_RST_PIN, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(0, INPUT_PULLUP);
attachInterrupt(0, interrupt0, FALLING);
init_lcd();
lcd.setCursor(0, 0);
lcd.print(String(0));
}
void init_lcd() {
digitalWrite(LCD_RST_PIN, LOW);
delay(10);
digitalWrite(LCD_RST_PIN, HIGH);
lcd.initialize();
}
void loop() {
// digitalWrite(LED_BUILTIN, ledState = !ledState);
lcd.setCursor(0, 0);
lcd.print(String(count++));
if (state) {
state = false;
init_lcd();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment