MassaM
Joined: 08 Jun 2019 Posts: 31
|
Single Digit 7 Segment Display with Increment/Decrement |
Posted: Thu Jul 18, 2019 5:27 am |
|
|
Code: |
/*
Tutorial: Single Digit 7 Segment - Increment and Decrement with buttons and display/output
Compiler: CCS-C-PIC - PCWHD Version 5.076
Author: MassaM SDC (Solutions Development Consultancy), June 2019
Contact: Reach me on,
Facebook: https://www.facebook.com/massam.sdc
Youtube: https://www.youtube.com/channel/UCbgiHBouih1yzir6Y1zX2VA
Chip: PIC16F877A@4Mhz
*/
#include <16f877A.h> // include the MCUs main header/definitions file
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD // MCU setup the fuses
#use delay(clock=4Mhz) // set the oscillator/clock to 4 Mhz
#byte TRISB = 0x86 //identify TRISB
#byte TRISC = 0x87 //identify TRISC
#byte PORTB = 0x06 //identify PORTB
#byte PORTC = 0x07 //identify PORTC
#bit TRISB0 = TRISB.0 // bit0 of TRISB
#bit TRISB1 = TRISB.1 // bit1 of TRISB
#bit SW_INC = PORTB.0 // bit0 of PORTB
#bit SW_DEC = PORTB.1 // bit1 of PORTB
void main() // main application start
{
// numbers in hex
// 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
int digit[]={0x3F, 0x06, 0x5b, 0x4F, 0x66, 0x6D, 0xFD, 0x7, 0xFF, 0x6F};
signed int count = 0; // initial count is 0
int switch_state = 1; // initial switch state is high
TRISB0 = 1; // Port B bit 0 is input
TRISB1 = 1; // Port B bit 1 is input
TRISC = 0; // Port C all are outputs for 7 Segments
SW_INC = 1;// initial port B bit 0 is high
SW_DEC = 1;// initial port B bit 1 is high
PORTC = digit[count]; // initial Port C is set to display digit 0
while(TRUE) // inifite/forever loop
{
if( SW_INC == 1 && SW_DEC == 1) // if both buttons are at high state
switch_state = 1; // set the switch state flag to 1 as high
if(switch_state == 1) // check if switch state flag is still at 1 as high
{
if(SW_INC == 0) // if Increment switch was pressed and in low state
{
switch_state = 0; // set the switch state flag to 0 as low
count++; // add 1 to count
if(count > 9) // if count reached nine,
count = 0; // then reset to zero
PORTC = digit[count]; // display/output the count as 7 segment digit
delay_ms(10); // delay for 7segment refresh time
}
if(SW_DEC == 0) // if Decrement switch was pressed and in low state
{
switch_state = 0; // set the switch state flag to 0 as low
count--; // minus 1 from count
if(count < 0) // if count reached zero,
count = 9; // then reset to nine
PORTC = digit[count]; // display/output the count as 7 segment digit
delay_ms(10); // delay for 7segment refresh time
}
} // switc_state == 1
} // while
} // main
|
Next edition will be with a 74HC164 to use only 2 pins of MCU. Stay tuned!
Hope this helps! _________________ while(!dead)
{
keepLearning();
} |
|