Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

help plz ... for use uart

Status
Not open for further replies.

lonely151

New Member
hi every one

i have problem in my code for send data to pc

i try to write it but it dont work !!

can any one help me

this is code
take data from sensors and convert from analog to digital
then send it to PC
but i have problem in uart

#define F_CPU 10000000UL
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "uart.h"
#define sbi(port, bit) (port) |= (1 << (bit))

#define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit))

FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);


void initUART()
{
UBRRH =0;
UBRRL = 31; // for baud rate=19200
UCSRC = 0x86; // mode 8N1
UCSRB = 0x18; // enable RX and TX
}



////////////////////////////////////////////////

void my_delay(int w) //Define delay function.
{
int i;
for(i=0; i<= w ; i++)
_delay_ms(1);
}


////////////////////////////////////////////////

void sendByte(char x)
{ while((UCSRA & 0x20)==0 )
; //wait till bit 5 of UCSRA is set check bit 5 (UDRE)
UDR=x;
}

//////////////////////////////////////

//stdout = stdin = &uart_str; // map stdin and stdout to serial port

int main(void)
{
ADMUX= 0b00100000; // chan 0, left adjusted,Vref=5
ADCSRA= 0b10000110; // ADC enabled, pre-scalar 64
DDRA= 0; //PORT A input
DDRB= 0b11111111; //PORT B output


unsigned x; //declare the temprature
unsigned char y; //declare the Humdaity
unsigned char z; //declare the Solar radition
unsigned char w;
unsigned int result;

uart_init();
stdout = stdin = &uart_str;
//uart_init();


while (1)
{
////////////////////////////////////////////////
ADMUX= 0b00100000; // chan 0, left adjusted,Vref=5
ADCSRA = ADCSRA | 0x40 ; // start conversion.
do {
w = ADCSRA & 0x10;
} while (w == 0); // repeat until EOC
result = ADC; // the sampled data is in result
// we can do whatever with the sampled data.
//For now, we will display in on PORTD
result = (result>>8); // truncate from 10 to
x=result;
/////////////////////////////////////////////
ADMUX= 0b00100001; // chan 0, left adjusted,Vref=5
ADCSRA = ADCSRA | 0x40 ; // start conversion.
do {
w = ADCSRA & 0x10;
} while (w == 0); // repeat until EOC
result = ADC; // the sampled data is in result
// we can do whatever with the sampled data.
//For now, we will display in on PORTD
result = (result>>8); // truncate from 10 to
y=result;
///////////////////////////////////////////
ADMUX= 0b00100010; // chan 0, left adjusted,Vref=5
ADCSRA = ADCSRA | 0x40 ; // start conversion.
do {
w = ADCSRA & 0x10;
} while (w == 0); // repeat until EOC
result = ADC; // the sampled data is in result
// we can do whatever with the sampled data.
//For now, we will display in on PORTD
result = (result>>8); // truncate from 10 to
z=result;
///////////////////////////////////////////////////////////
PORTB=x;
my_delay(1000);
PORTB=y;
my_delay(1000);
PORTB=z;
my_delay(1000);

initUART();
while(1){
sendByte(x);
sendByte(y);
sendByte(z);

}


}


return 0;
}
 
You'll get a lot more help if you give your variables names more than one letter long.
Code:
unsigned x; //declare the temprature
unsigned char y; //declare the Humdaity
unsigned char z; //declare the Solar radition
unsigned char w;
Are these different?
Code:
uart_init();
initUART()

AND It's incredibly confusing to declare x in main() and then use it as a local variable in all the subroutines. Maybe your compiler can keep them straight, and maybe you can keep track, but I can't.
 
thank mneary i will use variable name
i try to write uart funcation but every time appear problem
i have FYP and i want solve this problem fast plz
 
I wouldn't bother, it looks like he's disappeared - I hate it when that happens.
 
Sorry, I just get used to people asking questions then vanishing, I shouldn't presume everyone's like that.
 
i was month to write this code
every time happen problem
that is difficult for me becouse i didint study avr as course
 
