Skip to content

Instantly share code, notes, and snippets.

@zyxar
Last active August 29, 2015 14:05
Show Gist options
  • Save zyxar/5b138cdd8e255f38395d to your computer and use it in GitHub Desktop.
Save zyxar/5b138cdd8e255f38395d to your computer and use it in GitHub Desktop.
#define RS 8
#define EN 9
#define backlight 10
#define RS_L digitalWrite(RS,LOW)
#define RS_H digitalWrite(RS,HIGH)
#define EN_L digitalWrite(EN,LOW)
#define EN_H digitalWrite(EN,HIGH)
// commands
#define LCD_CLEARDISPLAY 0x01
#define LCD_RETURNHOME 0x02
#define LCD_ENTRYMODESET 0x04
#define LCD_DISPLAYCONTROL 0x08
#define LCD_CURSORSHIFT 0x10
#define LCD_FUNCTIONSET 0x20
#define LCD_SETCGRAMADDR 0x40
#define LCD_SETDDRAMADDR 0x80
// flags for display entry mode
#define LCD_ENTRYRIGHT 0x00
#define LCD_ENTRYLEFT 0x02
#define LCD_ENTRYSHIFTINCREMENT 0x01
#define LCD_ENTRYSHIFTDECREMENT 0x00
// flags for display on/off control
#define LCD_DISPLAYON 0x04
#define LCD_DISPLAYOFF 0x00
#define LCD_CURSORON 0x02
#define LCD_CURSOROFF 0x00
#define LCD_BLINKON 0x01
#define LCD_BLINKOFF 0x00
// flags for display/cursor shift
#define LCD_DISPLAYMOVE 0x08
#define LCD_CURSORMOVE 0x00
#define LCD_MOVERIGHT 0x04
#define LCD_MOVELEFT 0x00
// flags for function set
#define LCD_8BITMODE 0x10
#define LCD_4BITMODE 0x00
#define LCD_2LINE 0x08
#define LCD_1LINE 0x00
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00
int DB[] = {7, 6, 5, 4};
/********************************************************************/
void clear()
{
write_command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
delayMicroseconds(2000); // this command takes a long time!
}
void write_command(int command) {
int i, temp;
RS_L;
EN_L;
temp = command & 0xf0;
for (i = 0; i < 4; i++) {
if (temp & 0x80) {
digitalWrite(DB[i], HIGH);
}
else {
digitalWrite(DB[i], LOW);
}
temp <<= 1;
}
EN_H;
delayMicroseconds(1000);
EN_L;
temp = (command & 0x0f) << 4;
for (i = 0; i < 4; i++) {
if (temp & 0x80) {
digitalWrite(DB[i], HIGH);
}
else {
digitalWrite(DB[i], LOW);
}
temp <<= 1;
}
EN_H;
delayMicroseconds(1000);
EN_L;
}
/********************************************************************/
void write_data(int dat) {
int i = 0, temp;
RS_H;
EN_L;
temp = dat & 0xf0;
for (i = 0; i < 4; i++) {
if (temp & 0x80) {
digitalWrite(DB[i], HIGH);
}
else {
digitalWrite(DB[i], LOW);
}
temp <<= 1;
}
EN_H;
delayMicroseconds(1000);
EN_L;
temp = (dat & 0x0f) << 4;
for (i = 0; i < 4; i++) {
if (temp & 0x80) {
digitalWrite(DB[i], HIGH);
}
else {
digitalWrite(DB[i], LOW);
}
temp <<= 1;
}
EN_H;
delayMicroseconds(1000);
EN_L;
}
/********************************************************************/
void LCD_write_char( int x, int y, int dat) {
int address;
if (x == 0) {
address = 0x80 + y;
}
else {
address = 0xC0 + y;
}
write_command(address);
write_data(dat);
delayMicroseconds(1000);
}
/********************************************************************/
void lcd1602_init() {
int i = 0;
pinMode(RS, OUTPUT);
pinMode(EN, OUTPUT);
pinMode(backlight, OUTPUT);
digitalWrite(backlight, HIGH);
for (i = 0; i < 4; i++) {
pinMode(DB[i], OUTPUT);
}
// delayMicroseconds(50000);
digitalWrite(RS, LOW);
digitalWrite(EN, LOW);
delayMicroseconds(100);
write_command(0x28);
delay(50);
write_command(0x06);
delay(50);
write_command(0x0c);
delay(50);
write_command(0x80);
delay(50);
write_command(0x01);
delay(100);
}
/********************************************************************/
void setup (void) {
lcd1602_init();
clear();
delay(500);
}
/********************************************************************/
void loop (void) {
clear();
LCD_write_char(0, 4, 'H');
LCD_write_char(0, 5, 'e');
LCD_write_char(0, 6, 'l');
LCD_write_char(0, 7, 'l');
LCD_write_char(0, 8, 'o');
LCD_write_char(0, 9, 'w');
LCD_write_char(1, 4, 'w');
LCD_write_char(1, 5, 'o');
LCD_write_char(1, 6, 'r');
LCD_write_char(1, 7, 'l');
LCD_write_char(1, 8, 'd');
delay(500);
}
//Test and text transmission with RF module for Arduino http://zygzax.com
//RECEIVER
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600); // Configure the serial connection to the computer
vw_set_ptt_inverted(true); // Required by the RF module
vw_setup(2000); // bps connection speed
vw_set_rx_pin(3); // Arduino pin to connect the receiver data pin
vw_rx_start(); // Start the receiver
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // We check if we have received data
{
int i;
// Message with proper check
for (i = 0; i < buflen; i++)
{
Serial.write(buf[i]); // The received data is stored in the buffer
// and sent through the serial port to the computer
}
Serial.println();
}
}
//Test and text transmission with RF module for Arduino http://zygzax.com
//TRANSMITTER
#include <VirtualWire.h>
void setup()
{
vw_set_ptt_inverted(true); // Required by the RF module
vw_setup(2000); // bps connection speed
vw_set_tx_pin(3); // Arduino pin to connect the receiver data pin
}
void loop()
{
//Message to send:
const char *msg = "HELLO WORLD";
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // We wait to finish sending the message
delay(200); // We wait to send the message again
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment