0% found this document useful (0 votes)
27 views5 pages

Proteus 0x19: PIC16F877A Code Examples

The document contains multiple C code snippets for programming a PIC16F877A microcontroller. The first snippet implements a 4x4 keypad interface to control LED output based on key presses, while the second snippet displays numbers on a 7-segment display based on input conditions. The third snippet controls LED patterns based on button presses, demonstrating various output configurations.

Uploaded by

tuankhoa311204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Proteus 0x19: PIC16F877A Code Examples

The document contains multiple C code snippets for programming a PIC16F877A microcontroller. The first snippet implements a 4x4 keypad interface to control LED output based on key presses, while the second snippet displays numbers on a 7-segment display based on input conditions. The third snippet controls LED patterns based on button presses, demonstrating various output configurations.

Uploaded by

tuankhoa311204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercise 1

#include <16F877A.h>
#include <key_4x4.c>
#use delay(clock=20000000)
#BIT PORTCbits_RC0 = 0X07.0
#BIT PORTCbits_RC1 = 0X07.1
#BIT PORTCbits_RC3 = 0X07.3
const unsigned char font []= {0, 0, 0, 0, 0, 0, 0, 0,
3, 65,127,127,65, 3,0,0
0, 0, 0, 0, 0, 0, 0, 0,
};
void main()
{
unsigned int8 key;
SET_TRIS_C(0x00);
set_tris_b(0x00);
key_4x4_init();
unsigned rol, delay, col;
while(TRUE)
{
key=get_key_4x4();
if(key)
{
if ( key == '1'){
for(rol=0;rol<8;rol++)
{
for(delay=0;delay<20;delay++)
{
PORTCbits_RC0 = 1; PORTCbits_RC0 = 0; PORTCbits_RC1 = 1;
PORTCbits_RC3 = 1;
PORTCbits_RC3 = 0;
for(col=0;col<16;col++) // 16 hang va cot
{
OUTPUT_B (~ font[col + rol]);
delay_us(300);
PORTCbits_RC0 = 1; PORTCbits_RC0 = 0; PORTCbits_RC1 = 0;
PORTCbits_RC3 = 1;
PORTCbits_RC3 = 0;
}

}
while(key)key=get_key_4x4();

}
delay_ms(10);

}
}
}
}

1.
#include <16F877A.h>
#include <key_4x4.c>
#use delay(clock=20000000)
const char MA7D[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
void main(){
set_tris_a(0xFF);
set_tris_b(0x00);
set_tris_c(0b11111110);
set_tris_d(0x00);
int i = 0, dv, chuc;
output_low(PIN_C0);
while(TRUE){
if(input(PIN_A0) == 1){
output_b(MA7D[chuc]);
output_d(MA7D[dv]);
}
else{
i = i + 1;
dv = i%10;
chuc = i / 10;
output_b(MA7D[chuc]);
output_d(MA7D[dv]);
delay_ms(300);
if( i > 10){output_high(PIN_C0);}
}
}
}
2.

#include <16F877A.h>
#include <key_4x4.c>
#use delay(clock=20000000)
const char LED[8]={0b10000000, 0b01000000, 0b00100000, 0b00010000, 0b00001000,
0b00000100, 0b00000010, 0b00000001};
void main(){
set_tris_a(0xFF);
set_tris_b(0x00);
int i ;
while(TRUE){

if(input(PIN_A0) == 0){
for(i=0;i <9; i++)
{
switch(i)
{
case 0: output_b(0); break;
case 1: output_b(0b00000001); break;
case 2: output_b(0b00000010); break;
case 3: output_b(0b00000100); break;
case 4: output_b(0b00001000); break;
case 5: output_b(0b00010000); break;
case 6: output_b(0b00100000); break;
case 7: output_b(0b01000000); break;
case 8: output_b(0b10000000); break;
default:break;
}
delay_ms(300);}
} else if (input(PIN_A5) == 0){
for(i=0;i <9; i++)
{switch(i)
{
case 0: output_b(0); break;
case 1: output_b(0b10000000); break;
case 2: output_b(0b01000000); break;
case 3: output_b(0b00100000); break;
case 4: output_b(0b00010000); break;
case 5: output_b(0b00001000); break;
case 6: output_b(0b00000100); break;
case 7: output_b(0b00000010); break;
case 8: output_b(0b00000001); break;
default:break;
}
delay_ms(300);}
} else output_b(0);
}
}

You might also like