Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

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