Blog

Micro-controller Programming and Source Codes

USING PIC MICRO-CONTROLLER PORTS AND IT’S PIN OUTS

HOW TO USE THE MICROCONTROLLER PORTS AND ITS PINOUTS

The microcontroller chip consists of various ports which has number of bidirectional pinout which can be configured as an input/output as specified by the device datasheet. Below are examples of a microcontroller pinouts.

ma

Looking at the above PIC Microcontrollers, the PORTA, PORTB, PORTC, PORTD, etc. are the available ports of the microcontroller. While PORTA.0 means the pin 0 in PORTA, PORTB.7 means the pin 7 in PORTB, and so on. So if you are dealing with any of the pins during programming, you need to specify using the ‘.’ sign after the PORTA, PORTB, PORTC, or PORTD which you’re using for sending or receiving the signal for your circuit design.

The Input/ Output configurations of the PIC as shown above is stored in the TRIS Registers, for instance, the Input/ Output configurations of PORTA is stored in TRISA, while that of PORTB is stored in TRISB, and so on.

For instance, using the PIC16F84A microcontroller as shown above, if we want to make PORTB.0 as an output and PORTB.7 as and input using the Microcode Studio Compiler, we will use 1’s to denote inputs pins, and 0’s to denote output pins. So we declare it using eight boxes because the PIC microcontroller is an 8 bits microcontroller chip as shown below:

PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0
1 null null null null null null 0

 

So for the null’s, we can either place them to be input’s or outputs because we don’t really need them now in these example, so I will use them as output’s.

PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0
1 0 0 0 0 0 0 0

 

Therefore my TRISB Register will be:

TRISB = %10000000 (% sign means it’s a Binary value)

Let’s say we want to configure that of PORTA of the PIC16F84A microcontroller to be outputs throughout, we will then place the digits as shown below:

X X X PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0
null null null 0 0 0 0 0

 

Because the PIC16F84A microcontroller has only 5 pins in PORTA, we used X to indicate non existing pins.

Finally, we can then place the remaining unused pins of PORTA to be inputs because we don’t really need them now in these example, so I will place them as inputs.

X X X PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0
null null null 1 1 1 1 1

 

Therefore,

TRISA = %00011111 (% sign means it’s a Binary value)

So our final result for both configuration needed to use in the Microcode studio compiler will be as follow:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

TRISB=%10000000

TRISA=%00011111

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 

 

PRACTICAL CIRCUIT APPLICATIONS OF THE PIC16F84A PORTS/PINS

Below examples shows a practical application of the microcontroller for LED control using a push button. Example A made use of the push button in the pull-up mode, while example B made use of the push button in the pull-down mode.

mb

The above circuit uses a pull-up mode which allows a 5V signal input to microcontroller chip when the button is presses, PORTA.0 is meant to be input because it receives input voltage signal each time the switch is pressed, while PORTB.0 is meant to be output terminal because it sends voltage signal to the LED.

We will proceed by configuring the TRISA and TRISB registries.

Configuring PORTA.0 as input we have,

X X X PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0
Null null null 0 0 0 0 1

 

Because the PIC16F84A microcontroller has only 5 pins in PORTA, we used X to indicate non existing pins.

Finally, we can then place the remaining unused pins of PORTA to be outputs because we don’t really need them now in these example, so I will place them as outputs.

X X X PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0
0 0 0 0 0 0 0 1

 

Therefore,

TRISA = %00000001

Configuring PORTB.0 as output we have,

PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0
null null null null null null null 0

 

So for the null’s, we can either place them to be input’s or outputs because we don’t really need them now in these example, so I will use them as output’s.

PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0
0 0 0 0 0 0 0 0

 

Therefore,

TRISB = %00000000

In the Microcode Studio Compiler window, select the PIC16F84A from the device list as explained in the programming guide, then proceed with your code as shown below.

 

TRISA = %00000001             ; Configures PORTA.0 as INPUT for the switch

TRISB = %00000000             ; Configures PORTB.0 as OUTPUT for the Led

PORTA = 0                             ; Clears PORTA port

PORTB = 0                             ; Clears PORTB Port

START:                                   ; Program starting point

