Autor Tema: PIC18F2550 Problema con LCD a 48MHz y USB  (Leído 1609 veces)

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

Desconectado kobukguille

  • PIC10
  • *
  • Mensajes: 6
PIC18F2550 Problema con LCD a 48MHz y USB
« en: 25 de Septiembre de 2015, 04:14:59 »
Buenas,

Por más que busco y leo más me pierdo. Tenia un programa que me funcionaba con 4MHz, al querer subir a 48MHz para intentar hacer conexión USB ha dejado de funcionar. Me imagino que será un problema de delays. Puede ser? Alguien me puede ayudar?

Ahí va mi código:
Código: [Seleccionar]
#include <18F2550.h>
#fuses   HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use     delay(clock=48000000)

//#define USB_HID_DEVICE     FALSE             //disabled HID
//#define USB_EP1_TX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for IN bulk/interrupt transfers
//#define USB_EP1_RX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for OUT bulk/interrupt transfers
//#define USB_EP1_TX_SIZE    1                 //size to allocate for the tx endpoint 1 buffer
//#define USB_EP1_RX_SIZE    1                 //size to allocate for the rx endpoint 1 buffer

#include <lcd.c>
//#include <pic18_usb.h>                       //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
//#include "pic18_WinUSB.h"                    //Descriptors and USB configuration
//#include <usb.c>                             //handles usb setup tokens and get descriptor reports

#define LCD_ENABLE_PIN  PIN_B0                                    
#define LCD_RS_PIN      PIN_B1                                    
#define LCD_RW_PIN      PIN_B2                                    
#define LCD_DATA4       PIN_B4                                    
#define LCD_DATA5       PIN_B5                                    
#define LCD_DATA6       PIN_B6                                    
#define LCD_DATA7       PIN_B7
#define led1     PIN_C0
#define led2     PIN_C1
#define led3     PIN_C2
#define ledG     PIN_A5
#define ledY     PIN_A4
#define pin_on   output_high
#define pin_off   output_low
#define STARTING PIN_A0                    
#define RESTART   PIN_A1                    
#define SET       PIN_A2
#define GO       PIN_A3
#define ACTUADORv PIN_C7
#define ACTUADORsa PIN_C6

char varG=0,varS=0,varM=0,i=0;
int16 crono=0;
//int8 iBuff[1];                            //Tamaño Buffer a utilizar/1byte
//int8 oBuff[1];                            //Tamaño Buffer a utilizar/1byte

#INT_TIMER0
void TIMER0_isr(void)
{
   set_timer0(65533);      //reset TMR0
   if ((varG==1)&(STARTING==1))
   {
      if(crono>10000){      //CRONO HASTA 10 SEGUNDOS
         crono=0;      //SI -> t=0
      }
      else{         //(*) NO -> incrementa t
         crono++;
      }
   }
}
  
