UART with realtime/heavy computation

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

UART with realtime/heavy computation

Postby aurora » Tue Nov 15, 2011 9:03 pm

Hi guys,

Have been pondering for a long time, how would you prepare your code/program if you are require to do realtime/heavy computation, i.e. pooling readings from sensors, calculation, sending output, and at the same time transmit/receive massive data via UART?

The master microcontroller requires to do rapid calculation at refresh rate of 50Hz, and transmit 20-30words of each cycle. What I did earlier was to run the calculation, and then transmit the data. As my codes become longer, the idling time (per cycle) is not enough to transmit the data, even at highest baudrate possible.

The straight forward method I can think of is to use the transmit/receive hardware buffer (2 words for PIC16/18, and 4 words for dsPIC33), by filling up in batch and regularly check the buffer status, and repeat. It make my codes very messy, and the regular checking means I have to test my code every time I change it.

As my project grow, I have another microcontroller coming in and need to interface the master. The third will be connect and communicate using second UART.

Anyone has similar experience?
aurora
Discoverer
 
Posts: 126
Joined: Sun Jun 07, 2009 4:52 pm

Re: UART with realtime/heavy computation

Postby Brian Griffin » Tue Nov 15, 2011 9:27 pm

aurora WROTE:Hi guys,

Have been pondering for a long time, how would you prepare your code/program if you are require to do realtime/heavy computation, i.e. pooling readings from sensors, calculation, sending output, and at the same time transmit/receive massive data via UART?

The master microcontroller requires to do rapid calculation at refresh rate of 50Hz, and transmit 20-30words of each cycle. What I did earlier was to run the calculation, and then transmit the data. As my codes become longer, the idling time (per cycle) is not enough to transmit the data, even at highest baudrate possible.

The straight forward method I can think of is to use the transmit/receive hardware buffer (2 words for PIC16/18, and 4 words for dsPIC33), by filling up in batch and regularly check the buffer status, and repeat. It make my codes very messy, and the regular checking means I have to test my code every time I change it.

As my project grow, I have another microcontroller coming in and need to interface the master. The third will be connect and communicate using second UART.

Anyone has similar experience?


If your master microcontroller is a high-end one, easiest way out is use an RTOS.

Another thing is, if you have an oscilloscope (or an ICD), have you measured the times for the calculations, the transmitting activity, and the receiving activity?

Also, what are you reading from the sensors? Are these periodic, or aperiodic?
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: UART with realtime/heavy computation

Postby aurora » Fri Nov 18, 2011 5:02 pm

I was using PIC18 earlier, then shift to dsPIC, migrating the code is one tough job, have to change from different compiler. I can't afford a high end microcontroller, after all this is a pass-time hobby.

I don't have a oscilloscope, limited to using the stopwatch in Mplab SIM, and also a different timer circuit to check the time lapsed between transmissions.

The sensor reads at frequency of 50hz. The constraint is not at the sensor, but at the computation which take up most of the cycles. The UART transmission is important for it to send information to an slave microcontroller (more computing), and received further commands.
aurora
Discoverer
 
Posts: 126
Joined: Sun Jun 07, 2009 4:52 pm

Re: UART with realtime/heavy computation

Postby Brian Griffin » Fri Nov 18, 2011 8:14 pm

aurora WROTE:I was using PIC18 earlier, then shift to dsPIC, migrating the code is one tough job, have to change from different compiler. I can't afford a high end microcontroller, after all this is a pass-time hobby.

I don't have a oscilloscope, limited to using the stopwatch in Mplab SIM, and also a different timer circuit to check the time lapsed between transmissions.

The sensor reads at frequency of 50hz. The constraint is not at the sensor, but at the computation which take up most of the cycles. The UART transmission is important for it to send information to an slave microcontroller (more computing), and received further commands.


Do you have a hardware debugger, such as ICD2/3? You can measure the time taken to execute between the codes.

For the computation, have you write it in assembly? It may be tough but you can keep track of the no. of cycles used during the process.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: UART with realtime/heavy computation

Postby aurora » Sun Nov 20, 2011 5:05 pm

Yup, I have ICD2. I thought debug mode is the same MPLAB SIM? It has the stopwatch, watch window, and logic analyzer. I use the MPLAB SIM to track my code by single stepping or to nearest breakpoint.

Assembly is way out of the question, I have too many lines of code to be convert to assembly! Looks like there is no other way but to check the buffer flag between calculation.
aurora
Discoverer
 
Posts: 126
Joined: Sun Jun 07, 2009 4:52 pm

Re: UART with realtime/heavy computation

Postby Brian Griffin » Sun Nov 20, 2011 5:47 pm

aurora WROTE:Yup, I have ICD2. I thought debug mode is the same MPLAB SIM? It has the stopwatch, watch window, and logic analyzer. I use the MPLAB SIM to track my code by single stepping or to nearest breakpoint.

Assembly is way out of the question, I have too many lines of code to be convert to assembly! Looks like there is no other way but to check the buffer flag between calculation.


Hmm. Use the ICD2 for measuring time taken if you have it. Else, use the oscilloscope.

Have you measured the time taken to fill the buffer? And if your code have interrupts, have you measured the time taken for the interrupt execution (interrupt latency)?

Some microcontrollers (like the dSPIC33F and PIC32) contains hardware level DMA (Direct Memory Access). The data can go inside/outside the memory from the peripherals without the intervention of the CPU. However, to write the routines to access the DMA is not really simple.

Also, if you don't mind - is there an offending code fragment you can share to us? :)
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: UART with realtime/heavy computation

Postby Brian Griffin » Tue Jan 10, 2012 11:56 pm

Something to add on here:

I've checked the official FreeRTOS manual, and all of these can be easily done with that RTOS. The heavy transmitting/receiving the data through UART can be simply done by managing the queue in the RTOS, or by using a DMA (direct memory access).

I've tried a simple GLCD moving graphics using FreeRTOS and it can handle that pretty well.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: UART with realtime/heavy computation

Postby robosang » Thu Jan 12, 2012 9:56 am

none of my business, because I yet to "play" with RTOS, but hey! Good Work!
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 5 guests

cron