CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Single Digit 7 Segment Display with Increment/Decrement

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
MassaM



Joined: 08 Jun 2019
Posts: 31

View user's profile Send private message Visit poster's website

Single Digit 7 Segment Display with Increment/Decrement
PostPosted: Thu Jul 18, 2019 5:27 am     Reply with quote

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! Smile
_________________
while(!dead)
{
keepLearning();
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group