Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
himel1842
PostPosted: Jun 12, 2012 - 01:05 PM
Rookie


Joined: Oct 28, 2011
Posts: 26


As I posted few months back about optical wireless communicaiton, I have developed few things but now totally stuck. I can transmit some character from a microcontroller using USART port TxD0( I am using atmega 1284) through the green LED and can receive it in the receiver side using a photodiode.Now this signal is very low so I am amplifying it to 5V level. Now I want to re-transmit this signal to another node using 2nd microcontroller.What I was trying is giving this signal to RxD0 and retransmit using TxD1.
Is this the correct way to do this?

How do I do that?

How do I check 2nd microcontroller receiving the data properly?
 
 View user's profile Send private message  
Reply with quote Back to top
uracolix
PostPosted: Jun 12, 2012 - 03:57 PM
Hangaround


Joined: Jun 17, 2008
Posts: 454
Location: Meissen, Germany

>How do I check 2nd microcontroller receiving the data
properly?

You can connect the MCU via a serial interface to a PC and
dump the received characters to a terminal program.
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
ka7ehk
PostPosted: Jun 12, 2012 - 11:59 PM
10k+ Postman


Joined: Nov 22, 2002
Posts: 12046
Location: Tangent, OR, USA

Typically, you would use a second UART. Somewhere. There ARE AVRs with several UARTS (some with 4, even).

Otherwise, you might use two of the same kind, maybe even same code. Connect them by some parallel or serial link. SPI is relatively easy and pretty fast. Then, in your code, send anything received optically to the hardware data link where it is received by the other board. Other board transmits anything received by hardware data link out the optical port.

Could be pretty simple.

Jim

_________________
Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA

"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
 
 View user's profile Send private message  
Reply with quote Back to top
himel1842
PostPosted: Jun 13, 2012 - 10:42 AM
Rookie


Joined: Oct 28, 2011
Posts: 26


Jim, thanks a lot. I also thought it would be simple. But in reality it seems too complicated for me. I am using Atmega 1284 which has two USART. Block diagram of my system and and code is uploaded here. Can you please let me whats wrong there
Code:

#include <avr/io.h>
#include <util/delay.h>
void USARTInit(int ubrr_value);
void USARTWriteChar(char data);
int main(void)
{   
   USARTInit(25); 
    while(1)
    {
      USARTWriteChar('5'); // send the data to USART Tx for transmission
      
    }
}


void USARTInit(int ubrr_value)
{
   //Set Baud rate
   UBRR0L = ubrr_value;
   UBRR0H = (ubrr_value>>8);
    //Enable The receiver and transmitter
   UCSR0B=(1<<RXEN0)|(1<<TXEN0);
 
   UCSR0C=(1<<USBS0)|(3<<UCSZ00);

}

void USARTWriteChar(char data)
{
   while(!(UCSR0A & (1<<UDRE0)))
   {
     
   }
 
  UDR0=data;
}

[b]Code for Receiver side[/b]

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>

void USARTInit(int ubrr_value);
char USARTReadChar();
void USARTWriteChar( char data1);

int main()
{
   char data;
   USARTInit(25);
   while(1)
   {
      data=USARTReadChar();
      USARTWriteChar(data);
   }      
   
}

void USARTInit(int ubrr_value)
{
   //Set Baud rate
   UBRR0L = ubrr_value;
   UBRR0H = (ubrr_value>>8);
   UBRR1L = ubrr_value;
   UBRR1H = (ubrr_value>>8);
    //Enable The receiver and transmitter
   UCSR0B=(1<<RXEN0);
   UCSR0C=(1<<USBS0)|(3<<UCSZ00);
   UCSR1B=(1<<TXEN1);
   UCSR1C=(1<<USBS1)|(3<<UCSZ10);

}
char USARTReadChar()
{
   
   while(!(UCSR0A & (1<<RXC0)))
   {
     
   }
    return UDR0;
}

