1 | /**
|
2 | * I2C.c
|
3 | *
|
4 | * Created: 19.02.2015 22:39:00
|
5 | * Author: Pascal
|
6 | *
|
7 | * Pin definition on ATtiny2313
|
8 | * --------
|
9 | * (RESET/dW) PA2 1|° |20 VCC
|
10 | * (RXD) PD0 2| |19 PB7 (UCSK/SCL/PCINT7)
|
11 | * (TXD) PD1 3| |18 PB6 (MISO/DO/PCINT6)
|
12 | * (XTAL2) PA1 4| |17 PB5 (MOSI/DI/SDA/PCINT5)
|
13 | * (XTAL1) PA0 5| |16 PB4 (OC1B/PCINT4)
|
14 | * (CKOUT/XCK/INT0) PD2 6| |15 PB3 (OC1A/PCINT3)
|
15 | * (INT1) PD3 7| |14 PB2 (OC0A/PCINT2)
|
16 | * (T0) PD4 8| |13 PB1 (AIN1/PCINT1)
|
17 | * (OC0B/T1) PD5 9| |12 PB0 (AIN0/PCINT0)
|
18 | * GND 10| |11 PD6 (ICP)
|
19 | * --------
|
20 | */
|
21 |
|
22 |
|
23 | #define F_CPU 1000000UL
|
24 |
|
25 | #include <avr/io.h>
|
26 | #include <util/delay.h>
|
27 | #include <avr/interrupt.h>
|
28 | #include <usiTwiSlave.h> // Don Blake's library
|
29 |
|
30 | // The TWI address for this slave device
|
31 | const uint8_t TWI_SLAVE_ADDRESS = 0x05;
|
32 |
|
33 | // init array for RGB
|
34 | uint8_t data[3];
|
35 |
|
36 | #define pwm1 OCR0A
|
37 | #define pwm2 OCR0B
|
38 | #define pwm3 OCR1A
|
39 | //#define pwm4 OCR1B
|
40 |
|
41 | int main(void)
|
42 | {
|
43 |
|
44 | // Timer 0 PWM Init (fast PWM)
|
45 | TCCR0A = (1 << WGM00)
|
46 | | (1 << WGM01)
|
47 | | (1 << COM0A1)
|
48 | | (1 << COM0B1);
|
49 | TCCR0B = (0 << WGM02)
|
50 | | (0 << CS00)
|
51 | | (1 << CS01)
|
52 | | (0 << CS02);
|
53 | /**
|
54 | * CS02 CS01 CS00 Description
|
55 | * ---------------------------------------------------------------
|
56 | * 0 0 0 No clock source (Timer/Counter stopped)
|
57 | * 0 0 1 clkI/O/(No prescaling)
|
58 | * 0 1 0 clkI/O/8 (From prescaler)
|
59 | * 0 1 1 clkI/O/64 (From prescaler)
|
60 | * 1 0 0 clkI/O/256 (From prescaler)
|
61 | * 1 0 1 clkI/O/1024 (From prescaler)
|
62 | * 1 1 0 External clock source on T0 pin. Clock on falling edge.
|
63 | * 1 1 1 External clock source on T0 pin. Clock on rising edge.
|
64 | * ---------------------------------------------------------------
|
65 | */
|
66 |
|
67 |
|
68 | // Timer 1 PWM Init (fast PWM)
|
69 | TCCR1A = (1 << WGM10)
|
70 | | (0 << WGM11)
|
71 | | (1 << COM1A1)
|
72 | | (1 << COM1B1);
|
73 | TCCR1B = (1 << WGM12)
|
74 | | (0 << WGM13)
|
75 | | (0 << CS10)
|
76 | | (1 << CS11)
|
77 | | (0 << CS12);
|
78 |
|
79 | /**
|
80 | * CS12 CS11 CS10 Description
|
81 | * ---------------------------------------------------------------
|
82 | * 0 0 0 No clock source (Timer/Counter stopped).
|
83 | * 0 0 1 clkI/O/1 (No prescaling)
|
84 | * 0 1 0 clkI/O/8 (From prescaler)
|
85 | * 0 1 1 clkI/O/64 (From prescaler)
|
86 | * 1 0 0 clkI/O/256 (From prescaler)
|
87 | * 1 0 1 clkI/O/1024 (From prescaler)
|
88 | * 1 1 0 External clock source on T1 pin. Clock on falling edge.
|
89 | * 1 1 1 External clock source on T1 pin. Clock on rising edge.
|
90 | * ---------------------------------------------------------------
|
91 | */
|
92 |
|
93 |
|
94 |
|
95 | /**
|
96 | * OC0A on pin 14 (PB2)
|
97 | * OC1A on pin 15 (PB3)
|
98 | * OC0B on pin 9 (PD5)
|
99 | * OC1B on pin 16 (PB4)
|
100 | */
|
101 | DDRB |= (1 << PB2); // PWM output on PB2 - OC0A
|
102 | DDRB |= (1 << PB3); // PWM output on PB3 - OC1A
|
103 | DDRD |= (1 << PD5); // PWM output on PD5 - OC0B
|
104 | //DDRB |= (1 << PB4); // PWM output on PB4 - OC1B
|
105 |
|
106 | OCR0A = 0;
|
107 | OCR0B = 0;
|
108 | OCR1A = 0;
|
109 | // OCR1B = 0;
|
110 |
|
111 | // set port to input (for Sensor)
|
112 | DDRB &= ~(1 << PINB0); // data direction register set as input PINB1
|
113 | PORTB |= 1 << PINB0; // set PINB1 to a "high" reading
|
114 |
|
115 | // Initialise the I2C interface
|
116 | usiTwiSlaveInit(TWI_SLAVE_ADDRESS);
|
117 |
|
118 | // Start interrupts
|
119 | sei();
|
120 |
|
121 | // set variable for sensor status
|
122 | bool sensor_is_clear = false;
|
123 |
|
124 |
|
125 | /**
|
126 | * pwm1 = R
|
127 | * pwm2 = G
|
128 | * pwm3 = B
|
129 | */
|
130 | while (1)
|
131 | {
|
132 | // check sensor input
|
133 | if(bit_is_clear(PINB, 0))
|
134 | {
|
135 | sensor_is_clear = true;
|
136 | // set color white
|
137 | pwm1 = 255;
|
138 | pwm2 = 255;
|
139 | pwm3 = 255;
|
140 |
|
141 | // send value 1 to master if sensor is covered
|
142 | usiTwiTransmitByte(1);
|
143 | }
|
144 | else
|
145 | {
|
146 | if( sensor_is_clear )
|
147 | {
|
148 | // if sensor is free fade out white color
|
149 | for (int i=255; i>0; i--)
|
150 | {
|
151 | pwm1 = i; // R
|
152 | pwm2 = i; // G
|
153 | pwm3 = i; // B
|
154 | //_delay_ms(5);
|
155 | }
|
156 | sensor_is_clear = false;
|
157 | }
|
158 | else
|
159 | {
|
160 | // check if data is in the i2c receive buffer
|
161 | if(usiTwiDataInReceiveBuffer())
|
162 | {
|
163 | // get some bytes
|
164 | for (int i=0; i<3; i++)
|
165 | {
|
166 | data[i] = usiTwiReceiveByte();
|
167 | }
|
168 |
|
169 | pwm1 = data[0]; // R
|
170 | pwm2 = data[1]; // G
|
171 | pwm3 = data[2]; // B
|
172 | }
|
173 |
|
174 | // send 0 if sensor is clear
|
175 | usiTwiTransmitByte(0);
|
176 | }
|
177 | }
|
178 | }
|
179 | }
|