GLCD Library
A C Library for Embedded Applications
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
AVR8.c
Go to the documentation of this file.
1 
9 /*
10  Copyright (c) 2012, Andy Gock
11 
12  All rights reserved.
13 
14  Redistribution and use in source and binary forms, with or without
15  modification, are permitted provided that the following conditions are met:
16  * Redistributions of source code must retain the above copyright
17  notice, this list of conditions and the following disclaimer.
18  * Redistributions in binary form must reproduce the above copyright
19  notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the distribution.
21  * Neither the name of Andy Gock nor the
22  names of its contributors may be used to endorse or promote products
23  derived from this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  DISCLAIMED. IN NO EVENT SHALL ANDY GOCK BE LIABLE FOR ANY
29  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 
37 #include "../glcd.h"
38 
39 #if defined(GLCD_DEVICE_AVR8)
40 
41 void glcd_init(void)
42 {
43 
44 #if defined(GLCD_CONTROLLER_PCD8544)
45 
46  /* Set pin directions */
47 
48  /*
49  * Set up SPI for AVR8
50  * Note: AVR's SS pin must be set to output, regardless of whether we
51  * actually use it. This is a requirement of SPI mster mode.
52  */
53  sbi(DDR(AVR_SS_PORT),AVR_SS_PIN);
54 
55  /*
56  * Set MOSI, Master SS, SCK to output (otherwise SPI won't work)
57  * Must be done even if native SS pin not used
58  */
59  sbi(DDR(CONTROLLER_MOSI_PORT),CONTROLLER_MOSI_PIN);
60  sbi(DDR(CONTROLLER_SS_PORT),CONTROLLER_SS_PIN);
61  sbi(DDR(CONTROLLER_SCK_PORT),CONTROLLER_SCK_PIN);
62 
63  /* Set SS, DC and RST pins to output */
64  sbi( DDR(CONTROLLER_SS_PORT), CONTROLLER_SS_PIN );
65  sbi( DDR(CONTROLLER_DC_PORT), CONTROLLER_DC_PIN );
66  sbi( DDR(CONTROLLER_RST_PORT), CONTROLLER_RST_PIN );
67 
68  /* Deselect LCD */
69  GLCD_DESELECT();
70 
71  /*
72  * Max allowed SPI clock is 4 MHz from datasheet.
73  * Enable SPI, set master mode and clock rate to /4 (4MHz with F_CPU=8MHz)
74  */
75  SPCR = (1<<SPE)|(1<<MSTR);
76  SPSR = 0;
77 
78  glcd_reset();
79 
80  /* Get into the EXTENDED mode! */
82 
83  /* LCD bias select (4 is optimal?) */
85 
86  /* Set VOP */
87  glcd_command(PCD8544_SET_VOP | 50); // Experimentally determined
88 
89  /* Back to standard instructions */
91 
92  /* Normal mode */
94 
95  /* Select screen buffer */
97 
98  /* Clear screen, we are now ready to go */
99  glcd_clear();
100 
101 #elif defined(GLCD_CONTROLLER_ST7565R)
102 
103  /* Set up GPIO directions */
104 
105  /*
106  * Set up SPI for AVR8
107  * Note: AVR's SS pin must be set to output, regardless of whether we
108  * actually use it. This is a requirement of SPI mster mode.
109  */
110  sbi(DDR(AVR_SS_PORT),AVR_SS_PIN);
111 
112  /* Set SCK and MOSI as output */
113  sbi(DDR(CONTROLLER_SCK_PORT),CONTROLLER_SCK_PIN);
114  sbi(DDR(CONTROLLER_MOSI_PORT),CONTROLLER_MOSI_PIN);
115 
116  /*
117  * Set MISO as input with pullup. This needs to be set for
118  * SPI to work, even though we never use or read it.
119  */
120  cbi(DDR(CONTROLLER_MISO_PORT),CONTROLLER_MISO_PIN); // B3 MISO as input
121  sbi(CONTROLLER_MISO_PORT,CONTROLLER_MISO_PIN);
122 
123  /* Set pin to controller SS as output */
124  sbi(DDR(CONTROLLER_SS_PORT),CONTROLLER_SS_PIN); // A5
125 
126  /* Set LCD A0 pin as output */
127  sbi(DDR(CONTROLLER_A0_PORT),CONTROLLER_A0_PIN); // A6
128 
129  /* Init SS pin high (i.e LCD deselected) */
130  sbi(CONTROLLER_SS_PORT,CONTROLLER_SS_PIN);
131 
132  /* Deselect LCD */
133  GLCD_DESELECT();
134 
135  /* MSB first, double speed, SPI mode 0 */
136  SPCR = (1<<SPE) | (1<<MSTR) | (0<<CPOL) | (0<<CPHA);
137  sbi(SPSR,SPI2X);
138 
139  /* Enable interrupts */
140  sei();
141 
142  delay_ms(30); // example in datasheet does this (20ms)
143 
144  glcd_command(ST7565R_RESET); // internal reset
145  glcd_command(0xa2); // 1/9 bias
146  glcd_command(0xa0); // ADC select, normal
147  glcd_command(0xc8); // com output reverse
148  glcd_command(0xa4); // display all points normal
149  glcd_command(0x40); // display start line set
150  glcd_command(0x25); // internal resistor ratio
151  glcd_command(0x81); // electronic volume mode set
152  //glcd_command(0x10); // electronic volume - datasheet's contrast example doesn't work
153  glcd_command(45); // this works better
154  glcd_command(0x2f); // power controller set
155  glcd_command(0xaf); // display on
156 
157  glcd_all_on();
158 
159  delay_ms(500);
160  glcd_normal();
161 
163  glcd_clear_now();
164 
166 
167  glcd_clear();
168 
169 #else
170  #error "Controller not supported"
171 #endif /* GLCD_CONTROLLER_PCD8544 */
172 
173 }
174 
175 void glcd_spi_write(uint8_t c)
176 {
177  GLCD_SELECT();
178  SPDR = c;
179  while(!(SPSR & (1<<SPIF))); /* wait until transmission is complete */
180  GLCD_DESELECT();
181 }
182 
183 void glcd_reset(void)
184 {
185  /* Toggle RST low to reset. Minimum pulse 100ns on datasheet. */
186  GLCD_SELECT();
187  GLCD_RESET_LOW();
188  delay_ms(GLCD_RESET_TIME);
189  GLCD_RESET_HIGH();
190  GLCD_DESELECT();
191 }
192 
193 #endif /* defined(GLCD_DEVICE_AVR8) */
#define PCD8544_FUNCTION_SET
Definition: PCD8544.h:43
void glcd_spi_write(uint8_t c)
Write a byte to the connected SPI slave.
#define PCD8544_SET_VOP
Definition: PCD8544.h:75
void glcd_select_screen(uint8_t *buffer, glcd_BoundingBox_t *bbox)
Select screen buffer and bounding box structure.
Definition: glcd.c:133
void glcd_reset(void)
Reset the LCD.
#define PCD8544_EXTENDED_INSTRUCTION
Definition: PCD8544.h:47
#define PCD8544_DISPLAY_NORMAL
Definition: PCD8544.h:56
glcd_BoundingBox_t glcd_bbox
Keeps track of bounding box of area on LCD which need to be updated next reresh cycle.
Definition: glcd.c:55
#define PCD8544_DISPLAY_CONTROL
Definition: PCD8544.h:54
#define ST7565R_RESET
Definition: ST7565R.h:54
void glcd_all_on(void)
All display points on (native)
#define PCD8544_SET_BIAS
Definition: PCD8544.h:74
void glcd_set_start_line(uint8_t addr)
Set start line/page.
void glcd_command(uint8_t c)
Send command byte to LCD.
void glcd_clear(void)
Clear the display.
Definition: glcd.c:122
void glcd_clear_now(void)
Clear the display immediately, does not buffer.
uint8_t glcd_buffer[GLCD_LCD_WIDTH *GLCD_LCD_HEIGHT/8]
Screen buffer.
Definition: glcd.c:49
#define GLCD_RESET_TIME
Reset duration by glcd_reset(), in milliseconds.
Definition: glcd.h:156
void glcd_init(void)
Initialise the LCD.
void glcd_normal(void)
Set to normal mode.