#Include #Fuses #Use #Use: Unsigned Int16 Short Unsigned Int16 Const Unsigned Int16 Const Unsigned Int

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

C:\Users\vmmartinez\Desktop\Microchip_Pic_ccs\atrapa peluche\ap1.

c
1: //-------configuracion microcontrolador-------------------------------------
2:
3: #include <16f877.h>
4: #fuses xt,nowdt,noput,nobrownout,nolvp,noprotect,nocpd,nowrt,nodebug
5: #use delay(clock=4000000)
6: #use fast_io(E)
7:
8: //--------definicion de macros---------------------------------------------
9:
10: #define xup !input(PIN_B0)
11: #define xdown !input(PIN_B1)
12: #define yup !input(PIN_B2)
13: #define ydown !input(PIN_B3)
14: #define zup !input(PIN_B4)
15: #define xhome input(PIN_B5)
16: #define yhome input(PIN_B6)
17: #define zhome input(PIN_B7)
18: #define start_on output_high(PIN_E0);
19: #define start_off output_low(PIN_E0);
20: #define bobina_on output_high(PIN_E2);
21: #define bobina_off output_low(PIN_E2);
22: #define toggle_start output_toggle(PIN_E0);
23: #define moneda input(PIN_E1)
24: #define pasos 4
25: #define credito 60
26: #define INTS_PER_SECOND 15 //(4000000/(4*256*256))
27: //--------declaracion variables y constantes globales----------------------
28:
29: unsigned int16 x=0,xs=0,y=0,ys=0,z=0,zs=0,i=0,int_count,ajtiempo;
30: short ck=0;
31: unsigned int16 segundos;
32: const unsigned int16 xlimit=850,ylimit=100,zlimit=100;
33: const unsigned int secuencia[4]={0b1010,0b0110,0b0101,0b1001};
34:
35: //-------primitivas de funciones-------------------------------------------
36:
37: void velocidad(void);
38: void home(void);
39: void avanzar_x(void);
40: void retroceder_x(void);
41: void avanzar_y(void);
42: void retroceder_y(void);
43: void avanzar_z(void);
44: void retroceder_z(void);
45: void pinza(void);
46: //-----------interrupcion de tiempo----------------------------------------
47:
48: #INT_TIMER0
49: void segundero(void)
50: {
51: if(ck)
52: {
53: if(--int_count==0) // incrementa segundos cada 15 interrupciones
54: {
55: if(segundos>0)
56: segundos--;
57: toggle_start
58: int_count=INTS_PER_SECOND;
59: if(++ajtiempo==4) //cada 4 seg se aumenta 1 interrupcion
60: {
61: ++int_count; //para ajustar tiempo mas exacto
62: ajtiempo=0;
63: }
64: }
65: }
66: }
67:
68: //-----------programa principal---------------------------------------------
69: //----------inicializaciones-----------------------
70: void main(void)
71: {
72: setup_adc (ADC_OFF);
73: setup_adc_ports (NO_ANALOGS );
74: setup_psp(PSP_DISABLED);
75: setup_spi(FALSE);
76: setup_timer_1(T1_DISABLED);
77: setup_timer_2(T2_DISABLED,0,1);
78: disable_interrupts(INT_RDA);
79: set_tris_a(0);
80: output_a(0);
81: set_tris_b(0xff);
82: port_b_pullups(TRUE);

1
C:\Users\vmmartinez\Desktop\Microchip_Pic_ccs\atrapa peluche\ap1.c
83: set_tris_c(0);
84: output_c(0);
85: set_tris_d(0);
86: output_d(0);
87: set_tris_e(2); //RE1 IN
88: output_e(0);
89: int_count=INTS_PER_SECOND;
90: segundos=0;
91: disable_interrupts(INT_RB);
92: enable_interrupts(INT_TIMER0);
93: enable_interrupts(GLOBAL);
94: setup_timer_0 ( RTCC_DIV_256|RTCC_INTERNAL|RTCC_8_BIT);
95:
96: //----------ciclo principal----------------------
97: home();
98:
99: while(true)
100: {
101:
102: if(segundos != 0)
103: {
104: ck=1;
105: if(xup)
106: avanzar_x();
107: if(xdown)
108: retroceder_x();
109: if(yup)
110: avanzar_y();
111: if(ydown)
112: retroceder_y();
113: if(zup)
114: pinza();
115: }
116: else
117: {ck=0; start_off segundos=0; retroceder_x(); retroceder_y(); retroceder_z(); }
118:
119: if(moneda)
120: segundos=segundos+credito; while(moneda);
121:
122: }//while true
123:
124: }//main
125:
126: //----definicion de funciones---------------------------------------------
127:
128: void velocidad_x(void) {delay_ms(4);}
129: void velocidad_y(void) {delay_ms(10);}
130: void velocidad_z(void) {delay_ms(10);}
131:
132: void home(void)
133: {
134: x=900; y=255; z=255;
135: do
136: {
137: if(xhome) retroceder_x(); else {x=0; xs=0; }
138: if(yhome) retroceder_y(); else {y=0; ys=0; }
139: if(zhome) retroceder_z(); else {z=0; zs=0; }
140: }
141: while(x!=0 | y!=0 | z!=0 );
142: }
143:
144: void avanzar_x(void)
145: {
146: if(x < xlimit)
147: {
148: if(xs < pasos-1)
149: xs++;
150: else
151: {xs=0; x++; }
152: output_a(secuencia[xs]);
153: velocidad_x();
154: }
155: }
156:
157: void retroceder_x(void)
158: {
159: if( x != 0 | xs != 0)
160: {
161: if(xs == 0)
162: {if(x!=0){xs=3; x--;}}
163: else
164: xs--;

2
C:\Users\vmmartinez\Desktop\Microchip_Pic_ccs\atrapa peluche\ap1.c
165: output_a(secuencia[xs]);
166: velocidad_x();
167: }
168: }
169:
170: void avanzar_y(void)
171: {
172: if(y < ylimit)
173: {
174: if(ys < pasos-1)
175: ys++;
176: else
177: {ys=0; y++; }
178: output_c(secuencia[ys]);
179: velocidad_y();
180: }
181: }
182:
183: void retroceder_y(void)
184: {
185: if( y != 0 | ys != 0)
186: {
187: if(ys == 0)
188: {if(y!=0){ys=3; y--;}}
189: else
190: ys--;
191: output_c(secuencia[ys]);
192: velocidad_y();
193: }
194: }
195:
196:
197: void avanzar_z(void)
198: {
199: if(z < zlimit)
200: {
201: if(zs < pasos-1)
202: zs++;
203: else
204: {zs=0; z++; }
205: output_d(secuencia[zs]);
206: velocidad_z();
207: }
208: }
209:
210: void retroceder_z(void)
211: {
212: if( z != 0 | zs != 0)
213: {
214: if(zs == 0)
215: {if(z!=0){zs=3; z--;}}
216: else
217: zs--;
218: output_d(secuencia[zs]);
219: velocidad_z();
220: }
221: }
222:
223:
224: void pinza (void)
225: {
226:
227: for(i=0;i<zlimit*4;i++)
228: {
229: bobina_off
230: avanzar_z();
231: }
232:
233: for(i=zlimit*4;i!=0;i--)
234: {
235: bobina_on
236: retroceder_z();
237: }
238:
239: }

You might also like