Autor Tema: Lectura de posiciones y comunicación USB con PIC 18f4550  (Leído 1271 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado alvaro_salazar

  • PIC10
  • *
  • Mensajes: 1
Lectura de posiciones y comunicación USB con PIC 18f4550
« en: 15 de Febrero de 2013, 15:02:12 »
Hola que tal a todos, soy nuevo en el foro y nuevo en esto de la electrónica.

Tengo un proyecto donde necesito grabar solo 400 posiciones, separadas 10 ms cada una, de un encoder incremental en CCS e imprimirlas en el hyperterminal.

Aquí esta mi código, haber si alguien me puede indicar que estoy haciendo mal, por favor:

Código: CSS
  1. #include <18F4550.h>
  2. #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL4,CPUDIV1,VREGEN
  3. #use delay(clock=48000000)
  4.  
  5. #include "usb_cdc.h"
  6.  
  7. #byte porta=0xf80
  8. #byte portb=0xf81
  9. #byte portc=0xf82
  10. #byte portd=0xf83
  11. #byte porte=0Xf84
  12. #byte PIR1=0xf9E
  13. #byte INTCON= 0xff2
  14. #byte INTCON2= 0xff1
  15. #byte INTCON3= 0xff0
  16.  
  17. signed int16 posicion[400];
  18. signed int16 posicion_pendulo;
  19. unsigned int x=0;
  20. int end_flag=0;
  21.  
  22.  
  23. #INT_EXT1
  24. void encoder_pendulo()
  25. {
  26.   // bit_clear(INTCON,1);  
  27.    
  28.    if(bit_test(portb,2)) // Si hay entrada por el pin B2
  29.       {
  30.       posicion_pendulo++; // Aumenta posicion
  31.       }
  32.    else
  33.       {
  34.       posicion_pendulo--; // Decrementa posicion
  35.       }
  36. }
  37.  
  38. #INT_TIMER1
  39. void task_timer()
  40. {
  41.  
  42.   set_timer1(7000);
  43.    
  44.    posicion[x] = posicion_pendulo; // Guarda la posicion
  45.    if (x==399) // Si se cumple esto se encenderan ambos les (Azul y ambar)
  46.       {
  47.       x=0;
  48.       disable_interrupts (global);
  49.       end_flag=1;
  50.       }
  51.    else
  52.       {
  53.       x++; // Aumenta variable x
  54.       }
  55. }
  56.  
  57. void main(void)
  58. {
  59.    
  60.    set_tris_a(0b00000001);
  61.    set_tris_b(0b11000111);        
  62.    set_tris_c(0b00010000);        
  63.    set_tris_d(0b10000100);
  64.    set_tris_e(0b00000000);  
  65.  
  66.    portb=0x00;
  67.    portd=0X00;
  68.    portc=0x00;
  69.    portd=0x00;
  70.    porte=0x00;
  71.  
  72.    enable_interrupts(INT_TIMER1);
  73.    enable_interrupts (INT_EXT1);
  74.    enable_interrupts(GLOBAL);
  75.    setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);  
  76.    set_timer1(7000);
  77.    
  78.    while (end_flag==0);
  79.    output_high (PIN_D3); // Led Azul
  80.    Output_high (PIN_D4); // Led Ambar
  81.    
  82.    usb_cdc_init();
  83.    usb_init();
  84.  
  85.    while(!usb_cdc_connected()){}
  86.    do
  87.    {
  88.       usb_task();
  89.       if (usb_enumerated())
  90.       {
  91.          x=0;
  92.          for(x=0;x<=399;x++)
  93.          {
  94.             output_high (PIN_D6); //Led verde
  95.             printf(usb_cdc_putc, "%Ld\r\n",posicion[x]);
  96.          }
  97.        }      
  98.     } while (TRUE);
  99. }