-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·185 lines (121 loc) · 2.28 KB
/
main.c
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
//
// main.c
// ColorSorter
//
// PIC-1
//
// Created by Frederik Vanggaard on 4/2/11.
// Copyright 2011 Drudoo Entertainment. All rights reserved.
//
#pragma config =0x3fdf
#pragma config &=0x3ff7
#pragma config &=0x3ffc
#include "int16CXX.h"
void timerDelay() ;
void longDelay();
void convertValAD();
void getColorInput();
void sendInputToSecondPic();
void getQuestion();
uns16 valAD;
uns8 color, needData, data;
void main()
{
TRISC=0;
TRISA=0;
TRISB=0;
PORTB=0;
PORTA=0;
PORTC=0;
TRISB.7=1;
TX9=0;
SPEN=1;
TRMT=0;
TX9D=0;
SYNC=0;
BRGH=1;
BRG16=1;
SPBRG= 103;
TXEN=1;
TRISA.0=1;
ANSEL.0=1;
ADCON0=0b10000000;
ADCON1=0x00;
while (1) {
getQuestion();
timerDelay(160);
}
}
uns8 modtagData() {
uns8 data;
data = 0;
CREN = 0;
CREN = 1;
while(!RCIF) {
}
data = RCREG;
RCIF = 0;
return data;
}
void sendData(uns8 ud) {
while(!TXIF) {
}
TXREG=ud;
TXIF=0;
}
void getQuestion() {
needData = modtagData();
if(needData) {
convertValAD();
getColorInput();
sendInputToSecondPic();
}
}
void convertValAD() {
ADCON0.0=1;
timerDelay(80);
ADCON0.1=1;
while(!ADCON0.1)
nop();
valAD = ADRESH*256+ADRESL;
}
void getColorInput() {
if((valAD > 369)&&(valAD <= 471)) {
color = 1 ;
}
if((valAD > 0)&&(valAD <= 368)) {
color = 2;
}
/*
if((valAD > 327)&&(valAD <= 389)) {
color = 3;
}
if((valAD > 122)&&(valAD <= 163)) {
color = 4;
}
*/
if((valAD > 472)&&(valAD <= 1023)) {
color = 5;
}
/*
if((valAD > 348)&&(valAD <= 399)) {
color = 6;
}
*/
}
void sendInputToSecondPic() {
sendData(color);
}
void timerDelay(uns16 n) {
INTCON.2=0;
OPTION_REG=2;
TMR0 = -125 ;
do {
do {
}
while(!INTCON.2);
TMR0=-125;
INTCON.2=0;
}
while(--n>0);
}