Rev Author Line No. Line
504 kaklik 1 #include ".\cidla.h"
2 //#include <stdlib.h>
3  
4 #use rs232(baud=9600,parity=N,xmit=PIN_B3,bits=8,restart_wdt)
5  
6 #define IRRX PIN_B0
7  
8 #define TRESHOLD_MAX 70 // rozhodovaci uroven pro cidla cerna/bila
9 #define TRESHOLD_MIN 50
10 #define CIHLA 10 // doba, po kterou musi byt detekovana cihla
11  
12 unsigned int8 last_radius; // rozsah
13 unsigned int8 last_cidla; // co cidla videla minule
14 unsigned int8 shure; // citac doby, po kterou musi byt detekovana cihla
15  
16 //tuning
17 /*#define PULZACE 3 // urcuje rychlost pulzovani pomoci PWM
18  
19 //Vystup PWM je na PIN_B3
20 ////////////////////////////////////////////////////////////////////////////////
21 void pulzovani() // postupne rozsvecuje a zhasina podsvetleni
22 {
23 unsigned int8 i,n;
24 for(n=0;n<=3;n++)
25 {
26 for(i=0;i<255;i++) {set_pwm1_duty(i); Delay_ms(PULZACE);} // rozsvecovani
27 for(i=255;i>0;i--) {set_pwm1_duty(i); Delay_ms(PULZACE);} // zhasinani
28 }
29 }
30 */
31 ////////////////////////////////////////////////////////////////////////////////
32 void main()
33 {
34 int8 cidla;
35 unsigned int8 a;
36 unsigned int8 n;
37  
38 setup_adc_ports(sAN0|sAN1|sAN2|sAN3|sAN4|sAN5|sAN6|VSS_VDD);
39 setup_adc(ADC_CLOCK_INTERNAL);
40 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
41 setup_timer_1(T1_DISABLED);
42 setup_timer_2(T2_DISABLED,0,1);
43 setup_comparator(NC_NC_NC_NC);
44 setup_vref(FALSE);
45  
46 Delay_ms(500);
47 setup_spi(SPI_SLAVE|SPI_H_TO_L|SPI_SS_DISABLED);
48  
49 // diagnostika
50 printf("\n\r");
51 Delay_ms(100);
52 printf("***\n\r");
53 Delay_ms(100);
54 for (n=0; n<=6; n++)
55 {
56 set_adc_channel(n);
57 Delay_ms(100);
58 a=read_adc();
59 printf("sensor %u - %u\n\r",n,a);
60 }
61  
62 shure=0;
63 while(true)
64 {
65 set_adc_channel(0);
66 cidla=0;
67 Delay_us(10);
68 a=read_adc();
69  
70 set_adc_channel(1);
71 if(a<TRESHOLD_MAX) //hystereze cidel
72 {
73 if(a>TRESHOLD_MIN)
74 {
75 cidla |= (last_cidla & 0b00000001);
76 }
77 else cidla |= 0b00000001;
78 }
79  
80 a=read_adc();
81  
82 set_adc_channel(2);
83 if(a<TRESHOLD_MAX)
84 {
85 if(a>TRESHOLD_MIN)
86 {
87 cidla |= (last_cidla & 0b00000010);
88 }
89 else cidla |= 0b00000010;
90 }
91  
92 a=read_adc();
93  
94 set_adc_channel(3);
95 if(a<TRESHOLD_MAX)
96 {
97 if(a>TRESHOLD_MIN)
98 {
99 cidla |= (last_cidla & 0b00000100);
100 }
101 else cidla |= 0b00000100;
102 }
103  
104 a=read_adc();
105  
106 set_adc_channel(4);
107 if(a<TRESHOLD_MAX)
108 {
109 if(a>TRESHOLD_MIN)
110 {
111 cidla |= (last_cidla & 0b00001000);
112 }
113 else cidla |= 0b00001000;
114 }
115 a=read_adc();
116  
117 set_adc_channel(5);
118  
119 if(a<TRESHOLD_MAX)
120 {
121 if(a>TRESHOLD_MIN)
122 {
123 cidla |= (last_cidla & 0b00010000);
124 }
125 else cidla |= 0b00010000;
126 }
127 a=read_adc();
128  
129 set_adc_channel(6);
130 if(a<TRESHOLD_MAX)
131 {
132 if(a>TRESHOLD_MIN)
133 {
134 cidla |= (last_cidla & 0b00100000);
135 }
136 else cidla |= 0b00100000;
137 }
138 a=read_adc();
139  
140 if(a<TRESHOLD_MAX)
141 {
142 if(a>TRESHOLD_MIN)
143 {
144 cidla |=(last_cidla & 0b01000000);
145 }
146 else cidla |= 0b01000000;
147 }
148  
149 last_cidla=cidla;
150  
151 if (!input(IRRX)) {if (shure<255) shure++;} else {shure=0;};
152 if (shure>CIHLA) cidla|=0b10000000;
153  
154 cidla=~cidla;
155 spi_write(cidla);
156 }
157 }