Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

coupling 2 atmega via RS 232 or SPI ?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 64
Location: Aachen

germany.gif
PostPosted: Sun Aug 10, 2014 4:07 pm    Post subject: coupling 2 atmega via RS 232 or SPI ? Reply with quote

Dear All,
I am kindly asking for your advice how I may proceed.

I programmed a ATMega328 ( = CPU A) to control the movement of a stepper motor ( basically creating signals e.g. stepper-clock, direction, halfstep/fullstep) and write position data to a LCD display.
As the LCD display is a little bit slow ( this is what I think because the hickup correlates with writing) I decided to write to the LCD Display once every 200 stepper-clocks. I am using the library lcd4busy_anypin.lbx
While the motor is in movement this writing to the display causes some hickup in the movement of the stepper motor. I want to avoid this. It sounds ugly.

My idea is to use an ATMega 8 ( = CPU B) for handling the display communication.

CPU A takes care of the communication with the PC only when motor is not moving. CPU A needs the hardware UART of CPU A for this purpose.
CPU A handles also 2 push buttons that have several functions.
One function of the pushbuttons is scrolling the display.
So CPU A has to transfer to CPU B that a scrolling operation has to be peformed.

CPU B receives the data to display during the init routine of CPU A or after a command arrives from the PC. This does not happen very often.
The typical operqtion is that the ionformation of the actual position is tranferred after a movement is finished.
So CPU B is only listening to CPU A and writing to the display.

I see two possibilities:
#1: Programming a software RS 232 on CPU A and connect it to the hardware RXD of CPU B.
Advantages:
This method occupies one pin on each CPU only.
On the listening CPU B I can use the all the comfortable BASCOM features for RS 232 including ringbuffer and bytematch.
With this kind of operation like catching the frame etc.
I have some experience with RS232 communication.
Disadvantage : It is relatively slow.


#2: Using HW SPI on both CPUs.
As far as I could read it in the documentation of the processors, BASCOM help-files and the samples SPI communication is very fast.
I need the SPI interface for traferring the code via the programmer into the flash memories.
My idea is to connect the CPU via a special cable from one SPI socket to the other.
Advantage: very fast data transfer.
Disadvantage:
Consumption of at least 3 pins on each CPU.
I have no experience using the SPI interface.
Handling of a ring buffer on CPU B has to be programmed by me.
The comfortable BASCOM feaures are not available.

If I could send the data in one array of bytes string and read it into one big array of bytes I may come along using an overlay construction. This would help.
I started reading about IC . To my feeling it is like using a cannon against sparrows. Perhaps i am wrong.

I am also considering stay with one CPU and speed up the system clock from 6 MHz to e.g. 16 MHz.
The operations within the CPU will be handled faster. I doubt that writing the data to the LCD display is performed faster.
But it has the advantage that the board will not be so much crowded.


Please let me have your advice.

best regards

Christian

(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1534
Location: Basel, Switzerland

switzerland.gif
PostPosted: Mon Aug 11, 2014 5:47 am    Post subject: Reply with quote

Hi,

Why not use an ISR (Interrupt service routine) to handle the stepper motor. I've done something like that for a board with a LCD and a L297/L298. The main loop calculates how often the ISR should fire and the ISR just toggles the correct pins.

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5071
Location: Holland

blank.gif
PostPosted: Tue Aug 12, 2014 9:23 am    Post subject: Reply with quote

yes it is simplest to use a timer ISR. It can create all signals.
you can use a byte variables named bStepSate and check its value in the ISR. Then based on the value you take some action and change the value/state.
Depending on the frequency it will give some load on the system while executing but it will not slow down the main code much.

when splitting tasks you can use i2c. it is very simple and reliable. when having enough pins you could use SPI ot shiftin/shiftout as well.
i2c slave works in interrupt mode so i would use that, but my first choice would be to put the code in the main chip.

_________________
Mark
Back to top
View user's profile Visit poster's website
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 64
Location: Aachen

germany.gif
PostPosted: Wed Aug 13, 2014 8:28 pm    Post subject: Reply with quote

Dear Ian, Dear Mark,
many thanks for your advice.
After looking at the pors and cons I think that I will go with the Ic interface.
The interface consumes 2 pis only and is pretty fast.
So far I have no experience with Ic.
I was searching the web for some code examples that I may adapt to my requirements.
Declaring CPU as the master and sending data is logical to me.
How to receive a set of bytes with CPU B is not clear to me.

In the samples I could not find hints dealing with a CPU.

I am still free to use the hardware TWI on both CPUs.

Not clear to me is :
Is it necessary to declare CPU B to be a slave ?
How can I declare CPU B as a slave ?
What is the configuration of the receive buffer ? Array of bytes ?

The solution i am thinking about is to use an array of bytes, a index counter and an ISR.
The ISR executed when a byte is tranferred modifies the index in which the value is written to and increments the counter.
I the data transfer protocol I need to send a byte that indicates how the data has to be interpreted e.g. word, integer, single ...
or
I send a whole set of data ( appr. 20 bytes in my application) in a fixed frame and excerpt the data using the overlay feature.

If there are better or already in Bascom integrated solutions please let me know.

best regards

Christian
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1534
Location: Basel, Switzerland

switzerland.gif
PostPosted: Wed Aug 13, 2014 8:39 pm    Post subject: Reply with quote

Hi,

You need the i2c slave lib (which is a separate product).

Simple put when something happens on the i2c bus the lib calls the defined routine (that you write) to handle the action. For example:-

Code:

'Byte received from Master
'Twi holds the value sent
'Twi_btw holds the index
Twi_gotdata:
      ByteFromMaster = Twi                         'Receive from master
Return

'Master wants byte
'Twi_btr holds the index
'Twi should contain the value to be sent when the callback ends
Twi_master_needs_byte:
         Twi = 1                        'Return Byte          '
Return
 


I would still recommend using an ISR for driving the stepper motors, but it's your choice.

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum