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.

[AVR] A Little Help with Coding AT90USB

Status
Not open for further replies.

decapitary

Banned
Member level 3
Joined
Jul 16, 2014
Messages
62
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
0
I want to send real-time data(ADC) to my PC with AT90USB646. I've used Codevision Wizard but it failed some times.anyOne can help me with the coding? and I used LabView to get results and that was terrible if you can suggest a better softwer ill appreciate it,

regards
 

Did you use LabView for simulating the connection between PC and AT90USB646?
Hint :Data communication between microcontroller and PC is possible.You need to have either a USB or a Ethernet cable with
RJ-45 type connector on both the sides,for transmitting and receiving data.
 
Did you use LabView for simulating the connection between PC and AT90USB646?
Hint :Data communication between microcontroller and PC is possible.You need to have either a USB or a Ethernet cable with
RJ-45 type connector on both the sides,for transmitting and receiving data.

can you help me with the code?how should I do it? I want real time data with no errors
 

help with coding to send data to USB PC port

I used this code but I need a little help to improve it. Help me if you can please I really need it :(

Code:
/*******************************************************
This program was created by the
CodeWizardAVR V3.12 Advanced
Automatic Program Generator
© Copyright 1998-2014 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : ESG
Version : 1
Date    : 11/19/2014
Author  : Farad
Company : 
Comments: 


Chip type               : AT90USB646
Program type            : Application
AVR Core Clock frequency: 16.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 1024
*******************************************************/

#include <90usb646.h>

#include <delay.h>

// USB Device functions
#include <usb_device.h>

// USB CDC Virtual Serial Port functions
#include <usb_cdc.h>

// USB initialization
#include "usb_init.h"

// Declare your global variables here

// Standard Input/Output functions
#include <stdio.h>

// Voltage Reference: AREF pin
#define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (1<<ADLAR))

// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCH;
}

void main(void)
{
// Declare your local variables here

// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) | (0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port A initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In 
DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1) | (0<<DDA0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T 
PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);

// Port B initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In 
DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T 
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);

