Interrupt driven Adc and vusb in atmega8
Posted: Fri Jun 29, 2012 1:45 pm
I am trying to make usb based oscilloscope.USB startups and data transfers work well.However on starting adc channel it throws an usb error "-5".
and the code works sometimes too..
The code is:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include "usbdrv.h"
#include "oddebug.h"
#define START_ADC 0
#define STOP_ADC 1
#define READ_VAL 2
static int8_t j=0,k;
static uchar replyBuf[128];
USB_PUBLIC uchar usbFunctionSetup(uchar data[8]) {
usbRequest_t *rq = (void *)data; // cast data to correct type
switch(rq->bRequest) {
case START_ADC:
ADCSRA|=((1<<ADEN)|(1<<6));
return 0;
case STOP_ADC:
ADCSRA&=(0<<ADEN);
return 0;
case READ_VAL:
usbMsgPtr=replyBuf;
k=j;
j=0;
return k;
}
return 0;
}
int __attribute__((noreturn)) main(void) {
uchar i;
wdt_enable(WDTO_1S); // enable 1s watchdog timer
odDebugInit();
DBG1(0x00, 0, 0);
usbInit();
usbDeviceDisconnect(); // enforce re-enumeration
i=0;
while(--i){ /* fake USB disconnect for > 250 ms */
wdt_reset();
_delay_ms(1);
}
usbDeviceConnect();
sei(); // Enable interrupts after re-enumeration
DBG1(0x01, 0, 0);
ADCSRA=0x2f;
ADMUX=0x40;
while(1) {
DBG1(0x02, 0, 0);
wdt_reset();
usbPoll();
}
}
ISR(ADC_vect,ISR_NOBLOCK)
{
replyBuf[j]=ADCL;
replyBuf[++j]=ADCH;
j++;
}
Please help...... I am using 16Mhz crystall.
and the code works sometimes too..
The code is:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include "usbdrv.h"
#include "oddebug.h"
#define START_ADC 0
#define STOP_ADC 1
#define READ_VAL 2
static int8_t j=0,k;
static uchar replyBuf[128];
USB_PUBLIC uchar usbFunctionSetup(uchar data[8]) {
usbRequest_t *rq = (void *)data; // cast data to correct type
switch(rq->bRequest) {
case START_ADC:
ADCSRA|=((1<<ADEN)|(1<<6));
return 0;
case STOP_ADC:
ADCSRA&=(0<<ADEN);
return 0;
case READ_VAL:
usbMsgPtr=replyBuf;
k=j;
j=0;
return k;
}
return 0;
}
int __attribute__((noreturn)) main(void) {
uchar i;
wdt_enable(WDTO_1S); // enable 1s watchdog timer
odDebugInit();
DBG1(0x00, 0, 0);
usbInit();
usbDeviceDisconnect(); // enforce re-enumeration
i=0;
while(--i){ /* fake USB disconnect for > 250 ms */
wdt_reset();
_delay_ms(1);
}
usbDeviceConnect();
sei(); // Enable interrupts after re-enumeration
DBG1(0x01, 0, 0);
ADCSRA=0x2f;
ADMUX=0x40;
while(1) {
DBG1(0x02, 0, 0);
wdt_reset();
usbPoll();
}
}
ISR(ADC_vect,ISR_NOBLOCK)
{
replyBuf[j]=ADCL;
replyBuf[++j]=ADCH;
j++;
}
Please help...... I am using 16Mhz crystall.