Friday, August 29, 2014

Interfacing RTC DS1307 with microcontroller 8051 (89v51rd2)

The DS1307 is a real time clock capable to provide information of date and time. In this article we will see how to interface this chip with a 8051 based MCU and get Date Time information out of this chip and into our MCU which can be sent for display later on.

To communicate with DS1307 we need to use the I2C protocol. The memory layout of this chip is explained in detail in its datasheet and so I will omit that part here. Some important have to kept in mind beforehand though:
1. The data read from the DS1307 will be in Packed BCD format.
2. We need to write a time (hr:min:sec) and date (day:date:month:year) before we start reading. As per the memory layout we need to write from location 0x00 to 0x06 (please refer the data sheet).
3. Optionally a square wave output (at specified frequencies) can be obtained from Pin 7 of the chip, and to do so we need to set the control register located at  0x07.

Steps to Write Data:
1. Generate an I2C start condition - Keeping the SCL line high, provide a high to low pulse on the SDA.
2. Send the address 7 bit address 1101000 and 1 bit read/write mode option. In this case we have to provide option for write mode which is '0'. Hence send the byte 0xD0.
3. Send the starting address from where the data will be written in the DS1307. In our case we will send the address 0x00, since we want to write all the information of date & time.
4. Next we send the bytes for sec, min, hour, day, date, month, year one after the other. These bytes has to be converted into packed BCD, otherwise erroneous result might be obtained. After each byte write, the address gets incremented by 1, and so we need not write the next address every time. Only the above sequence has to be maintained. After writing each byte we need to get the acknowledgement from the DS1307 to verify that the data transmission was a success or not. SUCCESS = 0
5. Generate an I2C stop condition - Keeping the SCL line high, provide a low to high pulse on the SDA.
6. Provide some delay.

After doing the above steps, the RTC DS1307 will be up and running, and now if we read it we will get actual date time data.

Steps to Read Data:
1. Generate an I2C start condition - Keeping the SCL line high, provide a high to low pulse on the SDA.
2. Send the address 7 bit address 1101000 and 1 bit read/write mode option. In this case we have to provide option for write mode first which is '0', since we have to write the address first and then provide the read mode option. Hence send the byte 0xD0 first.
3. Send the starting address from where the data will be read from the DS1307. In our case we will send the address 0x00, since we want to read all the information of date & time.
4. Generate another I2C start condition.
5. Send the address 7 bit address 1101000 and 1 bit read/write mode option. In this case we have to provide option for read mode which is '1'. Hence send the byte 0xD1.
6. Now read the data byte by byte. The data will be sent from address 0x00 to 0x06. After reception of each byte, an acknowledgement has to be send to the DS1307. ACK = 0 for one clock pulse.
7. After reading the last byte a master non-acknowledgement has to be sent to denote end of read operation. ACK = 1 for one pulse.
8. Generate an I2C stop condition - Keeping the SCL line high, provide a low to high pulse on the SDA.
9. Provide some delay.



Thursday, May 5, 2011

Partition with fdisk


fdisk is started by typing (as root) fdisk device at the command prompt. device might be something like /dev/hda or /dev/sda.
To check the list of devices available type fdisk -l
The basic fdisk commands you need are:
p – Print the partition table.
n – Create a new partition.
d – Delete a partition.
q – Quit without saving changes.
a – Make a partition bootable.
w – Write the new partition table and exit.
Changes you make to the partition table do not take effect until you issue the write (w) command. Here is a sample partition table:

 
Disk /dev/sdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes
 
   Device Boot    Start       End    Blocks   Id  System
/dev/sdb1   *         1       184    370912+  83  Linux
/dev/sdb2           185       368    370944   83  Linux
/dev/sdb3           369       552    370944   83  Linux
/dev/sdb4           553       621    139104   82  Linux swap