void USARTWriteChar( char data1)
{
   while(!(UCSR1A & (1<<UDRE1)))
   {
      UDR1=data1;
   }


Last edited by himel1842 on Jun 14, 2012 - 10:59 AM; edited 3 times in total
 
 View user's profile Send private message  
Reply with quote Back to top
himel1842
PostPosted: Jun 13, 2012 - 10:55 AM
Rookie


Joined: Oct 28, 2011
Posts: 26


Attached is the block diagram of my system. I am sending '5' from 1st microcontroller through the LED and Receiving it by Photodiode in 1 meter apart. The received signal amplitude is very low so I am amplifying it to 5V siganl to give the Rx0 port of the 2nd microcontroller. I am trying to re-transmit the received signal by Tx1 of the 2nd microcontroller, but not getting anything. Please let me know where I am doing wrong.Thanks

Himel
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 13, 2012 - 12:50 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18555
Location: Lund, Sweden

To avoid that code ends up e.g. like this:
Quote:
UBRR0H = (ubrr_value>>Cool;

please study this illustration:
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
himel1842
PostPosted: Jun 13, 2012 - 04:16 PM
Rookie


Joined: Oct 28, 2011
Posts: 26


Thanks johan, i will follow it next time . Do you have any suggestion regarading my problems?
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 13, 2012 - 04:26 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18555
Location: Lund, Sweden

Quote:
Thanks johan, i will follow it next time

You can go back and edit your existing post.
Quote:
Do you have any suggestion regarading my problems?

No:
1) I refuse to read unstructured code.
2) In that post you also ask:
Quote:
I am using Atmega 1284 which has two USART. Block diagram of my system and and code is uploaded here. Can you please let me whats wrong there.

You either missed to supply any symtoms of the problems you are having, or you are expecting us to scrutinize your project "in blanco". Neither will do.

Sorry for the bluntness, but that is the way it is.

Remember that you only get as good answers as you ask good questions. You are in a competition against all other people coming here to ask about things. If you question isn't appealing, interesting and tempting then people will walk over to other questions. If your question seems un-necessarily hard to work with, or if people fear that they will be out on a "wild goose hunt" (no progress because of lack of information, or ill structured information etc) then people will walk over to other questions.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
larryvc
PostPosted: Jun 13, 2012 - 04:45 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

Hi himel842,

Since you are on a first name basis with everyone, why don't you tell us your name. Smile

I'll take a look at your code and try to help you just as soon as the caffeine hits. Please edit you code as Johan has asked.

Can you tell us what frequency you are running the ATmega1284 at and is it an internal/external crystal/resonator/oscillator?

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
himel1842
PostPosted: Jun 14, 2012 - 12:59 AM
Rookie


Joined: Oct 28, 2011
Posts: 26


Johan I think You can read my code now. My problem is I don't get any output in the Tx1 pin of second microcontroller.
Larry I am Himel and I am using external 8MHz crystal.Looking forward to hear from you.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 14, 2012 - 06:38 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18555
Location: Lund, Sweden

Quote:

Johan I think You can read my code now.

No. Did you even look at the result of your edits?

You have just lost the competition as far as I am concerned. I give up..
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
larryvc
PostPosted: Jun 14, 2012 - 09:28 AM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

Himel,

I was going to post some code for you and I will if you go back and edit your post correctly. It is quite simple. You put one "code" at the beginning of your code and one "/code" at the end of your code, of course they are in the "[ ]" brackets. Get rid of all the others.

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
himel1842
PostPosted: Jun 14, 2012 - 11:00 AM
Rookie


Joined: Oct 28, 2011
Posts: 26


OK, how about now? Please give me any suggestion if you have any with this mater.
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 14, 2012 - 05:41 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

