How to Program All Ports as Output of PIC16F877A
The following code demonstrate, how to write a program to use all ports of PIC16F877A as outputs. Bit level programming technique is used. As PORTA is multi-purpose port, to set PORTA as digital you have to write 0x06 value to ADCON1 register and PORTA.4 is open drain bit so it is externally pull-up by 4.7k resistor. The code is written in “mikroC PRO for PIC v.5.6.1” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download.
Code in mikroC
// functions proto-types
void PortABitValue(int val);
void PortBBitValue(int val);
void PortCBitValue(int val);
void PortDBitValue(int val);
void PortEBitValue(int val);
void PortAInitAsDO(void);
void PortBInitAsDO(void);
void PortCInitAsDO(void);
void PortDInitAsDO(void);
void PortEInitAsDO(void);
// direction signals porta
sbit LED_A0_dir at TRISA.B0;
sbit LED_A1_dir at TRISA.B1;
sbit LED_A2_dir at TRISA.B2;
sbit LED_A3_dir at TRISA.B3;
sbit LED_A4_dir at TRISA.B4;
sbit LED_A5_dir at TRISA.B5;
// direction signals portb
sbit LED_B0_dir at TRISB.B0;
sbit LED_B1_dir at TRISB.B1;
sbit LED_B2_dir at TRISB.B2;
sbit LED_B3_dir at TRISB.B3;
sbit LED_B4_dir at TRISB.B4;
sbit LED_B5_dir at TRISB.B5;
sbit LED_B6_dir at TRISB.B6;
sbit LED_B7_dir at TRISB.B7;
// direction signals portc
sbit LED_C0_dir at TRISC.B0;
sbit LED_C1_dir at TRISC.B1;
sbit LED_C2_dir at TRISC.B2;
sbit LED_C3_dir at TRISC.B3;
sbit LED_C4_dir at TRISC.B4;
sbit LED_C5_dir at TRISC.B5;
sbit LED_C6_dir at TRISC.B6;
sbit LED_C7_dir at TRISC.B7;
// direction signals portd
sbit LED_D0_dir at TRISD.B0;
sbit LED_D1_dir at TRISD.B1;
sbit LED_D2_dir at TRISD.B2;
sbit LED_D3_dir at TRISD.B3;
sbit LED_D4_dir at TRISD.B4;
sbit LED_D5_dir at TRISD.B5;
sbit LED_D6_dir at TRISD.B6;
sbit LED_D7_dir at TRISD.B7;
// direction signals porte
sbit LED_E0_dir at TRISE.B0;
sbit LED_E1_dir at TRISE.B1;
sbit LED_E2_dir at TRISE.B2;
// bit labels porta
sbit LED_A0 at PORTA.B0;
sbit LED_A1 at PORTA.B1;
sbit LED_A2 at PORTA.B2;
sbit LED_A3 at PORTA.B3;
sbit LED_A4 at PORTA.B4;
sbit LED_A5 at PORTA.B5;
// bit labels portb
sbit LED_B0 at PORTB.B0;
sbit LED_B1 at PORTB.B1;
sbit LED_B2 at PORTB.B2;
sbit LED_B3 at PORTB.B3;
sbit LED_B4 at PORTB.B4;
sbit LED_B5 at PORTB.B5;
sbit LED_B6 at PORTB.B6;
sbit LED_B7 at PORTB.B7;
// bit labels portc
sbit LED_C0 at PORTC.B0;
sbit LED_C1 at PORTC.B1;
sbit LED_C2 at PORTC.B2;
sbit LED_C3 at PORTC.B3;
sbit LED_C4 at PORTC.B4;
sbit LED_C5 at PORTC.B5;
sbit LED_C6 at PORTC.B6;
sbit LED_C7 at PORTC.B7;
// bit labels portd
sbit LED_D0 at PORTD.B0;
sbit LED_D1 at PORTD.B1;
sbit LED_D2 at PORTD.B2;
sbit LED_D3 at PORTD.B3;
sbit LED_D4 at PORTD.B4;
sbit LED_D5 at PORTD.B5;
sbit LED_D6 at PORTD.B6;
sbit LED_D7 at PORTD.B7;
// bit labels porte
sbit LED_E0 at PORTE.B0;
sbit LED_E1 at PORTE.B1;
sbit LED_E2 at PORTE.B2;
void PortABitValue(int val);
void PortBBitValue(int val);
void PortCBitValue(int val);
void PortDBitValue(int val);
void PortEBitValue(int val);
void PortAInitAsDO(void);
void PortBInitAsDO(void);
void PortCInitAsDO(void);
void PortDInitAsDO(void);
void PortEInitAsDO(void);
// direction signals porta
sbit LED_A0_dir at TRISA.B0;
sbit LED_A1_dir at TRISA.B1;
sbit LED_A2_dir at TRISA.B2;
sbit LED_A3_dir at TRISA.B3;
sbit LED_A4_dir at TRISA.B4;
sbit LED_A5_dir at TRISA.B5;
// direction signals portb
sbit LED_B0_dir at TRISB.B0;
sbit LED_B1_dir at TRISB.B1;
sbit LED_B2_dir at TRISB.B2;
sbit LED_B3_dir at TRISB.B3;
sbit LED_B4_dir at TRISB.B4;
sbit LED_B5_dir at TRISB.B5;
sbit LED_B6_dir at TRISB.B6;
sbit LED_B7_dir at TRISB.B7;
// direction signals portc
sbit LED_C0_dir at TRISC.B0;
sbit LED_C1_dir at TRISC.B1;
sbit LED_C2_dir at TRISC.B2;
sbit LED_C3_dir at TRISC.B3;
sbit LED_C4_dir at TRISC.B4;
sbit LED_C5_dir at TRISC.B5;
sbit LED_C6_dir at TRISC.B6;
sbit LED_C7_dir at TRISC.B7;
// direction signals portd
sbit LED_D0_dir at TRISD.B0;
sbit LED_D1_dir at TRISD.B1;
sbit LED_D2_dir at TRISD.B2;
sbit LED_D3_dir at TRISD.B3;
sbit LED_D4_dir at TRISD.B4;
sbit LED_D5_dir at TRISD.B5;
sbit LED_D6_dir at TRISD.B6;
sbit LED_D7_dir at TRISD.B7;
// direction signals porte
sbit LED_E0_dir at TRISE.B0;
sbit LED_E1_dir at TRISE.B1;
sbit LED_E2_dir at TRISE.B2;
// bit labels porta
sbit LED_A0 at PORTA.B0;
sbit LED_A1 at PORTA.B1;
sbit LED_A2 at PORTA.B2;
sbit LED_A3 at PORTA.B3;
sbit LED_A4 at PORTA.B4;
sbit LED_A5 at PORTA.B5;
// bit labels portb
sbit LED_B0 at PORTB.B0;
sbit LED_B1 at PORTB.B1;
sbit LED_B2 at PORTB.B2;
sbit LED_B3 at PORTB.B3;
sbit LED_B4 at PORTB.B4;
sbit LED_B5 at PORTB.B5;
sbit LED_B6 at PORTB.B6;
sbit LED_B7 at PORTB.B7;
// bit labels portc
sbit LED_C0 at PORTC.B0;
sbit LED_C1 at PORTC.B1;
sbit LED_C2 at PORTC.B2;
sbit LED_C3 at PORTC.B3;
sbit LED_C4 at PORTC.B4;
sbit LED_C5 at PORTC.B5;
sbit LED_C6 at PORTC.B6;
sbit LED_C7 at PORTC.B7;
// bit labels portd
sbit LED_D0 at PORTD.B0;
sbit LED_D1 at PORTD.B1;
sbit LED_D2 at PORTD.B2;
sbit LED_D3 at PORTD.B3;
sbit LED_D4 at PORTD.B4;
sbit LED_D5 at PORTD.B5;
sbit LED_D6 at PORTD.B6;
sbit LED_D7 at PORTD.B7;
// bit labels porte
sbit LED_E0 at PORTE.B0;
sbit LED_E1 at PORTE.B1;
sbit LED_E2 at PORTE.B2;
void main(void)
{
PortAInitAsDO();
PortBInitAsDO();
PortCInitAsDO();
PortDInitAsDO();
PortEInitAsDO();
while(1)
{
PortABitValue(0);
PortBBitValue(0);
PortCBitValue(0);
PortDBitValue(0);
PortEBitValue(0);
Delay_ms(500);
PortABitValue(1);
PortBBitValue(1);
PortCBitValue(1);
PortDBitValue(1);
PortEBitValue(1);
Delay_ms(500);
}
}
void PortABitValue(int val)
{
LED_A0 = val;
LED_A1 = val;
LED_A2 = val;
LED_A3 = val;
LED_A4 = val;
LED_A5 = val;
}
void PortBBitValue(int val)
{
LED_B0 = val;
LED_B1 = val;
LED_B2 = val;
LED_B3 = val;
LED_B4 = val;
LED_B5 = val;
LED_B6 = val;
LED_B7 = val;
}
void PortCBitValue(int val)
{
LED_C0 = val;
LED_C1 = val;
LED_C2 = val;
LED_C3 = val;
LED_C4 = val;
LED_C5 = val;
LED_C6 = val;
LED_C7 = val;
}
void PortDBitValue(int val)
{
LED_D0 = val;
LED_D1 = val;
LED_D2 = val;
LED_D3 = val;
LED_D4 = val;
LED_D5 = val;
LED_D6 = val;
LED_D7 = val;
}
void PortEBitValue(int val)
{
LED_E0 = val;
LED_E1 = val;
LED_E2 = val;
}
void PortAInitAsDO(void)
{
// make porta all digital
ADCON1 = 0x06;
// set direction as output
LED_A0_dir = 0;
LED_A1_dir = 0;
LED_A2_dir = 0;
LED_A3_dir = 0;
LED_A4_dir = 0;
LED_A5_dir = 0;
// init value
LED_A0 = 0;
LED_A1 = 0;
LED_A2 = 0;
LED_A3 = 0;
LED_A4 = 0;
LED_A5 = 0;
}
void PortBInitAsDO(void)
{
// set direction as output
LED_B0_dir = 0;
LED_B1_dir = 0;
LED_B2_dir = 0;
LED_B3_dir = 0;
LED_B4_dir = 0;
LED_B5_dir = 0;
LED_B6_dir = 0;
LED_B7_dir = 0;
// init value
LED_B0 = 0;
LED_B1 = 0;
LED_B2 = 0;
LED_B3 = 0;
LED_B4 = 0;
LED_B5 = 0;
LED_B6 = 0;
LED_B7 = 0;
}
void PortCInitAsDO(void)
{
// set direction as output
LED_C0_dir = 0;
LED_C1_dir = 0;
LED_C2_dir = 0;
LED_C3_dir = 0;
LED_C4_dir = 0;
LED_C5_dir = 0;
LED_C6_dir = 0;
LED_C7_dir = 0;
// init value
LED_C0 = 0;
LED_C1 = 0;
LED_C2 = 0;
LED_C3 = 0;
LED_C4 = 0;
LED_C5 = 0;
LED_C6 = 0;
LED_C7 = 0;
}
void PortDInitAsDO(void)
{
// set direction as output
LED_D0_dir = 0;
LED_D1_dir = 0;
LED_D2_dir = 0;
LED_D3_dir = 0;
LED_D4_dir = 0;
LED_D5_dir = 0;
LED_D6_dir = 0;
LED_D7_dir = 0;
// init value
LED_D0 = 0;
LED_D1 = 0;
LED_D2 = 0;
LED_D3 = 0;
LED_D4 = 0;
LED_D5 = 0;
LED_D6 = 0;
LED_D7 = 0;
}
void PortEInitAsDO(void)
{
// set direction as output
LED_E0_dir = 0;
LED_E1_dir = 0;
LED_E2_dir = 0;
// init value
LED_E0 = 0;
LED_E1 = 0;
LED_E2 = 0;
}
{
PortAInitAsDO();
PortBInitAsDO();
PortCInitAsDO();
PortDInitAsDO();
PortEInitAsDO();
while(1)
{
PortABitValue(0);
PortBBitValue(0);
PortCBitValue(0);
PortDBitValue(0);
PortEBitValue(0);
Delay_ms(500);
PortABitValue(1);
PortBBitValue(1);
PortCBitValue(1);
PortDBitValue(1);
PortEBitValue(1);
Delay_ms(500);
}
}
void PortABitValue(int val)
{
LED_A0 = val;
LED_A1 = val;
LED_A2 = val;
LED_A3 = val;
LED_A4 = val;
LED_A5 = val;
}
void PortBBitValue(int val)
{
LED_B0 = val;
LED_B1 = val;
LED_B2 = val;
LED_B3 = val;
LED_B4 = val;
LED_B5 = val;
LED_B6 = val;
LED_B7 = val;
}
void PortCBitValue(int val)
{
LED_C0 = val;
LED_C1 = val;
LED_C2 = val;
LED_C3 = val;
LED_C4 = val;
LED_C5 = val;
LED_C6 = val;
LED_C7 = val;
}
void PortDBitValue(int val)
{
LED_D0 = val;
LED_D1 = val;
LED_D2 = val;
LED_D3 = val;
LED_D4 = val;
LED_D5 = val;
LED_D6 = val;
LED_D7 = val;
}
void PortEBitValue(int val)
{
LED_E0 = val;
LED_E1 = val;
LED_E2 = val;
}
void PortAInitAsDO(void)
{
// make porta all digital
ADCON1 = 0x06;
// set direction as output
LED_A0_dir = 0;
LED_A1_dir = 0;
LED_A2_dir = 0;
LED_A3_dir = 0;
LED_A4_dir = 0;
LED_A5_dir = 0;
// init value
LED_A0 = 0;
LED_A1 = 0;
LED_A2 = 0;
LED_A3 = 0;
LED_A4 = 0;
LED_A5 = 0;
}
void PortBInitAsDO(void)
{
// set direction as output
LED_B0_dir = 0;
LED_B1_dir = 0;
LED_B2_dir = 0;
LED_B3_dir = 0;
LED_B4_dir = 0;
LED_B5_dir = 0;
LED_B6_dir = 0;
LED_B7_dir = 0;
// init value
LED_B0 = 0;
LED_B1 = 0;
LED_B2 = 0;
LED_B3 = 0;
LED_B4 = 0;
LED_B5 = 0;
LED_B6 = 0;
LED_B7 = 0;
}
void PortCInitAsDO(void)
{
// set direction as output
LED_C0_dir = 0;
LED_C1_dir = 0;
LED_C2_dir = 0;
LED_C3_dir = 0;
LED_C4_dir = 0;
LED_C5_dir = 0;
LED_C6_dir = 0;
LED_C7_dir = 0;
// init value
LED_C0 = 0;
LED_C1 = 0;
LED_C2 = 0;
LED_C3 = 0;
LED_C4 = 0;
LED_C5 = 0;
LED_C6 = 0;
LED_C7 = 0;
}
void PortDInitAsDO(void)
{
// set direction as output
LED_D0_dir = 0;
LED_D1_dir = 0;
LED_D2_dir = 0;
LED_D3_dir = 0;
LED_D4_dir = 0;
LED_D5_dir = 0;
LED_D6_dir = 0;
LED_D7_dir = 0;
// init value
LED_D0 = 0;
LED_D1 = 0;
LED_D2 = 0;
LED_D3 = 0;
LED_D4 = 0;
LED_D5 = 0;
LED_D6 = 0;
LED_D7 = 0;
}
void PortEInitAsDO(void)
{
// set direction as output
LED_E0_dir = 0;
LED_E1_dir = 0;
LED_E2_dir = 0;
// init value
LED_E0 = 0;
LED_E1 = 0;
LED_E2 = 0;
}
Download Files
For download “mikroC PRO for PIC” project and “Proteus 8.0” simulation files, click here.
Comments
Post a Comment