PIC24_DOG
main.c
gehe zur Dokumentation dieser Datei
1 /****************************************************************************/
26 /****************************************************************************/
30 /* Device header file */
31 #if defined(__XC16__)
32  #include <xc.h>
33 #elif defined(__C30__)
34  #if defined(__PIC24E__)
35  #include <p24Exxxx.h>
36  #elif defined (__PIC24F__)||defined (__PIC24FK__)
37  #include <p24Fxxxx.h>
38  #elif defined(__PIC24H__)
39  #include <p24Hxxxx.h>
40  #endif
41 #endif
42 
43 #define DOGM163_norm /* Define Displaytype for Init */
44 
45 
46 #include <stdint.h> /* Includes uint16_t definition */
47 #include <stdbool.h> /* Includes true/false definition */
48 #include <stdio.h>
49 #include <string.h>
50 #include "system.h" /* System funct/params, like osc/peripheral config */
51 #include "user.h" /* User funct/params, such as InitApp */
52 #include <pps.h>
53 #include "DOG.h"
54 
55 /****************************************************************************/
59 #define LCD_LED_TRIS TRISBbits.TRISB15 // LED Pin TRIS 0=OUT 1=INP
60 #define LCD_LED LATBbits.LATB15
61 #define LCD_RS_TRIS TRISBbits.TRISB14 // LCD RS Control=0 Data=1
62 #define LCD_RS LATBbits.LATB14
63 #define LCD_CS_TRIS TRISBbits.TRISB13 // LCD CS ChipSelect 0 aktiv 1 aus
64 #define LCD_CS LATBbits.LATB13
65 
66 
67 /****************************************************************************/
71 /* i.e. uint16_t <variable_name>; */
72  struct {
73  char line1[16] ;
74  char line2[16] ;
75  char line3[16] ;
76  } LCD;
77  char display[3][16];
78  char *displayptr = display[0];
79 /****************************************************************************/
83 /****************************************************************************/
92 void Delayms( uint16_t t)
93 {
94  T2CON = 0x8000; // enable tmr1, Tcy, 1:1
95  while (t--)
96  {
97  TMR2 = 0;
98  while (TMR2<16000);
99  }
100 } // Delayms
101 
102 /****************************************************************************/
112 int WriteSPI1(uint8_t data)
113 {
114  SPI1BUF = data; // write to buffer for TX
115  while( !SPI1STATbits.SPIRBF); // wait for transfer to complete
116  return SPI1BUF; // read the received value
117 }//WriteSPI1
118 
119 /****************************************************************************/
128 void Clear_LCD(void)
129 {
130  LCD_CS = 0;
131  LCD_RS = 0; //Commandmode
132  WriteSPI1(0x01);
133  Delayms(20);
134  LCD_RS = 0;
135  LCD_CS = 1;
136 }
137 
138 /****************************************************************************/
146 void SetContrast(uint8_t contrast)
147 {
148  uint8_t temp = 0;
149  if (contrast > 63) // Limit max =63
150  {
151  contrast =63;
152  }
153  LCD_RS = 0; //Command
154  LCD_CS = 0;
155  //Lower Bits
156  temp = contrast ;
157  WriteSPI1(0x70+(temp & 0b00000011));
158  //Higher bits
159  contrast = contrast >>4;
160  WriteSPI1(0x54+(contrast & 0b00000011));
161 
162  Delayms(20);
163  LCD_CS = 1;
164  LCD_RS = 0;
165 }
166 /****************************************************************************/
176 void Init_LCD(void)
177 {
178  LCD_LED = 1; // LCD Beleuchtung einschalten
179  LCD_CS = 0;
180  LCD_RS = 0 ; // Commandmode
181  Delayms(50); // ca. 50ms Wartezeit nach dem Einschalten
182  WriteSPI1(FUNCTIONSET_IT1); // 8bit Datenlänge 2Zeilen Instruction Table 1
183  WriteSPI1(BIASSET); // Biasset 1/5 3 zeiliges Display
184  WriteSPI1(POWERCONTROL); // Booster ON, Contrastset C5,C4
185  WriteSPI1(FOLLOWERCONTROL); // Follower and Gain set
186  WriteSPI1(CONTRASTSET); // Contrastset C3,C2,C1,C0
187  Delayms(50);
188  WriteSPI1(FUNCTIONSET_IT0); // Instruction Table 0
189  WriteSPI1(DISPLAY_ON_OFF); // Display ON,Cursor ON, Cursor blinken
190  WriteSPI1(CLEARDISPLAY); // Cleardisplay,Cursor Home
191  WriteSPI1(ENTRYMODESET); // Cursor autoincrement
192  LCD_RS = 1; // Datamode
193  LCD_CS = 1; // deselect
194  LCD_LED = 0;
195 }
196 
197 void Write_LCD_Data(uint8_t zeichen)
198 {
199  LCD_CS = 0;
200  LCD_RS = 1; // Datamode
201  WriteSPI1(zeichen);
202  LCD_RS = 1;
203  LCD_CS = 1;
204 }
205 
206 void Write_LCD_Command(uint8_t zeichen)
207 {
208  LCD_CS = 0;
209  LCD_RS = 0 ; // Commandmode
210  WriteSPI1(zeichen);
211  LCD_RS = 1;
212  LCD_CS = 1;
213 }
214 void eaDogM_WriteChr(char value)
215 {
216  LCD_CS = 0; //output_low(EADOGM_PIN_CSB);
217  LCD_RS = 1; //output_high(EADOGM_PIN_RS);
218  WriteSPI1(value); //eaDogM_outSPI(value);
219  LCD_RS = 0;
220  LCD_CS =1; //output_high(EADOGM_PIN_CSB);
221 // Delayms(1);
222 }
223 void eaDogM_WriteString(char *strPtr)
224 {
225  while (*strPtr != 0x00)
226  {
227  eaDogM_WriteChr(*strPtr);
228  strPtr ++;
229  }
230 }
231 
232 
233 void WriteString(char *strPtr, uint8_t lng)
234 {
235  while (lng >0 )
236  {
237  eaDogM_WriteChr(*strPtr);
238  strPtr ++;
239  lng --;
240  }
241 }
242 
243 /****************************************************************************/
252 //
253 void Set_Cursor_DOGM163 (char row, char column)
254 {
255  // Dislay Position ist Zeile Spalte der DDRAM Adresse. RS=0!
256  // 00 01 02 03 .... 0F
257  // 10 11 12 13 .... 1F
258  // 20 21 22 23 .... 2F
259  // DB7 = 1, DB6-DB0 = variable
260  // Da, DB7 eine 1 ist wird immer 0x80 oder 128 dazu addiert (CHARACTER_BUFFER_BASE_ADDRESS)
261  // Bsp. Zeile 2, Spalte 3 bedeutet 0x12, damit muss ans DDRAM 0x80 + 0x12 = 0x92 geschickt werden.
262  // Eleganter geht es natürlich mit den beiden Konstanten, da eine neue Zeile genau nach der Zeilenlänge
263  // an der ersten Position beginnt. Aus 0x0F (Spalte 16) wird 0x10, Spalte und 0x80 dazu addiert und fertig.
265 }
266 /******************************************************************************/
267 /* Main Program */
268 /******************************************************************************/
269 
270 int16_t main(void)
271 {
272 
273  /* Configure the oscillator for the device */
275 
276  /* Initialize IO ports and peripherals, remap ports */
277  ClrWdt();
278  InitApp();
279  LCD_CS = 1; // inaktiv
280  LCD_RS = 1; // Datamode
281 
282  /* TODO <INSERT USER APPLICATION CODE HERE> */
283  int loopcounter;
284 
285  char s[16]="Contrast is",t[16];
286  char *strPtr = t;
287  int y,z=99;
288  Init_LCD();
289  while(1)
290 
291  {
292  ClrWdt();
293  LCD_LED=1;
294  Clear_LCD();
295  strcpy (s,"DOGM163 & PIC24");
296  y = sprintf(t,"%s",s);
298  Set_Cursor_DOGM163 (1,3);
299  strcpy (s,"SPI - Modus");
300  y = sprintf(t,"%s",s);
302  Delayms(300);
303  Clear_LCD();
304 
305  for( z = 1 ;z<64;z++)
306  {
307 
308  strcpy (s,"Contrast is");
309  y = sprintf(t," %s:",s);
310  Set_Cursor_DOGM163 (0,0);
311  y = sprintf(t, "%s: %+04.1i",s,z);
312  WriteString(strPtr,y);
313  Set_Cursor_DOGM163 (1,0);
314  y = sprintf(t, "Length is: %03i",y);
315  WriteString(strPtr,y);
316  // eaDogM_WriteString(t);
317  Set_Cursor_DOGM163 (2,0);
318  y = sprintf(t, "Loops are: %03i",loopcounter);
319  WriteString(strPtr,y);
320  // eaDogM_WriteString(t);
321  Delayms(100);
322  void SetContrast(uint8_t z) ;
323  ClrWdt();
324  }
325  // for (a =0;a<256;a++)
326  // {
327  // LCD_LED=1;
328 /*
329  for (b=0;b<48;b++)
330  {
331  Write_LCD(a);
332  }
333  Delayms(500);
334  LCD_LED=0; */
335  // Clear_LCD();
336  // txt ="test";
337  // LCD.line1[] = "1111111111111111" ;
338  // LCD.line2[] = "2222222222222222" ;
339  // LCD.line3[] = "3333333333333333" ;
340 
341  // eaDogM_WriteString(s); // wegen Feldname = Pointer auf Feld
342  // eaDogM_WriteString(LCD.line2);
343  // eaDogM_WriteString(LCD.line3);
344  // a=1;
345  // Delayms(500);
346  loopcounter ++;
347  }
348 }
void WriteString(char *strPtr, uint8_t lng)
Definition: main.c:233
char line2[16]
Definition: main.c:74
#define LCD_CS
Definition: main.c:64
void InitApp(void)
Definition: user.c:32
int WriteSPI1(uint8_t data)
Description: send one byte of data and receive one back at the same time in: uint8_t data out: int...
Definition: main.c:112
void eaDogM_WriteString(char *strPtr)
Definition: main.c:223
#define CHARACTER_BUFFER_BASE_ADDRESS
Definition: DOG.h:24
void Set_Cursor_DOGM163(char row, char column)
Description: setzt den Coursor an eine bestimmte Zeile und Spalte. Nullnotation beachten! in: uint8_t...
Definition: main.c:253
void ConfigureOscillator(void)
Definition: system.c:41
char line1[16]
Definition: main.c:73
void Write_LCD_Data(uint8_t zeichen)
Definition: main.c:197
#define LCD_RS
Definition: main.c:62
char * displayptr
Definition: main.c:78
void Delayms(uint16_t t)
Description: Delay with Timer 2 in: uint16_t t out: None.
Definition: main.c:92
void SetContrast(uint8_t contrast)
Description: Set Displaycontrast in: Contrast Range <=63 out: void.
Definition: main.c:146
#define LCD_LED
Definition: main.c:60
struct @0 LCD
void Write_LCD_Command(uint8_t zeichen)
Definition: main.c:206
void eaDogM_WriteChr(char value)
Definition: main.c:214
void Init_LCD(void)
Description: send one byte of data and receive one back at the same time in: uint8_t data out: int...
Definition: main.c:176
char line3[16]
Definition: main.c:75
#define CHARACTERS_PER_ROW
Definition: DOG.h:26
char display[3][16]
Definition: main.c:77
int16_t main(void)
Definition: main.c:270
void Clear_LCD(void)
Description: Clear LCD-Display, Cursor at Position 0,0 in: void out: void.
Definition: main.c:128