1 | #include <avr/io.h>
|
2 | #include "tlc5916.h"
|
3 | // LE pin macros
|
4 | #define LE PD7
|
5 | #define LE_DDR DDRD
|
6 | #define LE_PORT PORTD
|
7 | #define LE_HI LE_PORT |= (1<<LE);
|
8 | #define LE_LO LE_PORT &= ~(1<<LE);
|
9 | #define LATCH LE_PORT ^= (1<<LE); LE_PORT ^= (1<<LE);
|
10 | // OE pin macros
|
11 | #define OE PD6
|
12 | #define OE_DDR DDRD
|
13 | #define OE_PORT PORTD
|
14 | #define OE_HI OE_PORT |= (1<<OE);
|
15 | #define OE_LO OE_PORT &= ~(1<<OE);
|
16 | // SCK pin macros
|
17 | #define SCK PB0
|
18 | #define SCK_DDR DDRB
|
19 | #define SCK_PORT PORTB
|
20 | #define SCK_HI SCK_PORT |= (1<<SCK);
|
21 | #define SCK_LO SCK_PORT &= ~(1<<SCK);
|
22 | #define CLOCK SCK_PORT ^= (1<<SCK); SCK_PORT ^= (1<<SCK);
|
23 | // MOSI pin macros
|
24 | #define MOSI PD1
|
25 | #define MOSI_DDR DDRD
|
26 | #define MOSI_PORT PORTD
|
27 | #define MOSI_HI MOSI_PORT |= (1<<MOSI);
|
28 | #define MOSI_LO MOSI_PORT &= ~(1<<MOSI);
|
29 | // MISO pin macros
|
30 | #define MISO PD0
|
31 | #define MISO_DDR DDRD
|
32 | #define MISO_PORT PORTD
|
33 | #define MISO_HI MISO_PORT |= (1<<MISO);
|
34 | #define MISO_LO MISO_PORT &= ~(1<<MISO);
|
35 | // spi macros
|
36 | #define SPI_DISABLE //usart_disable();
|
37 | #define SPI_ENABLE //usart_enable();
|
38 | #define SPI_TRANSMIT(x) softSPI(x); //usart_mspim_transceive(x);
|
39 | // tlc5916 modes
|
40 | #define NORMAL 0
|
41 | #define SPECIAL 1
|
42 | // function definitions
|
43 | void tlc5916_init (void) {
|
44 | LE_DDR |= (1<<LE);
|
45 | OE_DDR |= (1<<OE);
|
46 | SCK_DDR |= (1<<SCK);
|
47 | MOSI_DDR |= (1<<MOSI);
|
48 | MISO_DDR |= (1<<MISO);
|
49 | OE_HI
|
50 | LE_LO
|
51 | }
|
52 | void mode (uint8_t newmode) {
|
53 | SPI_DISABLE
|
54 | SCK_LO
|
55 | LE_LO
|
56 | OE_HI
|
57 | CLOCK
|
58 | OE_LO
|
59 | CLOCK
|
60 | OE_HI
|
61 | CLOCK
|
62 | if (newmode == SPECIAL) {
|
63 | LE_HI
|
64 | CLOCK
|
65 | LE_LO
|
66 | CLOCK
|
67 | } else {
|
68 | CLOCK
|
69 | CLOCK
|
70 | }
|
71 | SPI_ENABLE
|
72 | }
|
73 | void softSPI (uint8_t data) {
|
74 | SCK_LO
|
75 | uint8_t i;
|
76 | for (i = 0; i < 8; i++) {
|
77 | if ((data & (1<<(7-i)))) {
|
78 | MOSI_HI
|
79 | } else {
|
80 | MOSI_LO
|
81 | }
|
82 | CLOCK
|
83 | }
|
84 | }
|
85 | void tlc5916_out (uint8_t data, uint8_t device) {
|
86 | static uint8_t out[TLC5916_QTY] = {0x00, 0x00};
|
87 | uint8_t i;
|
88 | out[(uint8_t)device] = data;
|
89 | for (i = 0; i < TLC5916_QTY; i++) {
|
90 | SPI_TRANSMIT (out[i])
|
91 | }
|
92 | LATCH
|
93 | OE_LO
|
94 | }
|
95 | void tlc5916_cur (uint8_t data, uint8_t device) {
|
96 | static uint8_t cur[2] = {0xFF, 0xFF};
|
97 | uint8_t i;
|
98 | uint8_t tmp = 0x00;
|
99 | for (i = 0; i < 8; i++)
|
100 | tmp |= ((data >> (7 - i))<<i);
|
101 | cur[device] = tmp;
|
102 | mode (SPECIAL);
|
103 | for (i = 0; i < 2; i++) {
|
104 | SPI_TRANSMIT (cur[i])
|
105 | }
|
106 | LATCH
|
107 | mode (NORMAL);
|
108 | OE_LO
|
109 | }
|