Page 1 of 1

Usb Communication Problem

Posted: 19 Feb 2020 23:49
by doganmustafa
My first topic is deleted :evil: :twisted: and i still dont fix same annoying issue. I need help about this . I m working to fix it

Code: Select all

#define ChipSelect GPIOE_ODR.B3
char readbuff[64], writebuff[64];
char okunan=0;
signed int xi=0,yi=1,zi;

void kurulum()
{
   GPIO_Digital_Output(&GPIOE_ODR, _GPIO_PINMASK_3);
   ChipSelect=1;
  
   SPI1_Init();
   ChipSelect=0;
   SPI_Write(0x0F|0x80);  
   okunan=SPI1_Read(0);     
   ChipSelect=1;
       if(okunan==0x3F)
       {
        ChipSelect=0;
        SPI1_Write(0x20);     
        SPI1_Write(0x67);     
        ChipSelect=1;
       }

 }

 

 void ChipOku()
 {
  ChipSelect=0;
  SPI1_Write(0x27|0x80);  
  okunan=SPI1_Read(0);
  ChipSelect=1;

    if(okunan&1)
    {
     ChipSelect=0;
     SPI1_Write(0x28 | 0x80);
     i=SPI1_Read(0);
     ChipSelect=1;
     delay_us(10);
     ChipSelect=0;
     SPI1_Write(0x29 | 0x80);
     i=(SPI1_Read(0)<<8);
     ChipSelect=1;
    }

     if(okunan&2)
    {
     ChipSelect=0;
     SPI1_Write(0x2A | 0x80);
     yi=SPI1_Read(0);
     ChipSelect=1;
     delay_us(10);
     ChipSelect=0;
     SPI1_Write(0x2B | 0x80);
     yi=(SPI1_Read(0)<<8);
     ChipSelect=1;
    }

     if(okunan&4)
    {
     ChipSelect=0;
     SPI1_Write(0x2C | 0x80);
     zi=SPI1_Read(0);
     ChipSelect=1;
     delay_us(10);
     ChipSelect=0;
     SPI1_Write(0x2D | 0x80);
     zi=(SPI1_Read(0)<<8);
     ChipSelect=1;
    }
 }


void main()
{
    kurulum();
   HID_Enable(&readbuff, &writebuff);  
   do
   {
     ChipOku();

   writebuff[0]=xi;                          //
   writebuff[1]=yi;                                 // the problem is happening right here
   writebuff[2]=zi;                          //
   
   HID_Write(&writebuff, 64);
     Delay_ms(100);

   }
   while(1);
}

void USB_Kesmesi() iv IVT_INT_OTG_FS ics ICS_AUTO
{
  USB_Interrupt_Proc();
}

for example ;
xi=123 , it means writebuff[0] should be 123. But writebuff[0] = 0 , I checked it on hardware debug , again and again .

I also tried this codes below, and its working correctly

Code: Select all

int i =123;
writebuff[0]=i;
HID_Write(&writebuff, 64);
But however, its now working when i try to send xi,yi,zi datas, which is coming from accelerometer.

And please dont delete this post again i need help, i checked the forum , i cant find any issue similar like that
Im really confused :?:

Re: Usb Communication Problem

Posted: 20 Feb 2020 09:42
by AntiMember

Code: Select all

//char is 1 byte long, signed int is 2 byte long.....
#include <built_in.h>                           //include this (defines Hi, Lo)
#define ChipSelect GPIOE_ODR.B3
char readbuff[64], writebuff[64];
char okunan=0;
signed int xi=0,yi=1,zi;

void kurulum()
{
   GPIO_Digital_Output(&GPIOE_ODR, _GPIO_PINMASK_3);
   ChipSelect=1;
 
   SPI1_Init();
   ChipSelect=0;
   SPI_Write(0x0F|0x80); 
   okunan=SPI1_Read(0);     
   ChipSelect=1;
       if(okunan==0x3F)
       {
        ChipSelect=0;
        SPI1_Write(0x20);     
        SPI1_Write(0x67);     
        ChipSelect=1;
       }

 }

 

 void ChipOku()
 {
  ChipSelect=0;
  SPI1_Write(0x27|0x80); 
  okunan=SPI1_Read(0);
  ChipSelect=1;

    if(okunan&1)
    {
     ChipSelect=0;
     SPI1_Write(0x28 | 0x80);
     i=SPI1_Read(0);
     ChipSelect=1;
     delay_us(10);
     ChipSelect=0;
     SPI1_Write(0x29 | 0x80);
     xi=(SPI1_Read(0)<<8);         // i->xi - Fixed for remark stefan.filipovic :)
     ChipSelect=1;
    }

     if(okunan&2)
    {
     ChipSelect=0;
     SPI1_Write(0x2A | 0x80);
     yi=SPI1_Read(0);
     ChipSelect=1;
     delay_us(10);
     ChipSelect=0;
     SPI1_Write(0x2B | 0x80);
     yi=(SPI1_Read(0)<<8);
     ChipSelect=1;
    }

     if(okunan&4)
    {
     ChipSelect=0;
     SPI1_Write(0x2C | 0x80);
     zi=SPI1_Read(0);
     ChipSelect=1;
     delay_us(10);
     ChipSelect=0;
     SPI1_Write(0x2D | 0x80);
     zi=(SPI1_Read(0)<<8);
     ChipSelect=1;
    }
 }


void main()
{
    kurulum();
   HID_Enable(&readbuff, &writebuff); 
   do
   {
     ChipOku();

   writebuff[0]=Hi(xi);                     //write in Big endian
   writebuff[1]=Lo(xi);
   writebuff[2]=Hi(yi); 
   writebuff[3]=Lo(yi);
   writebuff[4]=Hi(zi); 
   writebuff[5]=Lo(zi);
   
   HID_Write(&writebuff, 64);
     Delay_ms(100);

   }
   while(1);
}

void USB_Kesmesi() iv IVT_INT_OTG_FS ics ICS_AUTO
{
  USB_Interrupt_Proc();
}

Re: Usb Communication Problem

Posted: 20 Feb 2020 21:00
by doganmustafa
Thank you im going to try and i will be post again about the result

Re: Usb Communication Problem

Posted: 24 Feb 2020 14:38
by stefan.filipovic
Hi,

Are you sure the value of xi should be 123?
I see nowhere in your code that you change the value of xi.

Kind regards,

Re: Usb Communication Problem

Posted: 05 Mar 2020 15:58
by doganmustafa
hi Stefan.
sorry for late reply i was really bussy
xi=123 was just an example

Code: Select all

 void ChipOku()
 {
  ChipSelect=0;
  SPI1_Write(0x27|0x80); 
  okunan=SPI1_Read(0);
  ChipSelect=1;

    if(okunan&1)
    {
     ChipSelect=0;
     SPI1_Write(0x28 | 0x80);
     xi=SPI1_Read(0);
     ChipSelect=1;
     delay_us(10);
     ChipSelect=0;
     SPI1_Write(0x29 | 0x80);
     xi=(SPI1_Read(0)<<8);                 // xi values . I missed it 
     ChipSelect=1;
    }
i found my problem was happening due to oversize .
Antimember's solution is also working well.
and also i create a new algoritm. the algorithm seperates each digits the value. But if you have any other advices about data sending. .im waiting in excitement your advices,