Category Archives: Programming

Posts related to computer related programming.

Leverage Existing Display Data Channel with I2C Adapter

You’ve been heard about the I2C before? However, do you know the exactly use of it?

I2C or Inter-Integrated Circuit is a two-wire serial bus that commonly used in computer for extremely low-level communication. I2C can easily found in the microcontrollers, embedded systems, and industrial computers.

DDC is simply an implementation of an I2C bus. The solid reason why you must use I2C because the cost is far cheaper than the Commercial USB to I2C, which can cost up to $100 to $250!

DDC is established in four wires: +5VDC, ground, serial data and serial clock, the pin numbers of which will also vary depending on the type of video port used.

For a 15-pin VGA cable, the pins function will be: Continue reading

New WinAVR 20080411 has been released

Open source rocks isn’t it? Here is a new WinAVR 20080411 release available for download. As always new release has lots of tweaks, bug fixes and optimizations. There are new XMEGA series microcontrollers preliminary included in supported device list. So we may have a chance to put hands on these new cool AVR microcontrollers. Here is a list of new things in WinAVR:

  • Support of new AVR devices: ATxmega128A1, ATxmega64A1, ATxmega32M1, ATxmega32C1, ATxmega32U4, ATtiny167;
  • New GCC4.3.0 compiler;
  • New version of AVR-LibC 1.6.2;
  • Two new Makefile templates added where one will generate a library instead of an application (Makefile.lib), while another will enable whole program optimization. This seems to be new thing – it will be interesting to experiment with.
  • New version of SRecord 1.38;
  • New version of SimulAVR 0.1.2.5;
  • Removed AVR-Ada (as they say – temporary)-I don’t need it anyway…

Just recompiled my old code – seems to be working fine. So I recommend you to update to newer WinAVR version.

xDRV-driver for LPT with interrupt service routine

Driver xDRV.sys is driver developed by Иванов Д. Ð’.(www.pcports.ru) and is capable to work(read write) with any PC port under windows NT, 2000, XP. But main advantage of this driver is that it can catch and handle LPT port interrupts (in this case LPT1 with address 0×378 with IRQ 07).

Program part of xDRV

Lets see how this driver can be put in action. First of all download packed archive with driver and library with functions.

xDRV.sys <13.2kB>

In this package you will find driver xDRV.sys, dynamical library xDRV.dll, statical library xDRV.lib, which may be needed while compiling project with statical connection of dll and header file with function prototype description which can control driver. Let see the functions:

bool xDRV_OpenDriver();

Driver xDRV.sys is dynamically loadable. For loading this driver and initializing this function is used. If driver is loaded successfully, then function returns TRUE otherwise FALSE. This function has to be called one time at the beginning. If function returned FALSE then try to restart computer might be xDRV.sys already has been loaded to the memory and function xDRV_StopDriver() wasn’t used. Driver and library has to be placed in same directory where project files are.

void xDRV_StopDriver();

As it was mentioned – this function is important. It has to be called at the end of program work. If not – driver will stay loaded in to memory and will take over interrupts. And during second load program will throw an error.

void xDRV_Write(unsigned short PortAdr, unsigned char PortValue);

This function is used to write one byte of data(PortValue) to port(PortAdr). For instance in order to make all data-lines high in port you should write: xDRV_Write(0×378, 255) ;

unsigned char xDRV_Read(unsigned short PortAdr);

This function obviously is used for reading byte from port(PortAdr).

void xDRV_InitInterput(void(*FunAdr)(void* p), void* param);

This function is most exiting in this driver. This function is the main reason why this driver was created. Driver in order to be able announce about interrupt it is important to give to interrupt function special function address with strict structure. Then when interrupt occur this function will be called. Function should look like:

void fn_name(void *var_name);

Second xDRV_InitInterput() parameter can be any, which will be passed to special function when it is called.

To make things more clear there are tow there are two ready projects with all driver functions used. One project is written in C/C++ language in Microsoft Visual C++ 6.0 environment (WinAPI) and another MFC ( MICROSOFT FOUNDATION CLASS LIBRARY):

WinAPI Test (xDRV.sys) <53.1kB>

MFC Test (xDRV.sys) <44.9kB>

Hardware part

Now its time to prepare hardware part that xDRV.sys could work properly. Go to Control Panel->System->Hardware->Device Manager->Ports(COM and LPT)->Printer Port(LPT1)->Properties. In tab Port Parameters select “Use any interrupt assigned to the port”.

Then in port header set pin 15 to “0”(STATUS register – ERROR) connecting this pin to ground. Now you can start program that uses xDRV.sys. You can try to write and read from port. But lets see how interrupts work. As we said 15 pin should be connected to ground. Driver registers interrupts from 10th pin of LPT port (ACK). Every time this pin goes LOW the interrupt will be registered.

If ERROR line wasn’t connected to ground, then only one interrupt could be registered, but in DATA and CONTROL register would start chaos which would last for about one minute. The reason of this is native port driver which tries to ping to printer which it thinks should be connected to port. But there is no any printer then this actions is useless. So put 15 pin to ground and don’t bother. Of course we lose one control line, but interrupt ability compensates this.

