AStar32U4 library
AStar32U4LCD.h
Go to the documentation of this file.
1 // Copyright Pololu Corporation. For more information, see http://www.pololu.com/
2 
5 #pragma once
6 
7 #include <PololuHD44780.h>
8 #include <FastGPIO.h>
9 #include <USBPause.h>
10 #include <SPIPause.h>
11 
56 {
57  // Pin assignments. Change this if you want to remap your pins.
58  static const uint8_t rs = 7, e = 8, db4 = 14, db5 = 17, db6 = 13, db7 = IO_D5;
59 
60 public:
61 
62  virtual void initPins()
63  {
65  }
66 
67  virtual void send(uint8_t data, bool rsValue, bool only4bits)
68  {
69  // Temporarily disable the AVR's hardware SPI module; if it is enabled
70  // then MISO is forced to be an input, and the Arduino SD library
71  // generally leaves it enabled.
72  SPIPause spiPause;
73 
74  // Temporarily disable USB interrupts because they write some pins
75  // we are using as LCD pins.
76  USBPause usbPause;
77 
78  // Save the state of the RS and data pins. The state automatically
79  // gets restored before this function returns.
80  FastGPIO::PinLoan<rs> loanRS;
81  FastGPIO::PinLoan<db4> loanDB4;
82  FastGPIO::PinLoan<db5> loanDB5;
83  FastGPIO::PinLoan<db6> loanDB6;
84  FastGPIO::PinLoan<db7> loanDB7;
85 
86  // Drive the RS pin high or low.
88 
89  // Send the data.
90  if (!only4bits) { sendNibble(data >> 4); }
91  sendNibble(data & 0x0F);
92  }
93 
94 private:
95 
96  void sendNibble(uint8_t data)
97  {
98  FastGPIO::Pin<db4>::setOutput(data >> 0 & 1);
99  FastGPIO::Pin<db5>::setOutput(data >> 1 & 1);
100  FastGPIO::Pin<db6>::setOutput(data >> 2 & 1);
101  FastGPIO::Pin<db7>::setOutput(data >> 3 & 1);
102 
104  _delay_us(1); // Must be at least 450 ns.
106  _delay_us(1); // Must be at least 550 ns.
107  }
108 };
109 
static void setOutput(bool value) __attribute__((always_inline))
Sets the pin as an output.
Definition: FastGPIO.h:260
virtual void send(uint8_t data, bool rsValue, bool only4bits)
Definition: AStar32U4LCD.h:67
Main class for interfacing with the A-Star 32U4 LCD.
Definition: AStar32U4LCD.h:55
General class for handling the HD44780 protocol.
Definition: PololuHD44780.h:56
virtual void initPins()
Definition: AStar32U4LCD.h:62
static void setOutputHigh() __attribute__((always_inline))
Configures the pin to be an output driving high.
Definition: FastGPIO.h:238
static void setOutputLow() __attribute__((always_inline))
Configures the pin to be an output driving low.
Definition: FastGPIO.h:226