Now here is how to partition a USB Drive for example:
1.      Type: fdisk /dev/sda or fdisk /dev/sdb (whatever may be your drive; to be sure which one is your drive just enter the USB Stick and check in the /dev, you will see a new device sda or sdb or sdc etc. is shown which was not there before inserting the stick.)
2.      Type: p to print the partition table of this device.
Command (m for help): p
 
Disk /dev/sdb: 64 heads, 63 sectors, 621 cylinders
Units = cylinders of 4032 * 512 bytes

3.      Now to create new partition type: n
        Command (m for help): n
        Command action
           e   extended
           p   primary partition (1-4)
Type: p for primary partition, and then do the following:
Partition number (1-4): 1
First cylinder (1-621, default 1) :<ENTER>
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-621, default 621):+384M
+384 – This is the size of the partition in MB; give whatever size you want.
4.      Next to set up the partition for swap, just create another partition the same way:
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (197-621, default 197):
Using default value 197
Last cylinder or +size or +sizeM or +sizeK (197-621, default 621): +128M
2 more partitions can be created the same way.
5.      Make the first partition bootable:
Command (m for help): a
Partition number (1-4): 1

6.      Make second partition of type swap:
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap)      
Command (m for help): p

7.      Finally issue a write command w to write the partition on to the disk. Without this command the changes will not be retained.


Friday, April 22, 2011

Windows CE Application - Create Buttons

Hi,
The content shown below will help you create a window, then create a button on top of it and add functionality to that button (i.e. do something if the button is pushed).
Here are the steps to to do so on Windows Embedded CE.

1. First of all lets create a window. Here is the code:

    g_hwnd1 = CreateWindow(c_szWndClass, TEXT(""), WS_VISIBLE, 0, 0,
         rcWorkArea.right, rcWorkArea.bottom, 
                                                         NULL, NULL, g_hInst, (LPVOID)NULL);

The c_szWndClass has to be a registered class, and the rcWorkArea.right and rcWorkArea.bottom parameters are the width and height of the screen respectively.

2. Now we will create a button on this window. Here is the code:

    g_hwndbtn1 = CreateWindow(L"button", L"Push Button", 
(WS_CHILD | WS_VISIBLE),
(rcWorkArea.right/2), 
(rcWorkArea.bottom), 
ButtonWidth, ButtonHeight, 
g_hwnd1, 
(HMENU)ID_BUTTON1, 
hInst, 
NULL);

    The ID_BUTTON1, ButtonWidth and ButtonHeight has to be defined prior to this code.
    hInst is the application instance which is passed into WinMain.

3. Now, lets add some functionality to this button, i.e. if this button is pushed then another window should open. Here we will use a callback function to achieve:


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent; 
    switch (message) 
    {
    case WM_COMMAND:
   wmId    = LOWORD(wParam); 
   wmEvent = HIWORD(wParam);
   switch (wmId)
   {
    case ID_BUTTON1:
SendMessage(hWnd,WM_LBUTTONDOWN,LOWORD(wParam),lParam);
     g_hwnd2 = CreateWindow(c_szWndClass, TEXT(""), WS_VISIBLE, 0, 0, rcWorkArea.right rcWorkArea.bottom, NULL, NULL, g_hInst, (LPVOID)NULL);
SendMessage(hWnd,WM_LBUTTONUP,wParam,lParam);
break;
          }
    }
return 0;
}


The other cases of the Callback function are not shown here, and they have to be added properly. Scroll down for the entire code:


#define ID_BUTTON1 0x8801
HWND g_hwnd1, g_hwnd2, g_hwndBtn1;
TCHAR const c_szWndClass[] = TEXT("Window");
HINSTANCE g_hInst;



int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{
MSG msg;
    HACCEL hAccelTable;
BOOL mess;
//Store the application instance in a global.
g_hInst = hInstance;
MyRegisterClass(hInstance);
// Perform application initialization:
    InitInstance (hInstance, nCmdShow);

    hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)180);
    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;    
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASS wc;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC) WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 4;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = c_szWndClass;
    return RegisterClass(&wc);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
