You are on page 1of 35

HCMC University of Technology and Education

Faculty of Electrical & Electronic Engineering


No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

MICROCONTROLLERS

NGUYEN THANH NGHIA


10/27/2020 11
NGUYEN THANH NGHIA
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering
No.1 Vo Van Ngan Street, Thu Duc Dist., HCMC, VN

CHAPTER 5:
LED CONTROL

NGUYEN THANH NGHIA


10/27/2020 22
NGUYEN THANH NGHIA
Outline
1. Introduction.
2. Conditional operator.
3. C program structure.
4. Example.

NGUYEN THANH NGHIA 3


Chapter 5: LED control
1. Introduction Basic connecting

NGUYEN THANH NGHIA 4


Chapter 5: LED control
1. Introduction Pins for programming

NGUYEN THANH NGHIA 5


Chapter 5: LED control
2. Input/Output ports
 Every port has its corresponding TRIS register:
TRISA, TRISB, TRISC etc. which determines the
performance of port bits, but not their contents.
 By clearing any bit of the TRIS register (bit=0),
the corresponding port pin is configured as an
output. Similarly, by setting any bit of the TRIS
register (bit=1), the corresponding port pin is
configured as an input.
 This rule is easy to remember 0 = Output, 1 =
Input.

NGUYEN THANH NGHIA 6


Chapter 5: LED control
2. Input/Output ports
The microcontroller's current supply and receiving capacity is 25mA.

NGUYEN THANH NGHIA 7


Chapter 5: LED control
2. Input/Output ports Port A
Port B is an 8-bit wide, bidirectional port. Bits of the TRISB register
determine the function of its pins.

NGUYEN THANH NGHIA 8


Chapter 5: LED control
2. Input/Output ports Port A
RA0 = AN0 (determined by the ANS0 bit of the ANSELregister)
RA1 = AN1 (determined by the ANS1 bit of the ANSELregister)
RA2 = AN2 (determined by the ANS2 bit of the ANSELregister)
RA3 = AN3 (determined by the ANS3 bit of the ANSELregister)
RA4 = AN4 (determined by the ANS4 bit of the ANSELregister)
Similar to bits of the TRISA register determine which of the pins are to
be configured as inputs and which ones as outputs, the appropriate bits of
the ANSEL register determine whether pins are to be configured as
analog inputs or digital inputs/outputs.

NGUYEN THANH NGHIA 9


Chapter 5: LED control
2. Input/Output ports Port B pull-up
Port B is an 8-bit wide, bidirectional port. Bits of the TRISB register
determine the function of its pins..

NGUYEN THANH NGHIA 10


Chapter 5: LED control
2. Input/Output ports Port B pull-up
Similar to port A, a logic one (1) in the TRISB register configures the
appropriate portB pin as an input and vice versa.
Six pins of this port can act as analog inputs (AN).
The bits of the ANSELH register determine whether these pins are to be
configured as analog inputs or digital inputs/outputs:
RB0 = AN12 (determined by the ANS12 bit of the ANSELH register)
RB1 = AN10 (determined by the ANS10 bit of the ANSELH register)
RB2 = AN8 (determined by the ANS8 bit of the ANSELH register)
RB3 = AN9 (determined by the ANS9 bit of the ANSELH register)

NGUYEN THANH NGHIA 11


Chapter 5: LED control
2. Input/Output ports Port B pull-up

 All the port B pins have built in pull-up resistors,


which make them ideal for connection to push
buttons (keyboard), switches and optocouplers.
 In order to connect these resistors to the
microcontroller ports, the appropriate bit of the
WPUB register should be set.

NGUYEN THANH NGHIA 12


Chapter 5: LED control
2. Input/Output ports Port B pull-up
Having a high level of resistance (several tens of kiloohms), these
‘virtual’ resistors do not affect pins configured as outputs, but serves
as a useful complement to inputs. As such, they are connected to the
inputs of CMOS logic circuits. Otherwise, they would act as if they are
floating due to their high input resistance.

NGUYEN THANH NGHIA 13


Chapter 5: LED control
2. Input/Output ports Port B pull-up
Apart from the bits of the WPUB register, there is
another bit affecting the installation of all pull-up
resistors. It is the RBPU bit of the OPTION_REG.
If enabled, each port B bit configured as an input may
cause an interrupt by changing its logic state. In order
to enable pins to cause an interrupt, the appropriate
bit of the IOCB register should be set.

NGUYEN THANH NGHIA 14


Chapter 5: LED control
2. Input/Output ports Port C
Port C is an 8-bit wide, bidirectional port. Bits of the
TRISC register determine the function of its pins.
Similar to other ports, a logic one (1) in the TRISC
register configures the appropriate portC pin as an
input.

NGUYEN THANH NGHIA 15


Chapter 5: LED control
2. Input/Output ports Port D
Port D is an 8-bit wide, bidirectional port. Bits of the
TRISD register determine the function of its pins. A
logic one (1) in the TRISD register configures the
appropriate portD pin as an input.

NGUYEN THANH NGHIA 16


Chapter 5: LED control
2. Input/Output ports Port E
Port E is a 4-bit wide, bidirectional port. The TRISE
register’s bits determine the function of its pins. Similar to
other ports, a logic one (1) in the TRISE register configures
the appropriate portE pin as an input. The exception is the
RE3 pin which is always configured as an input.

