Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
113 Kevin 1
#include "maindefs.h"
2
#include "led_driver.h"
3
#include "delays.h"
4
#include <spi.h>
5
 
6
unsigned int led_last_value;
7
 
8
#ifdef _SPI2_V1
9
/* Output Pins:
10
 * RA0 - LED Display Latch Enable (PPS)
11
 * RA1 - LED Display CLK (PPS)
12
 * RA2 - LED Display DIN (PPS)
13
 * RA3 - LED Display Output Enable (PPS)
14
 */
15
#endif
16
 
17
#ifdef _SPI2_V2
18
/* Output Pins:
19
 * RA0 - LED Display CLK (PPS)
20
 * RA1 - LED Display DIN (PPS)
21
 * RA2 - LED Display Latch Enable (PPS)
22
 * RA3 - LED Display Output Enable (PPS)
23
 */
24
#endif
25
 
26
void led_driver_init() {
27
#ifdef _SPI2_V1
28
    TRISAbits.TRISA0 = 0;   // LE
29
    TRISAbits.TRISA1 = 0;   // CLK
30
    TRISAbits.TRISA2 = 0;   // DAT
31
    TRISAbits.TRISA3 = 0;   // OE
32
 
33
    LATAbits.LATA0 = 0; // LE
34
    LATAbits.LATA1 = 0; // CLK
35
    LATAbits.LATA2 = 0; // DAT
36
    LATAbits.LATA3 = 0; // OE
37
#endif
38
 
39
#ifdef _SPI2_V2
40
    RPOR0 = 10; // CLK
41
    RPOR1 = 9;  // DATA
42
    OpenSPI2(SPI_FOSC_4, MODE_00, SMPMID);
43
#endif
44
 
45
    led_last_value = 0;
46
    led_driver_data(0);
47
    led_driver_data(0);
48
}
49
 
50
#ifdef _SPI2_V1
51
void led_driver_clock() {
52
    LATAbits.LATA1 = 0x1; // Simple clock output toggle
53
    Nop();
54
    LATAbits.LATA1 = 0x0;
55
    Nop();
56
}
57
#endif
58
 
59
void led_driver_data(char val) {
60
#ifdef _SPI2_V1
61
    int i;
62
    LATAbits.LATA0 = 0x0; // Set latch low to pause display
63
    for (i = 0; i < 8; i++) {
64
        LATAbits.LATA2 = val & 0x1; // Shift out bits
65
        led_driver_clock();
66
        val >>= 1;
67
    }
68
    LATAbits.LATA0 = 0x1; // Set latch high to resume display
69
#endif
70
 
71
#ifdef _SPI2_V2
72
    LATAbits.LATA0 = 0x0;
73
    WriteSPI2(val);
74
    LATAbits.LATA0 = 0x1;
75
#endif
76
}
77
 
78
void led_driver_num(unsigned char data) {
79
    unsigned char tmp = 0;
80
    led_last_value = 0;
81
 
82
    // Determine right character (1s digit)
83
    tmp = data % 10;
84
    switch (tmp) {
85
        case 0:
86
            led_last_value |= 0x0D70;
87
            break;
88
        case 1:
89
            led_last_value |= 0x0140;
90
            break;
91
        case 2:
92
            led_last_value |= 0x0E60;
93
            break;
94
        case 3:
95
            led_last_value |= 0x0760;
96
            break;
97
        case 4:
98
            led_last_value |= 0x0350;
99
            break;
100
        case 5:
101
            led_last_value |= 0x0730;
102
            break;
103
        case 6:
104
            led_last_value |= 0x0F30;
105
            break;
106
        case 7:
107
            led_last_value |= 0x0170;
108
            break;
109
        case 8:
110
            led_last_value |= 0x0F70;
111
            break;
112
        case 9:
113
            led_last_value |= 0x0770;
114
            break;
115
    }
116
 
117
    // Determine left character (10s digit)
118
    tmp = data / 10;
119
    switch (tmp) {
120
        case 0:
121
            led_last_value |= 0xE00D;
122
            break;
123
        case 1:
124
            led_last_value |= 0x2008;
125
            break;
126
        case 2:
127
            led_last_value |= 0xC00E;
128
            break;
129
        case 3:
130
            led_last_value |= 0x600E;
131
            break;
132
        case 4:
133
            led_last_value |= 0x200B;
134
            break;
135
        case 5:
136
            led_last_value |= 0x6007;
137
            break;
138
        case 6:
139
            led_last_value |= 0xE007;
140
            break;
141
        case 7:
142
            led_last_value |= 0x200D;
143
            break;
144
        case 8:
145
            led_last_value |= 0xE00F;
146
            break;
147
        case 9:
148
            led_last_value |= 0x600F;
149
            break;
150
    }
151
 
152
    led_driver_data(led_last_value & 0x00FF);
153
    led_driver_data((led_last_value & 0xFF00) >> 8);
154
}
155
 
156
void led_driver_show_last() {
157
    led_driver_data(led_last_value & 0x00FF);
158
    led_driver_data((led_last_value & 0xFF00) >> 8);
159
}