// Port C initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In 
DDRC=(0<<DDC7) | (0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T 
PORTC=(0<<PORTC7) | (0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);

// Port D initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In 
DDRD=(0<<DDD7) | (0<<DDD6) | (0<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (0<<DDD1) | (0<<DDD0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T 
PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);

// Port E initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In 
DDRE=(0<<DDE7) | (0<<DDE6) | (0<<DDE5) | (0<<DDE4) | (0<<DDE3) | (0<<DDE2) | (0<<DDE1) | (0<<DDE0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T 
PORTE=(0<<PORTE7) | (0<<PORTE6) | (0<<PORTE5) | (0<<PORTE4) | (0<<PORTE3) | (0<<PORTE2) | (0<<PORTE1) | (0<<PORTE0);

// Port F initialization
// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In 
DDRF=(0<<DDF7) | (0<<DDF6) | (0<<DDF5) | (0<<DDF4) | (0<<DDF3) | (0<<DDF2) | (0<<DDF1) | (0<<DDF0);
// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T 
PORTF=(0<<PORTF7) | (0<<PORTF6) | (0<<PORTF5) | (0<<PORTF4) | (0<<PORTF3) | (0<<PORTF2) | (0<<PORTF1) | (0<<PORTF0);

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=(0<<COM0A1) | (0<<COM0A0) | (0<<COM0B1) | (0<<COM0B0) | (0<<WGM01) | (0<<WGM00);
TCCR0B=(0<<WGM02) | (0<<CS02) | (0<<CS01) | (0<<CS00);
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// OC1C output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<COM1C1) | (0<<COM1C0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) | (0<<CS10);
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2A output: Disconnected
// OC2B output: Disconnected
ASSR=(0<<EXCLK) | (0<<AS2);
TCCR2A=(0<<COM2A1) | (0<<COM2A0) | (0<<COM2B1) | (0<<COM2B0) | (0<<WGM21) | (0<<WGM20);
TCCR2B=(0<<WGM22) | (0<<CS22) | (0<<CS21) | (0<<CS20);
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;

// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer3 Stopped
// Mode: Normal top=0xFFFF
// OC3A output: Disconnected
// OC3B output: Disconnected
// OC3C output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=(0<<COM3A1) | (0<<COM3A0) | (0<<COM3B1) | (0<<COM3B0) | (0<<COM3C1) | (0<<COM3C0) | (0<<WGM31) | (0<<WGM30);
TCCR3B=(0<<ICNC3) | (0<<ICES3) | (0<<WGM33) | (0<<WGM32) | (0<<CS32) | (0<<CS31) | (0<<CS30);
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;

// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=(0<<OCIE0B) | (0<<OCIE0A) | (0<<TOIE0);

// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=(0<<ICIE1) | (0<<OCIE1C) | (0<<OCIE1B) | (0<<OCIE1A) | (0<<TOIE1);

// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=(0<<OCIE2B) | (0<<OCIE2A) | (0<<TOIE2);

// Timer/Counter 3 Interrupt(s) initialization
TIMSK3=(0<<ICIE3) | (0<<OCIE3C) | (0<<OCIE3B) | (0<<OCIE3A) | (0<<TOIE3);

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=(0<<ISC31) | (0<<ISC30) | (0<<ISC21) | (0<<ISC20) | (0<<ISC11) | (0<<ISC10) | (0<<ISC01) | (0<<ISC00);
EICRB=(0<<ISC71) | (0<<ISC70) | (0<<ISC61) | (0<<ISC60) | (0<<ISC51) | (0<<ISC50) | (0<<ISC41) | (0<<ISC40);
EIMSK=(0<<INT7) | (0<<INT6) | (0<<INT5) | (0<<INT4) | (0<<INT3) | (0<<INT2) | (0<<INT1) | (0<<INT0);
// PCINT0 interrupt: Off
// PCINT1 interrupt: Off
// PCINT2 interrupt: Off
// PCINT3 interrupt: Off
// PCINT4 interrupt: Off
// PCINT5 interrupt: Off
// PCINT6 interrupt: Off
// PCINT7 interrupt: Off
PCMSK0=(0<<PCINT7) | (0<<PCINT6) | (0<<PCINT5) | (0<<PCINT4) | (0<<PCINT3) | (0<<PCINT2) | (0<<PCINT1) | (0<<PCINT0);
PCICR=(0<<PCIE0);

// USART1 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud Rate: 1200 (Double Speed Mode)
UCSR1A=(0<<RXC1) | (0<<TXC1) | (0<<UDRE1) | (0<<FE1) | (0<<DOR1) | (0<<UPE1) | (1<<U2X1) | (0<<MPCM1);
UCSR1B=(0<<RXCIE1) | (0<<TXCIE1) | (0<<UDRIE1) | (1<<RXEN1) | (1<<TXEN1) | (0<<UCSZ12) | (0<<RXB81) | (0<<TXB81);
UCSR1C=(0<<UMSEL11) | (0<<UMSEL10) | (0<<UPM11) | (0<<UPM10) | (0<<USBS1) | (1<<UCSZ11) | (1<<UCSZ10) | (0<<UCPOL1);
UBRR1H=0x06;
UBRR1L=0x82;

// Analog Comparator initialization
// Analog Comparator: Off
// The Analog Comparator's positive input is
// connected to the AIN0 pin
// The Analog Comparator's negative input is
// connected to the AIN1 pin
ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIC) | (0<<ACIS1) | (0<<ACIS0);
// Digital input buffer on AIN0: On
// Digital input buffer on AIN1: On
DIDR1=(0<<AIN0D) | (0<<AIN1D);

// ADC initialization
// ADC Clock frequency: 500.000 kHz
// ADC Voltage Reference: AREF pin
// ADC High Speed Mode: On
// Only the 8 most significant bits of
// the AD conversion result are used
// Digital input buffers on ADC0: Off, ADC1: Off, ADC2: Off, ADC3: Off
// ADC4: Off, ADC5: Off, ADC6: Off, ADC7: Off
DIDR0=(1<<ADC7D) | (1<<ADC6D) | (1<<ADC5D) | (1<<ADC4D) | (1<<ADC3D) | (1<<ADC2D) | (1<<ADC1D) | (1<<ADC0D);
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) | (0<<ADPS1) | (1<<ADPS0);
ADCSRB=(1<<ADHSM);

// SPI initialization
// SPI disabled
SPCR=(0<<SPIE) | (0<<SPE) | (0<<DORD) | (0<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0);

// TWI initialization
// TWI disabled
TWCR=(0<<TWEA) | (0<<TWSTA) | (0<<TWSTO) | (0<<TWEN) | (0<<TWIE);

// USB Controller initialization in device mode
// Note: This function also initializes the PLL
usb_init_device(&usb_config);

// Global enable interrupts
#asm("sei")

// Wait for the USB device to be enumerated by the host
while (!usb_enumerated);

// Wait 1.5 seconds for the operating system to
// load any drivers needed by the USB device
delay_ms(1500);

// Wait for the USB host to be ready to receive serial data by
// setting the CDC Virtual Serial Port's RS-232 DTR signal high
while (usb_cdc_serial.control.dtr==0);

while (1)
      {
      // Place your code here

      }
}
 

