Pololu3piPlus32U4 library
Pololu3piPlus32U4LCD.h
Go to the documentation of this file.
1 // Copyright (C) Pololu Corporation. See www.pololu.com for details.
2 
4 
5 #pragma once
6 
7 #include <PololuHD44780.h>
8 #include <FastGPIO.h>
9 #include <USBPause.h>
10 
11 namespace Pololu3piPlus32U4
12 {
13 
35 class LCD : public PololuHD44780Base
36 {
37  // Pin assignments
38  static const uint8_t rs = 0, e = 1, db4 = 14, db5 = 17, db6 = 13, db7 = IO_D5;
39 
40 public:
41 
42  virtual void initPins()
43  {
44  FastGPIO::Pin<e>::setOutputLow();
45  }
46 
47  virtual void send(uint8_t data, bool rsValue, bool only4bits)
48  {
49  // Temporarily disable USB interrupts because they write some pins
50  // we are using as LCD pins.
51  USBPause usbPause;
52 
53  // Save the state of the RS and data pins. The state automatically
54  // gets restored before this function returns.
55  FastGPIO::PinLoan<rs> loanRS;
56  FastGPIO::PinLoan<db4> loanDB4;
57  FastGPIO::PinLoan<db5> loanDB5;
58  FastGPIO::PinLoan<db6> loanDB6;
59  FastGPIO::PinLoan<db7> loanDB7;
60 
61  // Drive the RS pin high or low.
62  FastGPIO::Pin<rs>::setOutput(rsValue);
63 
64  // Send the data.
65  if (!only4bits) { sendNibble(data >> 4); }
66  sendNibble(data & 0x0F);
67  }
68 
72  void noAutoDisplay() {}
73 
74 private:
75 
76  void sendNibble(uint8_t data)
77  {
78  FastGPIO::Pin<db4>::setOutput(data >> 0 & 1);
79  FastGPIO::Pin<db5>::setOutput(data >> 1 & 1);
80  FastGPIO::Pin<db6>::setOutput(data >> 2 & 1);
81  FastGPIO::Pin<db7>::setOutput(data >> 3 & 1);
82 
83  FastGPIO::Pin<e>::setOutputHigh();
84  _delay_us(1); // Must be at least 450 ns.
85  FastGPIO::Pin<e>::setOutputLow();
86  _delay_us(1); // Must be at least 550 ns.
87  }
88 };
89 
90 }
Writes data to the LCD on the 3pi+ 32U4.
Top-level namespace for the Pololu3piPlus32U4 library.