Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F4550 USB problem

Status
Not open for further replies.

fuuton

Advanced Member level 4
Joined
Jul 21, 2010
Messages
104
Helped
9
Reputation
18
Reaction score
2
Trophy points
1,298
Location
Pakistan, Rawalpindi
Activity points
1,881
Hi all,

I hope that you are doing well.

I am facing some problems with the USB code. The code needs to send some data from PIC18F4550 to PC (VB.net 2008 application).

The problem is that the hardware continuously transmits all the data (sequentially) that I have to transmit inside if conditions.

For Example:

I have made a condition that the hardware should send 0x01 to the PC if I press a certain button (RA0_bit). But when I plug the hardware, the condition is bypassed and the data (0x01) is sent continuously regardless of the fact whether I have applied the voltage on RA0_bit or not.

Please help me out. I will be really thankful.

Code:
#include "lcdConfig.h"

unsigned char readbuff[2] absolute 0x500;   // Buffers should be in USB RAM, please consult datasheet
unsigned char writebuff[2] absolute 0x540;

char cnt;

unsigned int i = 0;
unsigned int adcResult1 = 0;
unsigned int adcResult2 = 0;
unsigned int adcResult3 = 0;
unsigned int adcResult4 = 0;
unsigned char  adcResult5 = 0;
unsigned int adcResult1_new = 0;
unsigned int adcResult2_new = 0;
unsigned int adcResult3_new = 0;
unsigned int adcResult4_new = 0;
unsigned int adcResult5_new = 0;

void interrupt()
{
     if(USBIF_bit)
     {
       USBIF_bit = 0;
       USB_Interrupt_Proc();             // USB servicing is done inside the interrupt
     }
}

void adcInit(void) {
    ADCON1  = 0X0A;
    ADCON0  = 0X10;        // select channel 4      (note: I changed it to 0x0F to check if it for LCD)
    ADCON2.ADFM  = 1;           // AD result :: right justified
    ADCON2       = 0b10001010;  // Tacq = 2 Tad; AD clock = Fosc / 32
    ADCON0.ADON  = 1;           // turn ON ADC
}


void init(){

    CMCON   = 0x07;
    TRISA   = 0xFF;
    TRISB   = 0x00;
    TRISD   = 0x00;
    TRISE   = 0;
    PORTD   = 0;
    PORTE   = 0;
    adcInit();
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Cmd(_LCD_CURSOR_OFF);
    Lcd_Out(1, 1, "Welcome hello 11");
    Delay_ms(1000);
}

void main(void){

  init();
  RB6_bit = 1;

  /*UART1_Init(9600);
  UART1_Write_Text("Welcome\r\n");
  Delay_ms(400);  */
  HID_Enable(&readbuff,&writebuff);       // Enable HID communication
  /*Delay_ms(100);
  UART1_Write_Text("HID Enabled\r\n");*/

  while(1) {

       Lcd_Cmd(_LCD_CLEAR);

       ADCON0.GO    = 1;
       while(ADCON0.DONE);

       adcResult1    = ADC_Get_Sample(0);
       adcResult2    = ADC_Get_Sample(1);
       adcResult3    = ADC_Get_Sample(2);
       adcResult4    = ADC_Get_Sample(3);
       adcResult5    = ADC_Get_Sample(4);

       if (adcResult1 != adcResult1_new){
          Lcd_Out(1,1,"the value is new 1");
          Delay_ms(200);
          writebuff[0] = adcResult1;
          writebuff[1] = 0x01;
          adcResult1_new = adcResult1;
          while(!HID_Write(&writebuff,2));
       }
       
       if (adcResult2 != adcResult2_new){
          Lcd_Out(1,1,"the value is new 2");
          Delay_ms(200);
          writebuff[0] = adcResult2;
          writebuff[1] = 0x02;
          adcResult2_new = adcResult2;
          while(!HID_Write(&writebuff,2));
       }
       
       if (adcResult3 != adcResult3_new){
          Lcd_Out(1,1,"the value is new 3");
          Delay_ms(200);
          writebuff[0] = adcResult3;
          writebuff[1] = 0x03;
          adcResult3_new = adcResult3;
          while(!HID_Write(&writebuff,2));
       }
       
       if (adcResult4 != adcResult4_new){
          Lcd_Out(1,1,"the value is new 4");
          Delay_ms(200);
          writebuff[0] = adcResult4;
          writebuff[1] = 0x04;
          adcResult4_new = adcResult4;
          while(!HID_Write(&writebuff,2));
       }
       
       if (adcResult5 != adcResult5_new){
          Lcd_Out(1,1,"the value is new 5");
          Delay_ms(200);
          writebuff[0] = adcResult5;
          writebuff[1] = 0x05;
          adcResult5_new = adcResult5;
          while(!HID_Write(&writebuff,2));
       }





       //while(!HID_Write(&writebuff,2));


     }
  }
 