Since you're still on it, I'll give it a closer look when I get back later.

Which compiler are you using? I'm using WinAVR.

Which AVR are you using? I'm not finding that in your C source code.

While I'm asking silly questions, what is your CPU speed and can you please show your baud rate calculation? Is your clock 10 MHz? (9.83?)
 
Last edited:
OK I found something, chip independent and compiler independent.

Your program initializes your uart twice with two different routines. One of them might be wrong? Choose one, validate it and get rid of the other.
Initialize the uart at the beginning of main() where you initialized the i/o ports.
Your transmission is surrounded by a while(1) control. If it ever started sending characters it would never go back and get new ADC values.
 
Since you're still on it, I'll give it a closer look when I get back later.

Which compiler are you using? I'm using WinAVR.

Which AVR are you using? I'm not finding that in your C source code.

While I'm asking silly questions, what is your CPU speed and can you please show your baud rate calculation? Is your clock 10 MHz? (9.83?)

thanks

i also use winavr and i use atmega32
and cpu 10 MHz

i want ask her in this part for code
initUART();
while(1){
sendByte(x);
sendByte(y);
sendByte(z);

i use sendbye or printe to send from TX pin ?
and it corecet use while(1) ?
 
I found more.

Your ADC was not working correctly. To align it you must shift right two, not eight.

Code:
result = (result>>2);         // truncate from 10 to
The following lines prevent a clean compile.
Code:
    stdout = stdin = &uart_str; 
                                // map stdin and stdout to serial port
That, along with eliminating

Code:
uart_init();
and cleaning up ambiguous variables leaves me with code that compiles and appears to work.
 
thank u mneary

i try to solve the problem and i end in
this code

but not worken yet
i dont know now what is the problem!!
i use proteus program
when i use any value appear in vitral termnal
but when use anlog to digital not appear !!
can help me plz if u know!!




#define F_CPU 3686400UL
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "uart.h"
#define sbi(port, bit) (port) |= (1 << (bit))

#define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit))

FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);

//////////////////////////////////////

//stdout = stdin = &uart_str; // map stdin and stdout to serial port
void initUART()
{
UBRRH =0;
UBRRL = 11; // for baud rate=19200
UCSRC = 0x86; // mode 8N1
UCSRB = 0x18; // enable RX and TX
}
//---------------------------------------------------
void sendByte(char x)
{ while((UCSRA & 0x20)==0 )
; //wait till bit 5 of UCSRA is set check bit 5 (UDRE)
UDR=x;
}
int main(void)
{
char c1 , c2,lsb , msb ;
char hex[] = { '0' ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' };

ADMUX= 0b00100000; // chan 0, left adjusted,Vref=5
ADCSRA= 0b10000110; // ADC enabled, pre-scalar 64
DDRA= 0; //PORT A input


char x; //declare the temprature
char w;
unsigned int result;

while (1)
{
////////////////////////////////////////////////
ADMUX= 0b00100000; // chan 0, left adjusted,Vref=5
ADCSRA = ADCSRA | 0x40 ; // start conversion.
do {
w = ADCSRA & 0x10;
} while (w == 0); // repeat until EOC
result = ADC; // the sampled data is in result
// we can do whatever with the sampled data.
//For now, we will display in on PORTD
result = (result>>8); // truncate from 10 to
x=result;

lsb =x & 0xF ;
msb = (x>>4) & 0xF ;
c1 =hex[lsb];
c2= hex[msb];
/////////////////////////////////////////////




initUART();
while(1){

sendByte(c1);
sendByte(c2);
}


}

return 0;
}
 
You are still using the 'quote' tags instead of the 'code' tags (go advanced). I will have to copy your code so I can understand it. Maybe in a few days. You can speed this up if you edit your message and change
to
Code:
 in your raw message.



[edit] I get the feeling that I'm working harder on this than you are.
In any case, the best of luck to you.
 
Last edited:
thanks sir
i solve ths problem
and appear the data in vitral termnal
but not the same data !!
u know what is the problem ??
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top