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.
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));
}
}