Author Topic: H-Bridge Control via the PC's Parallel Port  (Read 4317 times)

0 Members and 1 Guest are viewing this topic.

jack11

  • Jr. Member
  • **
  • Posts: 95
  • Country: us
H-Bridge Control via the PC's Parallel Port
« on: March 29, 2016, 03:44:27 PM »
I guess the general Code questions are allowed here, so let me ask for feedback from C experts (I use the old Turbo C ver 2.0 running under MS-DOS ver 5.0).

Controlling the H-bridge via the PC's parallel port, everything works fine, the control bandwidth is there, an infinite loop is preferred so I am using the for(;;) or do-while 1 loops (both work fine), and for now I am using bit-banging (I can maintain the UL1741 60Hz freq tolerance using this simple scheme).

The question is how to get out of this loop on forced program termination, without testing any conditions to cause return(), such as kbhit(), that mess up the control timing. The loop must only contain the outportb() and delay() control functions, nothing else, or the control signal timing to the parallel port may be disturbed (even with the interrupts disabled).

Ctrlbrk() work ONLY if there is printf() inside the loop ??? – I can't have that. Otherwise it will not get out of the loop and stop the program on hitting ctrl-C or ctrl-Break.

Also tried setcbrk() and signal(), but was not successful.

Has anyone dealt with this before, how to terminate tight infinite control loops and programs, using ctrl-C/ctrl-Break, or in any other way that does not disturb the signal timing while controlling something?

Thanks, Jack

frackers

  • Sr. Member
  • ****
  • Posts: 435
  • Country: nz
  • Picard spits "Hello"
Re: H-Bridge Control via the PC's Parallel Port
« Reply #1 on: March 29, 2016, 06:50:56 PM »
Off the top of my head, something like this - not tested so the headers might require adjustment

Code: [Select]
#include <signal.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <syslog.h>

bool controlC = false; 

static void
sig_ctlc (int UNUSED (signo))
{
   controlC = true;
}

int main(void)
{
   if (signal (SIGINT, sig_ctlc) == SIG_ERR)
   {
      log_printf (LOG_EMERG, "Error: could not setup int handler");
      exit (EXIT_FAILURE);
   }

do
{
// do your loop here
}   while (!controlC);


Runs until you hit control-C

Robin Down Under (Or Are You Up Over)

jack11

  • Jr. Member
  • **
  • Posts: 95
  • Country: us
Re: H-Bridge Control via the PC's Parallel Port
« Reply #2 on: March 30, 2016, 02:40:38 PM »
thanks, I'll give it a shot and post the result later.

So far the only way for me to kill my loop/program is ctrl-alt-delete to reboot DOS - not a good way, even though the program runs for hours once started. Hopefully your code will help others programming on other platforms too.

jack11

  • Jr. Member
  • **
  • Posts: 95
  • Country: us
Re: H-Bridge Control via the PC's Parallel Port
« Reply #3 on: March 31, 2016, 03:35:53 PM »
still a problem. I made it real simple now, took all my code out, put in some dummy statements in the do-while loop.
Had to change your code some to fit it into the old Turbo-C framework, but the idea is the same. Compiles OK with one warning (related to your UNUSED argument).

As is, the program gets caught in the infinite loop, and can't get out using ^C, need to reboot DOS to terminate.
But, un-comment either printf() or while(!kbhit()), and it works just fine to terminate on ^C, but this messes up the control timing (introduces time biases and phase jitter in the AC waveform).

Would anyone like to take a shot at making it go? See the code below.

#include <stdio.h>
#include <dos.h>
#include <signal.h>

int controlC=0; /*global*/

static void sig_ctlc(int unused) {controlC=1;} /*^C handler*/

int main(void)
 {
 if (signal(SIGINT,sig_ctlc)==SIG_ERR) {exit(1);}

 do
  {
  /*printf("***inv running***");*/
  outportb(0x378,0x00); outportb(0x378,0xFF); /*dummy outputs*/
  }
 while (!controlC);
 /*while(!kbhit());*/
 }

DamonHD

  • Administrator
  • Super Hero Member Plus
  • *****
  • Posts: 4125
  • Country: gb
    • Earth Notes
Re: H-Bridge Control via the PC's Parallel Port
« Reply #4 on: March 31, 2016, 04:16:30 PM »
At the very least you should mark the variable controlC as 'volatile' so that the compiler knows not to optimise away checks for its value changing in the background (ie the signal routine).

I would declare it as:

static volatile int controlC;

to keep it local to the file (static) and mark it as used by interrupt handlers or threads (volatile).

Its initial value will be zero by default.

Rgds

Damon

PS. And actually this would be the right place to use a boolean/bool, but that's for another day!
Podcast: https://www.earth.org.uk/SECTION_podcast.html

@DamonHD@mastodon.social

jack11

  • Jr. Member
  • **
  • Posts: 95
  • Country: us
Re: H-Bridge Control via the PC's Parallel Port
« Reply #5 on: April 02, 2016, 01:22:57 PM »
... still not working after Damon's declaration.
Single-stepping through the program: on ^C the program never gets trapped into the ^C handler, controlC var never gets changed from 0 to 1, the program never gets out of the infinite loop.
This may have something to do with not seeing the ^C interrupt, even though in this simple program above I am not doing anything to disable the interrupts ...

DamonHD

  • Administrator
  • Super Hero Member Plus
  • *****
  • Posts: 4125
  • Country: gb
    • Earth Notes
Re: H-Bridge Control via the PC's Parallel Port
« Reply #6 on: April 02, 2016, 02:25:25 PM »
DOS may simply not notice a ^C if you are not reading the keyboard; it is quite an old single-threaded OS.

So in the loop you may want to use one of the routines that polls to see if a key has been pressed and is waiting, but does not stop if not.

Haven't done this for 20+ years, but was it something like kbhit()?

Rgds

Damon
Podcast: https://www.earth.org.uk/SECTION_podcast.html

@DamonHD@mastodon.social

jack11

  • Jr. Member
  • **
  • Posts: 95
  • Country: us
Re: H-Bridge Control via the PC's Parallel Port
« Reply #7 on: April 02, 2016, 02:50:10 PM »
... yes, it does work with do-while(!kbhit), but it messes up the AC waveform timing.
And, DOS recognizes ^C just fine when printf() is in the loop ???
See above.

DamonHD

  • Administrator
  • Super Hero Member Plus
  • *****
  • Posts: 4125
  • Country: gb
    • Earth Notes
Re: H-Bridge Control via the PC's Parallel Port
« Reply #8 on: April 02, 2016, 03:10:16 PM »
Yes, unless you do explicit I/O or find some other way to hand control to DOS to check for ^C it isn't going to happen.  You might be able to get a TSR to do it for you for example.

DOS simply does not handle async I/O well.  It's old.

You might do better with a Linux of some flavour, which will still be free, and should let you use the parallel port, and shouldn't mess up your timing, but which should at least do the ^C stuff right!

Also, this is the sort of area where a dedicated Arduino (or clone) excels.

Rgds

Damon
Podcast: https://www.earth.org.uk/SECTION_podcast.html

@DamonHD@mastodon.social

jack11

  • Jr. Member
  • **
  • Posts: 95
  • Country: us
Re: H-Bridge Control via the PC's Parallel Port
« Reply #9 on: April 04, 2016, 12:18:31 PM »
thanks Damon,

not a big deal for me right now, I can always do ctrl-alt-del to kill it, it's just bugging me.
I'll try to mess with TSR functions when I have time, always wanted to do some deeper OS/DOS/BIOS programming.

And Linux, another item on my plate I never had time to do, this may be the project that'll finally make me do it.
Jack