go_away

Author Topic: uart problem plzz see  (Read 1621 times)

0 Members and 1 Guest are viewing this topic.

Offline superchikuTopic starter

  • Supreme Robot
  • *****
  • Posts: 953
  • Helpful? 5
  • cooll
uart problem plzz see
« on: April 01, 2008, 03:02:49 AM »
hey ppl recently i got a new sharp ir , show i decided calibrate it by using uart , but when i got into hyperterminal but the hyperterminal window remained completely blank ..

my code is like this

# include<stdio.h>
# include<avr/io.h>
# include<util/delay.h>
# include<avr/interrupt.h>

#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)






unsigned int a;

void adcinitialize();
void uartinitialize();
void adcgetvalue();

 int main()
  {
   adcinitialize();
   uartinitialize();
   while(1)
    {
    adcgetvalue();
    while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
    UDR = a; // Send out the byte value in the variable "a"
   while((UCSRA &(1<< UDRE))==0) {};
   
    }       
   
   
   return(1);   
   }
 
 
 
 void adcinitialize()
  {
   
  ADCSRA|=_BV(7)|_BV(2)|_BV(1);
  ADMUX=0x60;
  ADCSRA|=(1<<ADSC);
  while(!(ADCSRA & (1<<ADIF)));
  ADCSRA|=(1<<ADIF);
  a=ADCH;
 
  }
 
  void uartinitialize()
   {
    UCSRB|=(1<<RXEN)|(1<<TXEN);
    UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
    UBRRL=BAUD_PRESCALE;
   UBRRH=(BAUD_PRESCALE>>8);
   }
void adcgetvalue()
 {
 
   ADMUX=0x61;
   ADCSRA|=(1<<ADSC);
  while(!(ADCSRA & (1<<ADIF)));
  ADCSRA|=(1<<ADIF);
  a=ADCH;
 }
   
   
   can anyone tell me whats the fault with my code , i need to know abt it as quickly as possible , oh yes my mcu is clocked at 8 mhz internal clock thanks..
« Last Edit: April 01, 2008, 04:53:09 AM by superchiku »
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

Offline Trumpkin

  • Supreme Robot
  • *****
  • Posts: 1,177
  • Helpful? 5
Re: uart problem plzz see
« Reply #1 on: April 01, 2008, 10:29:44 AM »
Well, first of all you have a  8) face in the middle of your code, second of all you didn't use the insert code button. (sorry I just had to say that)
Robots are awesome!

Offline superchikuTopic starter

  • Supreme Robot
  • *****
  • Posts: 953
  • Helpful? 5
  • cooll
Re: uart problem plzz see
« Reply #2 on: April 02, 2008, 01:17:16 AM »
ok now iam using the code button with the modified code plzz check it

Code: [Select]
# include<stdio.h>
# include<avr/io.h>

# include<util/delay.h>
# include<avr/interrupt.h>
# include<stdlib.h>
# include<string.h>
# include<avr/pgmspace.h>



#define USART_BAUDRATE 38400
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL)))- 1)


 char u[11]="adcvalue\r\n";
 char *d;
 char b[10];
 char *p;
 
 unsigned int a;

void adcinitialize();
void uartinitialize();
void adcgetvalue();
void transferusart();
void printvalue(char *);

 int main(void)
  {
   adcinitialize();
   uartinitialize();
   while(1)
    {
adcgetvalue();

     
p=itoa(a,b,10);

strcat_P(b,PSTR("\r\n"));

d=&u[0];

transferusart();

     
     }       
   

   return(0);
   }
 
 
 
 void adcinitialize()
  {
   
  ADCSRA|=_BV(7)|_BV(ADPS2)|_BV(ADPS1);
  ADMUX=0x60;
  ADCSRA|=(1<<ADSC);
  while(!(ADCSRA & (1<<ADIF)));
  ADCSRA|=(1<<ADIF);
  a=ADCH;
 
  }
 
  void uartinitialize()
   {
    UCSRB|= (1<<RXEN) | (1<<TXEN);
    UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
    UBRRL=BAUD_PRESCALE;
UBRRH=(BAUD_PRESCALE>>8);
   }
void adcgetvalue()
 {
 
   ADMUX=0x61;
   ADCSRA|=(1<<ADSC);
  while(!(ADCSRA & (1<<ADIF)));
  ADCSRA|=(1<<ADIF);
  a=ADCH;
 }
 
void transferusart()
 {
   
   while (*d != '\0')
    {

     while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
      UDR =*d ; // Echo back the received byte back to the computer
      d++;
}
 printvalue(p);
 }
 
 void printvalue(char *l)
  {
   while (pgm_read_byte(l) != 0x00)
    {
 

  while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
      UDR =*l ; // Echo back the received byte back to the computer
     
     
     l++;
    }
  }
 

   
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,586
  • Helpful? 169
    • Society of Robots
Re: uart problem plzz see
« Reply #3 on: April 06, 2008, 11:05:09 AM »
Well, you don't have anything in your code that outputs anything to UART other than '\r\n'

In your while loop, just print out something simple. Testing all of your code at once makes it hard to debug.

Offline superchikuTopic starter

  • Supreme Robot
  • *****
  • Posts: 953
  • Helpful? 5
  • cooll
Re: uart problem plzz see
« Reply #4 on: April 06, 2008, 11:07:08 AM »
no no i got it to work thanks for ur help , if anyone needs the code then i can post n the forum
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

 

Related Topics

  Subject / Started by Replies Last post
2 Replies
2132 Views
Last post April 07, 2008, 05:53:11 PM
by Admin
8 Replies
2153 Views
Last post May 29, 2008, 03:48:22 PM
by flimflap
12 Replies
3410 Views
Last post September 16, 2008, 05:50:05 AM
by vidam
12 Replies
2416 Views
Last post July 24, 2009, 09:59:04 PM
by Admin


Get Your Ad Here