That old blinking LED trick
User Rating: / 5
PoorBest 
Written by Bryce Ringwood   

Light emitting diodes or LEDs have now become the method of choice for indicator and panel lamps as well as making inroads into the lighting market, particularly so, since the advent of high-intensity white LEDs.

The voltage and current in the ballast resistor obey Ohm's Law:

 

\textstyle R_{Ballast}  = {\displaystyle {V - V_f}\over \displaystyle I}

\textstyle P = V^2 / R_{Ballast} 

Where:

        V = Applied (Battery) Voltage
        Vf = LED forward voltage
        I = Current
        P = Power wasted in heating the ballast resistor
        RBallast = LED Series Resistance

Note that most of the power is dissipated in the ballast resistor.

That Blinking LED lamp for the PC

For this experiment, you need to connect the anode of an LED lamp to pin 2 of a male DB25 connector, and a 200 or so Ohm resistor between pin 22 and the LED cathode. (We put the ballast resistor between cathode and negative, this time.) If you want, you can connect 7 more LEDs between pins 3 to 8 , a resistor, and ground.

Note that you will have to find out  the address of your parallel port before you begin. Normally it is 0x278 or 0x378. On my computer itis 0x3bc. These numbers are represented in a hexadecimal numbering system, hence the "0x" prefix. There is an article on number systems later in this series.

Here is the listing of blink.c:

#include<stdio.h>
#include<conio.h>

 void delay(int dly);

 void delay(int dly)
 {
 int i,j,a;
      for(i =0 ; i < dly; i++)
      {
      /*The original PC would probably have blinked about every second*/
      /*Yours will blink much more quickly  */
           for(j=0; j < 2801; j++)
           {
                a=0;

           }
      }
 }


 main()
 {
      for(;;)
      {

            delay(100);
            outport(0x3bc,0x01);
            delay(100);
            outport(0x3bc,0x00);
      }
 }

 

Lising of PC version of Blink.c

 Now you can comple the program and run it. Yes it DOES run but whoops? - there's a serious flaw isn't there ? (It wouldn't be a problem on the PIC or Arduino) - but on the PC its bad news.

There's also a style problem - the 0x3bc appears as a magic number. It might have been better to use:

/**** Port definition  ***/

#define PORT 0x3bc

at the beginning of the program. We will look at the #define macro in more detail in the LCD experiment.

The blinking LED for the Arduino

This is the program on the chip when you get the kit. Arduino prefers to call its program listings"sketches". This is understandable, since they want the chip to be used by "artists" :)

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */

void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);    
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}

'Blink' for the arduino - Its a sketch not a program!

Well, it certainly looks like a 'C' program.  Somewhere, HIGH, LOW and OUTPUT have been #defined - but where ? (Yes I should have #defined HIGH, LOW and OUTPUT in my effort - but I didn't.) My guess is that all those definitions, the main routine and the endless loop are all contained in the stub program called the "bootloader".

Its quite easy to make my DOS program look exactly like the Arduino program - maybe you'd like to give it a try ?

Blink for the PIC-TRIX

The PIC-TRIX development board comes with 8 LEDs all connected to the pins of port D via 470 Ohm resistors. The blink program became a bit stale, so I changed one of Microchip's example programs to operate as a binary counter.:

#include <htc.h>
__CONFIG(XT & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTDIS &
 DEBUGDIS & DUNPROT & UNPROTECT);
void init(void)
{
 // Here we are going to set port D   1 = input, 0 = output
 TRISD = 0b00000000;

}
char counter;
void main(void)
{
     counter =0;
     init();
     while (1)

     {        

             for (int i=0;i <10; i++)

             {
                 _delay(10000);
             }   
 
             PORTD = counter;
             _delay(10000);
             counter++;
     }
}

Binary Counter Program using LEDs on PORTD

Follow the Code

In this section, the key item presented is the

for((control variable ; condition ; control variable iincrement/change)

{

... lines of code to be repetitively executed

}

The control variable (usually of type int) has to be initiallised to some value - usually 0. When some codition is met, the loop ends, and finally, the control variable changes value in some specified way.

You can see how a "delay" function can be implemented by simp[ly repeating an instruction many times over in a for loop. A word of caution here - some compilers will see that you are effectively doing nothing and short-circuit your loop without executing it.

Another way to repeat a set of instructions is to use the "while" loop :-

while(some condition is true)

{

... repeat instructions which at some point can change the condition to false

}

Each computer chip seems to have its own way of making pins and ports go high or low. I think they are all fairly obvious to understand. Usually an entire port is addressed, rather than an individual pin. The required pin is selected by a process of masking see the article on number systems.

Conclusion - Try it out

Unfortunately, the Arduino example is all done for you. If you have an Arduino, maybe its a good plan to get hold of an old DOS PC and get the example working on that.

For the PIC people - can that program really go on for ever?

 
Joomla template by a4joomla