You are on page 1of 10

User name

User name

Password FAQ Remember me


Log-in

Search

Register Log in

Log in to check your priva RSS

Maximize

Log in Problems? New User? Sign Up!

[Help!] ADC to UART


AVR Freaks Forum Index AVR (8-bit) Technical Forums AVR forum

Search

Author
loveyust99 Posted: Apr 23, 2006 - 01:39 PM Hi, What I want to do is: Joined: Apr 06, 2006 Posts: 38 ADC -> UART -> host pc

Message

But I can't receive any data. I don't know why. I attached /* Code # the code #1 and tell the problem?

I also attached /* Code #2 */ which receives the data from host pc a successful. Thanks a lot. Peter Jr. Code:

/* Code #1 */ // PROGRAM: a2d_uart.c //******************************************* //* HARDWARE A2D to UART //* //* This program describes how to make ADC an UART //* by interrupt. The result data of ADC is t pc

//* and displayed on the terminal screen. //* Target: ATmega8535 //* Clock: 11.0592MHz //******************************************* #include #include #include #include <avr/io.h> <avr/iom8535.h> <avr/ina90.h> <avr/signal.h>

#define F_CPU 11059200 #define UART_BAUD_RATE 57600

// Code compatibility to new AVR-libc // outb(), inb(), BV(), sbi(), cbi(), sei(), #ifndef outb #define outb(addr, data) addr = (data) #endif #ifndef inb #define inb(addr) (addr) #endif #ifndef BV #define BV(bit) (1<<(bit)) #endif #ifndef cbi #define cbi(reg,bit) reg &= ~(BV(bit)) #endif #ifndef sbi #define sbi(reg,bit) reg |= (BV(bit)) #endif #ifndef cli #define cli() __asm__ __volatile__ #endif #ifndef sei #define sei() __asm__ __volatile__ #endif typedef unsigned char u08; typedef unsigned short u16; typedef unsigned long u32; unsigned int i = 0, j; u08 a2d_lowbyte, a2d_highbyte;

/* a2d global

SIGNAL(SIG_UART_DATA){ j = i%2; if(j==0){ UDR = a2d_lowbyte; i++; } else if(j==1){ UDR = a2d_highbyte; i++; UCSRB &= 0xdf; //USART Data Disable ADCSRA |= 0x08; // ADC interr } }

SIGNAL(SIG_ADC){ a2d_lowbyte = ADCL; a2d_highbyte = ADCH; ADCSRA &= 0xf7; // ADC interrupt disa UCSRB |= (1 << UDRIE); // USART Data

Enable }

// set the uart baud rate void uartSetBaudRate(u32 baudrate) { // calculate division factor for requested u16 bauddiv = ((F_CPU+(baudrate*8L))/(baud outb(UBRRL, bauddiv); #ifdef UBRRH outb(UBRRH, bauddiv>>8); #endif }

int main(void) { /* enable TxD */ outp((1<<TXEN),UCSRB); /* set baud rate */ uartSetBaudRate(UART_BAUD_RATE); /* Set frame format: asynchronous, 8D */ UCSRC = 0x86; ADMUX = 0x60; Adjusted ADCSRA = 0x9f; 128 DDRA = 0xfe; PORTA = 0xfe; // PA0(ADC0) input DDRD = 0xfe; PORTD = 0xfe; // PD1(TXD) output //

Vref=AVcc, Single

// ADC enable, ADC In

ADCSRA |= 0x40; // Start ADC conversi sei(); /* Enable glob while(1){ ; } }

Code:

/* Code #2 */ // PROGRAM: uart_test.c //******************************************* //* HARDWARE UART //* //* This program describes how to make a half //* interrupt. Received character is retransm displayed //* on the terminal screen. //* Target: ATmega8535 //* Clock: 11.0592MHz //******************************************* #include <avr/io.h>

#include <avr/iom8535.h> #include <avr/ina90.h> #include <avr/signal.h> #define F_CPU 11059200 #define UART_BAUD_RATE 9600

// Code compatibility to new AVR-libc // outb(), inb(), BV(), sbi(), cbi(), sei(), #ifndef outb #define outb(addr, data) addr = (data) #endif #ifndef inb #define inb(addr) (addr) #endif #ifndef BV #define BV(bit) (1<<(bit)) #endif #ifndef cbi #define cbi(reg,bit) reg &= ~(BV(bit)) #endif #ifndef sbi #define sbi(reg,bit) reg |= (BV(bit)) #endif #ifndef cli #define cli() __asm__ __volatile__ #endif #ifndef sei #define sei() __asm__ __volatile__ #endif typedef unsigned char u08; typedef unsigned short u16; typedef unsigned long u32; u08 uart_rec; /* uart globals */

