Digital Power Starter Kit 3 Firmware
dsPIC33C Boost Converter Voltage Mode Control Example
dev_lcd.c
1 //======================================================================================================================
2 // Copyright(c) 2018 Microchip Technology Inc. and its subsidiaries.
3 // Subject to your compliance with these terms, you may use Microchip software and any derivatives exclusively with
4 // Microchip products. It is your responsibility to comply with third party license terms applicable to your use of
5 // third-party software (including open source software) that may accompany Microchip software.
6 // THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO
7 // THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR
8 // PURPOSE.
9 // IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE,
10 // COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED
11 // OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY
12 // ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE
13 // PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
14 //======================================================================================================================
15 
16 //======================================================================================================================
17 // @file dev_lcd.c
18 // @author M91406
19 // @brief LC display device driver source file
20 //
21 //======================================================================================================================
22 
23 #include <xc.h> // include processor files - each processor file is guarded.
24 #include <stdint.h> // include standard integer types header file
25 #include <stdbool.h> // include standard boolean types header file
26 
27 #include "common/delay.h"
28 #include "lcd/drivers/drv_lcd_interface.h"
29 
30 /*********************************************************************************
31  * @ingroup lib-layer-lcd-properties-private
32  * @{
33  * @brief Newhaven NHD-C0216CZ-FSW-FBW LCD controller command set
34  * @details
35  * Defines for the command set of a Newhaven NHD-C0216CZ-FSW-FBW LCD controller
36  **********************************************************************************/
37 
38 //==================================================================================
39 // Private defines
40 //==================================================================================
41 
42 // there is a difference in real display size and display memory
43 #define LCD_ADDRESS_LINE_1 0x00
44 #define LCD_ADDRESS_LINE_2 0x40
45 
46 #define LCD_DISPLAYSIZE_X 16
47 #define LCD_DISPLAYSIZE_Y 2
48 
49 #define LCD_CLEAR 0x01
50 #define LCD_HOME 0x02
51 
52 #define CURSOR_nSHIFT 0x00
53 #define CURSOR_SHIFT 0x01
54 #define DATA_DECREMENT 0x00
55 #define DATA_INCREMENT 0x02
56 #define LCD_ENTRY_MODE 0x04
57 
58 #define CURSOR_OFF 0x00
59 #define CURSOR_ON 0x02
60 #define BLINK_OFF 0x00
61 #define BLINK_ON 0x01
62 #define LCD_DISPLAY_OFF 0x08
63 #define LCD_DISPLAY_ON 0x0C
64 
65 #define FUNCTION_nIS 0x00
66 #define FUNCTION_IS 0x01
67 #define FUNCTION_1_HIGH 0x00
68 #define FUNCTION_2_HIGH 0x04
69 #define FUNCTION_1_LINE 0x00
70 #define FUNCTION_2_LINE 0x08
71 #define FUNCTION_4BITS 0x00
72 #define FUNCTION_8BITS 0x10
73 
74 #define LCD_FUNCTION 0x20
75 
76 #define LCD_CGRAM_ADDRESS(adr) (0x40 | (adr & 0x3F))
77 #define LCD_DDRAM_ADDRESS(adr) (0x80 | (adr & 0x7F))
78 
79 // Second Instruction Page (IS)
80 #define BIAS_1_5 0x00
81 #define BIAS_1_4 0x08
82 #define FREQ_CNTRL(f) (f&0x07)
83 #define LCD_OSC_FREQ 0x10
84 
85 #define LCD_ICON_ADDRESS(adr) (0x40 | (adr & 0x0F))
86 
87 #define nICON 0x00
88 #define ICON 0x08
89 #define nBOOST 0x00
90 #define BOOSTLCD 0x04
91 #define CONTRAST(c) (c&0x03)
92 #define LCD_PWR_CONTROL 0x50
93 
94 #define FOLLOWER_GAIN(g) (g&0x07)
95 #define LCD_FOLLOWER_OFF 0x60
96 #define LCD_FOLLOWER_ON 0x68
97 
98 #define LCD_CONTRAST(c) (0x70 | (c & 0x0F))
99 
100 #define LCD_BUSY_FLAG_MASK 0x80
101 #define LCD_ADDRESS_MASK 0x7F
102 
103 //==================================================================================
104 // Private Variables
105 //==================================================================================
106 
107 const uint8_t line_address[] = {LCD_ADDRESS_LINE_1, LCD_ADDRESS_LINE_2};
108 uint8_t pos_x = 0;
109 uint8_t pos_y = 0;
110 uint8_t change_position = false;
111  // lib-layer-lcd-properties-private
113 
114 
115 /*********************************************************************************
116  * @ingroup lib-layer-lcd-functions-public
117  * @fn void dev_Lcd_Initialize(void)
118  * @brief Initializes the LCD Device
119  * @param void
120  * @return void
121  * @details
122  * This function initializes the LCD Device Driver. It needs to be called
123  * once at boot-up before that device can be used.
124  **********************************************************************************/
125 
126 void dev_Lcd_Initialize(void)
127 {
128  drv_LcdInterface_Initialize();
129  drv_LcdInterface_Reset();
130 
131  drv_LcdInterface_SendCmd(LCD_FUNCTION | FUNCTION_8BITS | FUNCTION_1_HIGH | FUNCTION_1_LINE | FUNCTION_nIS); //function set
132 
133  __delay_ms(1);
134 
135  drv_LcdInterface_SendCmd(LCD_FUNCTION | FUNCTION_8BITS | FUNCTION_1_HIGH | FUNCTION_1_LINE | FUNCTION_nIS); //function set
136  drv_LcdInterface_SendCmd(LCD_FUNCTION | FUNCTION_8BITS | FUNCTION_1_HIGH | FUNCTION_1_LINE | FUNCTION_nIS); //function set
137 
138  // Enter the second page of instructions
139  drv_LcdInterface_SendCmd(LCD_FUNCTION | FUNCTION_8BITS | FUNCTION_1_HIGH | FUNCTION_2_LINE | FUNCTION_IS); //function set
140  drv_LcdInterface_SendCmd(LCD_OSC_FREQ | BIAS_1_5 | FREQ_CNTRL(4)); //internal osc frequency
141  drv_LcdInterface_SendCmd(LCD_PWR_CONTROL | nICON | BOOSTLCD | CONTRAST(2)); //power control
142  drv_LcdInterface_SendCmd(LCD_FOLLOWER_ON | FOLLOWER_GAIN(5)); //follower control
143  drv_LcdInterface_SendCmd(LCD_CONTRAST(0)); //contrast
144  // leave second instruction page
145 
146  //drv_Lcd_WriteCommand(LCD_FUNCTION | FUNCTION_8BITS | FUNCTION_1_HIGH | FUNCTION_2_LINE | FUNCTION_nIS); //function set
147  drv_LcdInterface_SendCmd(LCD_DISPLAY_ON | CURSOR_OFF | BLINK_OFF); //display on
148  drv_LcdInterface_SendCmd(LCD_ENTRY_MODE | CURSOR_nSHIFT | DATA_INCREMENT); //entry mode
149  drv_LcdInterface_SendCmd(LCD_CLEAR); //clear
150 
151  __delay_ms(1);
152 }
153 
154 /*********************************************************************************
155  * @ingroup lib-layer-lcd-functions-public
156  * @fn void dev_Lcd_Clear(void)
157  * @brief Clears the LC Display Screen
158  * @param void
159  * @return void
160  * @details
161  * This function clears the LCD screen and sets the cursor position at 0,0
162  * (left upper corner). Instead of calling this function, clearing the screen
163  * can also be achieved by sending the character '\f' within a string
164  **********************************************************************************/
165 
166 void dev_Lcd_Clear(void)
167 {
168  drv_LcdInterface_SendCmd(LCD_CLEAR);
169  __delay_ms(1);
170 }
171 
172 /*********************************************************************************
173  * @ingroup lib-layer-lcd-functions-public
174  * @fn void dev_Lcd_GotoXY(volatile uint8_t x, volatile uint8_t y)
175  * @brief Sets the cursor position to the given x- and y-coordinates
176  * @param x x-coordinate for the new cursor position starting with zero
177  * @param y y-coordinate for the new cursor position starting with zero
178  * @return void
179  * @details
180  * This function sets the cursor position to the given x- and y-coordinates
181  * starting at (0, 0) in the upper left corner.
182  **********************************************************************************/
183 
184 void dev_Lcd_GotoXY(volatile uint8_t x, volatile uint8_t y)
185 {
186  drv_LcdInterface_SendCmd(LCD_DDRAM_ADDRESS((line_address[y] + x)));
187  pos_x = x;
188  pos_y = y;
189  change_position = false;
190 }
191 
192 /*********************************************************************************
193  * @ingroup lib-layer-lcd-functions-public
194  * @fn void dev_Lcd_WriteChar(const char ch)
195  * @brief Writes a character on the LCD screen
196  * @param ch character be written to the LCD screen
197  * @return void
198  * @details
199  * This function writes the single character 'ch' to the currently
200  * selected position on LCD screen. The following special characters
201  * will result in a specific response of the LCD:
202  *
203  * - Character '\f' clears the screen and positions the cursor on the upper left corner
204  * - Character '\r' sets the x position of the cursor to 0
205  * - Character '\n' puts the cursor on the next line (without changing the x-position)
206  *
207  **********************************************************************************/
208 
209 void dev_Lcd_WriteChar(const char ch)
210 {
211  if (change_position)
212  {
213  dev_Lcd_GotoXY(pos_x, pos_y);
214  change_position = false;
215  }
216 
217  switch (ch)
218  {
219  case '\f': //sets position to 0,0 after clearing the screen. this is slow (1ms)!
220  dev_Lcd_Clear();
221  pos_x = 0;
222  pos_y = 0;
223  change_position = false;
224  break;
225  case '\v': //sets position to 0,0 without clearing the screen
226  pos_x = 0;
227  pos_y = 0;
228  change_position = true;
229  break;
230  case '\r': //carriage return ==> x=0;
231  pos_x = 0;
232  change_position = true;
233  break;
234  case '\n': //new line return ==> y++;
235  pos_y++;
236  change_position = true;
237  break;
238  default:
239  if (pos_x < LCD_DISPLAYSIZE_X && pos_y < LCD_DISPLAYSIZE_Y)
240  drv_LcdInterface_SendChar(ch);
241  if (++pos_x >= LCD_DISPLAYSIZE_X)
242  {
243  pos_x = 0;
244  pos_y++;
245  change_position = true;
246  }
247  break;
248  }
249 }
250 
251 /*********************************************************************************
252  * @ingroup lib-layer-lcd-functions-public
253  * @fn void dev_Lcd_WriteString(const char *str)
254  * @brief Writes a complete string on the LCD screen
255  * @param str Pointer to a string variable to be written on the LCD screen
256  * @return void
257  * @details
258  * This function writes a complete string 'str' to the currently
259  * selected position on LCD screen. If the length of the string
260  * exceeds the available number display digits, the position will
261  * overrun and start again from position (0, 0).
262  *
263  * The following special characters within the string will result in a
264  * specific response of the LCD:
265  *
266  * - Character '\f' clears the screen and positions the cursor on the upper left corner
267  * - Character '\r' sets the x position of the cursor to 0
268  * - Character '\n' puts the cursor on the next line (without changing the x-position)
269  *
270  **********************************************************************************/
271 
272 void dev_Lcd_WriteString(const char *str)
273 {
274  while(*str)
275  {
276  dev_Lcd_WriteChar(*str);
277  str++;
278  }
279 }
280 
281 /*********************************************************************************
282  * @ingroup lib-layer-lcd-functions-public
283  * @fn void dev_Lcd_WriteStringXY(uint8_t column_index, uint8_t line_index, const char *str)
284  * @brief Sets the cursor position to the given x- and y-coordinates and writes the given string on the lcd screen
285  * @param column_index x-coordinates for the new cursor position starting with zero
286  * @param line_index y-coordinates for the new cursor position starting with zero
287  * @param str pointer to a string variable to be written on the LCD screen
288  * @return void
289  * @details
290  * This function positions the cursor at the location defined by parameters
291  * x and y and writes a complete string 'str' to the newly selected
292  * position on LCD screen. If the length of the string exceeds the available
293  * number display digits, the position will overrun and start again from position
294  * (0, 0) in the upper left corner.
295  *
296  * The following special characters within the string will result in a
297  * specific response of the LCD:
298  *
299  * - Character '\f' clears the screen and positions the cursor on the upper left corner
300  * - Character '\r' sets the x position of the cursor to 0
301  * - Character '\n' puts the cursor on the next line (without changing the x-position)
302  *
303  **********************************************************************************/
304 
305 void dev_Lcd_WriteStringXY(volatile uint8_t column_index, volatile uint8_t line_index, const char *str)
306 {
307  dev_Lcd_GotoXY(column_index, line_index);
308  dev_Lcd_WriteString(str);
309 }
310 
311 // end of file