void main(void)
{
   set_tris_a(0x0F);
   set_tris_b(0x00);
   set_tris_c(0x78);
   delay_cycles(5);

   pin_off(led1);                            //Apagar y encender LEDs
   pin_off(led2);
   pin_off(led3);
   pin_on(ledG);
   pin_off(ledY);
   pin_off(ACTUADORv);
   pin_off(ACTUADORsa);  

//   disable_interrupts(GLOBAL);
  
//   usb_init();
//   usb_task();
//   usb_wait_for_enumeration();
  
   enable_interrupts(INT_TIMER0);            //interrupcion TIMER0 activada
   setup_counters(RTCC_INTERNAL,RTCC_DIV_4); //configuracion interrupcion TMR0
   set_timer0(65533);                            //carga TMR0
   enable_interrupts(GLOBAL);                //activadas interrupciones
  
   lcd_init();                         // Inicia LCD
   delay_ms(500);                     // Retardo
   lcd_putc(" Are you ready!?\n");     // Saca texto
   lcd_putc("   let's go!    ");       // Saca texto
   delay_ms(1000);                     // Retardo
   lcd_putc("\f");                     // Limpia pantalla
   lcd_putc("Reaction Time\n");        // Saca texto
  
   while (TRUE)
   {
//      if(usb_enumerated())                   //Conexión USB establecida
//      {
         pin_on(ledY);
         if((varS==0)&(varM==0)&(SET==1))  // envia rutina 1 al actuador
         {
            delay_ms(100);
    varM=1;
    pin_on(led1);            
            i=10;
            while(!i==0)
            {  
               i--;
               pin_on(ACTUADORv);
       pin_on(ACTUADORsa);
               delay_ms(500);
               pin_off(ACTUADORv);
       pin_off(ACTUADORsa);
               delay_ms(500);
            }
//            aux=0;
         }
         if((varM==1)&(varS==0)&(SET==1))   //envia rutina 2 al actuador
         {
            delay_ms(100);
            varS=1;
    pin_on(led2);
            pin_on(ACTUADORv);
    pin_on(ACTUADORsa);
            delay_ms(1000);
    pin_off(ACTUADORsa);
         }
         if((varS==1)&(GO==1))     //envia rutina 3 y activa crono
         {
            delay_ms(100);
    varG=1;
    pin_on(led3);
            pin_off(ACTUADORv);
    pin_on(ACTUADORsa);
            delay_ms(300);
    pin_off(ACTUADORsa);
            while(STARTING==1){}
//            oBuff[0] = crono;
//            usb_put_packet(1, oBuff, 1, USB_DTS_TOGGLE); // Send 1 byte packet to EP 1
         }
         if((varG==1)&(STARTING==0))  // muestra crono LCD
         {
            lcd_gotoxy(11,2);                     // Acomoda cursor LCD
            printf(lcd_putc,"%5LU",crono);       // Imprime el conteo por la lcd
            delay_ms(20);
         }
         if(RESTART==1)      //reinicia sistema
         {
            varM=0;
            varS=0;
            varG=0;
    i=0;
            crono=0;
//      reset=0;
//          aux=0;
//          start=0;            
            pin_off(led1);
            pin_off(led2);
            pin_off(led3);
    pin_off(ACTUADORv);
    pin_on(ACTUADORsa);
            delay_ms(400);
    pin_off(ACTUADORsa);
    delay_ms(150);
    pin_on(ACTUADORsa);
    delay_ms(200);
    pin_off(ACTUADORsa);              
         }
//         if (usb_kbhit(1))                   //Verificar recepción de datos
//         {
//            pin_off(ledY);
//            usb_get_packet(1, iBuff, 1);     //Leer paquete de 1 byte EP 1
//            if (iBuff[0] == 1) aux=1;
//            if (iBuff[0] == 2) start=1;
//            if (iBuff[0] == 3) reset=1;
//         }
//      }
   }
}

Tengo comentada toda la parte del USB por ir descartando cosas. Que no fuera eso que hiciera que no funcionara. Pero no... El display LCD se muestra toda la primera linea negra, eso ya me habia pasado en otra ocasion por qüestión de delay però ahora no se porque és...

Muchas gràcias!
« Última modificación: 25 de Septiembre de 2015, 04:20:12 por kobukguille »

Desconectado kobukguille

  • PIC10
  • *
  • Mensajes: 6
Re: PIC18F2550 Problema con LCD a 48MHz y USB
« Respuesta #1 en: 05 de Octubre de 2015, 07:26:57 »
Conseguí arreglar en parte el código, ahora funciona todo menos el USB. El ordenador lo reconoce pero ahora falta que se comunique con mi programa en labview. Alguien controla un poco de labview que me pueda ayudar con el USB?

Os dejo el código que funciona:
Código: [Seleccionar]
#include <18F2550.h>
#fuses   HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use     delay(clock=48000000)

#define USB_HID_DEVICE     FALSE             //disabled HID
#define USB_EP1_TX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for IN bulk/interrupt transfers
#define USB_EP1_RX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE    1                 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE    1                 //size to allocate for the rx endpoint 1 buffer

#use standard_io(B)
#define use_portB_lcd TRUE
#include <lcd.c>
#include <pic18_usb.h>                       //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include "pic18_WinUSB.h"                    //Descriptors and USB configuration
#include <usb.c>                             //handles usb setup tokens and get descriptor reports

#define led1        PIN_C0
#define led2        PIN_C1
#define led3        PIN_C2
#define ledG        PIN_A4
#define ledY        PIN_A5
#define pin_on      output_high
#define pin_off     output_low
#define ACTUADORv   PIN_C7
#define ACTUADORsa  PIN_C6

char varG=0,varS=0,varM=0,i=0,t=0;
int16 crono=0;
//int8 iBuff[1];                            //Buffer a utilizar/1byte
//int8 oBuff[1];                            //Buffer a utilizar/1byte

#INT_TIMER0
void TIMER0_isr(void){
   set_timer0(65235);      //reset TMR0
   if ((varG==1)&(input(pin_A0)==1))
   {
      if(t>9){
         t=0;
         crono++;
         if(crono>10000){      //CRONO HASTA 10 SEGUNDOS
            crono=0;      //SI -> crono=0
         }
      }
      else{         //(*) NO -> incrementa crono
         t++;
      }
   }
}
  