Source: http://www.pcports.ru

 

Control LPT port under windows XP using Delphi

Another way of controlling LPT port under Windows 2000 and XP using Delphi language. In this case library inpout32.dll is used which allows controlling LPT port registers.

Ready project for Borland Delphi 7.0 you can download here: Project Files<187.5kB>

And now how to do this from beginning. Start Borland Delphi 7.0 and make simple form where you can enter Data to be sent to port, Port Address, buttons for writing to and reading from port.

Delphi_LPT.gif

 

If you are familiar with building forms this should be ease task.

OK now lets start programming. First of all wee need somehow to include inpout32.dll in to the project. For this Delphi has several ways, but lets stay to the easiest one when library is in same directory, where project is. Then in header in section uses we have to place function prototypes Out32 and Inp32 with special compiler directive external, saying where to find this finction.

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

 function Inp32(PortAdr: word): byte; stdcall; external 'inpout32.dll';
 function Out32(PortAdr: word; Data: byte): byte; stdcall; external 'inpout32.dll';

 

Then lets go to the button methods. For this double click on button in form and you will be directed to editor. Firs o all lets work with wiring to port function. Place data to the variables Port and Data from text fields convert them from text to numerical data and then call function Out32() with these parameters.

 

procedure TForm1.Button1Click(Sender: TObject);
var
    Port: word;
    Data: Byte;
begin
    Data:= StrToInt(Edit1.Text);
    Port:= StrToInt(Edit2.Text);
    Out32(Port, Data);
end;

 

Same way we have to deal with reading. Port Address is taken from Port Address text field. This is where data will be read from. Then call function Inp32() with port number and output received data to the window.

 

procedure TForm1.Button2Click(Sender: TObject);
var
    Port: word;
    Data: Byte;
begin
    Port:= StrToInt(Edit3.Text);
    Data:= Inp32(Port);
    MessageDlg('Value: '+ IntToStr(Data), mtInformation, [mbOK], 0);
end;

 

Compile program and start. Try to write to DATA register of LPT port number 0. For this port address will be 888(or hexadecimal $378). Press button and if you have some tester connected to LPT you will se the results. Same way you can work with reading.

Source: www.pcports.ru

 

Controlling external devices using COM port communications programmed using VB language

There are a lot of Radio amateurs that want to control external devices using computer standard ports. One of them is COM poert. Everybody wants things to be ease as people doing electronics are more hardware people not software. COM port is more often used than LPT because COM port is more resistive to bigger loads and there is less chances of failing.

So if you know Visual Basic a little bit then this shouldn’t be very hard to use MSComm Control component which is located in Project->Components. You should check check box MSComm Control. Later you have to add this control to form and write some code for it.

Main difficulty with this is that you have to follow RS232 protocol. This is why it is better to use microcontrollers that have built in USART interface. Of course MSComm component allows to read and control single COM pins and this way to control any external devices without using RS232 protocol.

One good example is popular programming software PonyProg (which is programmed in other language than VB, but principals are same). You can see various supported circuits that PonyProg supports and you can see that Rx(2) and Tx(3) signals aren’t used at all. All data transfer is done via CTS(8), DSR(6), DTR(4), RTS(7) (in some places Tx(3) is used).

In order to read pin state of port it is enough to send unipolar positive signals without converting TTL-RS232. Of course this doesn’t comply with RS232 standard but this way works perfectly. But this is only recommended to hobby circuits. Professional hardware should have conversion.

So we can read three pins of COM port: CD, CTS, DSR. Command reading CTS(8) would look as follows:

If MSComm1.CTSHolding = False Then

or

If MSComm1.CTSHolding = True Then

With this command we can read weather logical 0 or 1 is on pin CTS.

COM port pins DTR and RTS are capable to output (+12V) and (-12V) and this way to light a LED or turn on Relay or other device. For instance output for pin RTS command:

MSComm1.RTSEnable = False (+12v on 7 pin)

MSComm1.RTSEnable = True (-12v on 7 pin)

Thats it. Using these commands it is possible to program simple data transfer or complicated protocols for instance I2C, SPI, MicroWire and so on. One of good examples is DS1621 pc thermometer, developed by Alberto Ricci. He programmed I2C protocol for data transfer between DS1621 thermometer.

You can construct simple circuit in few minutes (you can assemble it directly on DB9 header).

Com_sw.gif

 

Then you can start program Com_device to see how program reacts on button press and toggles LED using exactly same commands that we described above.

If your device requires pulse signal this also non difficult task but in this case you should know RS232 protocol a little bit:

rs232.gif

 

Then you can start program Com_device to see how program reacts on button press and toggles LED using exactly same commands that we described above.

If your device requires pulse signal this also non difficult task but in this case you should know RS232 protocol a little bit:

rs232_impulse.gif

 