IF PORTA.0 = 1 THEN ONLED            ; Goto ‘ONLED’ if PORTA.0 button is pressed

GOTO START                                         ; Return to the Label START

ONLED:                                                               ; Led Routine Label

PORTB.0 = 1                                            ; Send an output signal to PORTB.0

PAUSE 500                                              ; Delay for 500 milliseconds

GOTO START                                         ; Return to the Label START

END                                                                     ; End of Program

 

 

USING THE PULL-DOWN PUSH BUTTON MODE

mc

The above circuit uses a pull-down mode which allows a 0V signal input to microcontroller chip when the button is presses, PORTA.4 is meant to be input because it receives input voltage signal each time the switch is pressed, while PORTB.0 is meant to be output terminal because it sends voltage signal to the LED.

We will proceed by configuring the TRISA and TRISB registries.

Configuring PORTA.0 as input we have,

X X X PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0
Null null null 1 0 0 0 0

 

Because the PIC16F84A microcontroller has only 5 pins in PORTA, we used X to indicate non existing pins.

Finally, we can then place the remaining unused pins of PORTA to be outputs because we don’t really need them now in these example, so I will place them as outputs.

X X X PORTA.4 PORTA.3 PORTA.2 PORTA.1 PORTA.0
0 0 0 1 0 0 0 0

 

Therefore,

TRISA = %00010000

Configuring PORTB.0 as output we have,

PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0
null null null null Null null null 0

 

So for the null’s, we can either place them to be input’s or outputs because we don’t really need them now in these example, so I will use them as output’s.

PORTB.7 PORTB.6 PORTB.5 PORTB.4 PORTB.3 PORTB.2 PORTB.1 PORTB.0
0 0 0 0 0 0 0 0

 

Therefore,

TRISB = %00000000

In the Microcode Studio Compiler window, select the PIC16F84A from the device list as explained in the programming guide, then proceed with your code as shown below.

 

TRISA = %00010000             ; Configures PORTA.4 as INPUT for the switch

TRISB = %00000000             ; Configures PORTB.0 as OUTPUT for the Led

PORTA = 0                             ; Clears PORTA port

PORTB = 0                             ; Clears PORTB Port

START:                                   ; Program starting point

IF PORTA.4 = 0 THEN ONLED            ; Goto ‘ONLED’ if PORTA.4 button is pressed

GOTO START                                         ; Return to the Label START

ONLED:                                                               ; Led Routine Label

PORTB.0 = 1                                            ; Send an output signal to PORTB.0

PAUSE 500                                              ; Delay for 500 milliseconds

GOTO START                                         ; Return to the Label START

END                                                                     ; End of Program

 

Note:

When checking for an Input Signal use ‘IF’ condition and then followed by the Port and Pin you want to check. For Example,

IF PORTB.7 = 1 (1 means if there’s a valid input signal at PORTB pin 7 terminal).

IF PORTB.7 = 0 (0 means if there’s no valid input signal at PORTB pin 7 terminal).

After the ‘IF’ and the PORTB.7 = 1 or PORTB.7 = 0, use ‘THEN’ followed by the final result for the stated condition which could be asking it to go to a Label or place the result directly.

But when giving an output signal to a pin for example, use

PORTA.0 = 1 (1 means give an output signal to PORTA pin 0).

PORTA.0 = 0 (0 means no output signal or clear the output signal at PORTA pin 0).

To place a Label which could be a name, alphabet, phrase, etc. simply insert the name, alphabet, phrase followed by the ‘:’. For example, JOHN: creates the Label with JOHN, or AB: creates the Label AB.

 

Electronics Tutorials, Formulas, and Calculations

LIGHT EMITTING DIODE (LED) LIMITING RESISTANCE CALCULATIONS

CALCULATING FOR SINGLE LED

a

To calculate for the resistance required to lit up the led we use the formula,

RD = VS – VD / ID

Where,

RD is the required current limiting resistance of the LED.

Vs is the Supply Voltage from the Power Supply or Integrated Circuit (IC).

VD is the Led required forward voltage.

ID is the Led required forward current.

For example, we want to calculate for the above led with a forward voltage of 2V and a forward current of 30mA (0.03A).

