1 | /*
|
2 | * File: main7219.c
|
3 | * Author: BeastyK
|
4 | *
|
5 | * Created on 9. Oktober 2016, 17:54
|
6 | */
|
7 |
|
8 |
|
9 | #include <xc.h>
|
10 | #include <delays.h>
|
11 | #include <spi.h>
|
12 | #include <timers.h>
|
13 | #include <stdio.h>
|
14 | #include <stdlib.h>
|
15 | #include "m7219.h"
|
16 |
|
17 | #define _XTAL_FREQ 20000000
|
18 |
|
19 | void Timer1Init();
|
20 | void interrupt_isr(void);
|
21 | //void max7219_time_write(unsigned int std, unsigned int min, unsigned int sek);
|
22 |
|
23 | /*****************************************************************************/
|
24 | /** C O N F I G U R A T I O N ************************************************/
|
25 | /*****************************************************************************/
|
26 |
|
27 |
|
28 | // PIC18F4455 Configuration Bit Settings
|
29 |
|
30 | // 'C' source line config statements
|
31 |
|
32 | // CONFIG1L
|
33 | #pragma config PLLDIV = 5 // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
|
34 | #pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
|
35 | #pragma config USBDIV = 2 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)
|
36 |
|
37 | // CONFIG1H
|
38 | #pragma config FOSC = HSPLL_HS // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
|
39 | #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
|
40 | #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
|
41 |
|
42 | // CONFIG2L
|
43 | #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
|
44 | #pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
|
45 | #pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
|
46 | #pragma config VREGEN = ON // USB Voltage Regulator Enable bit (USB voltage regulator enabled)
|
47 |
|
48 | // CONFIG2H
|
49 | #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
|
50 | #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
|
51 |
|
52 | // CONFIG3H
|
53 | #pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
|
54 | #pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
|
55 | #pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
|
56 | #pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
|
57 |
|
58 | // CONFIG4L
|
59 | #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
|
60 | #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
|
61 | #pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
|
62 | #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
|
63 |
|
64 | // CONFIG5L
|
65 | #pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
|
66 | #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
|
67 | #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
|
68 |
|
69 | // CONFIG5H
|
70 | #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
|
71 | #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
|
72 |
|
73 | // CONFIG6L
|
74 | #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
|
75 | #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
|
76 | #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
|
77 |
|
78 | // CONFIG6H
|
79 | #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
|
80 | #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
|
81 | #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
|
82 |
|
83 | // CONFIG7L
|
84 | #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
|
85 | #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
|
86 | #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
|
87 |
|
88 | // CONFIG7H
|
89 | #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
|
90 |
|
91 | // #pragma config statements should precede project file includes.
|
92 | // Use project enums instead of #define for ON and OFF.
|
93 |
|
94 | /*****************************************************************************/
|
95 | /**** M A I N P R O G R A M ************************************************/
|
96 | /*****************************************************************************/
|
97 |
|
98 | unsigned char i;
|
99 | unsigned int second = 0, minute = 0, hour = 0;
|
100 |
|
101 | void Timer1Init() {
|
102 |
|
103 | //Timer1 ist ein 16bit Timer (FFFFh oder 65535) mit jeweils 8bit Timer High
|
104 | //und 8bit Timer Low mit externen Uhrenquarz von 32768Hz wird der Interrupt nach
|
105 | //2 Sekunden gesetzt, daher wird TMR1H auf 80h gesetzt (8000h ist der gesamte
|
106 | //Timer1 aus TMR1H und TMR1L und entspricht 32768)
|
107 | TMR1H = 0x80; //setzt Msb des oberen 8bit des Timer1 auf 1
|
108 | TMR1L = 0; //setzt die unteren 8bit des Timers1 auf Null
|
109 | TRISCbits.TRISC0 = 1; //setzt TRISC0 auf Eingang (warum aber nicht TRISC1?)
|
110 | LATBbits.LATB6 = 1; //setzt den PM-Zeit bit
|
111 | T1CON = 0b00001111; // 2x8bit, externer Takt, Osc enabled, asynch, start!
|
112 | PIE1bits.TMR1IE = 1;
|
113 | INTCONbits.PEIE = 1;
|
114 | //RCONbits.IPEN=0x01;
|
115 | //IPR1bits.TMR1IP=0x01; // TMR1 high priority ,TMR1 Overflow Interrupt Priority bit
|
116 | INTCONbits.GIE = 1;
|
117 | PIR1bits.TMR1IF = 0; //setzt den Timerinterrupt von Timer1 auf Null
|
118 | }
|
119 |
|
120 | void interrupt isr(void)
|
121 | {
|
122 | //TMR1H |= 0x80;
|
123 | if (PIR1bits.TMR1IF)
|
124 | {
|
125 | unsigned int d = 0, e = 0, f = 0, std = 0, min = 0, sek = 0;
|
126 | second++;
|
127 | // max7219new++;
|
128 | TMR1H = 0x70;
|
129 | //PIR1bits.TMR1IF = 0; //TMR1IF überlauf muß per software zurückgesetzt werden (Datenblatt Seite 102)
|
130 |
|
131 |
|
132 | if (second >= 60) //ist second gleich oder größer 60 dann
|
133 | {
|
134 | minute++; //Variable minute um 1 erhöhen (60 Sekunden sind eine Minute)
|
135 | second = 0; //die Variable second beim Timerinterrupt wieder von 0 anfangen lassen zu zählen
|
136 | }
|
137 |
|
138 | if (minute >= 60) //ist minute gleich oder größer 60 dann
|
139 | {
|
140 | hour++; //Variable hour um 1 erhöhen (60 Minuten sind eine Stunde)
|
141 | minute = 0; //die Variable minute wieder auf 0 setzen damit eine neue Minute gezählt wird
|
142 | }
|
143 | if (hour >= 24)
|
144 | {
|
145 | hour = 0;
|
146 | }
|
147 |
|
148 | //if (max7219new >= 1)
|
149 | // {
|
150 | // max7219_time_write(hour, minute, second);
|
151 | // max7219new = 0;
|
152 | // }
|
153 | //d = hour / 10;
|
154 | //std = hour - (d * 10);
|
155 | //max7219_digitwrite(3, d);
|
156 | //max7219_digitwrite(4, std);
|
157 | //e = minute / 10;
|
158 | //min = minute - (e * 10);
|
159 | //max7219_digitwrite(5, e);
|
160 | //max7219_digitwrite(6, min);
|
161 | f = second / 10;
|
162 | sek = second - (f * 10);
|
163 | max7219_digitwrite(7, f);
|
164 | max7219_digitwrite(8, sek);
|
165 |
|
166 | PIR1bits.TMR1IF = 0; //TMR1IF überlauf muß per software zurückgesetzt werden (Datenblatt Seite 102)
|
167 | }
|
168 | }
|
169 |
|
170 | /*void max7219_time_write(unsigned int std, unsigned int min, unsigned int sek)
|
171 | {
|
172 | unsigned int d = 0, e = 0, f = 0;
|
173 |
|
174 | d = std / 10;
|
175 | std = std - (d * 10);
|
176 | max7219_digitwrite(3, d);
|
177 | max7219_digitwrite(4, std);
|
178 | e = min / 10;
|
179 | min = min - (e * 10);
|
180 | max7219_digitwrite(5, e);
|
181 | max7219_digitwrite(6, min);
|
182 | f = sek / 10;
|
183 | sek = sek - (f * 10);
|
184 | max7219_digitwrite(7, f);
|
185 | max7219_digitwrite(8, sek);
|
186 |
|
187 |
|
188 | }
|
189 | */
|
190 | void main(void)
|
191 | {
|
192 | __delay_ms(25); //Delay of 100ms
|
193 | __delay_ms(25);
|
194 | __delay_ms(25);
|
195 | __delay_ms(25);
|
196 | OpenSPI(SPI_FOSC_16, MODE_00, SMPMID); // MODE besteht aus CKP/CKE, ist CKP gleich 1, SCK sendet nicht bei hohen Pegel oder CKP ist 0, SCK
|
197 | //sendet nicht bei niedrigen Pegel.
|
198 | __delay_ms(25); //Delay of 100ms
|
199 | __delay_ms(25);
|
200 | __delay_ms(25); //Delay of 100ms
|
201 | __delay_ms(25);
|
202 |
|
203 | TRISC &= 0xFB; // 1111 1011
|
204 | TRISD = 0xF0; // 1111 0000
|
205 | LATDbits.LATD0 = 1; //Set D0 high
|
206 | __delay_ms(25); //Delay of 100ms
|
207 | __delay_ms(25);
|
208 | __delay_ms(25); //Delay of 100ms
|
209 | __delay_ms(25);
|
210 | max7219_init();
|
211 | __delay_ms(25); //Delay of 100ms
|
212 | __delay_ms(25);
|
213 | __delay_ms(25); //Delay of 100ms
|
214 | __delay_ms(25);
|
215 | Timer1Init();
|
216 | __delay_ms(25); //Delay of 100ms
|
217 | __delay_ms(25);
|
218 | __delay_ms(25); //Delay of 100ms
|
219 | __delay_ms(25);
|
220 |
|
221 | while(1)
|
222 | {
|
223 |
|
224 | }
|
225 | }
|