Crossware

Table of Contents        Previous topic       Next topic       

Using the Visual C++ Custom AppWizard->Tutorial - Creating an Extension with the AppWizard->Toggling a Port Pin from your Extension

The initial files in your extension project that you will be interested in are ExtensionState.cpp and ExtensionState.h.  These are the main files that receive calls from the Virtual Workshop in a clean C++ form.  The other files in your extension are concerned with exporting the C interface and converting the C calls to C++ calls.

Return to the Microsoft Developer Studio with your extension project open and open file ExtensionState.cpp.

All of the Virtual Workshop interface is represented in this file.  However, you will initially be concerned with only a few of the functions present.

We will add a simple piece of code for toggling a port pin.

Scroll down to the function CExtensionState::GetPortPins().

Add the following lines to this function:

 if (nPortAddress == 0X90)
 {
   // port 1
   if ( (m_nMachineCycles % 100) < 10)
   {
     // lower pin P1.0 for 10 cycles every 100 machine cycles
     *pnPins &= ~0X01;
   }
   else
   {
     // pin P1.0 is high except every 100 cycles
     *pnPins |= 0X01;
   }
   // tell the Virtual Workshop that P1.0 has been handled
   *pnHandledPins |= 0X01;
 }


The modified function should look like this (the lines you have added are shown in bold):

void CExtensionState::GetPortPins(BYTE nPortAddress, BYTE* pnPins, BYTE* pnHandledPins, BOOL bSimulating)
{
 // Clear or set only the pins relevant to this extension
 // If you set or clear any of the pins, then set the corresponding bit in pnHandledPins
 if (nPortAddress == 0X90)
 {
   // port 1
   if ( (m_nMachineCycles % 100) == 0)
   {
     // lower pin P1.0 every 100 machine cycles
     *pnPins &= ~0X01;
   }
   else
   {
     // pin P1.0 is high except every 100 cycles
     *pnPins |= 0X01;
   }
   *pnHandledPins |= 0X01;   // tell the Virtual Workshop that P1.0 has been handled
 }
}

Select Build esim0.dll from the Build menu to rebuild your extension.

Return to your Tutorial project in the Crossware Embedded Development Studio.  Open file main.c.

Remove the line:

printf("Hello world\n");

and replace it with:

while (1);

Select Build from the Build menu to rebuild your 8051 program.

Ensure that there is a tick next to the Displaying Extension Activity item in the Simulate menu.

Select Go from the simulate menu to run your modified 8051 program together with your modified extension.

As your program runs you will see the data that you are putting onto port 1 (0X90) pin 0.   Usually the pin is high (Port 0X90: xxxxxxx1) but periodically it goes low (Port 0X90: xxxxxxx0).

Select Close Simulator from the Simulate menu to close the Virtual Workshop.

Select Display Extension Activity from the Simulate menu to disable the displaying of extension activity.

We will now modify your 8051 program so that is does something with P1.0.

Add the lines:

#include <sfr.h>
_sfrbit mypin = _p1^0;

to the top of main.c.

Add the line:

char nCount = 0;

immediately after the line:

main()

Modify:

 while (1);

to:

 while(1)
 {
   if (mypin == 0)
   {
     nCount++;
   }
 }

Since mypin has been assigned to pin P1.0, every time P1.0 goes low, nCount should increase by 1.

Your main() function should now look like this:

main()
{
 char nCount = 0;
 // delete the following line if you do not want to use the 8051 serial port for i/o
 _InitSerialPort();
 // TODO: Add your source code here
 while(1)
 {
   if (mypin == 0)
   {
     nCount++;
   }
 }
}

We do not want to single step through the startup code in startup.asm and so we will disable assembler debug records:

Select Settings from the Build menu.

Click on the Assembler tab.

Click Generate Debug Records to disable assembler debug records.

Rebuild your 8051 program.  (Select Build from the Build menu.  Click on Yes if asked whether or not you wish to rebuild all affected files.)

Select Step Into from the Simulate menu.  Your program will execute up to your main() function.

Select Local Symbols from the Windows menu.  An empty Locals window will be displayed  nCount is not yet in scope.

Select Trace from the Simulate menu.

You can now watch local variable nCount increment as your program executes.

If your want to watch Port 1, select Port 1 from the View menu.

Let's now look at the cycle count information available:

Select Halt Simulator from the Simulate menu.

Place the cursor on the nCount++; line.

Select Set Breakpoint at Cursor from the Simulate menu.

You should now see a red mark next to the nCount++; line indicating that a breakpoint has been set on this line.

Select Go from the Simulate menu.

Your program will halt when the nCount++; line is executed.

Select Clear Trip Counter from the Simulate menu.

Select Trace from the Simulate menu.

Your program will run in trace mode and you will see the trip counter on the status bar increment to 99 (or sometimes 104).

(The number of machine cycles is not exactly 100 because GetPortPins() is only called between each 8051 instruction.  Since some instructions take more that 1 cycle, the extension may not always receive a call when the machine cycle counter is exactly at 100.)