First of all, we will measure the supply voltage where we want to connect the Led to be it a DC Power supply or an IC Chip with a Digital/Analog Meter place at the DC terminal. Then we use the measured DC voltage as the VS Value. Let’s assume the output voltage from the microcontroller terminal we want to connect the Led is 5V. Taking the VD to be 2V, and ID to be 30mA (0.03A).

Therefore,

RD = 5V – 2V / 0.03A = 3V/0.03A = 100Ω

The resistor wattage value will be,

Positive difference between the Supply voltage and the Led voltage (VS – VD) multiplied by the Diode current ID.

Resistor Wattage Value = (VS – VD) x ID = 3V x 0.03A = 0.09Watts

Note: if the wattage value is not available, you can select the closest higher value. For instance, we can choose a value of 1/8 Watts.

 

CALCULATING FOR MULTIPLE LED

b

For multiple Led, we just multiply the number of Led with the forward current value of one Led.

For instance, if we want to connect three Led as shown above with forward current of one to be 15mA (0.015A), therefore the required forward current to lit them up will be 0.015 x 3 = 0.045A. The forward voltage will be the same for all the Led, which will be 1.5V using the 1.5V/15mA from the above table as example. Assuming the supply voltage is 9V, therefore the required Led Resistance will be:

RD = 9V – 1.5V / 0.045A = 7.5V/0.045A = 166.6Ω

Resistor Wattage Value = (VS – VD) x ID = 7.5V x 0.045A = 0.34Watts

Note: If the resistance value is not available, you can choose the next closest value and a wattage above the calculated value. 

TRANSFORMER CIRCUIT DESIGN AND CALCULATIONS

TRANSFORMER AC TO DC POWER SUPPLY DESIGN

DESIGNING A STEPDOWN DC POWER SUPPLY CIRCUIT

A typical Stepdown DC Power Supply consists of Stepdown Transformer, a Rectifier, a Smoothing Capacitor, a LED indicator and a Voltage Regulator.

USING FULL WAVE RECTIFIER

c

T are Stepdown Transformers.

D are the rectifier diodes.

C is the smoothing capacitors.

RD are Current Limiting Resistor for the LEDs.

IC are Positive Voltage Regulators.

To Calculate the Peak to Peak Secondary Voltage Value of the Stepdown Transformer:

VPEAK = VRMS x √2

Selecting a Rectifier Diode:

VOLTAGE RANGE MAXIMUM CURRENT DEVICE PART NUMBER
50V – 1000V 1A 1N4001 – 1N4007
50V – 1000V 3A 1N5400 – 1N5408

 

To Calculate Maximum Load Current:

ISUPPLY = VSUPPLY / RLOAD

VSUPPLY = Required DC Supply for the Circuit.

RLOAD = Measured DC Resistance of the Resistive Load.

Note: ISUPPLY can also be assumed by summing the individual electronic components operating current as specified in their datasheets.

To Calculate the Capacitance Value for Smoothing Capacitor (C):

C = 5 x ISUPPLY / 100 x VPEAK

To Calculate the Current Limiting Resistor Resistance Value (RD):

RD = VPEAK – VD / ID (VD is between 1.5V- 2.5V, ID is between 10mA – 50mA).

Selecting a Positive Voltage Regulator:

REQUIRED OUTPUT VOLTAGE MAXIMUM OUTPUT CURRENT POSITIVE REGULATOR DEVICE NUMBER
5V 1A 7805
6V 1A 7806
8V 1A 7808
9V 1A 7809
12V 1A 7812
15V 1A 7815
18V 1A 7818

 

USING HALF WAVE RECTIFIER

d

To Calculate the Peak to Peak Secondary Voltage Value of the Stepdown Transformer:

VPEAK = VRMS x √2

Selecting a Rectifier Diode:

VOLTAGE RANGE MAXIMUM CURRENT DEVICE PART NUMBER
50V – 1000V 1A 1N4001 – 1N4007
50V – 1000V 3A 1N5400 – 1N5408

 

To Calculate Maximum Load Current:

ISUPPLY = VSUPPLY / RLOAD

VSUPPLY = Required DC Supply for the Circuit.

RLOAD = Measured DC Resistance of the Resistive Load.