Re: help with coding to send data to USB PC port

please, would you like to precise what's used for and what's not working.

mention any reference if you can.
 

Advice in ADC AT90USB series

Hi
if I want to get real-time data which mode should i choose? with interupt?

shoudl I write
Code:
read_adc=x; putchar(x);
codevision
 

Code:
read_adc=x; putchar(x);
Meaned this?
Code:
x=read_adc();putchar(x);
AVR ADC outputs 10-Bit data, so the code isn't particularly useful. And you want to write the data to USB rather than UART, where is the data sent by putchar()?

If you send binary data over USB through virtual COM port protocol, you usually want some kind of data framing or print it to ASCII format. O.K., single channel 8-bit ADC data can send without framing if this is sufficient for your application.

In post #4, you copied an USB virtual COM port device template, but the most interesting block stayed empty
Code:
// Place your code here
There's no user code yet. I'm pretty sure that Atmel has a lot of working USB code examples that can give you an idea which code should be placed.
 
Code:
read_adc=x; putchar(x);
Meaned this?
Code:
x=read_adc();putchar(x);
AVR ADC outputs 10-Bit data, so the code isn't particularly useful. And you want to write the data to USB rather than UART, where is the data sent by putchar()?

If you send binary data over USB through virtual COM port protocol, you usually want some kind of data framing or print it to ASCII format. O.K., single channel 8-bit ADC data can send without framing if this is sufficient for your application.

In post #4, you copied an USB virtual COM port device template, but the most interesting block stayed empty
Code:
// Place your code here
There's no user code yet. I'm pretty sure that Atmel has a lot of working USB code examples that can give you an idea which code should be placed.

I used AT90USb646 ADC USART and USB activate but still sometimes there is a gap in ECK signal and then the micro will send them all to together

- - - Updated - - -

thank you so much.im hoping for a solotuion for
Code:
// Place your code here
part.

regards.
 

You have to use the below function in while(1) loop to send data out of USB.

usb_serial_putchar(c);


Code C - [expand]
1
2
3
4
5
6
void usb_send_string(char *str) {
    
    while(*str) {
        usb_serial_putchar(*str++);
    }
}



Convert your adc value to string using sprintf() and then use strcat() to concatanate \r\n to the string and pass this string to the above function like shown below.


usb_send_string(strADCVal);
 
I need int data(digital) real time no error char doesnt work well
 

You want to send out data as integer ? Then if your adc value is integer type that is 16 bits and adc value is 10 bits then you have to send it as two bytes.