SIGNAL(SIG_UART_RECV){ uart_rec = UDR; //input UDR //outp((1<<UDRIE),UCSRB); // USART Da Enable UCSRB |= (1 << UDRIE); // USART Data Enable }

SIGNAL(SIG_UART_DATA){ UDR = uart_rec; UCSRB &= 0xdf; //USART Data Register }

// set the uart baud rate void uartSetBaudRate(u32 baudrate) { // calculate division factor for requested u16 bauddiv = ((F_CPU+(baudrate*8L))/(baud outb(UBRRL, bauddiv); #ifdef UBRRH outb(UBRRH, bauddiv>>8); #endif } int main(void) { /* enable RxD/TxD and ints */

outp((1<<RXCIE)|(1<<TXEN)|(1<<RXEN),U /* set baud rate */ uartSetBaudRate(UART_BAUD_RATE); //UBRRL = 71; /* Set frame format: asynchronous, 8D */ UCSRC = 0x86; DDRD = 0xfe; PORTD = 0xfe; sei(); while(1){ ; } }

/* enable inte

mckenney

Posted: Apr 23, 2006 - 04:40 PM

1) The way this is written, it will take one sample, send it, then stop whatever reason you miss the first two bytes you won't see anythin you want it to send continually, you need to restart the ADC (set A again), probably after sending a2d_highbyte in the UDRE ISR. Joined: Mar 27, 2002 Posts: 2071 Location: Selkirk, NY, USA 2) Code: outp((1<<TXEN),UCSRB);

The definition of outp() in my version of compat/deprecated.h defi the opposite order. (Historically outp/outb were a bother since they their arguments were in different order.) It would seem that the com but all the same I suggest you avoid using outp() (and outb() for th Code: UCSRB = (1 << TXEN);

loveyust99

Posted: Apr 23, 2006 - 07:51 PM Hi, mckenney. Thanks a lot for your reply.

Joined: Apr 06, 2006 Posts: 38

I followed your suggestion, but it doesn't work either. I modified as on the board, I can receive no more than two characters. Code:

SIGNAL(SIG_UART_DATA){ j = i%2; if(j==0){ UDR = a2d_lowbyte; i++; } else if(j==1){ UDR = a2d_highbyte; i++; UCSRB &= 0xdf; //USART Data Disable ADCSRA |= 0x48; // ADC interr conversion } }

and Code: UCSRB = (1 << TXEN);

loveyust99

Posted: Apr 23, 2006 - 08:28 PM

And also I find an interesting thing, that is, I write a simple code to endlessly. But no data received at host pc. I don't know what the problem is. @_@ -> /* Code #3 */ Joined: Apr 06, 2006 Posts: 38 But /* Code #4 */ which just receives data and sends it back to the Code:

/* Code #3 */ ------------------#include <avr/io.h> #include <avr/iom8535.h> unsigned char tmp = 'U'; void transmit(void) { while ((UCSRA & 0x20)==0x00); //UDRE=1 UCSRA &= 0xdf; //clear UDRE UDR = tmp; //trasmit tmp to } int main( void ) {

DDRD = 0xfe; PORTD =0xfe; */ UCSRC = 0x86; UBRRL = 71; UCSRB = 0x08; do {

//PD<0>=input PD<1>=

/* Set frame format: asynchronous, 8D

//9600 bps // TXEN=1

transmit(); }while(1); }

//transm

Code: /* Code #4 */ ------------------#include <avr/io.h> #include <avr/iom8535.h> unsigned char rx_data;

void transmit(void) { while ((UCSRA & 0x20)==0x00); //UDRE=1 UCSRA &= 0xdf; //clear UDRE UDR = rx_data; //trasmit rx }

void receive(void) { while ((UCSRA & 0x80)==0x00); //RXC=1? UCSRA &= 0x7f; //clear RXC rx_data = UDR; //if yes, get } int main( void ) { DDRD = 0xfe; PORTD =0xfe; */ UCSRC = 0x86; UBRRL = 71; UCSRB = 0x18; do { receive(); transmit(); }while(1); }

//PD<0>=input PD<1>=ou

/* Set frame format: asynchronous, 8D

//9600 bps //RXEN=1 T

//recei //trans

mckenney

Posted: Apr 23, 2006 - 08:40 PM

I'm not familiar with Tera Term, but if it has a setting for Flow Con it should be set to "None".

Joined: Mar 27, 2002 Posts: 2071 Location: Selkirk, NY, USA

mckenney

Posted: Apr 23, 2006 - 10:14 PM

I just hacked Code #3 to run on an ATmega8 @3.68MHz on an ST BrayTerm, and it ran fine.

I.E., I tested a different program on a different device at a different Joined: Mar 27, 2002 Posts: 2071 Location: Selkirk, NY, USA talking to a different terminal emulator. Proof! ( )

Koshchi

Posted: Apr 23, 2006 - 10:59 PM The lines: Code: UCSRA &= 0xdf; UCSRA &= 0x7f;

//clear UDRE //clear RXC

Joined: Nov 17, 2004 Posts: 14627 Location: Vancouver, BC

are unnecessary. Both the UDRE flag and the RXC flag are read on clearing of them is handled automatically with reading and writing you with your problem though. I would also explicitly set UBRRH to 0.

Also, you shouldn't be directly including avr/iom8535.h. What pro makefile (or in the project configuration options in AVR Studio). _________________ Regards, Steve A. The Board helps those that help themselves.

loveyust99

Posted: Apr 24, 2006 - 06:11 AM

Yes, I set the flow control none. Hi, mckenney. Can you show me the source code you ran on an AT

Joined: Apr 06, 2006 Posts: 38

loveyust99

Posted: Apr 24, 2006 - 11:35 AM Help!!!

Joined: Apr 06, 2006 Posts: 38

loveyust99

Posted: Apr 24, 2006 - 02:08 PM Thanks a lot! I finally solved the problem and find what problem it is. first, I forget to connect the the #5 pin of DB9 to the ground. second, I set the baudrate too small. ^^

Joined: Apr 06, 2006 Posts: 38

Display posts from previous:

All Posts

Oldest First

Jump to:

| |-- AVR forum

AVR Freaks Forum Index AVR (8-bit) Technical Forums AVR forum

Powered by PNphpBB2 2003-2006 The PNphpBB Gro Credits

You might also like