Subversion Repositories svnkaklik

Rev

Go to most recent revision | 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=110,parity=N,xmit=PIN_B1,rcv=PIN_B0,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          50    // 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
   shure=0;
52
 
53
   while(true)
54
   {
55
      set_adc_channel(0);
56
      cidla=0;
57
      Delay_us(10);
58
      a=read_adc();
59
 
60
      set_adc_channel(1);
61
      if(a<TRESHOLD_MAX)         //hystereze cidel
62
      {
63
         if(a>TRESHOLD_MIN)
64
         {
65
            cidla |= (last_cidla & 0b00000001);
66
         }
67
         else cidla |= 0b00000001;
68
      }
69
 
70
      a=read_adc();
71
 
72
      set_adc_channel(2);
73
      if(a<TRESHOLD_MAX)
74
      {
75
         if(a>TRESHOLD_MIN)
76
         {
77
            cidla |= (last_cidla & 0b00000010);
78
         }
79
         else cidla |= 0b00000010;
80
      }
81
 
82
      a=read_adc();
83
 
84
      set_adc_channel(3);
85
      if(a<TRESHOLD_MAX)
86
      {
87
         if(a>TRESHOLD_MIN)
88
         {
89
            cidla |= (last_cidla & 0b00000100);
90
         }
91
         else cidla |= 0b00000100;
92
      }
93
 
94
      a=read_adc();
95
 
96
      set_adc_channel(4);
97
      if(a<TRESHOLD_MAX)
98
      {
99
         if(a>TRESHOLD_MIN)
100
         {
101
            cidla |= (last_cidla & 0b00001000);
102
         }
103
         else cidla |= 0b00001000;
104
      }
105
      a=read_adc();
106
 
107
      set_adc_channel(5);
108
 
109
      if(a<TRESHOLD_MAX)
110
      {
111
         if(a>TRESHOLD_MIN)
112
         {
113
            cidla |= (last_cidla & 0b00010000);
114
         }
115
         else cidla |= 0b00010000;
116
      }
117
      a=read_adc();
118
 
119
      set_adc_channel(6);
120
      if(a<TRESHOLD_MAX)
121
      {
122
         if(a>TRESHOLD_MIN)
123
         {
124
            cidla |= (last_cidla & 0b00100000);
125
         }
126
         else cidla |= 0b00100000;
127
      }
128
      a=read_adc();
129
 
130
      if(a<TRESHOLD_MAX)
131
      {
132
         if(a>TRESHOLD_MIN)
133
         {
134
            cidla |=(last_cidla & 0b01000000);
135
         }
136
         else cidla |= 0b01000000;
137
      }
138
 
139
      last_cidla=cidla;
140
 
141
      if (!input(IRRX)) {if (shure<255) shure++;} else {shure=0;};
142
      if (shure>CIHLA) cidla|=0b10000000;
143
 
144
      cidla=~cidla;
145
      spi_write(cidla);
146
   }
147
}