I think you should try a more simple condition on the if structure, just for check... Because there are a lot of possibilities for (adcResult4 != adcResult4_new), as you can have noise in the adc read etc...

I suggest you to try for example:

Code:
     int test_Value =1;
     if (test_Value == 0){
          Lcd_Out(1,1,"Not Working");
          Delay_ms(200);
          writebuff[0] = 0xFF;
          writebuff[1] = 0xF1;
          while(!HID_Write(&writebuff,2));
       }

You will probably have to to change the condition of the if structure for something like

Code:
   unsigned int adm_error= 5; //  for example an error of +/- 4 on the reading
   ......

   if ((adcResult1 < (adcResult1_new  - adm_error ) ) || (adcResult1 > (adcResult1_new  + adm_error ) ) )
   {
          Lcd_Out(1,1,"the value is new 1");
          Delay_ms(200);
          writebuff[0] = adcResult1;
          writebuff[1] = 0x01;
          adcResult1_new = adcResult1;
          while(!HID_Write(&writebuff,2));
       }

this way you will create a "band pass" for the if... I'm saying this because you will only have the same exact value eventually... also be aware that the code I've posted does not takes in count the variable roll over..
 

Thanks for the post.

The simplest condition isnt working in the code that I posted. For example:

The same problem occurs, even when there is no applied voltage on the RD0 pin. The controller executes all the statements within the if statement regardless of the condition

Code:
#include "lcdConfig.h"

unsigned char readbuff[2] absolute 0x500;   // Buffers should be in USB RAM, please consult datasheet
unsigned char writebuff[2] absolute 0x540;

char cnt;

unsigned int i = 0;
unsigned int adcResult1 = 0;
unsigned int adcResult2 = 0;
unsigned int adcResult3 = 0;
unsigned int adcResult4 = 0;
unsigned char  adcResult5 = 0;
unsigned int adcResult1_new = 0;
unsigned int adcResult2_new = 0;
unsigned int adcResult3_new = 0;
unsigned int adcResult4_new = 0;
unsigned int adcResult5_new = 0;

void interrupt()
{
     if(USBIF_bit)
     {
       USBIF_bit = 0;
       USB_Interrupt_Proc();             // USB servicing is done inside the interrupt
     }
}

void adcInit(void) {
    ADCON1  = 0X0A;
    ADCON0  = 0X10;        // select channel 4      (note: I changed it to 0x0F to check if it for LCD)
    ADCON2.ADFM  = 1;           // AD result :: right justified
    ADCON2       = 0b10001010;  // Tacq = 2 Tad; AD clock = Fosc / 32
    ADCON0.ADON  = 1;           // turn ON ADC
}


void init(){

    CMCON   = 0x07;
    TRISA   = 0xFF;
    TRISB   = 0x00;
    TRISD   = 0xFF;
    TRISE   = 0;
    PORTD   = 0;
    PORTE   = 0;
    adcInit();
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Cmd(_LCD_CURSOR_OFF);
    Lcd_Out(1, 1, "Welcome hello 11");
    Delay_ms(1000);
}

void main(void){

  init();
  RB6_bit = 1;

  HID_Enable(&readbuff,&writebuff);       // Enable HID communication

  while(1) {

       Lcd_Cmd(_LCD_CLEAR);

       if (RD0_bit == 1){
          Lcd_Out(1,1,"the value is new 5");
          Delay_ms(200);
          writebuff[0] = 0x01;
          writebuff[1] = 0x05;
          while(!HID_Write(&writebuff,2));
       }





       //while(!HID_Write(&writebuff,2));


     }
  }
 

The same problem occurs, even when there is no applied voltage on the RD0 pin.

But is the pin connected to ground or do you left it unconnected?... you can't left it unconnected as it's values will be floating
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top