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