## Thursday, December 30, 2010

### Inkscape

Inkscape is a vector graphic editor similar to Illustrator or CorelDraw. Inkscape is better in the sense of cost because it is free. It is also an open source software. It can be downloaded for free at http://inkscape.org/ The nice feature is that graphics can be saved in pdf format. It allows high quality graphics when you use it with pdflatex to produce pdf files. Since they are Scalable Vector Graphics, unlike bitmap images, there is no problem even when they are magnified.

The following is an Inkscape example to draw a line with arrow head. 1. Select Bezier Curves tool. Click to start a line. Double click (or click then press enter key) to end the line. 2. Click Fill and Stroke... command under Object menu (or click Fill: or Stroke: near lower left corner). In the Stroke style tab, change End Markers to arrow. 3. Line color can also be changed in Stroke paint tab. Click Extensions->Modify Path->Color Markers to Match Stroke to change the arrow head color also. To insert math symbols, you can press ctrl+u and type the hexadecimal Unicode code point of the symbol in a text box. Then, press enter.

http://wiki.inkscape.org/wiki/index.php/FAQ
http://www.unicode.org/charts/PDF/U0370.pdf

## Monday, July 12, 2010

### Random Password Generator

I have a few nagging computer user accounts that regularly ask me to change password. Some of them even remember history of about ten old passwords to prevent me from using them again.
As a side effect, I have to think of new passwords again and again. Later, I use random password generators that are freely available on the Internet. Just for fun, I have also developed a random password generator of my own and you can see it at the following link.

Source PHP file

Ref: http://en.wikipedia.org/wiki/Password_generator

## 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.

### Variable not declared volatile

If an interrupt service routine changes variables which are accessed by other functions these variables have to be declared volatile. See http://en.wikipedia.org/wiki/Volatile_variable.

### Non-atomic access

If the access to these variables is not atomic (i.e. the processor needs more than one instruction for the access and could be interrupted while accessing the variable) the interrupt must be disabled during the access to avoid inconsistent data. Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs and should be protected by disabling interrupts. You’re not automatically on the safe side if you use 8 bit variables though. For example, on the 8051 the harmless looking ”flags |= 0x80;” is not atomic if flags resides in xdata. Setting ”flags |= 0x40;” from within an interrupt routine might get lost if the interrupt occurs at the wrong time. ”counter += 8;” is not atomic on the 8051 even if counter is located in data memory. Bugs like these are hard to reproduce and can cause a lot of trouble.

### Stack overflow

The return address and the registers used in the interrupt service routine are saved on the stack so there must be sufficient stack space. If there isn’t enough stack space, variables or registers (or even the return address itself) will be corrupted. This stack overflow is most likely to happen if the interrupt occurs during the ”deepest” subroutine when the stack is already in use for i.e. many return addresses.

### Use of non-reentrant functions

Calling other functions from an interrupt service routine is not recommended, avoid it if possible. Furthermore nonreentrant functions should not be called from the main program while the interrupt service routine might be active. They also must not be called from low priority interrupt service routines while a high priority interrupt service routine might be active. You could use semaphores or make the function critical if all parameters are passed in registers. Good luck with your programming!

## 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.

### 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 at

UART-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

### 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 .
1. It can sometimes be better in performance to emulate SPI in software.
2. It is more reliable because it is simpler and it can avoid potential pitfalls of using interrupt.
3. 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.
4. Most importantly, it is portable and it is not dependent on hardware.
SPI does not have formal standard. It is just like a shift register. One can easily understand once s/he sees the timing diagram. The following is an example C function to send and receive a byte of SPI data.
//-------------------------------------
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.

## 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.
1. We hoped that the name is suitable for her at all stages of life.
2. We took extra caution not to have any reason why our child might be teased because of the name we've chosen.
3. We also think that the name should have good meaning.
4. 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.
5. 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.
6. We like to have one word from father's name and one word from mother's name in the name of our baby.
7. 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.
#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.
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