NGUYEN THANH NGHIA 17


Chapter 5: LED control
3. Main I/O commands
output_low(pin)
this command will set the pin to 0
EX: output_low(pin_d0);
this command sets the pin D0 to 0
output_high(pin)
this command will set the pin to 1
EX: output_high(pin_d0);
this command sets the pin D0 to 1
NGUYEN THANH NGHIA 18
Chapter 5: LED control
3. Main I/O commands
output_bit(pin, value)
this command will set the pin to 0 or 1 base
on value
EX: output_bit(pin_d0, 1) ;
this command sets the pin D0 to 1

NGUYEN THANH NGHIA 19


Chapter 5: LED control
3. Main I/O commands

input(pin)
read the value of pin
EX:
int1 x;
x=input(pin_d0);
read the value of pin D0 and save in x

NGUYEN THANH NGHIA 20


Chapter 5: LED control
3. Main I/O commands
output_X(value)
for PORT X (A-E) outputs “value” to the
entire port
EX:
output_d(0x0f) ;
4 lower bits of portd are set to 1 and 4
higher bit ofs portd are set to 0

NGUYEN THANH NGHIA 21


Chapter 5: LED control
3. Main I/O commands

input_X()
read the value of PORT X (A-E)
EX:
usigned int8 temp;
temp = input_d() ;
read the value of portd and save in temp

NGUYEN THANH NGHIA 22


Chapter 5: LED control
3. Main I/O commands
port_b_pullups(value)
allows the use of pullup resistors on
individual pins of port B
EX:
port_b_pullups(0x80);
Enable pullup resistor for pin b7 others are
disable

NGUYEN THANH NGHIA 23


Chapter 5: LED control
3. Main I/O commands
set_tris_X(value)
set the data direction. Bit 1 indicates input,
bit 0 indicates output .

EX:
set_tris_d(0x01);
Pin d0 is input others are output

NGUYEN THANH NGHIA 24


Chapter 5: LED control
4. Application 1: 8-LEDs control
4.1 Control 8-LED blinking
 LED diode:

NGUYEN THANH NGHIA 25


Chapter 5: LED control
4. Application 1: 8-LEDs control
4.1 Control 8-LED blinking
 Schematic:

NGUYEN THANH NGHIA 26


Chapter 5: LED control
4. Application 1: 8-LEDs control
4.1 Control 8-LED blinking
 Flowchart:

NGUYEN THANH NGHIA 27


Chapter 5: LED control
4. Application 1: 8-LEDs control
4.1 Control 8-LED blinking
 Code:
#include <16F887.h>
#FUSES NOWDT, PUT, HS, NOPROTECT, NOLVP
#USE DELAY(CLOCK=20M)

void main()
{
SET_TRIS_D(0x00);
while(TRUE)
{
OUTPUT_D(0xFF);
DELAY_MS(100);
OUTPUT_D(0x00);
DELAY_MS(100);
}
}

NGUYEN THANH NGHIA 28


Chapter 5: LED control
4. Application 1: 8-LEDs control
4.2 Control 8-LED blinking with 10 times
 Schematic:

NGUYEN THANH NGHIA 29


Chapter 5: LED control
4. Application 1: 8-LEDs control
4.2 Control 8-LED blinking with 10 times
 Code:

NGUYEN THANH NGHIA 30


Chapter 5: LED control
4. Application 1: 8-LEDs control
4.2 Control 8-LED blinking with 10 times
#include <16F887.h>
 Code: #FUSES NOWDT, PUT, HS,
NOPROTECT, NOLVP
#USE DELAY(CLOCK=20M)
UNSIGNED INT8 I;
void main()
{
SET_TRIS_D(0x00);
FOR(I=0;I<10;I++)
{
OUTPUT_D(0xFF);
DELAY_MS(100);
OUTPUT_D(0x00);
DELAY_MS(100);
}
while(TRUE) { }
}
NGUYEN THANH NGHIA 31
Chapter 5: LED control
4. Application 1: 8-LEDs control
4.3 Control 8-LED Johnson from right to left
 Schematic:

NGUYEN THANH NGHIA 32


Chapter 5: LED control
4. Application 1: 8-LEDs control
4.3 Control 8-LED Johnson from right to left
 Flowchart:

NGUYEN THANH NGHIA 33


Chapter 5: LED control
4. Application 1: 8-LEDs control
4.3 Control 8-LED Johnson from right to left
 Code:
#include <16F887.h>
#FUSES NOWDT, PUT, HS, NOPROTECT, NOLVP
#USE DELAY(CLOCK=20M)
UNSIGNED INT8 I, X;
void main()
{
SET_TRIS_D(0x00); X=0X00; OUTPUT_D(X); DELAY_MS(100);
while(TRUE)
{
FOR(I=0;I<8;I++) //ON
{ X = (X<<1)+0X01; OUTPUT_D(X); DELAY_MS(100);}
FOR(I=0;I<8;I++) //OFF
{ X = (X<<1);OUTPUT_D(X); DELAY_MS(100);}
}
}

NGUYEN THANH NGHIA 34


Chapter 5: LED control

The end!

NGUYEN THANH NGHIA 35

You might also like