Code C - [expand]
1
2
3
4
5
void usb_send_integer(unsigned int dat) {
      
        usb_serial_putchar(dat & 0xFF);
    usb_serial_putchar((dat & 0xFF00) >> 8);    
}

 
Virtual COM port is a stream of characters, so starting with two data bytes, you run into the "framing necessary" problem, otherwise there'a certain risk of losing lock with the word boundary, confusing high and low byte.

But it's quite easy to use unique fill bits when splitting the 10 bit data.
 
Virtual COM port is a stream of characters, so starting with two data bytes, you run into the "framing necessary" problem, otherwise there'a certain risk of losing lock with the word boundary, confusing high and low byte.

But it's quite easy to use unique fill bits when splitting the 10 bit data.

so I need to use characters value...what about interrupt? should I use that?and what is baud rate for USB 2?
 

I don't now what transfer rate codevisionAVR USB CDC library uses. You refer to CodeVisionAVR Manual and also email your questions to CodeVisionAVR support team.

Just try the given code and see if that works. You can worry about the transfer rate later.

Why do you want to use interrupt ? Interrupts are usually used to receive data. You are only sending data.
 
I don't now what transfer rate codevisionAVR USB CDC library uses. You refer to CodeVisionAVR Manual and also email your questions to CodeVisionAVR support team.

Just try the given code and see if that works. You can worry about the transfer rate later.

Why do you want to use interrupt ? Interrupts are usually used to receive data. You are only sending data.


thank youuuu very much.I didnt know that.so I just need to activate ADC USART an USB then add the code
Code:
void usb_send_integer(unsigned int dat) {
      
        usb_serial_putchar(dat & 0xFF);
    usb_serial_putchar((dat & 0xFF00) >> 8);    
}
void usb_send_string(char *str) {
    
    while(*str) {
        usb_serial_putchar(*str++);
    }
}
dat=read_adc();
putchar(dat);
]

?
 

Your dat is an unsigned integer which is 16 bits and contains 10 bit adc result. putchar() accepts char as argument. You can't pass dat to putchar. Use my usb_sent_integer() function to send two bytes of data. First low bute is send and then high byte.
 
Your dat is an unsigned integer which is 16 bits and contains 10 bit adc result. putchar() accepts char as argument. You can't pass dat to putchar. Use my usb_sent_integer() function to send two bytes of data. First low bute is send and then high byte.

so that is
Code:
void usb_send_integer(unsigned int dat) {
      dat=read_adc()
        usb_serial_putchar(dat & 0xFF);
    usb_serial_putchar((dat & 0xFF00) >> 8);    
}
void usb_send_string(char *str) {
    
    while(*str) {
        usb_serial_putchar(*str++);
    }
}
dat=read_adc();

- - - Updated - - -

or read_adc()=%c
 

You only need this function.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
void usb_send_integer(unsigned int dat) {
      dat=read_adc()
        usb_serial_putchar(dat & 0xFF);
    usb_serial_putchar((dat & 0xFF00) >> 8);    
}
 
unsigned int dat = 0;
 
dat = adc_read();
 
usb_send_integer(dat);

 
or
Code:
dat=adc_data[]

- - - Updated - - -

You only need this function.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
void usb_send_integer(unsigned int dat) {
      dat=read_adc()
        usb_serial_putchar(dat & 0xFF);
    usb_serial_putchar((dat & 0xFF00) >> 8);    
}
 
unsigned int dat = 0;
 
dat = adc_read();
 
usb_send_integer(dat);


I need 10 bit data can I use
Code:
 usb_serial_putchar((dat & 0xFF00) >> 10);


that helped so much thank you
 

You need both bytes and so you need both of these.


Code C - [expand]
1
2
usb_serial_putchar(dat & 0xFF);
usb_serial_putchar((dat & 0xFF00) >> 8);



The first is to extract and send low byte and the second is to extract and send high byte. The low byte contains 8 lower bits of dat and high byte contains the lower 2 bits of the high byte (dat)
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top