|
|
View previous topic :: View next topic |
Author |
Message |
rfjhh
Joined: 31 Mar 2011 Posts: 51 Location: Mexico
|
USB communication pc pic |
Posted: Wed May 01, 2019 8:27 pm |
|
|
Hi
I'm trying to connect a PIC18f4550 with a PC. I have several errors but I don't know why.
The code is as follows:
Code: |
#include<18f4550.h>
#device adc=10; //Conversor A/D de 10 bits
#use delay(clock=20000000) //Frecuencia del cristal oscilador externo
#fuses HSPLL, NOWDT, NOLVP, USBDIV, PLL5, CPUDIV1, VREGEN
#include <usb_cdc.h> // Biblioteca de control USB
#include <usb_desc_cdc.h>
void main()
{
int16 valor_digital; //Valor digital obtenido del conversor A/D
float valor_analogico; //Valor analógico medido en el conversor A/D
char recepcion; //Dato recibido del PC
int1 control; //falso o verdadero para mostrar tensión
setup_adc_ports(AN0); //Definición pin a utilizar como entrada analógica
setup_adc(ADC_CLOCK_INTERNAL);//Definición de la fuente de reloj para el A/D
set_adc_channel(0); //Selección del canal analógico
usb_cdc_init(); //Inicialización del modo CDC
usb_init(); //Inicialización del control del USB
for(;;)
{
usb_task(); //Detección de la conexión de dispositivo USB
//TRUE si dispositivo ha sido enumerado por el PC
if (usb_enumerated())
{
valor_digital=read_adc(); //Lectura del canal analógico
valor_analogico=5.0*valor_digital/1024.0;
if (usb_cdc_kbhit())//Si se ha recibido dato...
{
recepcion=usb_cdc_getc(); //lo lee
//si es caracter vacio(barra espaciadora)enciende o apaga visualización
if (recepcion==' ')
control=control+1; }
//Si control=1 envia a PC valor de tensión leido
if (control==1)
printf(usb_cdc_putc, "\f Voltage = %01.2fV", valor_analogico);
//sino deja de enviarlo y limpia la pantalla del hyperterminal
else
printf(usb_cdc_putc, "\f ");
delay_ms(300);
}
}
}
|
The errors are:
Code: |
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 596(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 598(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 600(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 601(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 603(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 604(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 606(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 607(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 609(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 610(4,26): Undefined identifier usb_ep1_tx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 638(6,39): Undefined identifier usb_ep2_rx_buffer
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 653(21,22): Undefined identifier USBIE
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 653(21,22): Undefined identifier USBIE
*** Error 12 "C:\Program Files (x86)\PICC\devices\usb_cdc.h" Line 668(23,24): Undefined identifier USBIE
|
May somebody help me?
Regards |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 01, 2019 9:16 pm |
|
|
What's your compiler version ? I don't get those errors with vs. 5.078.
I just get the usual re-entrancy warnings that are normal for the CCS
USB drivers:
Quote: | >>> Warning 203 "C:\Program Files (x86)\PICC\drivers\pic18_usb.c" Line 727(1,1): Condition always TRUE
>>> Warning 216 "PCH_test.c" Line 43(1,2): Interrupts disabled during call to prevent re-entrancy: (usb_token_reset)
>>> Warning 216 "PCH_test.c" Line 43(1,2): Interrupts disabled during call to prevent re-entrancy: (usb_cdc_get_discard)
>>> Warning 216 "PCH_test.c" Line 43(1,2): Interrupts disabled during call to prevent re-entrancy: (usb_cdc_flush_tx_buffer)
>>> Warning 216 "PCH_test.c" Line 43(1,2): Interrupts disabled during call to prevent re-entrancy: (usb_tbe)
>>> Warning 216 "PCH_test.c" Line 43(1,2): Interrupts disabled during call to prevent re-entrancy: (usb_cdc_init)
Memory usage: ROM=18% RAM=21% - 23%
0 Errors, 6 Warnings.
Build Successful.
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19497
|
|
Posted: Thu May 02, 2019 1:40 am |
|
|
Some comments inline:
Code: |
#include<18f4550.h>
#device adc=10; //Conversor A/D de 10 bits
#use delay(clock=20000000) //Frecuencia del cristal oscilador externo
#fuses HSPLL, NOWDT, NOLVP, USBDIV, PLL5, CPUDIV1, VREGEN
//The clock settings here are not possible.
//You are selecting HSPLL, which says that the CPU clock comes from the
//USB PLL. With CPUDIV1, assuming you have a 20MHz crystal (which
//PLL5 implies to feed the USB PLL with 4MHz), your CPU will be running
//at 48MHz.
/* Possible settings
#fuses HS, NOWDT, NOLVP, USBDIV, PLL5, CPUDIV1, VREGEN
#use delay(clock=20000000) //Frecuencia del cristal oscilador externo
//This feeds the CPU off the crystal
#fuses HSPLL, NOWDT, NOLVP, USBDIV, PLL5, CPUDIV1, VREGEN
#use delay(clock=48000000) //Frecuencia del cristal oscilador externo
//This feeds it from the PLL
*/
//Now comment here. Is the chip being powered from the USB?
//If not, USB _requires_ that you implement connection sense.
//Not 'optional', _required_. This needs to be setup before loading
//The USB drivers (#define PIN_USB_SENSE).
//Now your error suggests that the hardware layer (pic18_usb.h)
//is not being loaded. usb_cdc.h should automatically load this.
//Have checked back and even really old (V3) compilers load this
//So means your usb_cdc.h, has probably been modified and this
//is missing. If so, you need:
#include <pic18_usb.h>
#include <usb_cdc.h> // Biblioteca de control USB
#include <usb_desc_cdc.h>
void main()
{
int16 valor_digital; //Valor digital obtenido del conversor A/D
float valor_analogico; //Valor analógico medido en el conversor A/D
char recepcion; //Dato recibido del PC
int1 control; //falso o verdadero para mostrar tensión
setup_adc_ports(AN0); //Definición pin a utilizar como entrada analógica
//This is not a legal clock for the ADC at this frequency. It'll work
//but give poor results. This is a mistake that CCS also make in their
//examples....
setup_adc(ADC_CLOCK_INTERNAL);//Definición de la fuente de reloj para el A/D
set_adc_channel(0); //Selección del canal analógico
//usb_init, should be called before cdc_init.
usb_cdc_init(); //Inicialización del modo CDC
usb_init(); //Inicialización del control del USB
for(;;)
{
usb_task(); //Detección de la conexión de dispositivo USB
//TRUE si dispositivo ha sido enumerado por el PC
if (usb_enumerated())
{
valor_digital=read_adc(); //Lectura del canal analógico
valor_analogico=5.0*valor_digital/1024.0;
if (usb_cdc_kbhit())//Si se ha recibido dato...
{
recepcion=usb_cdc_getc(); //lo lee
//si es caracter vacio(barra espaciadora)enciende o apaga visualización
if (recepcion==' ')
control=control+1; }
//Si control=1 envia a PC valor de tensión leido
if (control==1)
printf(usb_cdc_putc, "\f Voltage = %01.2fV", valor_analogico);
//sino deja de enviarlo y limpia la pantalla del hyperterminal
else
printf(usb_cdc_putc, "\f ");
delay_ms(300);
}
}
}
|
There is also an issue that there is no delay in the loop, if there is not
a character received on the USB. The adc requires time to reacquire.
However the time involved in the maths done to convert the ADC reading
to a numeric value will probably be enough. |
|
|
rfjhh
Joined: 31 Mar 2011 Posts: 51 Location: Mexico
|
Compiler version |
Posted: Thu May 02, 2019 8:37 am |
|
|
I'm using version 5.015 |
|
|
rfjhh
Joined: 31 Mar 2011 Posts: 51 Location: Mexico
|
|
Posted: Thu May 02, 2019 9:09 am |
|
|
Yes, the crystal is 20MHz divided by 5 is 4MHz clock needed for 96 MHz PLL.
The chip is not powered from the USB. The power is supplied by VDD.
The pic18_usb.h file I think is included:
#if __USB_PIC_PERIF__
#if defined(__PCM__)
#include <pic16f_usb.h> //Microchip PIC16Fxxxx hardware layer for usb.c
#elif defined(__PCH__)
#include <pic18_usb.h> //Microchip 18Fxx5x hardware layer for usb.c
#else |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|