Note: ISUPPLY can also be assumed by summing the individual electronic components operating current as specified in their datasheets.

To Calculate the Capacitance Value for Smoothing Capacitor (C):

C = 5 x ISUPPLY / 50 x VPEAK

To Calculate the Current Limiting Resistor Resistance Value (RD):

RD = VPEAK – VD / ID (VD is between 1.5V- 2.5V, ID is between 10mA – 50mA).

Selecting a Positive Voltage Regulator:

REQUIRED OUTPUT VOLTAGE MAXIMUM OUTPUT CURRENT POSITIVE REGULATOR DEVICE NUMBER
5V 1A 7805
6V 1A 7806
8V 1A 7808
9V 1A 7809
12V 1A 7812
15V 1A 7815
18V 1A 7818

 

CHOOSING CHARGING CURRENT FOR LEAD ACID BATTERY CHARGER

e

T is the Stepdown Transformer which should have a maximum secondary voltage value of 15V.

To calculate for the Battery Charging Current Resistance value R, we use the formula;

R = Vs – VBATT / IBATT

R = ((VRMS x √2) – VBATT) / IBATT

Where,

R is the Resistance Value.

VS is the Peak Voltage output from the Rectifier.

VRMS is the transformer secondary AC Voltage Value.

VBATT is the battery standby use charging Voltage Value.

IBATT is the battery stated standby use charging current.

Selecting a Positive Voltage Regulator (IC):

REQUIRED OUTPUT VOLTAGE MAXIMUM OUTPUT CURRENT POSITIVE REGULATOR DEVICE NUMBER
5V 1A 7805
6V 1A 7806
8V 1A 7808
9V 1A 7809
12V 1A 7812
15V 1A 7815
18V 1A 7818

 

3.3V DC POWER SUPPLY CIRCUIT

f

 

5V DC POWER SUPPLY CIRCUIT

g

 

6V DC POWER SUPPLY CIRCUIT

h

9V DC POWER SUPPLY CIRCUIT

9v

 

12V DC POWER SUPPLY CIRCUIT

12v

 

15V DC POWER SUPPLY CIRCUIT

k

 

12V/5V DC POWER SUPPLY CIRCUIT

l

 

TRANSFORMER-LESS AC TO DC POWER SUPPLY DESIGN

Transformer-less DC power supply eliminates the solely use of a Stepdown transformer to provide lower DC supply power to drive our electronic circuits. The main component of a transformer-less power supply circuit is Voltage dropping capacitor or X-rated capacitor, which are specially designed for AC mains. This X rated capacitor is connected in series of Phase line of AC, to drop the voltage. This type of Transformer-less power supply is called Capacitor Power Supply.

tx

CIRCUIT DESCRIPTION:

D1-D4 is a full wave bridge rectifier configuration which converts the AC to Pulsating DC Voltage (also known as the Rectifier).

C1 is the X Rated Capacitor which is the core part of this power supply as it will drop the excess mains voltage across it.

R1 resistor is connected in parallel with the X-rated capacitor to discharge the stored current in the capacitor when circuit is switched off, thus preventing electric shock. This resistance is called Bleeder resistor.

R2 is provided to prevent excess transient current that can flow when the power supply is switched on.

C2 is the filtering capacitor which will filter the rectified ac voltage.

RD is the current limiting resistor of the LED diode, which limits the current going to the LED Device.

IC is the Positive voltage regulator that provides a constant terminal voltage output.

0.1uF is used to improve the DC Transient output against slight fluctuations.

 

CALCULATION:

Peak input supply voltage (Vinpeak) = √2 x Mains Supply Voltage (240V)

Peak Output from the Rectifier (Voutpeak) = √2 x Required Operating DC Voltage.

Required voltage drop across the X-Rated Capacitor will be,

(Vinpeak – Voutpeak) = I x (Xc || R1)

To Calculate the Parallel combination,

Xc || R1 = (Xc x R1) / (Xc + R1)

(Vinpeak – Voutpeak) = (Xc x R1) / (Xc + R1)

But,

Xc = 1/6.284 x F x C

Where,

X = Reactance of Capacitor

F = frequency of AC

C = Capacitance of X-rated capacitor

Therefore by substituting both formulas, we have:

(Vinpeak – Voutpeak) = (R1/6.284 x F x C) / (1/6.284 x F x C + R1)

Below is the table for output current and output voltage (without the Load), of different values of X-rated capacitors.

Capacitor Code X-Rated Capacitor Value Output Voltage Maximum Load Supply Current
104k 0.1uF/400V 4v 8mA
334k 0.33uF/400V 10v 22mA
474k 0.47uF/400V 12v 25mA
684k 0.68uF/400V 18v 100mA
105k 1uF/400V 24v 40mA
225k 2.2uF/400V 24v 100mA

 

Selection of voltage dropping capacitor is important, it is based on Reactance of Capacitor and the amount of current to be withdrawn. The Reactance of the capacitor is given by below formula:

Notes:

  • Make it on your own risk, it’s extremely dangerous to work with AC mains without proper experience and precaution. Do take extreme caution while building this circuit.
  • Don’t replace X-Rated capacitor with normal capacitor, otherwise it will burst.
  • If more output voltage and current is required then use different value of X-Rated capacitor, according to the table.
  • A 1 ampere fuse can be also be used before X-rated capacitor, in series with phase line, for safety purpose.

 

 

3.3V DC POWER SUPPLY EXAMPLE

tx3.3v

 

5V DC POWER SUPPLY EXAMPLE

tx5v

 

6V DC POWER SUPPLY EXAMPLE

tx6v

 

9V DC POWER SUPPLY EXAMPLE

tx9v

 

12V DC POWER SUPPLY EXAMPLE

tx12v

 

15V DC POWER SUPPLY EXAMPLE

Latest Inventions, Innovations and Researches

CONSTRUCTION OF ANGLE 40 DEGREE BY TRISECTING ANGLE 120 DEGREE USING A RULER AND PAIR OF COMPASS

CONSTRUCTION PROCEDURE:
1. Draw a line XY of any given length.
2. Place your compass to any given radius and then draw a semicircle AB from the point O along the horizontal line XY.
3. With the same radius used in the semicircle, place your compass at point B, make an arc C, and then place a point C and then make another arc D.
4. Draw a straight line OF passing through point D of the arc and the semicircle, such that the angle between lines XY and OF equals 120 degree. And then repeat by drawing a straight line OE passing through point C of the arc and the semicircle such that the angle between lines XY and OE equals 60 degree.
5. Mark the points, B and D where the semicircle and the lines XY and OF intersects each other.
6. Draw a straight line to meet the points B and D.
7. Then construct angle 90 degree at both points B and D by placing your compass with a given radius at point B then draw a semicircle and mark 1 and 2 where it touches line XY. Then extend the compass to any given radius greater than the initial radius earlier used for semicircle. Place the compass at points 1 and 2 and draw arcs for each point respectively. Repeat the process for point D as earlier stated for point B and then number the points where the semicircle touches the line OF as 4 and 5.
8. Mark the points of intersection of arcs drawn from points 1 and 2 as 3, and then for arcs drawn from points 4 and 5 as 6.
9. Draw a straight line from point B passing through point 3 and terminates at the 60 degree line drawn earlier. Repeat the same process by drawing a straight line from point D passing through point 6 and them terminates at the 60 degree line drawn earlier.
10. Name the new point where both lines meet along the 60 degrees line as E.
11. Draw an arc labeled 7 and 8 of the same radius used for OA or OB semicircle from point E to pass across both 90 degrees lines drawn from points B and D earlier.
12. Name the new point Q where the arc touches any of the 90 degrees lines and point P where the line OE meets the line BD and then draw a straight line from point P to point Q.
13. Then draw a line from point O passing through point where the line PQ touches the semicircle AB to any given length to obtain the 40 degrees angle required.
NOTE: With the introduction of this concept, other angle such as 2.5, 5, 10, 20, 40, 80, 160, 200, 240, 260, 280, 300, 320, 340, 360 Degrees.

_______________________________________________

WIRELESS CHARGING SYSTEM

Wireless charging of a mobile phone using inductive coils.

DERIVED FORMULA:

Vs = Vp – (3.1526 x D)

Where,

Vs is the received coil supply voltage.

Vp is the transmitting coil supply voltage.

D is the distance between the coils.