Serial communication with atmega32 and the Arduino 2560

Go To Last Post
30 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to send and receive data from an AVR board which has an atmega32 on it and the Arduino 2560 .I have connected TX of one to the TR of another and visa versa.with the common ground. 

But I have not found a simple example code to upload to both and see it working .I use TX1 and TR1 of Arduino 2560 since TX0 and TR0 is used for serial usb connection to PC.

Please help me to try and run a simple example on establishing talk between two boards.   

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

azizollah wrote:
I have not found a simple example code to upload to both

It doesn't have to be the same code on both.

 

Serial comms is serial comms - it does not rely on having identical  code at both ends!

 

But, as always, do not try to do this all in one go; because, if it doesn't work, you have no idea which part is not working - or if both are not working!

 

Start with one end - say the Arduino - and verify that it can send to & receive from a PC.

 

Then verify the other end with the PC

 

Finally - only when the previous two steps are done and working - connect the two together.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Arduino mega has a xtal on it so it's clock is accurate and stable, does the M32 have one, or are you trying to use its internal RC clock (bad idea)?

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your advise.I have done that before with the sample program for Arduino as follows:

 

   

/*
  Multple Serial test
 Receives from the main serial port, sends to the others.
 Receives from serial port 1, sends to the main serial (Serial 0).
 This example works only with boards with more than one serial like Arduino Mega, Due, Zero etc
 The circuit:
 * Any serial device attached to Serial port 1
 * Serial monitor open on Serial port 0:
 created 30 Dec. 2008
 modified 20 May 2012
 by Tom Igoe & Jed Roach
 modified 27 Nov 2015
 by Arturo Guadalupi
 This example code is in the public domain.
 */
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);           // select the pins used on the LCD panel
void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}
void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
    Serial1.println(inByte);
    lcd.setCursor(0,0);
    lcd.print(inByte);
  }
  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
    Serial.println(inByte);
    lcd.setCursor(10,1);
    lcd.print(inByte);
  }
}

//and the C code for the AVR board which is written in codevision is as follows:

#include <mega32a.h>
#include <io.h>
//header to enable data flow control over pins
#define F_CPU 8000000UL
//telling controller crystal frequency attached
#include <delay.h>
//header to enable delay function in program
///*=====================================================
// This program sends data and receivs data too.
// it was written after the program "transmitter side"

//*/====================================================
int UBBRValue = 6;//AS described before setting baud rate
 unsigned char receiveData;
