0% found this document useful (0 votes)
3 views3 pages

PIC16F887 7-Segment Counter Code

This document contains a C program for the PIC16F887 microcontroller that implements a counting mechanism using two switches and a 7-segment display. The program increments a count from 0 to 9 based on the state of the switches, with specific conditions for starting and stopping the count. It includes a lookup table for the 7-segment display encoding and uses a 4 MHz clock for timing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

PIC16F887 7-Segment Counter Code

This document contains a C program for the PIC16F887 microcontroller that implements a counting mechanism using two switches and a 7-segment display. The program increments a count from 0 to 9 based on the state of the switches, with specific conditions for starting and stopping the count. It includes a lookup table for the 7-segment display encoding and uses a 4 MHz clock for timing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//MUHAMMAD AFIQ IRFAN BIN MOHAMED RODWAN

//060817060441

#include <16f887.h> // PIC16f887 Header File

#fuses XT, NOWDT, NOLVP, NOPROTECT, BROWNOUT, PUT // External clock, no watchdog
timer, no low voltage programming

#use delay(clock=4000000) // 4 MHz crystal

// Pin Definitions

#define S1 PIN_A0 // Switch 1

#define S2 PIN_A1 // Switch 2

// 7-Segment Display Lookup Table (common cathode)

const int segCode[10] = {

0b00111111, // 0

0b00000110, // 1

0b01011011, // 2

0b01001111, // 3

0b01100110, // 4

0b01101101, // 5

0b01111101, // 6

0b00000111, // 7

0b01111111, // 8

0b01101111 // 9

};
void main() {

int count = 0;

short isCounting = 0;

set_tris_d(0);

set_tris_a(0xFF);

output_d(0);

while (true) {

if (input(S1) && input(S2)) {

isCounting = 0;

delay_ms(200);

else if (input(S1)) {

isCounting = 1; //

delay_ms(200); //

else if (!input(S1) && !input(S2)) {

isCounting = 0;

output_d(0);

}
if (isCounting) {

output_d(segCode[count]);

delay_ms(1000);

count++;

if (count > 9) count = 0;

Suis tu pastikan dalam kedudukan tertutup sebab litar dalam keaadan active high

Baru tekan sw1 lepastu sw 2

You might also like