MAXREFDES70# Code Documentation  V02.00
High-precision long battery life heat/flow meter
 All Data Structures Files Functions Variables Enumerator Macros Pages
dogm163.c
Go to the documentation of this file.
1 
43 #include <stdio.h>
44 
45 #include "dogm163.h"
46 #include "batteryLevel.h"
47 #include "em_cmu.h"
48 #include "em_gpio.h"
49 #include "em_usart.h"
50 
51 
52 extern void Delay(uint32_t dlyTicks);
53 
54 /**************************************************************************/
62 int DOGM163_SpiTransmit(uint8_t* data, uint32_t len) {
63 
64  /* The design by default sample at the trailing edge (CLKPHA = USART_CTRL_CLKPHA); */
65  /* CLKPHA needs to be modified for every DOGM163 transfer because it uses leading edge. */
66  USART1 ->CTRL = USART_CTRL_SYNC | USART_CTRL_MSBF;
67  // Clear CS pin.
68  GPIO_PinOutClear(gpioPortB, 11);
69 
70  while (len > 0) {
71  // Transmit starts only when the TX buffer is empty.
72  USART_Tx(USART1, *(uint8_t*)data);
73  len--;
74  data++;
75 
76  // Wait for transfer to finish.
77  // If USART1 has error, the program is dead! // to do... exception handling.
78  while (!(USART1 ->STATUS & USART_STATUS_TXC))
79  ;
80  }
81 
82  // Set CS pin.
83  GPIO_PinOutSet(gpioPortB, 11);
84  /* The design by default sample at the trailing edge (CLKPHA = USART_CTRL_CLKPHA); */
85  USART1 ->CTRL = USART_CTRL_SYNC | USART_CTRL_MSBF | USART_CTRL_CLKPHA;
86 
87  return 0;
88 }
89 
90 /**************************************************************************/
97 int DOGM163_WriteCmdByte(uint8_t byte) {
98  NVIC_DisableIRQ(GPIO_EVEN_IRQn);
99  NVIC_DisableIRQ(GPIO_ODD_IRQn);
100 
101  uint8_t cmd = byte;
102 
103  // Clear DOGM163 RS pin (PB13).
104  GPIO_PinOutClear(gpioPortB, 13);
105 
106  // Write the command byte.
107  DOGM163_SpiTransmit(&cmd, 1);
108 
109  NVIC_EnableIRQ(GPIO_EVEN_IRQn);
110  NVIC_EnableIRQ(GPIO_ODD_IRQn);
111 
112  return 0;
113 }
114 
115 /**************************************************************************/
122 int DOGM163_WriteDataByte(uint8_t byte) {
123  NVIC_DisableIRQ(GPIO_EVEN_IRQn);
124  NVIC_DisableIRQ(GPIO_ODD_IRQn);
125 
126  uint8_t data = byte;
127 
128  // Set DOGM163 RS pin (PB13).
129  GPIO_PinOutSet(gpioPortB, 13);
130 
131  // Write the data byte.
132  DOGM163_SpiTransmit(&data, 1);
133 
134  NVIC_EnableIRQ(GPIO_EVEN_IRQn);
135  NVIC_EnableIRQ(GPIO_ODD_IRQn);
136 
137  return 0;
138 }
139 
140 /**************************************************************************/
145 void DOGM163_Init(void)
146 {
147  // Switch on the power for DOGM163 (PD6).
148  GPIO_PinOutSet(gpioPortD, 6);
149 
150  // Wait at least 40ms after VDD stable.
151  Delay(40);
152 
153  /**************************************************************************/
159  uint8_t contrast = 85 - batteryLevel(); // 63~27
160 
161  // Function Set command: 0x39, 8-bit data, 3-line, instruction table 1.
162  DOGM163_WriteCmdByte(0x39);
163  Delay(1);
164 
165  // Bias Set command: 0x1D, BS 1/4, 3-line.
166  DOGM163_WriteCmdByte(0x1D);
167  Delay(1);
168 
169  // Contrast Set command: 0x70+ C3|C2|C1|C0.
170  DOGM163_WriteCmdByte(0x70 + (contrast & 0xF));
171  Delay(1);
172 
173  // Power/ICON/Contrast Set command: 0x54+ C5|C4, ICON OFF, booster ON, C5|C4.
174  DOGM163_WriteCmdByte(0x54 + ((contrast>>4) & 0xF));
175  Delay(1);
176 
177  // Follower Control command: 0x6C, follower ON, gain Rab[2..0]= [1,0,0].
178  DOGM163_WriteCmdByte(0x6C);
179  Delay(50);
180 
181  // Display ON/OFF command: 0x0F, display ON, cursor Off, cursor position Off. // to do: cursor on or off, position on or off?
182  DOGM163_WriteCmdByte(0x0C);
183  Delay(1);
184 
185  // Clear Display command: 0x01, clear display.
186  DOGM163_WriteCmdByte(0x01);
187  Delay(2);
188 
189  // Entry Mode command: 0x06, auto increment.
190  DOGM163_WriteCmdByte(0x06);
191  Delay(1);
192 }
193 
194 /**************************************************************************/
200 {
201  // DOGM163 CS pin (PB11).
202  GPIO_PinOutClear(gpioPortB, 11);
203  // DOGM163 RS pin (PB13).
204  GPIO_PinOutClear(gpioPortB, 13);
205  // Switch off the power for DOGM163 (PD6).
206  GPIO_PinOutClear(gpioPortD, 6);
207 
208 }
209 
210 
211 int DOGM163_ClearChars(uint8_t start_row, uint8_t start_column, uint8_t number)
212 {
213  for(uint8_t loop = 0; loop < number; loop++)
214  {
215  DOGM163_WriteCmdByte(0x80 + 0x10*start_row + start_column + loop); // Set DDRAM Address.
216  //Delay(1);
217  DOGM163_WriteDataByte(0x20); // Write SPACE to RAM.
218  //Delay(1);
219  }
220 
221  return 0;
222 
223 }
224 // If the number is larger than the string length, print the complete string.
225 // If the number is less than the string length, print the number of chars.
226 int DOGM163_PrintChars(uint8_t start_row, uint8_t start_column, uint8_t number, char* string)
227 {
228  uint8_t nOfChars = (number > strlen(string)) ? strlen(string): number;
229 
230  for(uint8_t loop = 0; loop < nOfChars; loop++)
231  {
232  DOGM163_WriteCmdByte(0x80 + 0x10*start_row + start_column + loop); // Set DDRAM Address.
233  //Delay(1);
234  DOGM163_WriteDataByte(*(string + loop)); // Write SPACE to RAM.
235  //Delay(1);
236  }
237 
238  return 0;
239 
240 }
241 
242 // limit the positive integer to 16 bits (5 decimal digits).
243 // fixed width, zero padded.
244 int DOGM163_PrintInteger(uint8_t start_row, uint8_t start_column, uint8_t width, uint16_t integer)
245 {
246  DOGM163_ClearChars(start_row, start_column, width);
247 
248  char string[6] = "";
249  snprintf(string, width+1, "%0*d", width, integer);
250 
251  uint8_t len = strlen(string);
252 
253  // Print len charaters.
254  for(uint8_t loop = 0; loop < len; loop++)
255  {
256  DOGM163_WriteCmdByte(0x80 + 0x10*start_row + start_column + loop); // Set DDRAM Address.
257  Delay(1);
258  DOGM163_WriteDataByte(*(string + loop)); // Write DATA to RAM.
259  Delay(1);
260  }
261 
262  return 0;
263 }
264 
265 // Print welcome message.
267 {
268  DOGM163_ClearChars(0, 0, 48);
269  DOGM163_PrintChars(0, 0, 16, "Flow/Heat Meter");
270  DOGM163_PrintChars(1, 0, 16, "Reference Design");
271  DOGM163_PrintChars(2, 0, 16, "MAXIM INTEGRATED");
272 }
273