-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
214 lines (191 loc) · 4.7 KB
/
main.c
File metadata and controls
214 lines (191 loc) · 4.7 KB
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
#include "i2c.h"
#include <stdio.h>
#define READ 1
#define WRITE 0
#define threshold 250.0 // unit : lux
//*******************************************************************************************//
// Automatic-Solar-Tracking-System //
// Author : Chen-Ting Li //
// MCU : 8051, Servo Motor : SG90, Sensor : BH1750 //
// 1. Self-defined IIC protocol for BH1750 (i2c.c/.h) //
// 2. Using Timer2 to generate PWM signal for servo motor //
//*******************************************************************************************//
// Declartion of BH1750 variable
volatile unsigned char rdata_h=0;
volatile unsigned char rdata_l=0;
volatile unsigned char receive;
volatile int count=0;
volatile unsigned char tracking=0;
volatile unsigned char check_ti=0;
volatile unsigned char check_ri=0;
volatile float sunlight;
// Declartion of servo motor variable
sbit pwm_out=P1^0;
volatile unsigned char pin_state;
volatile unsigned long duty_cycle;
void delay_2s(void){
#pragma asm
delay_2:
MOV R5,#100
aaaa:
MOV R6,#0FFH
bbbb:
MOV R7,#0FFH
cccc:
DJNZ R7,cccc
DJNZ R6,bbbb
DJNZ R5,aaaa
RET
#pragma endasm
}
void TIMER_INIT(){
//setting timer 2 as PWM
T2CON=0x00; // auto-reload, timer mode, TR2=0
RCAP2H=TH2=(unsigned char)((65536-duty_cycle)>>8);
RCAP2L=TL2=(unsigned char)((65536-duty_cycle) & 0xFF);
//setting timer 1 this is uart baudrate
TMOD=0x21;
TH1=217;
TL1=217;
//setting uart
SCON=0x50;
AUXR2|=0x40;
//setting timer 0 this is how long enter the interrupt
TH0=0x15;
TL0=0x9f; //60ms enter interrupt
TF0=0;
TR0=1;
TR1=1;
PS=1;
ET0=1;
ES=1;
ET2=1;
EA=1;
}
void PWM_INIT(){
duty_cycle=1500;
P1M1 |= 0x01;
P1M0 &= ~(0x01);
pwm_out=1;
pin_state=1;
TR2=1;
}
void CLEAR_GY302_LASTDATA(){
START();
WRITE_ADDRESS(35,WRITE);
WRITE_OPECODE(1);
STOP();
delay_2_4_us();
START();
WRITE_ADDRESS(35,WRITE);
WRITE_OPECODE(7);
STOP();
delay_120ms();
}
void GET_GY302(){
START();
WRITE_ADDRESS(35,WRITE);
WRITE_OPECODE(1);
STOP();
delay_2_4_us();
START();
WRITE_ADDRESS(35,WRITE);
WRITE_OPECODE(32);
STOP();
delay_120ms();
START();
WRITE_ADDRESS(35,READ);
READ_DATA(&rdata_h,&rdata_l);
STOP();
}
void SERVO(unsigned char angle){
unsigned long temp;
if(angle>180) angle=180;
temp=((unsigned long)angle*100+4)/9 +500;
EA=0;
duty_cycle=temp;
EA=1;
}
void SERVO_INITPOS(unsigned char angle){
SERVO(angle);
delay_2s();
}
void TRACKINGv2() {
float sunlight_pre = 0; // save the previous sunlight data
static int angle_per_step = 90; // servo motor initial position (unit:degree)
static int direction = 1; // rotating direction for decision strategy
int step=2; // rotating angle per step
GET_GY302();
sunlight = (rdata_h * 256 + rdata_l) / 1.2;
sunlight_pre = sunlight;
while (sunlight < threshold) {
angle_per_step += (step*direction);
// to avoid the rotating angle out of the range
if (angle_per_step > 180) { angle_per_step = 180; direction = -1; }
if (angle_per_step < 0) { angle_per_step = 0; direction = 1; }
SERVO((unsigned char)angle_per_step);
delay_120ms();
// update the sensor data and send to user by serial port
rdata_h = 0; rdata_l = 0;
GET_GY302();
sunlight = (rdata_h * 256 + rdata_l) / 1.2;
SBUF=rdata_h;
delay_120ms();
SBUF=rdata_l;
delay_120ms();
// the decision for rotating direction
if (sunlight < sunlight_pre && (sunlight_pre-sunlight)>3.0) {
direction = -direction;
}
// update sunlight_pre
sunlight_pre = sunlight;
if (sunlight >= threshold) break;
delay_120ms();
}
}
void main(){
PORT_INIT();
TIMER_INIT();
PWM_INIT();
SERVO_INITPOS(90);
while(1){
if(tracking==1){
tracking=0;
TRACKINGv2();
}
}
}
void timer0() interrupt 1{
TH0=0xD8; // every 10000us(10ms, 0.01sec) enter this ISR
TL0=0xEF;
count++;
if(count==1000){
count=0;
tracking=1;
}
}
void uart() interrupt 4{
if(TI==1){
TI=0;
check_ti=1;
}
else {
RI=0;
check_ri=1;
receive=SBUF;
}
}
void timer2() interrupt 5{
TF2=0;
if(pin_state==1){
pin_state=0;
pwm_out=0;
TH2=(unsigned char)((65536-duty_cycle)>>8);
TL2=(unsigned char)((65536-duty_cycle) & 0xFF);
}else{
pin_state=1;
pwm_out=1;
TH2=(unsigned char)((65536-duty_cycle)>>8);
TL2=(unsigned char)((65536-duty_cycle) & 0xFF);
}
}