My favorites
▼
|
Sign in
tinkercode
Miscellaneous Code Snippets for AVR programming
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
automation-framework
/
light_sensor2
/
main.c
‹r126
r143
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
///////////////////////////////////////////////////////////////////////////////
// main.c - Light Sensor
//
// Copyright (C) 2009 Jochen Toppe <jochen@jtoee.com>. All right reserved.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUTPUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
///////////////////////////////////////////////////////////////////////////////
// purpose:
// light sensor using up to three LDRs to sense light and remotely transmit whether
// light was detected or not. The LDRs are connected externally and are gated
// via NPN transistors as attinys lack analog-to-digital converter inputs.
//
// Sends periodic TX32 commands to the master station (id 0xf) which contain
// one data byte with the lowest three bist returning the result led checks:
//
// LED 1 on: 0x1, LED 2 on: 0x2, LED 1 and 2 on: 0x3 (you get the idea, binary).
//
// hardware setup:
//
// 16MHz external clocked attiny2313
// TX: data in of rf transmitter @ 1200 baud
// PB0: Turn on/off transmitter circuit
// PB1: LDR #1
// PB2: LDR #2
// PB3: LDR #3
// PB4: On/Off state mirror for LED 1 sensor (high if LED 1 detected light)
// PB5: On/Off state mirror for LED 2 sensor (high if LED 2 detected light)
// PB6: On/Off state mirror for LED 3 sensor (high if LED 3 detected light)
// No receiver circuitry, i.e. no support for command processing.
//
// Device id select via PD 2,3,4,5 by pulling them to ground (they're pulled
// up to high via the internal avr pull-up resistors)
#ifdef TEST
#include <stdio.h>
#else
#include <avr/io.h>
#include <util/delay.h>
#endif
#include "../common/config.h"
#include "../common/uart.h"
#include "../common/commands.h"
#include "../common/protocol.h"
#include "../common/commandFilter.h"
#include "../common/util.h"
#ifndef TEST
// pins
// enables the transmitter
#define TRANSMITTER_ENABLE_PIN 0x0
// N and P sides of first light sensing LED
#define LDR1 0x1
#define LDR2 0x2
#define LDR3 0x3
// status ports
#define LED1_STATUS 0x4
#define LED2_STATUS 0x5
#define LED3_STATUS 0x6
// convenience - for digital write
#define LOW 0
#define HIGH 1
// convenience for pinMode
#define OUTPUT 1
#define INPUT 0
// milliseconds to wait before re-testing and re-sending - x 6.5 seconds
#define WAIT_LOOPS 5
//
// wait function that goes beyond the 6 seconds _delay_ms can do
//
void wait(void) {
uint8_t i = WAIT_LOOPS;
do {
_delay_ms(6500);
} while (i-- > 0);
}
// set the mode of the selected pin on portb to in/out
// starts with pin0
void pinMode(uint8_t pin, uint8_t mode)
{
if (mode == OUTPUT)
DDRB |= (1<<pin);
if (mode == INPUT)
DDRB &= ~(1<<pin);
}
// write to a digital output pin, pins starting at 0
void digitalWrite(uint8_t pin, uint8_t value)
{
if (value == HIGH)
PORTB |= (1<<pin);
if (value == LOW)
PORTB &= ~(1<<pin);
}
// turn transmitter on prior to calling the rf send
void pre_hook(void)
{
digitalWrite(TRANSMITTER_ENABLE_PIN, HIGH);
// let the transmitter settle
_delay_ms(100);
}
// turn transmitter off after sending the rf command out
void post_hook(void)
{
_delay_ms(100);
digitalWrite(TRANSMITTER_ENABLE_PIN, LOW);
}
// determine the device id
void determine_device_id(void)
{
// set pins 2,3,4,5 to IN by turning the bits off
DDRD &= 0xC3;
// enable pull-ups
PORTD |= 0x3C;
// settle
_delay_ms(10);
// get bits 2-5 into one number
setDeviceId((PIND >> 2) & 0xF);
}
// initialize
void init_sequence(void)
{
// set pin mode for receiver enable pin
pinMode(TRANSMITTER_ENABLE_PIN, OUTPUT);
// set ldr1..3 to input and turn on the internal pull-up resistors
pinMode(LDR1,INPUT); digitalWrite(LDR1,HIGH);
pinMode(LDR2,INPUT); digitalWrite(LDR2,HIGH);
pinMode(LDR3,INPUT); digitalWrite(LDR3,HIGH);
// LEDs to output
pinMode(LED1_STATUS,OUTPUT);
pinMode(LED2_STATUS,OUTPUT);
pinMode(LED3_STATUS,OUTPUT);
// determine device id and set it
determine_device_id();
// turn on rf
uart_initialize(0 /* rx */ ,1 /* tx */);
// set hooks for transmitter
setPreSendHook(pre_hook);
setPostSendHook(post_hook);
// signal we're ready
digitalWrite(LED1_STATUS, HIGH);
_delay_ms(500);
digitalWrite(LED1_STATUS, LOW);
digitalWrite(LED2_STATUS, HIGH);
_delay_ms(500);
digitalWrite(LED2_STATUS, LOW);
digitalWrite(LED3_STATUS, HIGH);
_delay_ms(500);
digitalWrite(LED3_STATUS, LOW);
}
//
// code for the relay receiver module.
//
int main(void)
{
// initialize the device
init_sequence();
// start processing happily until the power goes out.
uint8_t result = 0;
while (1)
{
result = 0;
// the LDRs are connected via NPN transistors (I was out of PNP's) acting as pull-down.
// So dark = bit enabled, bright = bit disabled
//
if(bit_is_set(PINB, LDR1)==0) {
digitalWrite(LED1_STATUS, LOW);
} else {
digitalWrite(LED1_STATUS, HIGH);
result |= 0x1;
}
//
if(bit_is_set(PINB, LDR2)==0) {
digitalWrite(LED2_STATUS, LOW);
} else {
digitalWrite(LED2_STATUS, HIGH);
result |= 0x2;
}
//
if(bit_is_set(PINB, LDR3)==0) {
digitalWrite(LED3_STATUS, LOW);
} else {
digitalWrite(LED3_STATUS, HIGH);
result |= 0x4 ;
}
// send it
uint8_t data[] = {result};
assemble_command(COMMAND_TX32, next_request_id(), 0xF /* master */, 1 /* data size */, data);
send_command();
// wait for the next transmission
wait();
}
return 0;
}
#else
// unit tests
// ////////////////
extern int commonSuite();
int main(void)
{
return commonSuite();
}
#endif
Show details
Hide details
Change log
r143
by jochen.toppe on Jan 25, 2010
Diff
[No log message]
Go to:
...n-framework/light_sensor2/main.c
Project members,
sign in
to write a code review
Older revisions
r126
by jochen.toppe on Jan 7, 2010
Diff
refactor
r124
by jochen.toppe on Jan 4, 2010
Diff
[No log message]
r123
by jochen.toppe on Jan 4, 2010
Diff
[No log message]
All revisions of this file
File info
Size: 6436 bytes, 239 lines
View raw file
Powered by
Google Project Hosting