void main(void)
{
                DDRD =0b11111110;// 1 << PIND1;//pin1 of portD as OUTPUT
                DDRA =0b00000000;// ~(1 << PIND0);//pin0 of portD as INPUT
                PORTC=0b11111111;// 1 << PIND0;
                PORTC=0;

                //Put the upper part of the baud number here (bits 8 to 11)
                UBRRH = (unsigned char) (UBBRValue >> 8);

                //Put the remaining part of the baud number here
                UBRRL = (unsigned char) UBBRValue;

                //Enable the receiver and transmitter
                UCSRB = (1 << RXEN) | (1 << TXEN);

                //Set 2 stop bits and data bit length is 8-bit
                UCSRC = (1 << USBS) | (3 << UCSZ0);

                while (1)
                {
                               // if (PINA.0==0)//once button is pressed
                               // {

                                    while (! (UCSRA & (1 << UDRE)) );
                                     {
                                      UDR ='a';// 0b11110000;//once transmitter is ready sent eight bit data
                                     }
                                   delay_ms(220);
                                   while (! (UCSRA & (1 << RXC)) );

                                  receiveData = UDR;
                                 if (receiveData == 'a'//0b10101010)
                                  {
                                  PORTC=receiveData;// << PINC0);
                                  delay_ms(1000);
                                  PORTC=~PORTC;
                                  }
                               // }

                                delay_ms(200);
                    }
                    }         

This works ok ,but only with Arduino board and the PC ,not with the AVR board.

Last Edited: Thu. Oct 12, 2017 - 09:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azizollah wrote:
not with the AVR board.

So what's the problem?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

azizollah wrote:
not with the AVR board.

So what's the problem?

The Problem is communicating with AVR  board not the PC .If I disconnect the usb from Pc  and want to start a talk with AVR board and the Arduino ,it won't work.

I may say that the AVR board is running on internal clock at 8 mega hertz.This may be a problem but it should send and receive something?No?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azizollah wrote:
.If I disconnect the usb from Pc  and want to start a talk with AVR board and the Arduino ,it won't work.

Does it work when USB is still connected?

 

Tell us about how you supply power to the boards.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When usb is connected it works wth PC serial port not the AVR board.

 

Supply to the board is done in two ways:

1- from Arduino board.

2- from AVR programer which is connected to PC via usb.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm confused. You have two boards. One Arduino2560 and one (custom?) board with an ATmega32. To start with let's try to keep things clear by never saying just "the board" but all the time refer to either the "Arduino2560 board" or the "ATmega32 board", OK?

 

It is not clear to me how those two boards are supplied with power.

 

Questions:

 

1. Do I understand it correctly that the Arduino2560 board is always connected to USB?

 

2. How is the ATmega32 board powered.

 

3. What programmer are you using?

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

4. When you connect TX from the UART on one to Rx of the UART on the other (and RX to TX) do you also connect the Ground signal between the two as well?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
I'm confused.

So am I.

 

Trying to describe this in words is not working - draw a diagram (or diagrams)

 

Instructions for posting images: http://www.avrfreaks.net/comment...

 

Setting up serial comms with a PC is a basic beginner step - you should be able to find plenty of illustrations of this.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry if I have confused you.

Answer to your questions.

1- yes and no! sometimes its connected to usb ,and sometimes its not,depends on which I am communicating with.

2- If programmer (Progisp) is connected to ATmega32 board, power is by that ,if not by two wire I connect the power from the Arduino board to the ATmega board.  I must say that both type of powering at        the same time is also possible ( or it seems to me to be so).

3-The programmer I am using is something called  Progisp which is run by a file called Progisp.exe

 

Hope I have answered your questions. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So where's the diagram(s), then?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

4. When you connect TX from the UART on one to Rx of the UART on the other (and RX to TX) do you also connect the Ground signal between the two as well?

Yes I do connect the Ground signal between the two as well. 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

So where's the diagram(s), then?

Sorry for the unusable pics

diagrams I don;t know how to draw and  send . Sorry

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azizollah wrote:
diagrams I don;t know how to draw and  send . 

Go on - you're doing electronics and you don't have any means to draw diagrams?!

 

surprise

 

At the very least, you can use pencil & paper & post a photo of it - surely?

 

​Do you not have a scanner?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't see any power bypass caps on the M32, each pair of power pins NEED 0.1uf caps from VCC to Ground, AVCC to Ground as close to the micro as possible.

While your modifying your board to add those, also add a xtal and 18pf caps as well, serial comm is SO MUCH easier to get working when using an Accurate and Stable clock source!

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So tell me if this is a correct and reasonably complete description:

 

There are two boards.

  • One Arduino2560 (with LCD shield on top) which is always powered through the USB cable to the host/development PC.
  • One cutsom-build board with an ATmega32.

The two boards have their UARTs connected together (Rx to Tx, Tx to Rx, GND to GND)

 

There are two different cases of how the ATmega32 board is powered:

 

CASE 1

The ATmega32 board is powered from a "progisp" programmer.

In this case UART communication  between the  two boards work.

 

CASE 2

The ATmega32 board is powered from the Arduino2560 board.

In this case UART communication between the two boards does NOT work.

 

Is this a correct description of your setups andd problem?

 

Questions:

 

  1. In CASE 2 (ATmega32 board powered from the Arduino2560 board): Do you remove the connection between the ATmega32 board and the programmer, or do you simply pull the USB cable on the programmer? (If the programmer is still connected, it might pull power from the ATmega32 board...)
  2. Have you measured the supply voltage on the ATmega32 board for both cases? Is the voltage the same in both cases?
  3. Have you tried to power the ATmega32 board from a separate power supply?
  4. There's lots of more things connected to the ATmega32 board. What are those? Things that draw "much" power?
  5. You've more or less described the symptoms of the problem as just "won't work". Can you be more detailed in describing the testing method? How are you observing the communication? Can you be more specific about the symptoms you see? E.g. are you seeing garbage characters? Do you get framing or parity errors? Etc..

 

I am asking about supply voltages and suggesting a separate (stable!) power supply since you are running the ATmega32 on its internal clock. When you draw more power out of a supply than it can deliver then it is not unusual for the voltage to drop (the other alternative is that it simply shuts down, but that is obviously not happening top you). A voltsage drop will affect the frequency of the internal clock.Variations in voltage will make the CPU (and thus also UART) clock vary. From the data sheet (figure 190 on page 317 Calibrated 8MHz RC Oscillator Frequency vs. Vcc) we can see that if voltage  drops from eg. 5 Volts to 4.5 Volts then the internal RC oscillator goes from 8 MHz to roughly 7.8 MHz. That's a 2.5 % drop which might very well take you out of the +/- 2% "comfort zone" for UART communications. 

 

In general: Please try to be complete and precise and non-confusing in your descriptions. Wen you are confusing it uses our tiem to sort things out. That should be time you spent to make things as clear as possible.

 

azizollah wrote:
diagrams I don;t know how to draw and  send

As has been suggested: If nothing else, use pen and paper. Then take a photo and post that. Try to get lighting and focus as good as you can. Possibly pull photo into any image editing program to fix contrast etc before posting. (Even Windows photo viewer can do this.)

 

Remember that the better you describe the problem to us, the better help you will get.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
 Please try to be complete and precise and non-confusing in your descriptions.

Absolutely.

 

And by far the best to do that is with a diagram (or diagrams) - especially when trying to do it in a foreign language!

 

When you are confusing it uses our time to sort things out. That should be time you spent to make things as clear as possible.

Absolutely.

 

Remember that the better you describe the problem to us, the better help you will get.

+99

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
 use pencil & paper & post a photo

Examples: http://www.avrfreaks.net/comment...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

This is my schematic.The capacitors from supply to ground do not seem to be a problem.

The explanation will come soon. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the schematic sketch! It helps us understand.

 

 

But.. Again, this time with emphasis:

 

Questions:

 

  1. In CASE 2 (ATmega32 board powered from the Arduino2560 board): Do you remove the connection between the ATmega32 board and the programmer, or do you simply pull the USB cable on the programmer? (If the programmer is still connected, it might pull power from the ATmega32 board...)
  2. Have you measured the supply voltage on the ATmega32 board for both cases? Is the voltage the same in both cases?
  3. Have you tried to power the ATmega32 board from a separate power supply?
  4. There's lots of more things connected to the ATmega32 board. What are those? Things that draw "much" power?
  5. You've more or less described the symptoms of the problem as just "won't work". Can you be more detailed in describing the testing method? How are you observing the communication? Can you be more specific about the symptoms you see? E.g. are you seeing garbage characters? Do you get framing or parity errors? Etc..

 

This is like pulling teeth. Supply us something we can work with. If not, I'm out..

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Description of the program:

When circuit is done as on the schematic, on my lap top I open the serial window monitor which is on the Arduino editor.The I type the letter "a" and press send ,then I get number 97,13,10 ,one above the other.When I type the letter "b" I get 98,13,10. When I type the letter "c",I get   99,13,10. and so on.only the first number increases by 1.

If I type number "1" and press the send key I get 49,13,10.If I write "2" I get 50,13,10.

So this means that I am comunicating with the pc by serial usb link.But while the Atmega32 board is at the same time connected,there seem to be idle !.I am not getting anything from or to that board.

Did you get my points?

The voltages I measured is between 4.8 to 4.9 volts.

The resistors of the leds are 10k ohm.so not much current in them.

 

Last Edited: Fri. Oct 13, 2017 - 08:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good News!

I changed the AVR program as below,and now I receive data from ATmega board and read that in the pc window.but when I type a letter and send I get what I used to get on the pc window but not to the ATmega 32 board.

So where am I wrong?

/*****************************************************
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    : 23/09/2017
Author  : Microsoft
Company : Microsoft
Comments: 


Chip type               : ATmega32A
Program type            : Application
AVR Core Clock frequency: 8/000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 512
*****************************************************/

#include <mega32a.h>
#include <delay.h>
// Standard Input/Output functions
#include <stdio.h>

// Declare your global variables here
char data=0;
void main(void)





{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTC=0x00;
DDRC=0xFF;

// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=In 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=T 
PORTD=0x00;
DDRD=0xFE;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x33;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

 UCSRB = (1 << RXEN) | (1 << TXEN);

while (1)
      {                                       
                                             
                                                     while (! (UCSRA & (1 << UDRE)) );
                                                        {                   
                                                               putchar('A');
                                                               putchar('B');
                                                                putchar('C');
                                                                putchar(data);    
                                                        }
                                                       
                                                        delay_ms(1000);
                                              
                                                while(!(UCSRA&(1<<RXC))); //Receive
                                                      {
                                                        data=getchar();       
                                                          if(data=='d')
                                                           {
                                                            PORTC.6=1;
                                                            delay_ms(200);
                                                           }
                                                         else
                                                           {
                                                            PORTC.6=0;
                                                            delay_ms(200);
                                                           }
                                                       }
                                                                      
                                                       delay_ms(1000);                  
                                                
                                                PORTC.7=1;
                                                delay_ms(200);
                                                PORTC.7=0;            
                                                delay_ms(220);
      }
}





 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The LCD shield can draw a lot of power with the backlight on, try powering your mega via its external power port from a 9v power adapter. 

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No Jim the voltages are ok near 5volts

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I give up:

 

azizollah wrote:
changed the AVR program as below,and now I receive data from ATmega board and read that in the pc window.but when I type a letter and send I get what I used to get on the pc window but not to the ATmega 32 board.

That is more or less as far from a structured clear and understandable description as it can be.. I am completely lost as to what is working and what is not.

 

azizollah wrote:
No Jim the voltages are ok near 5volts

For some reason you determine the voltage is all-right but rather than give us an actual value you are again unclear with "near 5 Volts". Is 4.995 near. Yes. Is 4.9 near? Sort-of. Is 4.5 near? No. We have to trust you that they are close enough to 5 Volts to have the baud rate of the UART within the two percent required, yet you've shown little understanding of what can affect UART communications.

 

And no answer to any of my questions.

 

I'm out.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You code indentation is a mess.

 

Please take the time to tidy it up before posting.

 

Again, as JohanEkdahl wrote:
Remember that the better you describe the problem to us, the better help you will get.

 

Also, paying some attention in tidying the code will often lead one to spot one's own mistakes ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dumb question time........

On the Arduino with the Mega2560 the USB interface is connected to one of the USARTS. Are you trying to connect your AVR board to this same USART? If yes, then it won't work as the drivers are all fighting each other. This would explain who the PC connection works, but not the AVR to Arduino connection
Make sure you use a different USART on the Arduino than the one that connects to the USB interface, the 2560 has 4 USARTS so pick 1, 2, or 3.

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Fri. Oct 13, 2017 - 10:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry I was not able to put my case properly ,may be some other time or other place !