Mọi người giúp em xíu,em đang cố gắng giao tiếp 18f4550 qua usb,nhưng thất bại liên tục,huhu,em đã chạy mô phỏng trên proteus thành công,manage của mycomputer đã thông báo nhận đc thiết bị,bây giờ em cắm thử trên testboard,cũng có khi nhận đc thông báo USBnot recognized,nhưng đa số không có thông báo ji,em dùng thạch anh 12Mhz và 2 tụ 12pF như trong khai báo chuơng trình,các chân nguồn và D+ và D- đã kiểm tra kĩ,em dùng tụ 1uF mắc vào chân 18Vusb không biết có sao không vì trên mạng thấy có mạch dùng 47uF,mạch thì dùng 470nF,em nge nói chân 18 Vusb diện áp phải là 3.3V,không biết có đúng không nữa,nhưng em đo thì chẳng có Vôn nào,không biết như vậy là sai phải không,em post 1 đoạn code,mọi người xem giúp có ji không ổn không?thanhks all
#include "usb_demo_bulk.h"
#include <pic18_usb.h>
#include <usb_desc_scope1.h>
#include <usb.c>
#include <lcd4bit.c>
void usb_debug_task(void)
{
static int8 last_connected;
static int8 last_enumerated;
int8 new_connected;
int8 new_enumerated;
new_connected=usb_attached();
new_enumerated=usb_enumerated();
if (new_connected && !last_connected)
printf("\r\n\nUSB connected, waiting for enumaration...");
if (!new_connected && last_connected)
printf("\r\n\nUSB disconnected, waiting for connection...");
if (new_enumerated && !last_enumerated)
printf("\r\n\nUSB enumerated by PC/HOST");
if (!new_enumerated && last_enumerated)
printf("\r\n\nUSB unenumerated by PC/HOST, waiting for enumeration...");
last_connected=new_connected;
last_enumerated=new_enumerated;
}
void main()
{
int8 out_data[3];
int8 in_data[64];
int8 i=0;
char c;
int8 send_timer=0;
int8 count=0;
setup();
usb_init_cs();
lcd_init();
while (TRUE)
{
usb_task();
usb_debug_task();
if(usb_enumerated())
{
if (!send_timer)
{
count++;
send_timer=250;
out_data[0]=100;
out_data[1]=111;
out_data[2]=233;
if (usb_put_packet(1, out_data, 3, USB_DTS_TOGGLE))
printf("\r\n<-- Sending 2 bytes: 0x%X", out_data[0]);
}
if (usb_kbhit(1))
{
usb_get_packet(1, in_data, 64);
lcd_putc("\f");i=0;
for(;i<32;i++)
{
if(i==15) lcd_putc("\n");
c=in_data[i];
lcd_putc(c);
}
//printf("\r\n--> Received data: %s",in_data);
}
send_timer--;
delay_ms(1);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <18F4550.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES EC_IO //External clock
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOMCLR //Master Clear pin enabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL3 // PLL PreScaler 3
#FUSES USBDIV
#FUSES VREGEN
#FUSES CPUDIV1
#FUSES HSPLL
#DEFINE USB_HID_DEVICE FALSE
#define USB_EP1_TX_ENABLE USB_ENABLE_BULK //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_RX_ENABLE USB_ENABLE_BULK //turn on EP1 for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE 64 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE 64 //size to allocate for the rx endpoint 1 buffer
#use delay(clock=12000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
void setup()
{
setup_adc_ports(AN0|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
}
#include "usb_demo_bulk.h"
#include <pic18_usb.h>
#include <usb_desc_scope1.h>
#include <usb.c>
#include <lcd4bit.c>
void usb_debug_task(void)
{
static int8 last_connected;
static int8 last_enumerated;
int8 new_connected;
int8 new_enumerated;
new_connected=usb_attached();
new_enumerated=usb_enumerated();
if (new_connected && !last_connected)
printf("\r\n\nUSB connected, waiting for enumaration...");
if (!new_connected && last_connected)
printf("\r\n\nUSB disconnected, waiting for connection...");
if (new_enumerated && !last_enumerated)
printf("\r\n\nUSB enumerated by PC/HOST");
if (!new_enumerated && last_enumerated)
printf("\r\n\nUSB unenumerated by PC/HOST, waiting for enumeration...");
last_connected=new_connected;
last_enumerated=new_enumerated;
}
void main()
{
int8 out_data[3];
int8 in_data[64];
int8 i=0;
char c;
int8 send_timer=0;
int8 count=0;
setup();
usb_init_cs();
lcd_init();
while (TRUE)
{
usb_task();
usb_debug_task();
if(usb_enumerated())
{
if (!send_timer)
{
count++;
send_timer=250;
out_data[0]=100;
out_data[1]=111;
out_data[2]=233;
if (usb_put_packet(1, out_data, 3, USB_DTS_TOGGLE))
printf("\r\n<-- Sending 2 bytes: 0x%X", out_data[0]);
}
if (usb_kbhit(1))
{
usb_get_packet(1, in_data, 64);
lcd_putc("\f");i=0;
for(;i<32;i++)
{
if(i==15) lcd_putc("\n");
c=in_data[i];
lcd_putc(c);
}
//printf("\r\n--> Received data: %s",in_data);
}
send_timer--;
delay_ms(1);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <18F4550.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES EC_IO //External clock
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOMCLR //Master Clear pin enabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL3 // PLL PreScaler 3
#FUSES USBDIV
#FUSES VREGEN
#FUSES CPUDIV1
#FUSES HSPLL
#DEFINE USB_HID_DEVICE FALSE
#define USB_EP1_TX_ENABLE USB_ENABLE_BULK //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_RX_ENABLE USB_ENABLE_BULK //turn on EP1 for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE 64 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE 64 //size to allocate for the rx endpoint 1 buffer
#use delay(clock=12000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
void setup()
{
setup_adc_ports(AN0|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
}
Comment