void main(void)
{
   set_tris_a(0x0F);
   set_tris_c(0x78);
   delay_cycles(5);

   pin_off(led1);                            //Apagar y encender LEDs
   pin_off(led2);
   pin_off(led3);
   pin_on(ledG);
   pin_off(ledY);
   pin_off(ACTUADORv);
   pin_off(ACTUADORsa);
  
   disable_interrupts(GLOBAL);
  
   usb_init();
   usb_task();
   usb_wait_for_enumeration();
  
   enable_interrupts(INT_TIMER0);            //interrupcion TIMER0 activada
   setup_counters(RTCC_INTERNAL,RTCC_DIV_4); //configuracion interrupcion TMR0
   set_timer0(65235);                            //carga TMR0
   enable_interrupts(GLOBAL);                //activadas interrupciones
  
   lcd_init();                         // Inicia LCD
   delay_ms(100);                     // Retardo
   printf(lcd_putc, "AEDA chrono sys.\n");     // Saca texto
   printf(lcd_putc, " Wait please... ");       // Saca texto
   delay_ms(1000);                     // Retardo
   lcd_putc("\f");                     // Limpia pantalla
   lcd_putc("Reaction Time:\n");        // Saca texto
   lcd_putc("  Ready to go!  ");       // Saca texto
  
   while (TRUE)
   {
      if(usb_enumerated())                   //Conexión USB establecida
      {
                  pin_on(ledY);
         if((varS==0)&(varM==0)&(input(pin_A2)==1))  // envia rutina 1 al actuador
         {
            delay_ms(100);
            varM=1;
            lcd_gotoxy(0,2);                     // Acomoda cursor LCD
            lcd_putc("  ON YOUR MARKS");
            pin_on(led1);            
            i=10;
            while(!i==0)
            {  
               i--;
               pin_on(ACTUADORv);
               pin_on(ACTUADORsa);  
               delay_ms(500);
               pin_off(ACTUADORv);
               pin_off(ACTUADORsa);
               delay_ms(500);
            }
//            aux=0;
         }
         if((varM==1)&(varS==0)&(input(pin_A2)==1))   //envia rutina 2 al actuador
         {
            delay_ms(100);
            varS=1;
            lcd_gotoxy(0,2);                     // Acomoda cursor LCD
            lcd_putc("            SET");
            pin_on(led2);
            pin_on(ACTUADORv);
            pin_on(ACTUADORsa);  
            delay_ms(1000);
            pin_off(ACTUADORsa);
         }
         if((varS==1)&(input(pin_A3)==1))     //envia rutina 3 y activa crono
         {
            delay_ms(100);
            varG=1;
            lcd_gotoxy(0,2);                     // Acomoda cursor LCD
            lcd_putc("             GO");
            pin_on(led3);
            pin_off(ACTUADORv);
            pin_on(ACTUADORsa);  
            delay_ms(300);
            pin_off(ACTUADORsa);
            while(input(pin_A0)==1){}
//            oBuff[0] = crono;
//            usb_put_packet(1, oBuff, 1, USB_DTS_TOGGLE); // Send 1 byte packet to EP 1
         }
         if((varG==1)&(input(pin_A0)==0))  // muestra crono LCD
         {
            lcd_gotoxy(11,2);                     // Acomoda cursor LCD
            printf(lcd_putc,"%5LU",crono);       // Imprime el conteo por la lcd
            delay_ms(20);
         }
         if(input(pin_A1)==1)      //reinicia sistema
         {
            varM=0;
            varS=0;
            varG=0;
            i=0;
            crono=0;
//          reset=0;
//          aux=0;
//          start=0;
            lcd_putc("Reaction Time:\n");
            lcd_putc("  Ready to go!  ");          
            pin_off(led1);
            pin_off(led2);
            pin_off(led3);
            pin_off(ACTUADORv);
            pin_on(ACTUADORsa);  
            delay_ms(400);
            pin_off(ACTUADORsa);
            delay_ms(150);
            pin_on(ACTUADORsa);
            delay_ms(200);
            pin_off(ACTUADORsa);              
         }
         if (usb_kbhit(1))                   //Verificar recepción de datos
         {
            pin_off(ledY);
            //usb_get_packet(1, iBuff, 1);
            //if (iBuff[0] == 1) reset=1;
            //if (iBuff[0] == 2) start=1;
            //if (iBuff[0] == 3) aux=1;
         }
      }
   }
}