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:
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