Friday, June 18, 2010
Common Interrupt Pitfalls
I just want to share some interesting facts that I found in SDCC Compiler User Guide.
From my experience, I think these facts are very important to keep in mind of a firmware programmer. Last time, I got an experience that my program executed wrongly even though I could not find any fault in my program. Suddenly, I remembered stack overflow problem and the program worked correctly after I changed the stack size.
Thursday, June 10, 2010
Circular Buffered UART Com Module for 8051 Microcontroller
A lot of embedded systems uses UART communication. That is why, I would like to share a circular buffered UART comm module here. I have developed the module for 8051 microcontroller but it can easily be modified for other microcontrollers as well.
UART-Timer-8051 on GitHub
Using Circular Buffered UART Com Module
You need to put the header files in the module you want to use them. In my example, I put all my header files into 'headers.h' file and I just need to include that file. Transmit and receive buffer sizes need to be defined in ComConfig.h file. And then, define a function to call on receive event. In the main function, poll the ComChkRx() function to retrieve the received data from the buffer. The source code for the example can be seen atUART-Timer-8051 on GitHub
Thursday, June 3, 2010
Soft-Timer Module for 8051 Microcontroller
Almost every embedded system uses timers in their firmware. 8051 microcontroller has only two or three hardware timers, and generally, it is not enough to use hardware for all the timers your system needs to have.
Furthermore, not all timers need to have hard timing requirement. For example, blinking an LED indicator every second is accurate enough if the timing error is less than a few millisecond.
That is why, I normally use software to implement all the timers that have soft timing requirement. Here, I would like to share a soft-timer module that I developed for 8051 microcontroller but it can easily be modified for other microcontrollers as well.
The source code for the example can be seen at
UART-Timer-8051 on GitHub
Write a function to be called when the timer time out. Write another function to set the timing parameters and start the timer. In my example, they are SysSBYLEDTmrTO() and SysSBYLEDInit() in System.c module.
Open TmrConfig.h and follow the 3 steps as indicated in the comment. If your compiler does not support function pointers, you can always replace with switch structure.
In the main function, initialize and poll the timer module by calling TmrInit() and TmrTask() respectively which are defined in Tmr.c module.
UART-Timer-8051 on GitHub
Using soft-timer module
You need to put the header files for soft-timer in the module you want to use them. In my example, I put all my header files into 'headers.h' file and I just need to include that file.Write a function to be called when the timer time out. Write another function to set the timing parameters and start the timer. In my example, they are SysSBYLEDTmrTO() and SysSBYLEDInit() in System.c module.
Open TmrConfig.h and follow the 3 steps as indicated in the comment. If your compiler does not support function pointers, you can always replace with switch structure.
In the main function, initialize and poll the timer module by calling TmrInit() and TmrTask() respectively which are defined in Tmr.c module.
Wednesday, May 26, 2010
Using SPI on Low-End Microcontroller
SPI is a simple and efficient inter-IC communication bus. A lot of peripheral chips such as Real Time Clock and EEPROM come with SPI or I2C bus. If there is no special reason, I prefer to use SPI than I2C because it is faster and simpler. It is also very easy to emulate in software.
Last time, I used 10MHz SPI LED driver chip with low end 4MHz microcontroller. Design priority was cost efficiency. Microcontroller cost less than a dollar but it had enough flash to store the firmware and a few display fonts. At first, I used interrupt and circular buffers to send and receive to and from SPI bus. I just wrote to the buffer and let the hardware and interrupt handled all the communication tasks as I usually do with slower long distance buses such as RS232 and CAN bus. It was OK in normal condition. The problem was that I wanted to update big 96x16 dot-matrix LED at the frame rate of 125Hz and the CPU utilization was very high. Consequently, it could not perform fast enough when it was executing some simple graphic manipulation tasks such as scrolling the text. Later, I realized that the most used SPI function where CPU spent most of its time was not efficient. Using hardware interrupt is more efficient normally, but it was different in this case- slow CPU with very fast and heavily used SPI. For each byte to SPI, send and receive interrupt functions which cost a lot of CPU cycles had to be performed. I found polling or emulation is faster than using interrupt to send a byte to SPI. Polling is still limited to the bus speeds supported by the hardware. After I modified the firmware to improve SPI function and it worked well. According to my experience, let me highlight some advantages of emulating SPI .
Last time, I used 10MHz SPI LED driver chip with low end 4MHz microcontroller. Design priority was cost efficiency. Microcontroller cost less than a dollar but it had enough flash to store the firmware and a few display fonts. At first, I used interrupt and circular buffers to send and receive to and from SPI bus. I just wrote to the buffer and let the hardware and interrupt handled all the communication tasks as I usually do with slower long distance buses such as RS232 and CAN bus. It was OK in normal condition. The problem was that I wanted to update big 96x16 dot-matrix LED at the frame rate of 125Hz and the CPU utilization was very high. Consequently, it could not perform fast enough when it was executing some simple graphic manipulation tasks such as scrolling the text. Later, I realized that the most used SPI function where CPU spent most of its time was not efficient. Using hardware interrupt is more efficient normally, but it was different in this case- slow CPU with very fast and heavily used SPI. For each byte to SPI, send and receive interrupt functions which cost a lot of CPU cycles had to be performed. I found polling or emulation is faster than using interrupt to send a byte to SPI. Polling is still limited to the bus speeds supported by the hardware. After I modified the firmware to improve SPI function and it worked well. According to my experience, let me highlight some advantages of emulating SPI .
- It can sometimes be better in performance to emulate SPI in software.
- It is more reliable because it is simpler and it can avoid potential pitfalls of using interrupt.
- It is faster, easier and less error prone to write a simple code rather than reading datasheet for variety of register settings for every new microcontroller you encountered.
- Most importantly, it is portable and it is not dependent on hardware.
//-------------------------------------
unsigned char spi(unsigned char d)
{
unsigned char i;
SCLK=1;
EN=1;
for(i=0;i<8;i++)
{
MOSI=(d & 0x80)?1:0;
//Delay(period/2)-optional for slower SPI bus speed
SCLK=0;
d<<=1;
d|=MISO;
//Delay(period/2)-optional for slower SPI bus speed
SCLK=1;
}
EN=0;
return d;
}
//-------------------------------------
Monday, May 24, 2010
Astable Multivibrator using Op-amp
One of my friends wanted to calculate the oscillation frequency of an op-amp circuit. He had measured the output frequency of the circuit and it was 109 kHz. He asked my help to derive the relation between the oscillation frequency and its passive components. Although I have been away from analog circuits for a long time, I agreed to have a look. Then, he took the picture of circuit diagram using his hand phone and sent to me using MMS.
The basic idea behind the circuit is simple, so I calculated the frequency, took the following pictures using my hand phone and sent back to him.
The basic idea behind the circuit is simple, so I calculated the frequency, took the following pictures using my hand phone and sent back to him.
Saturday, May 1, 2010
Choosing name for our daughter
Here are a few things that we considered when we were choosing a name for our newborn baby. Most of the idea is inspired from the book 'Conception, pregnancy and birth' by Dr. Miriam Stoppard.
- We hoped that the name is suitable for her at all stages of life.
- We took extra caution not to have any reason why our child might be teased because of the name we've chosen.
- We also think that the name should have good meaning.
- In case of nationality and traditions, we liked to give her a Myanmar name that follows traditions. For example, to have a first name that matches with the weekday she was born.
- We like a name because of its sound. It should be naturally harmonious and sounds good like a sweet music. It should also be read smoothly like a poem.
- We like to have one word from father's name and one word from mother's name in the name of our baby.
- Although we might be influenced by many considerations, we had to remind ourselves that the name we were choosing was for our baby, and hopefully it will please her throughout the whole of her life.
Wednesday, April 28, 2010
C Programming on Windows
Some of my friends who started learning C programming have asked me which IDE is good to use on Windows.
For me, I personally like to use Microsoft Visual Studio Express which is available for free at http:// www.microsoft.com /express/
Dev-C++ from Bloodshed is also a popular one and it can be downloaded from http:// www.bloodshed.net/ devcpp.html.
The following is an example to create new C project on Visual C++ 2008 Express Edition. Go to File menu>>New>>Project... New Project window will appear. Select Win32 in Project types: Visual C++ Select Win32 Console Application in Templates: Visual Studio Installed templates Enter project name in the name text box and browse the folder to save the project. Click OK. Win32 Application Wizard box will appear. Click Next. Choose Console application for Application type and Check Empty project in additional options: Click Finish button. In the Solution Explorer window near the left, right click Source Files and click Add>>New Item... as shown in the following picture.
Add new item window will appear.
In the Name text box, enter the file name with .c extension e.g. StrPos.c
I have been frequently asked how to write a program to find the case insensitive string without using C library functions and the following is an example.
Dev-C++ from Bloodshed is also a popular one and it can be downloaded from http:// www.bloodshed.net/ devcpp.html.
The following is an example to create new C project on Visual C++ 2008 Express Edition. Go to File menu>>New>>Project... New Project window will appear. Select Win32 in Project types: Visual C++ Select Win32 Console Application in Templates: Visual Studio Installed templates Enter project name in the name text box and browse the folder to save the project. Click OK. Win32 Application Wizard box will appear. Click Next. Choose Console application for Application type and Check Empty project in additional options: Click Finish button. In the Solution Explorer window near the left, right click Source Files and click Add>>New Item... as shown in the following picture.
Add new item window will appear.
In the Name text box, enter the file name with .c extension e.g. StrPos.c
I have been frequently asked how to write a program to find the case insensitive string without using C library functions and the following is an example.
#include <stdio.h>
typedef signed char CHAR;
typedef signed int POSITION;
#define ToL(c) (((c)>='A')&&((c)<='Z')?(c+0x20):(c))
POSITION strcmp(CHAR* s1,CHAR* s2)
{
for(;*s2;s1++,s2++) if(ToL(*s1)!=ToL(*s2)) return 0;
return 1;
}
POSITION stripos (CHAR* haystack,CHAR* needle,POSITION offset)
{
for(;*(haystack+offset);offset++) if(strcmp(haystack+offset,needle)) return offset;
return -1;
}
int main(int argc,char *argv[])
{
CHAR str1[]="Hello! Good morning!";
CHAR str2[]="good";
printf("\nFound at: %d \n",stripos(str1,str2,0));
return 0;
}
After that, you can run the program by pressing F5 or by clicking Debug menu>>Start Debugging.
You can also click Debug menu>>Start Without Debugging.
Tuesday, February 2, 2010
VB2005 Timers
There are several types of timers offered by the .NET Framework. Inside Windows Forms applications, you can use the System.Windows.Forms.Timer control.
You can use either the System.Threading.Timer class or the System.Timers.Timer class if your application doesn't have a user interface.
The following is an example that uses the Timer class in the System.Threading namespace to call back a given procedure. After the timer is running, you can change timer values only by means of a Change method, which takes only two arguments, the due time and the period. The Timer object has no Stop method. You stop the timer by calling its Dispose method.
The following is an example that uses the Timer class in the System.Threading namespace to call back a given procedure. After the timer is running, you can change timer values only by means of a Change method, which takes only two arguments, the due time and the period. The Timer object has no Stop method. You stop the timer by calling its Dispose method.
Imports System.Threading Dim dueTime as New TimeSpan(0,0,1) Dim period as New TimeSpan(0,0,0,0,500) Dim t As New Timer(AddressOf TimerProc, Nothing, dueTime, period) Dim tEn As Boolean = True Private Sub TimerProc(ByVal state As Object) If tEn = True Then 'Do timer things End If End Sub
Friday, December 11, 2009
Protecting Flash Memory of a Fujitsu MCU
I had a requirement to protect flash memory of Fujitsu MB95F128JB MCU but I could not find any satisfactory hardware/software tool to protect it.
Later, I conceived an idea to hack its .mhx file. After adding the following line before the last line of the .mhx file, it had been protected.
S104400001BARef: http://en.wikipedia.org/wiki/SREC_%28file_format%29
Saturday, October 10, 2009
Multidrop network for RS232
I got a requirement to communicate one master device and eight slave devices. I intended to use RS485 half-duplex communication for this system but all devices happened to have only RS232 interfaces.
RS232 communication is a sort of communication that is to be used for one-to-one system. There is no problem if only the master device transmits and all slaves are receiving. The problem is that transmit lines of the slaves cannot be disabled and so that they cannot connect to the same line.
After analyzing the voltage signals, I have got an alternative solution to this problem. By adding a diode to the transmit line of each slave, I can use the system just like RS485 half-duplex communication. See the following figure for the hardware connection.
Updated: 2017 Sep 02
Thanks for all your comments. I have added more configurations according to your comments.
Updated: 2017 Sep 02
Thanks for all your comments. I have added more configurations according to your comments.
RS232 network with multiple masters and multiple slaves
UART/TTL-Serial network with single master and multiple slaves
If you are not using RS232 transceivers to change the physical signal voltages, the outputs of UARTs are still TTL level signals. In this case, you can connect multiple UARTs to a single master as follow.UART/TTL-Serial network with multiple masters and multiple slaves
Subscribe to:
Posts (Atom)



