Quantcast
Channel: Objective Development Forums
Viewing all articles
Browse latest Browse all 4524

Re: multiple ADC channels

$
0
0
ulao wrote:here is how I do it.

//voltage reader.
#define ADC_VREF_TYPE 0x40
static int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
_delay_us(150);
ADCSRA|=0x40; // Start the AD conversion
while ((ADCSRA & 0x10)==0);// Wait for complete
ADCSRA|=0x10;
return ADCW;
}

reportBuffer[1]= (read_adc(0));//pc0
reportBuffer[2]= (read_adc(1))*-1;//pc1

I can not help you with noise that is ore then likely your issue.


i have added the code and now its not reporting anything at all. :(
the device wil disconect and reconnect when i add the ADC interrupt.


i have added these lines:
declaration of reportBuffer
static uchar    reportBuffer[8];

added ADC setup to the hardwareInit.
   
        ADCSRA |= ((1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0));
   ADMUX |= 1<<ADLAR;
   ADCSRA |= (1<<ADEN);

then comes the read_adc

static int read_adc(unsigned char adc_input)
{
   ADMUX=adc_input|ADC_VREF_TYPE;
   _delay_us(150);
   ADCSRA|=0x40; // Start the AD conversion
   while ((ADCSRA & 0x10)==0);// Wait for complete
   ADCSRA|=0x10;
   return ADCW;

}


and last i have the input.
unsigned char input()
{
   reportBuffer[1]= (read_adc(0));//pc0
   reportBuffer[2]= (read_adc(1))*-1;//pc1
}


complete code.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <stdlib.h>


#define F_CPU 20000000L
#include <util/delay.h>
#include "usbdrv.h"
#include "oddebug.h"
//voltage reader.
#define ADC_VREF_TYPE 1<<REFS0 //0x40


static int read_adc(unsigned char adc_input);

const PROGMEM char usbHidReportDescriptor[77] = {
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x15, 0x00,                    // LOGICAL_MINIMUM (0)
    0x09, 0x04,                    // USAGE (Joystick)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x05, 0x02,                    //   USAGE_PAGE (Simulation Controls)
    0x09, 0xbb,                    //   USAGE (Throttle)
    0x15, 0x81,                    //   LOGICAL_MINIMUM (-127)
    0x25, 0x7f,                    //   LOGICAL_MAXIMUM (127)
    0x75, 0x08,                    //   REPORT_SIZE (8)
    0x95, 0x01,                    //   REPORT_COUNT (1)
    0x81, 0x02,                    //   INPUT (Data,Var,Abs)
    0x05, 0x01,                    //   USAGE_PAGE (Generic Desktop)
    0x09, 0x01,                    //   USAGE (Pointer)
    0xa1, 0x00,                    //   COLLECTION (Physical)
    0x09, 0x30,                    //     USAGE (X)
    0x09, 0x31,                    //     USAGE (Y)
    0x95, 0x02,                    //     REPORT_COUNT (2)
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
    0xc0,                          //   END_COLLECTION
    0x09, 0x39,                    //   USAGE (Hat switch)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x25, 0x03,                    //   LOGICAL_MAXIMUM (3)
    0x35, 0x00,                    //   PHYSICAL_MINIMUM (0)
    0x46, 0x0e, 0x01,              //   PHYSICAL_MAXIMUM (270)
    0x65, 0x14,                    //   UNIT (Eng Rot:Angular Pos)
    0x75, 0x04,                    //   REPORT_SIZE (4)
    0x95, 0x01,                    //   REPORT_COUNT (1)
    0x81, 0x02,                    //   INPUT (Data,Var,Abs)
    0x05, 0x09,                    //   USAGE_PAGE (Button)
    0x19, 0x01,                    //   USAGE_MINIMUM (Button 1)
    0x29, 0x04,                    //   USAGE_MAXIMUM (Button 4)
    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
    0x75, 0x01,                    //   REPORT_SIZE (1)
    0x95, 0x04,                    //   REPORT_COUNT (4)
    0x55, 0x00,                    //   UNIT_EXPONENT (0)
    0x65, 0x00,                    //   UNIT (None)
    0x81, 0x02,                    //   INPUT (Data,Var,Abs)
    0xc0                           // END_COLLECTION
};


static uchar    reportBuffer[8];
static uchar    idleRate;


usbMsgLen_t usbFunctionSetup(uint8_t data[8])
{
   usbRequest_t    *rq = (void *)data;

   
   if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){    /* class request type */
      if(rq->bRequest == USBRQ_HID_GET_REPORT){  /* wValue: ReportType (highbyte), ReportID (lowbyte) */
         usbMsgPtr = (void *)&reportBuffer;
         return sizeof(reportBuffer);
            
            }
            else if(rq->bRequest == USBRQ_HID_GET_IDLE){
            usbMsgPtr = &idleRate;
            return 1;
            }      
            else if(rq->bRequest == USBRQ_HID_SET_IDLE){
            idleRate = rq->wValue.bytes[1];
            }
         }
    else{
        /* no vendor specific requests implemented */
    }
   return 0;
}



static void hardwareInit(void)
{   
   PORTB = 0xff;   /* activate all pull-ups */
   DDRB = 0;       /* all pins input */
   DDRC = 0;       /* all pins input */
   PORTD = 0xfa;   /* 1111 1010 bin: activate pull-ups except on USB lines */   
   

   
   uchar   i;
   wdt_enable(WDTO_1S);
   usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
   
   i = 0;
    while(--i){             /* fake USB disconnect for > 250 ms */
        wdt_reset();
        _delay_ms(1);
    }
   
   usbDeviceConnect();
   
   ADCSRA |= ((1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0));
   ADMUX |= 1<<ADLAR;
   ADCSRA |= (1<<ADEN);

   
}

static int read_adc(unsigned char adc_input)
{
   ADMUX=adc_input|ADC_VREF_TYPE;
   _delay_us(150);
   ADCSRA|=0x40; // Start the AD conversion
   while ((ADCSRA & 0x10)==0);// Wait for complete
   ADCSRA|=0x10;
   return ADCW;

}

   


unsigned char input()
{
   reportBuffer[1]= (read_adc(0));//pc0
   reportBuffer[2]= (read_adc(1))*-1;//pc1
   reportBuffer[3]= 128;
}

int main(void)
{

   hardwareInit();
   usbInit();   
   sei();

   
   
   for(;;)
   {
      wdt_reset();
      usbPoll();
      input();
      
      if(usbInterruptIsReady()){
            /* called after every poll of the interrupt endpoint */
         
            usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
        }
      
   }
}

Viewing all articles
Browse latest Browse all 4524

Trending Articles