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.

Send data From AVR to PC

Status
Not open for further replies.

decapitary

Banned
Joined
Jul 16, 2014
Messages
62
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
0
How Can I send Serial data From atmega8 to USB port? I have RT232 but it doesnt work...please help me I need to send an analog EKG wave(0-100hz) to my C to process the data
here is the code im using:
Code:
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 8/10/2014
Author  : asooZ
Company : 
Comments: 


Chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 16.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/
            #include <mega8.h>

#include <delay.h>

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

#define ADC_VREF_TYPE 0x00
#include <io.h>
#include <inttypes.h>

void InitADC()
{
//Aref= AVcc and ADC become 8 bit resolution


//Enable ADC and prescalar div factor 128
ADCSRA|= (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); 
}

uint8_t ReadADC()
{
// Channel ADC0 is selected, so no need to set MUX3 to MUX0 in ADMUX

// Start single conversion 
ADCSRA|= (1<<ADSC); 

//Wait for conversion to complete
while (!(ADCSRA & (1<<ADIF)));

// Clear bit ADIF
ADCSRA|= (1<<ADIF);

return (ADCH);
}

void InitUSART(uint16_t ubrr_value)
{
// Set baud rate
UBRRL= ubrr_value;
UBRRH= (ubrr_value >>8);

// Set frame format,i.e. asynchronous mode, no parity, 1 stopbit, char size= 8
UCSRC= (1<<URSEL)|(3<<UCSZ0);

// Enable the transmitter
UCSRB= (1<<TXEN);
}

void USARTWriteChar(int data)
{
// Wait until the transmitter is ready
while(!(UCSRA & (1<<UDRE)))
{
// Do nothing
}

// Clear bit TxC
UCSRA|= (1<<TXC);

// Write data to USART buffer
UDR= data;
}

void main()
{
int data;
           UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x67;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 125.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x87;
// Initialize the USART
InitUSART(51);

// Initialize ADC
InitADC();

while(1)
{
// Read data from ADC 
data= ReadADC();

// Send data from ADC out through TxD
USARTWriteChar(data);
}
}


// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

// Declare your global variables here
 

As almost all converters are bi-directional it doesn't really matter which way you look at it.

What you need is a cheap USB/serial converter. You can buy them for very little or make your own. Look at FTDI devices if you want make it yourself, these have interfaces which will connect directly to your AVR device and the PC. Commercially made ones will come with the convenience of a USB cable and plug already fitted but their serial side is normally a 'D' connector with an RS232 interface so you would need to convert the AVR serial output to RS232 levels first using a MAX232/MAX3232 or similar.

Brian.
 
As almost all converters are bi-directional it doesn't really matter which way you look at it.

What you need is a cheap USB/serial converter. You can buy them for very little or make your own. Look at FTDI devices if you want make it yourself, these have interfaces which will connect directly to your AVR device and the PC. Commercially made ones will come with the convenience of a USB cable and plug already fitted but their serial side is normally a 'D' connector with an RS232 interface so you would need to convert the AVR serial output to RS232 levels first using a MAX232/MAX3232 or similar.

Brian.

Im connectin my atmega8 to max232 then to female usb port but it doesnt work.You mean I have to connect it to USB/serial converter?
 

RS232 and USB are completely incompatible, you need a converter to link them.

The two options are

1. without converting to RS232:
atmega8 <--> FT232xx <--> PC USB Port

2. converting to RS232 first:
atmega8 <--> MAX232 <--> USB/serial converter <--> PC USB Port

So for simple data linking the RS232 step isn't really necessary.

The FT232xx devices from FTDI will convert the serial signals from the pins of the atmega8 directly to USB, the '232' in their part numbers was chosen because they perform a similar function to MAX232 devices but to USB instead of RS232. There are several versions of FT232 with slightly different circuit requirements but they should all work in your application.

Brian.
 
RS232 and USB are completely incompatible, you need a converter to link them.

The two options are

1. without converting to RS232:
atmega8 <--> FT232xx <--> PC USB Port

2. converting to RS232 first:
atmega8 <--> MAX232 <--> USB/serial converter <--> PC USB Port

So for simple data linking the RS232 step isn't really necessary.

The FT232xx devices from FTDI will convert the serial signals from the pins of the atmega8 directly to USB, the '232' in their part numbers was chosen because they perform a similar function to MAX232 devices but to USB instead of RS232. There are several versions of FT232 with slightly different circuit requirements but they should all work in your application.

Brian.

THank You so much. Can You Intoduce me the best FT232xx? I want to Design a Medical Device I need the best common parts
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top