Digital Power Starter Kit 3 Firmware
dsPIC33C Buck 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 
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 
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 
127 {
130 
132 
133  __delay_ms(25);
134 
137 
138  // Enter the second page of instructions
140  drv_LcdInterface_SendCmd(LCD_OSC_FREQ | BIAS_1_5 | FREQ_CNTRL(4)); //internal osc frequency
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
150 
151  __delay_ms(150);
152 }
153 
154 
166 void dev_Lcd_Clear(void)
167 {
169  __delay_ms(1);
170 }
171 
172 
184 void dev_Lcd_GotoXY(volatile uint8_t x, volatile uint8_t y)
185 {
187  pos_x = x;
188  pos_y = y;
189  change_position = false;
190 }
191 
192 
209 void dev_Lcd_WriteChar(const char ch)
210 {
211  if (change_position)
212  {
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:
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 
272 void dev_Lcd_WriteString(const char *str)
273 {
274  while(*str)
275  {
276  dev_Lcd_WriteChar(*str);
277  str++;
278  }
279 }
280 
281 
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
change_position
uint8_t change_position
Definition: dev_lcd.c:110
line_address
const uint8_t line_address[]
Definition: dev_lcd.c:107
LCD_DDRAM_ADDRESS
#define LCD_DDRAM_ADDRESS(adr)
Definition: dev_lcd.c:77
LCD_OSC_FREQ
#define LCD_OSC_FREQ
Definition: dev_lcd.c:83
dev_Lcd_WriteChar
void dev_Lcd_WriteChar(const char ch)
Writes a character on the LCD screen.
Definition: dev_lcd.c:209
LCD_FOLLOWER_ON
#define LCD_FOLLOWER_ON
Definition: dev_lcd.c:96
FOLLOWER_GAIN
#define FOLLOWER_GAIN(g)
Definition: dev_lcd.c:94
BOOSTLCD
#define BOOSTLCD
Definition: dev_lcd.c:90
FUNCTION_1_LINE
#define FUNCTION_1_LINE
Definition: dev_lcd.c:69
LCD_ADDRESS_LINE_1
#define LCD_ADDRESS_LINE_1
Newhaven NHD-C0216CZ-FSW-FBW LCD controller command set.
Definition: dev_lcd.c:43
LCD_DISPLAYSIZE_Y
#define LCD_DISPLAYSIZE_Y
Definition: dev_lcd.c:47
drv_LcdInterface_Initialize
void drv_LcdInterface_Initialize(void)
Initializes the LCD interface driver.
Definition: drv_lcd_interface.c:52
dev_Lcd_Initialize
void dev_Lcd_Initialize(void)
Initializes the LCD Device.
Definition: dev_lcd.c:126
LCD_ENTRY_MODE
#define LCD_ENTRY_MODE
Definition: dev_lcd.c:56
pos_y
uint8_t pos_y
Definition: dev_lcd.c:109
dev_Lcd_WriteStringXY
void dev_Lcd_WriteStringXY(volatile uint8_t column_index, volatile uint8_t line_index, const char *str)
Sets the cursor position to the given x- and y-coordinates and writes the given string on the lcd scr...
Definition: dev_lcd.c:305
FUNCTION_1_HIGH
#define FUNCTION_1_HIGH
Definition: dev_lcd.c:67
LCD_FUNCTION
#define LCD_FUNCTION
Definition: dev_lcd.c:74
FUNCTION_8BITS
#define FUNCTION_8BITS
Definition: dev_lcd.c:72
FUNCTION_IS
#define FUNCTION_IS
Definition: dev_lcd.c:66
drv_LcdInterface_SendCmd
void drv_LcdInterface_SendCmd(uint8_t cmd)
Sends a command to the LCD controller.
Definition: drv_lcd_interface.c:92
pos_x
uint8_t pos_x
Definition: dev_lcd.c:108
CURSOR_OFF
#define CURSOR_OFF
Definition: dev_lcd.c:58
nICON
#define nICON
Definition: dev_lcd.c:87
LCD_ADDRESS_LINE_2
#define LCD_ADDRESS_LINE_2
Definition: dev_lcd.c:44
BIAS_1_5
#define BIAS_1_5
Definition: dev_lcd.c:80
CONTRAST
#define CONTRAST(c)
Definition: dev_lcd.c:91
dev_Lcd_GotoXY
void dev_Lcd_GotoXY(volatile uint8_t x, volatile uint8_t y)
Sets the cursor position to the given x- and y-coordinates.
Definition: dev_lcd.c:184
FUNCTION_2_LINE
#define FUNCTION_2_LINE
Definition: dev_lcd.c:70
FUNCTION_nIS
#define FUNCTION_nIS
Definition: dev_lcd.c:65
dev_Lcd_Clear
void dev_Lcd_Clear(void)
Clears the LC Display Screen.
Definition: dev_lcd.c:166
LCD_PWR_CONTROL
#define LCD_PWR_CONTROL
Definition: dev_lcd.c:92
LCD_CONTRAST
#define LCD_CONTRAST(c)
Definition: dev_lcd.c:98
drv_LcdInterface_Reset
void drv_LcdInterface_Reset(void)
Resets the LCD controller.
Definition: drv_lcd_interface.c:76
LCD_CLEAR
#define LCD_CLEAR
Definition: dev_lcd.c:49
DATA_INCREMENT
#define DATA_INCREMENT
Definition: dev_lcd.c:55
LCD_DISPLAY_ON
#define LCD_DISPLAY_ON
Definition: dev_lcd.c:63
dev_Lcd_WriteString
void dev_Lcd_WriteString(const char *str)
Writes a complete string on the LCD screen.
Definition: dev_lcd.c:272
FREQ_CNTRL
#define FREQ_CNTRL(f)
Definition: dev_lcd.c:82
LCD_DISPLAYSIZE_X
#define LCD_DISPLAYSIZE_X
Definition: dev_lcd.c:46
BLINK_OFF
#define BLINK_OFF
Definition: dev_lcd.c:60
CURSOR_nSHIFT
#define CURSOR_nSHIFT
Definition: dev_lcd.c:52