1 | /*
|
2 | * File: meinLCD_1.c
|
3 | * Author: gerhard hinze
|
4 | *
|
5 | * Created on 02. April 2017, 16:03
|
6 | */
|
7 | // CONFIG
|
8 | #pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
|
9 | #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
|
10 | #pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
|
11 | #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
|
12 | #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
|
13 | #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
|
14 | #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
|
15 | #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
|
16 |
|
17 | // #pragma config statements should precede project file includes.
|
18 | // Use project enums instead of #define for ON and OFF.
|
19 | #define _XTAL_FREQ 4000000 //EasyPIC-40 FQ= 4.0MHz
|
20 | //#define _XTAL_FREQ 3686400 //EasyPIC-40 FQ= 3.6864MHz
|
21 | #include <xc.h>
|
22 | /* ========================================================================== */
|
23 | /* */
|
24 | /* MCU = PIC16F877A XTAL = 4.000 MHz */
|
25 | /* */
|
26 | /* ========================================================================== */
|
27 | /* */
|
28 | /* Definitionen LCD 16x2 Anschlüsse */
|
29 | /* */
|
30 | /* ========================================================================== */
|
31 |
|
32 | #define RS RA1
|
33 | #define RW RA2
|
34 | #define EN RA3
|
35 |
|
36 | #define D0 RD0
|
37 | #define D1 RD1
|
38 | #define D2 RD2
|
39 | #define D3 RD3
|
40 | #define D4 RD4
|
41 | #define D5 RD5
|
42 | #define D6 RD6
|
43 | #define D7 RD7
|
44 |
|
45 | #define SET_CMD_PORT TRISA
|
46 | #define SET_DAT_PORT TRISD
|
47 | #define DATA_PORT PORTD
|
48 | #define CMD_PORT PORTA
|
49 |
|
50 | #define DELAY_INIT __delay_ms(200)
|
51 | #define DELAY_CMD __delay_ms(50)
|
52 | #define DELAY_DAT __delay_ms(10)
|
53 | /* ========================================================================== */
|
54 | /* */
|
55 | /* */
|
56 | /* */
|
57 | /* ========================================================================== */
|
58 | void LCD_Init();
|
59 | void CMD_Init();
|
60 | void CHR_Init();
|
61 | void CMD_Write(unsigned char cmd);
|
62 | void CHR_Write(unsigned char chr);
|
63 | void TXT_Write(char *txt, unsigned char x, unsigned char y);
|
64 | void SET_Pos(unsigned char x, unsigned char y);
|
65 | /* ========================================================================== */
|
66 | /* */
|
67 | /* LCD Initialisieren */
|
68 | /* */
|
69 | /* ========================================================================== */
|
70 | void LCD_Init()
|
71 | {
|
72 | SET_CMD_PORT = 0x00; // PORTA alles OUTPUT
|
73 | SET_DAT_PORT = 0x00; // PORTD alles OUTPUT
|
74 | CMD_PORT = 0x00; // PORTA alles LOW - Pegel
|
75 | DATA_PORT = 0x00; // PORTD alles LOW - Pegel
|
76 |
|
77 | DELAY_INIT; // Warten bis Ub(LCD) sich stabilisiert hat
|
78 | CMD_Write(0x01); // Display clear, Cursor auf Pos. 1.1
|
79 | CMD_Init();
|
80 |
|
81 | CMD_Write(0x38); // 8-BIT 5*7Pkt
|
82 | CMD_Init();
|
83 |
|
84 | CMD_Write(0x0C); // Display ein und Cursor OFF, blinkt nicht
|
85 | CMD_Init();
|
86 |
|
87 | CMD_Write(0x06); // Cursor autoincrement
|
88 | CMD_Init();
|
89 |
|
90 | CMD_Write(0x80); // Setze Cursor Zeile 1, Spalte 0
|
91 | CMD_Init();
|
92 |
|
93 | }
|
94 | /* ========================================================================== */
|
95 | /* */
|
96 | /* Kommando Init zur Kommandouebernahme vom PORTD */
|
97 | /* */
|
98 | /* ========================================================================== */
|
99 | void CMD_Init()
|
100 | {
|
101 | RS = 0; // Kommando
|
102 | RW = 0;
|
103 | EN = 0;
|
104 | DELAY_CMD;
|
105 | EN = 1;
|
106 | DELAY_CMD;
|
107 | }
|
108 |
|
109 | /* ========================================================================== */
|
110 | /* */
|
111 | /* Kommando Init zur Datenuebernahme vom PORTD */
|
112 | /* */
|
113 | /* ========================================================================== */
|
114 | void CHR_Init()
|
115 | {
|
116 | RS = 1; //Daten
|
117 | RW = 0;
|
118 | EN = 0;
|
119 | DELAY_DAT;
|
120 | EN = 1;
|
121 | DELAY_DAT;
|
122 | }
|
123 | /* ========================================================================== */
|
124 | /* */
|
125 | /* Kommando Schreiben */
|
126 | /* */
|
127 | /* ========================================================================== */
|
128 | void CMD_Write(unsigned char cmd)
|
129 | {
|
130 | DATA_PORT = cmd; //PORTD = cmd(Befehlswert)
|
131 | CMD_Init();
|
132 | }
|
133 | /* ========================================================================== */
|
134 | /* */
|
135 | /* 1 Zeichen schreiben */
|
136 | /* */
|
137 | /* ========================================================================== */
|
138 | void CHR_Write(unsigned char chr)
|
139 | {
|
140 | DATA_PORT = chr; //PORTD = chr (Zeichen/Byte)
|
141 | CHR_Init();
|
142 |
|
143 | }
|
144 |
|
145 | /* ========================================================================== */
|
146 | /* */
|
147 | /* Cursor Position schreiben */
|
148 | /* */
|
149 | /* ========================================================================== */
|
150 | void SET_Pos(unsigned char x, unsigned char y)
|
151 | {
|
152 | if(y == 1) // Zeile 1
|
153 | CMD_Write(0x80 + x); // Setze RAM-Adresse + X-position
|
154 | else if(y == 2) // Zeile 2
|
155 | CMD_Write(0xC0 + x); // Setze RAM-Adresse + X-position
|
156 | }
|
157 |
|
158 | /* ========================================================================== */
|
159 | /* */
|
160 | /* Text schreiben */
|
161 | /* */
|
162 | /* ========================================================================== */
|
163 | void TXT_Write(char *txt, unsigned char x, unsigned char y)
|
164 | { SET_Pos(x,y);
|
165 | int i;
|
166 | for(i=0;i<=16;i++)
|
167 | {// Durchlauf Textstring bis Stringende: "0"
|
168 | if (txt[i]== 0)
|
169 | return;
|
170 | else
|
171 | CHR_Write(txt[i]);
|
172 | }// Char ("Zeichen") schreiben
|
173 | }
|
174 |
|
175 | /* ========================================================================== */
|
176 | /* */
|
177 | /* */
|
178 | /* */
|
179 | /* */
|
180 | /* ========================================================================== */
|
181 |
|
182 |
|
183 | void init()
|
184 | {
|
185 | ADCON1 = 0x07; // PORTA als digitaler I/O
|
186 |
|
187 | }
|
188 |
|
189 | /* ========================================================================== */
|
190 | /* */
|
191 | /* Mainroutine/Hauptprogramm */
|
192 | /* */
|
193 | /* ========================================================================== */
|
194 |
|
195 |
|
196 | void main(void) {
|
197 | init();
|
198 | LCD_Init();
|
199 | __delay_ms(200);
|
200 | TXT_Write("Hallo",6,1); // Schreibe in Zeile 1 ab Spalte 5
|
201 | TXT_Write("PIC16F877A",3,2); // Schreibe in Zeile 2 ab Spalte 3
|
202 |
|
203 | while(1);
|
204 | }
|