RECT rcWorkArea;
SystemParametersInfo(SPI_GETWORKAREA, 0, (void*)&rcWorkArea, 0);
  // Create a window
 g_hwnd1 = CreateWindow(c_szWndClass, TEXT(""), WS_VISIBLE, 0, 0,  rcWorkArea.right, rcWorkArea.bottom, NULL, NULL, g_hInst, (LPVOID)NULL);
 ShowWindow(g_hwnd1 , nCmdShow);
 UpdateWindow(g_hwnd1 );
 // Create a button.

g_hwndbtn1 = CreateWindow(L"button", L"Push Button", (WS_CHILD | WS_VISIBLE),  (rcWorkArea.right/2), (rcWorkArea.bottom), ButtonWidth, ButtonHeight, g_hwnd1,  (HMENU)ID_BUTTON1, hInst, NULL);


  return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
 switch (message) 
 {
case WM_COMMAND:
wmId    = LOWORD(wParam); 
wmEvent = HIWORD(wParam);
switch (wmId)
{                 
      case ID_BUTTON1:
          SendMessage(hWnd,WM_LBUTTONDOWN,LOWORD(wParam),lParam);
            g_hwnd2 = CreateWindow(c_szWndClass, TEXT(""), WS_VISIBLE, 0, 0,
                      rcWorkArea.right, rcWorkArea.bottom, NULL, NULL, g_hInst, (LPVOID) NULL);
          SendMessage(hWnd,WM_LBUTTONUP,wParam,lParam);
break;
      }
      default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

Friday, March 25, 2011

Developing Simple Application on Windows Embedded CE 6.0

Hello,
Here is a simple application on Windows Embedded CE 6.0. Below contains the steps of creating the application, customizing it and testing it on the emulator. For those who are just beginning application development on Wince, this would be something you should try. So here it goes..

Before building a new Wince application you must have a built and tested (i.e. running) Device Emulator workspace. If you don't have it the follow this link.

Here we will take the example of a hello world application. The code is provided by Wince itself, so there is nothing much to do about writing the application. To start, first go to the solution explorer, subproject and right click on it. See below:


Click on Add New Sub-project, and you will get the following window:


Select WCE Application and give any name, as shown above. The following window will appear:


For a hello world application check the last radio button, as shown above. Then click on Finish. Now you will see the a new sub-project has been created under sub-project menu. See below:






To test this application directly, just right click on the application name, on the solution explorer and click Build. Then run the emulator to test the application. You should get something like this:

Well, that was really simple! It all happened in mouse clicks. Now its time to customize this application, put some buttons, create different windows etc.

To do anything on this application, first we need to understand the code. Once you are done with it, you can change the code confidently, without facing much problems.

The first customization would be changing the string "hello world!". If you search the source file, you will not find the string. So how does it get displayed? If you go through the code, you will come across a API CreateWindow which takes the second argument as the title. The title is defined in the resouce file (i.e. the '.rc' file). Hence to change this string, just change the title in the resource file.

For creating buttons and other stuff click here.

Cheers!!

Thursday, March 3, 2011

Building a Workspace for Device Emulator in Windows Embedded CE 6.0

Windows Embedded CE or WINCE 6.0 is a Real Time OS which can run on many hardware architectures. But in-case you are new to wince, you would prefer to build your newly installed OS on an emulator and test it.

For information about how to install wince 6.0 please click on the following link:
http://tech-stuff-home.blogspot.com/2011/03/how-to-download-and-install-windows.html

The following steps will guide you to build a binary image for ARMv4I Device Emulator, which is by default provided in wince 6.0 by Microsoft, and finally test it on your Desktop.

1. Creating a new Platform Builder project:

The first page when you run Microsoft Visual Studio 2005, will look something like this:


Now to create a new Platform Builder (i.e. wince) project, just go to File -> New -> Project and click on it; you will get the following screen:


Select Platform Builder for CE 6.0 and click on OS Design on the right hand side. Give a name like "Emulator". By default the location will be inside "drivename\WINCE600\OSDesigns". Just click Ok and in the following screen just give a Next. Then you will this window:


This window consists the set of Board Support Packages (BSP) which are currently installed in wince. Select the "Device Emulator: ARMV4I" BSP and press Next. The next window look somthing like this:


This window shows you the kind of device you are targeting. Just click on Industrial Device as shown and click Next. On you will get the following window:


Select Internet Appliance and press Next. On the following two windows there is nothing to worry about and Next. Finally a window will come saying OS Design Project Wizard Complete. Just press Finish to complete the process. You are almost done. A Catalog Item Notification window may come; just press acknowledge and you will get the following on your Visual Studio:





Now, you have successfully created a new Platform Builder project and your workspace is ready for building.

2. Building the Workspace:

There are two kinds of build you can do in wince -- Debug Build and Release Build. By default the mode is selected for Debug which will take a long time, so here is how to change to Release Build. On the top middle of the toolbar, there is a Solution Configuration Drop Down box, see below:




Select the Release Build option as shown above. Now its time to build. Go to Build and press Build Solution. See below:


It will take about 15 to 20 minutes to build, and finally you will see a success message; obviously if there are no errors. Now its time to test it.

3. Testing the built workspace:

For testing the new OS, go to Target -> Connectivity Options, see below:


Click on it and the following window will come. Just verify that you have the setting as shown below. If not, then use the drop down boxes to get these settings:


Once you have these settings, just click apply, then close and its ready. on the same Target tab click on the first option that is Attach Device you will get the following:


Once the download is complete, you will get Windows Embedded CE 6.0 running on your Desktop on a different window, well it may take some time! Here is the screen shot of wince 6.0 (you may get a smaller screen due to some settings:




Now that you have built a workspace successfully, you would like to write some application on it. For creating applications and more click here.

Please comment if you have any queries regarding this post!!

Wednesday, March 2, 2011

How to Download and Install Windows Embedded CE 6.0 Evaluation Version

Prerequisites for Installing Windows Embedded CE 6.0:
1. The PC must have Windows XP Service Pack 2 running.
2. Microsoft Visual Studio 8 IDE must be installed.
3. Make sure you have at least 10GB space in any drive, where Windows CE 6.0 will be installed.

The files required to download are:
1. Windows Embedded CE 6.0 Evaluation Version -- Setup.exe
2. Windows Embedded CE 6.0 Platform Builder Service Pack 1.msi
3. For Windows CE 6.0 R2 -- CE6R2.iso and setup.exe
4. For Windows CE 6.0 R3 -- CE6R3.iso and setup.exe

For Downloading Windows CE 6.0 click here. Then you can see the first setup.exe. Download it and then scroll down to find "What Others Are Downloading", there you can find steps 2, 3 and 4 to download. Download all the files mentioned above for complete installation. Keep the four parts of the download in four different folders for reducing confusion (you can name each folder steps 1 through 4).

Now after all these files are downloaded, its time to install Windows CE 6.0. Here are the steps:
1. Run the setup.exe of step1. This is to install Platform Builder. Installing wince is just like installing any other software on Windows XP. The only window which is to be understood is shown below:


This window shows the available BSPs and some wince components/code which can be installed. Make sure to include the Shared Source, which is marked as X. Also a list of BSPs are given as an option for installation. ARMv4I which is the BSP for Emulator, is by default marked as 'yes'. If required you can select any other also (eg. x86 or MIPS).

2. The second step of the installing process is to install the Platform Builder Service Pack. Just run the Windows Embedded CE 6.0 Platform Builder Service Pack 1.msi file and the rest is easy.

3. Third step is to install 'R2'. Since the ISO file is already downloaded, just run the setup file and follow the instructions and you are done.


4. Similarly, for the final step, i.e. installing 'R3', just run the setup and follow the instruction.


Hence, by following these simple steps your Windows Embedded CE 6.0 Real Time OS Evaluation Version is ready and configured for usage. Now you can develop real time applications and driver as per your requirement.


Please comment if you face any doubts..