ATtiny85 Template Code
Code snippets for the ATtiny85
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
nokialcd.c
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------*
2 * Nokia LCD Interface
3 *---------------------------------------------------------------------------*
4 * 26-Apr-2014 ShaneG
5 *
6 * Simple routines for controlling a Nokia LCD.
7 *--------------------------------------------------------------------------*/
8 #include <avr/io.h>
9 #include <avr/pgmspace.h>
10 #include "../hardware.h"
11 #include "utility.h"
12 #include "iohelp.h"
13 #include "smallfont.h"
14 #include "nokialcd.h"
15 
16 // Only provide the functions if the driver is enabled
17 #ifdef LCD_ENABLED
18 
23 void lcdData(uint8_t data) {
24  // Bring CD high
25  PORTB |= (1 << LCD_CD);
26  // Send the data
27  sspiOutMSB(LCD_SCK, LCD_MOSI, data, 8);
28  }
29 
34 void lcdCommand(uint8_t cmd) {
35  // Bring CD low
36  PORTB &= ~(1 << LCD_CD);
37  // Send the data
38  sspiOutMSB(LCD_SCK, LCD_MOSI, cmd, 8);
39  }
40 
47 void lcdInit() {
48  // Set up the output pins, ensuring they are all 'low' to start
49  uint8_t val = (1 << LCD_SCK) | (1 << LCD_MOSI) | (1 << LCD_RESET) | (1 << LCD_CD);
50  PORTB &= ~val;
51  DDRB |= val;
52  // Do a hard reset on the LCD
53  wait(10);
54  PORTB |= (1 << LCD_RESET);
55  // Initialise the LCD
56  lcdCommand(0x21); // LCD Extended Commands.
57  lcdCommand(0xA1); // Set LCD Vop (Contrast).
58  lcdCommand(0x04); // Set Temp coefficent.
59  lcdCommand(0x14); // LCD bias mode 1:48.
60  lcdCommand(0x20); // LCD Normal commands
61  lcdCommand(0x0C); // Normal display, horizontal addressing
62  }
63 
71 void lcdClear(bool invert) {
72  uint8_t fill = invert?0xFF:0x00;
73  // Set the position
74  lcdCommand(0x80);
75  lcdCommand(0x40);
76  // Fill in the whole display
77  for(uint16_t index = 0; index < (LCD_COL * LCD_ROW); index++)
78  lcdData(fill);
79  }
80 
89 void lcdClearRow(uint8_t row, bool invert) {
90  uint8_t fill = invert?0xFF:0x00;
91  // Set the position
92  lcdCommand(0x80);
93  lcdCommand(0x40 | (row % LCD_ROW));
94  // Fill in the row
95  for(uint8_t index = 0; index < LCD_COL; index++)
96  lcdData(fill);
97  }
98 
115 void lcdPrintChar(uint8_t row, uint8_t col, char ch, bool invert) {
116  // Make sure it is on the screen
117  if((row>=LCD_ROW)||(col>=LCD_COL))
118  return;
119  // If the character is invalid replace it with the '?'
120  if((ch<0x20)||(ch>0x7f))
121  ch = '?';
122  // Set the starting address
123  lcdCommand(0x80 | col);
124  lcdCommand(0x40 | (row % LCD_ROW));
125  // And send the column data
126  const uint8_t *chdata = SMALL_FONT + ((ch - 0x20) * 5);
127  for(uint8_t pixels = 0; (pixels < DATA_WIDTH) && (col < LCD_COL); pixels++, col++, chdata++) {
128  uint8_t data = pgm_read_byte_near(chdata);
129  lcdData(invert?~data:data);
130  }
131  // Add the padding byte
132  if(col < LCD_COL)
133  lcdData(invert?0xFF:0x00);
134  }
135 
155 void lcdPrint(uint8_t row, uint8_t col, const char *str, bool invert) {
156  for(;(*str!='\0')&&(col<LCD_COL);col+=CHAR_WIDTH,str++)
157  lcdPrintChar(row, col, *str, invert);
158  }
159 
179 void lcdPrintP(uint8_t row, uint8_t col, const char *str, bool invert) {
180  while(col<LCD_COL) {
181  char ch = pgm_read_byte_near(str);
182  if(ch=='\0')
183  return;
184  lcdPrintChar(row, col, ch, invert);
185  col += CHAR_WIDTH;
186  str++;
187  }
188  }
189 
206 void lcdImageP(uint8_t row, uint8_t col, const uint8_t *img, bool invert) {
207  // Break out the width and height
208  uint8_t data = pgm_read_byte_near(img++);
209  uint8_t height = (data >> 6) + 1;
210  uint8_t width = (data & 0x03F) + 1;
211  while((height>0)&&(row<LCD_ROW)) {
212  // Set the starting address
213  lcdCommand(0x80 | col);
214  lcdCommand(0x40 | row);
215  // Send the data (up to the end of the screen)
216  for(uint8_t offset=0; offset<width; offset++, img++) {
217  if((col+offset)<LCD_COL) {
218  if(invert)
219  lcdData(~pgm_read_byte_near(img));
220  else
221  lcdData(pgm_read_byte_near(img));
222  }
223  }
224  height--;
225  row++;
226  }
227  }
228 
229 // Only provide the functions if the driver is enabled
230 #endif /* LCD_ENABLED */
231 
void lcdClearRow(uint8_t row, bool invert)
#define LCD_CD
Definition: hardware.h:118
void lcdClear(bool invert)
void wait(uint16_t millis)
Definition: utility.c:21
#define LCD_RESET
Definition: hardware.h:119
#define LCD_SCK
Definition: hardware.h:117
#define LCD_COL
Definition: nokialcd.h:16
void lcdInit()
#define LCD_ROW
Definition: nokialcd.h:19
#define DATA_WIDTH
Definition: smallfont.h:21
#define LCD_MOSI
Definition: hardware.h:116
void sspiOutMSB(uint8_t sck, uint8_t mosi, uint16_t data, uint8_t bits)
Definition: softspi.c:40
void lcdData(uint8_t data)
void lcdPrint(uint8_t row, uint8_t col, const char *str, bool invert)
void lcdPrintP(uint8_t row, uint8_t col, const char *str, bool invert)
void lcdCommand(uint8_t cmd)
void lcdPrintChar(uint8_t row, uint8_t col, char ch, bool invert)
void lcdImageP(uint8_t row, uint8_t col, const uint8_t *img, bool invert)
#define CHAR_WIDTH
Definition: smallfont.h:27