OK. I do see an error in your slave routine above. UDR1=data1; needs to be after the while.
Code:
void USARTWriteChar( char data1)
{
   while(!(UCSR1A & (1<<UDRE1)))
   {
      UDR1=data1;
   }

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc


Last edited by larryvc on Jun 14, 2012 - 06:25 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 14, 2012 - 05:49 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

Himel,

This code will enable you to test your Master/Slave. You will have to connect the TX of the master to the RX of the slave and the RX of the master to the TX of the slave.

I did not have time to finish the LED code in the Master code. You will have to decide what port you attach the 2 LEDs to.

Master Code:
Code:
/*
 * Himel1842Master.c
 *
 * Created: 6/14/2012 1:00:00 AM
 *  Author: larryvc
 */

#define F_CPU 8000000UL      // <-- This is required for and has to be defined before
#include <util/delay.h>      // <-- this.

#include <avr/io.h>

void USARTInit(int ubrr_value);
char USART0ReadChar();
void USART0WriteChar(char data);

// UART Baud Rate
#ifndef BAUDRATE
#define BAUDRATE 57600
#endif

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

int main()
{
   char sentchar = '5';
   char data = sentchar;

   USARTInit(BAUD_PRESCALE);

   while (1) {
      USART0WriteChar(data);
      data = USART0ReadChar();
      if (data == sentchar) {
         // turn on good led
      } else {
         // turn on bad led
      }
      _delay_ms(5);
   }
}

void USARTInit(int ubrr_value)
{
   //Set Baud rate
   UBRR0 = ubrr_value;

   //Enable The receiver and transmitters
   UCSR0B = (1 << RXEN0) | (1 << TXEN0);
   UCSR0C = (1 << USBS0) | (3 << UCSZ00);
}

char USART0ReadChar()
{
   while (!(UCSR0A & (1 << RXC0))) {
   }
   return UDR0;
}

void USART0WriteChar(char data)
{
   while (!(UCSR0A & (1 << UDRE0))) {
   }
   UDR0 = data;
}

Slave Code:
Code:
/*
 * Himel1842Slave.c
 *
 * Created: 6/14/2012 1:00:00 AM
 *  Author: larryvc
 */

#define F_CPU 8000000UL      // <-- This is required for and has to be defined before
#include <util/delay.h>      // <-- this.

#include <avr/io.h>

void USARTInit(int ubrr_value);
char USART0ReadChar();
void USART0WriteChar(char data);

// UART Baud Rate
#ifndef BAUDRATE
#define BAUDRATE 57600
#endif

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

int main()
{
   char data;

   USARTInit(BAUD_PRESCALE);

   while (1) {
      data = USART0ReadChar();
      USART0WriteChar(data);
      _delay_ms(5);
   }
}

void USARTInit(int ubrr_value)
{
   //Set Baud rate
   UBRR0 = ubrr_value;

   //Enable The receiver and transmitters
   UCSR0B = (1 << RXEN0) | (1 << TXEN0);
   UCSR0C = (1 << USBS0) | (3 << UCSZ00);
}

char USART0ReadChar()
{
   while (!(UCSR0A & (1 << RXC0))) {
   }
   return UDR0;
}

void USART0WriteChar(char data)
{
   while (!(UCSR0A & (1 << UDRE0))) {
   }
   UDR0 = data;
}

Code:
=========================================================================================

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
himel1842
PostPosted: Jun 21, 2012 - 02:35 PM
Rookie


Joined: Oct 28, 2011
Posts: 26


Hi Larry ,thanks a lot. I tried your code with Atmega 1284 but didn't work. Did you check with chip?
Actually, I tried with Proteus as well but don't get any results. If I change my code in the receiver as you indicated, it works with Proteus but not in real hardware. I am trying all these just connecting master and slave by wire. However my target is to get communication through optical wireless.
Can you please check your code with real connection?
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 21, 2012 - 04:07 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

I don't use Proteus. Is your Proteus licensed?

himel1842 wrote:
Can you please check your code with real connection?


That is very presumptuous of you. Have you run it on real hardware?

I think at this point that you need to check your hardware. Are you really running at 8Mhz?

I'm out.

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
himel1842
PostPosted: Jun 21, 2012 - 05:40 PM
Rookie


Joined: Oct 28, 2011
Posts: 26


Quote:

I don't use Proteus. Is your Proteus licensed?


Yes, I use proteus Licensed version. I am also running it at 8MHz.
Quote:

That is very presumptuous of you. Have you run it on real hardware?

Sorry for the miscommunication.Your help is very much appreciated.
At this point I am just connecting Tx1 of first microcontroller to the Rx1 of second microcontroller directly by wire.Supply of both microcontroller is different . Is that a problem?
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 21, 2012 - 06:20 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

himel1842 wrote:
At this point I am just connecting Tx1 of first microcontroller to the Rx1 of second microcontroller directly by wire...

"That" is a problem. Do you see what USART the code is written for? How about trying:

TXD0 of the master to RXD0 of the slave

TXD0 of the slave to RXD0 of the master

Did you write the code to turn on the LEDs? You won't see anything happen until you do.

The code I wrote will test your connections. It is up to you to modify it to make it do what you want.

EDIT: I must also say that the code I wrote is very rudimentary (crude, al dente, barely cooked). Smile

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 21, 2012 - 11:28 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18555
Location: Lund, Sweden

Quote:
At this point I am just connecting Tx1 of first microcontroller to the Rx1 of second microcontroller directly by wire.Supply of both microcontroller is different . Is that a problem?

Not even a shared signal ground? Yes, that is definitively a problem.

If you only want simplex communications, e.g. data just going one way, then one TxD to RxD connection might be okay. If you want "duplex", i.e. communication both ways, then you need the crossed TxD-RxD connections as stated by Larry.

In all cases you need a common signal ground connected.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits