Chào các bạn hôm nay mình sẽ hướng dẫn các bạn lập trình cho PIC16F877A đếm từ 0 đến 99 sử dụng 2 nút nhấn, Sơ đồ như hình bên dưới.
Đây là code chương trình.
#define _XTAL_FREQ 8000000
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
// CONFIG
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = ON
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
} //case end
}
unsigned short digit_no, digit10, digit1, digit;
void interrupt ISR (void)
{
if (digit_no==0) {
RB7=0;
RB6 = 0; // Turn off all 7seg displays
PORTD = digit1; // send mask for ones digit to PORTD
RB7 = 1; // turn on 1st 7 seg., turn off 2nd
digit_no = 1;
} else {
RB6=0;
RB7 = 0; // Turn off all 7seg displays
PORTD = digit10; // send mask for tens digit to PORTD
RB6 = 1; // turn on 2nd 7 seg., turn off 1st
digit_no = 0;
}
TMR0 = 0; // clear TMRO
INTCON = 0x20; // clear TMR0IF and set TMR0IE
}
void display(unsigned short i){
unsigned short digit;
digit=i%10;
digit1=mask(digit); //hien thi hang don vi
digit=i/10;
digit10=mask(digit); // hien thi hang chuc
__delay_ms(500);
}
void main() {
interrupt ISR();
unsigned short i;
OPTION_REG = 0x80; // Timer0 settings
TMR0 = 0;
INTCON = 0xA0; // Disable PEIE,INTE,RBIE,T0IE
TRISB=0x03;
TRISD=0x00;
TRISA=0X00;
PORTB=0;
PORTD=0;
i=0;
display(i);
for(;;){
lap:
if(!RB0){
i=i++;
display(i);
if(i==22)
i=0;
}
if(i>0){
if(!RB1){
i=i--;
display(i);
}
} goto lap;
}
}
Đây là code chương trình.
#define _XTAL_FREQ 8000000
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
// CONFIG
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = ON
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
} //case end
}
unsigned short digit_no, digit10, digit1, digit;
void interrupt ISR (void)
{
if (digit_no==0) {
RB7=0;
RB6 = 0; // Turn off all 7seg displays
PORTD = digit1; // send mask for ones digit to PORTD
RB7 = 1; // turn on 1st 7 seg., turn off 2nd
digit_no = 1;
} else {
RB6=0;
RB7 = 0; // Turn off all 7seg displays
PORTD = digit10; // send mask for tens digit to PORTD
RB6 = 1; // turn on 2nd 7 seg., turn off 1st
digit_no = 0;
}
TMR0 = 0; // clear TMRO
INTCON = 0x20; // clear TMR0IF and set TMR0IE
}
void display(unsigned short i){
unsigned short digit;
digit=i%10;
digit1=mask(digit); //hien thi hang don vi
digit=i/10;
digit10=mask(digit); // hien thi hang chuc
__delay_ms(500);
}
void main() {
interrupt ISR();
unsigned short i;
OPTION_REG = 0x80; // Timer0 settings
TMR0 = 0;
INTCON = 0xA0; // Disable PEIE,INTE,RBIE,T0IE
TRISB=0x03;
TRISD=0x00;
TRISA=0X00;
PORTB=0;
PORTD=0;
i=0;
display(i);
for(;;){
lap:
if(!RB0){
i=i++;
display(i);
if(i==22)
i=0;
}
if(i>0){
if(!RB1){
i=i--;
display(i);
}
} goto lap;
}
}
Link download project.
EmoticonEmoticon