Changing number of impulses for “0” and “1” we can change impulse width in ine byte limits with one bit step. So we can send numbers FF, FE, FC, F8, F0, E0, C0, 80, 00 to port to get all awailabe interval of impulse widths. Picture above is F0.

In order to send to port such signal we need to send following command:

MSComm1.Output = “symbol or string”

Using this command you can send any ASCII character to port. So if you want to send F0 (decimal 240), then use following:

MSComm1.Output = Chr(240)

This way we can generate 8 level PWM and control motor speed or LED brightness – just signal has to be amplified, because maximal current driven from port is 25mA.

Port settings can be changed with command

MSComm1.Settings = “1200,N,8,1″

this way we can change baud rate parity, number of bits and number of stop bits.

Source: http://www.schemz.narod.ru

 

Sample routine of working with LPT1 port under windows XP in CPP language

This is simple routine of sending and reading of byte from LPT1 port under Windows XP.

LPT port has four types of pins:

  • 8 output pins accessed via the DATA Port
  • 5 input pins (one inverted) accessed via the STATUS Port
  • 4 output pins (three inverted) accessed via the CONTROL Port
  • The remaining 8 pins are grounded
LPT_pinout

Now we are interested in Data pins.

Set up driver according to post: Acces LPT and COM ports easily under windows NT-2000-XP. I have written and compiled example under DEV-CPP tool-set, which you can download from http://www.bloodshed.net/.

Start New console project

New_DEV_CPP_Proj.PNG

Create new cpp file and save it to project directory. Also copy porttalk_IOCTL.h and pt_ioctl.c files to project directory. These files you will find in the package portalk22.zip.

Test program:

sample_LPT1_XP.PNG

Compile this program and run it. You should see results like this:

running_ports_on_XP.PNG

Now it is time to connect your microcontroller and start experimenting. Good luck.

Test routine project files for DEV-CPP are here:LPT1 Sample Project

Program LPT and COM ports easily under windows NT-2000-XP

If you are working with embedded projects usually you have concerned about how directly control computer ports like LPT or COM. Basically no one wants to mess up with driver writing or reading tons of documentations in order to send some bytes via IO port to your target board.

Earlier when DOS, win95 and win98 operating systems were popular accessing I/O ports was easy as there weren’t any protections – simple code could do the job. Under NT/2000/XP situation is different. These operating systems has strict control of I/O ports. Read more about this here: www.beyondlogic.org.
If you will try to access ports directly under windows XP, then you will get error mesage:

ioports_error.gif

How to solve this problem without writing your own driver and without rewriting old DOS or win98 programs. Well there is a solution that allow you to run programs under windows 2000/XP and talk to ports directly. Download PortTalk program from www.beyondlogic.org.

Now you have two ways of running programs – but don’t forget to read porttalk.pdf which is included in package.

  • Solution No.1

Download portalk22.zip and unzip it in separate folder somewhere. Then copy allowio.exe directly to the directory where is you program which you want to run. Then create shortcut of your compiled program for instance demo.exe. Then open files properties and in line target do following changes:

Instead of:

D:/project/demo.exe

change to

D:/project/allowio.exe D:/project/demo.exe /a

This is it. Just remember that you must have administrator rights on this computer. Other wise you should run your program with administrator user rights. Dirty trick but it works.

  • Solution No.2

Download same program packet portalk22.zip. In the packet besides the driver there is header files for working with ports directly under Windows NT/2000/XP.

Set up your compiler like DEV-CPP. It is free IDE and C/C++ compiler. Or you can use any commercial compiler package like Microsoft Visual C++ or even C++ builder.

Before you start compiling your program copy portalk.sys file to WINDOWS/System32/Drivers and copy pt_ioctl.c and PortTalk_IOCTL.h files to your project directory. Now you can start developing.

  • Few words about Linux OS

For parallel port program uses ppdev interface of kernel. This is why with command lsmod you should make sure if this module is loaded:

root@xxx# /sbin/lsmod
Module                  Size  Used by    Not tainted
ppdev                   5580   0  (unused)
parport_pc             14724   0
parport                23264   0  [ppdev parport_pc]

Otherwise you have to load this module with command modprobe which is in the root:

root@xxx# /sbin/modprobe parport
root@xxx# /sbin/modprobe parport_pc
root@xxx# /sbin/modprobe ppdev

It is good practice if this command is performed during startup of operating system. These commands cam be added to script file rc.

For RedHat would be /etc/rc.d/rc.local or write to file /etc/modules.conf

For Slackware add them to file /etc/rc.d/rc.modules

#### PC parallel port support ###
/sbin/modprobe parport_pc
/sbin/modprobe ppdev
/sbin/modprobe parport

Iy you want to work as other user without root rights you have to chmod ppdev interface by using comand (has to be performed under root rights):

# chmod 666 /dev/parport0

Before running your program make sure if there is no other programs running that serve for LPT printer. Otherwise you have to stop it before running your application.

Source: www.sapunoff.ru

New on WinAVR Tutorial New on WinARM Tutorial