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 support@ccsinfo.com

counting on a 7 segment display (please help!)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jov_damo86



Joined: 29 Nov 2007
Posts: 9
Location: Sarrat, Ilocos Norte,Philippines

View user's profile Send private message

counting on a 7 segment display (please help!)
PostPosted: Thu Nov 29, 2007 3:45 am     Reply with quote

please any code for PIC16f877A to display a simple counting 0-9 and then reset on portB using 7 segment display.

i really appreciate any help for this project!
thank you! Wink
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Thu Nov 29, 2007 4:20 am     Reply with quote

This should help
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011019
jov_damo86



Joined: 29 Nov 2007
Posts: 9
Location: Sarrat, Ilocos Norte,Philippines

View user's profile Send private message

PostPosted: Thu Nov 29, 2007 4:39 am     Reply with quote

Thanks a lot but i need for C language using CCS C compiler!

your contribution help me a lot!

God Bless!
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Thu Nov 29, 2007 4:47 am     Reply with quote

Feel free to use this code.

It uses 6x7 segment LED display
SPI input




Code:

#include <16F818.H>

#use delay(clock=8000000)

//////// Fuses: LP,XT,HS,EC_IO,INTRC_IO,INTRC,RC_IO,RC,WDT,NOWDT,PUT,NOPUT
//////// Fuses: MCLR,NOMCLR,BROWNOUT,NOBROWNOUT,LVP,NOLVP,CPD,NOCPD,WRT
//////// Fuses: NOWRT,DEBUG,NODEBUG,CCPB2,CCPB3,PROTECT,NOPROTECT

#fuses INTRC_IO,NOPROTECT,NOWDT,PUT,NOLVP,NOMCLR,NOWRT,NODEBUG,BROWNOUT,CCPB3
#byte OSCCON = 0x8F
#bit CCP1IF = 0x0C.2

                                 // Clock mode=1 for Address B
#define Display_type 0            // Normal mode=0 for address A



#if Display_type==0
#define SLAVE_ADDRESS 'A'
#endif

#if Display_type==1
#define SLAVE_ADDRESS 'B'
#endif

#define SEGMENT_MODE 2           // 1=NPN OR 2=PNP

struct HighLow
{
char l;
char h;
};

union hl
{
long L;
struct HighLow hl;
};


//          0-A
//       ********
//    5-F*      *1-B
//       *  6-G *
//       ********
//    4-E*      *2-C
//       *  3-D *
//       ********
//

//0123456789_- //
byte CONST LED_MAP[13] =
{0b11000000,0b11111001,0b10100100,0b10110000,0b10011001,0b10010010
,0b10000010,0b11111000,0b10000000,0b10010000,0b11110111,0b10111111,0b11111111};


#define SEG_A PIN_A0
#define SEG_B PIN_A1
#define SEG_C PIN_A2
#define SEG_D PIN_A3
#define SEG_E PIN_A4
#define SEG_F PIN_A6
#define SEG_G PIN_A7

//#define DIG_1 PIN_B6
//#define DIG_2 PIN_B7
//#define DIG_3 PIN_B0
//#define DIG_4 PIN_B5
//#define DIG_5 PIN_B3
//#define DIG_6 PIN_B5

#define DIG_1 PIN_B5
#define DIG_2 PIN_B3
#define DIG_3 PIN_B6
#define DIG_4 PIN_B0
#define DIG_5 PIN_B7
//#define DIG_6 PIN_B5

#if SEGMENT_MODE==1 //NPN
#define SEG_A_ON output_low(SEG_A)
#define SEG_B_ON output_low(SEG_B)
#define SEG_C_ON output_low(SEG_C)
#define SEG_D_ON output_low(SEG_D)
#define SEG_E_ON output_low(SEG_E)
#define SEG_F_ON output_low(SEG_F)
#define SEG_G_ON output_low(SEG_G)

#define SEG_A_OFF output_high(SEG_A)
#define SEG_B_OFF output_high(SEG_B)
#define SEG_C_OFF output_high(SEG_C)
#define SEG_D_OFF output_high(SEG_D)
#define SEG_E_OFF output_high(SEG_E)
#define SEG_F_OFF output_high(SEG_F)
#define SEG_G_OFF output_high(SEG_G)

#define DIG_1_ON output_high(DIG_1);
#define DIG_2_ON output_high(DIG_2);
#define DIG_3_ON output_high(DIG_3);
#define DIG_4_ON output_high(DIG_4);
#define DIG_5_ON output_high(DIG_5);
#define DIG_6_ON output_high(DIG_6);

#define DIG_1_OFF output_low(DIG_1);
#define DIG_2_OFF output_low(DIG_2);
#define DIG_3_OFF output_low(DIG_3);
#define DIG_4_OFF output_low(DIG_4);
#define DIG_5_OFF output_low(DIG_5);
#define DIG_6_OFF output_low(DIG_6);


#endif

#if SEGMENT_MODE==2  // PNP
#define SEG_A_ON output_high(SEG_A)
#define SEG_B_ON output_high(SEG_B)
#define SEG_C_ON output_high(SEG_C)
#define SEG_D_ON output_high(SEG_D)
#define SEG_E_ON output_high(SEG_E)
#define SEG_F_ON output_high(SEG_F)
#define SEG_G_ON output_high(SEG_G)

#define SEG_A_OFF output_low(SEG_A)
#define SEG_B_OFF output_low(SEG_B)
#define SEG_C_OFF output_low(SEG_C)
#define SEG_D_OFF output_low(SEG_D)
#define SEG_E_OFF output_low(SEG_E)
#define SEG_F_OFF output_low(SEG_F)
#define SEG_G_OFF output_low(SEG_G)

#define DIG_1_ON output_low(DIG_1);
#define DIG_2_ON output_low(DIG_2);
#define DIG_3_ON output_low(DIG_3);
#define DIG_4_ON output_low(DIG_4);
#define DIG_5_ON output_low(DIG_5);
#define DIG_6_ON output_low(DIG_6);

#define DIG_1_OFF output_high(DIG_1);
#define DIG_2_OFF output_high(DIG_2);
#define DIG_3_OFF output_high(DIG_3);
#define DIG_4_OFF output_high(DIG_4);
#define DIG_5_OFF output_high(DIG_5);
#define DIG_6_OFF output_high(DIG_6);


#endif


byte string_data[7];
byte display_data[7];
byte display_data1[7];
byte display_output=0;
int display_index=0;
int rx_index=0;
int display_char;
int display_seg;
int rx_in;
int check_sum;
int temp=0;

short refresh_bit=0;

union HL display_bits;

#INT_SSP
void ssp_interupt ()
   {

   rx_in=spi_read();

   if(rx_index==0)
   if (rx_in==SLAVE_ADDRESS)
      {
      rx_index=1;
      return;
      }

   if(rx_index==1)
      {
      display_bits.hl.h=rx_in;
      rx_index=2;
      return;
      }


   if(rx_index==2)
      {
      display_bits.hl.l=rx_in;
      rx_index=3;
      return;
      }

   if(rx_index==3)
      {
      check_sum=rx_in;
      rx_index=4;
      return;
      }
   rx_index=0;
   setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_CLK_DIV_16 | SPI_SS_DISABLED);
   enable_interrupts(INT_SSP);
   }



#int_ccp1   // **************** ccp1 second timer interrupt *******************//
void refresh_display ()
   {
refresh_bit=0;
display_index+=1;
if (display_index>4)
   display_index=0;

   DIG_1_OFF;
   DIG_2_OFF;
   DIG_3_OFF;
   DIG_4_OFF;
   DIG_5_OFF;


display_output=display_data[display_index];
display_seg=display_data1[display_index];

if (bit_test(display_output,0))
   SEG_A_ON;
else
   SEG_A_OFF;

if (bit_test(display_output,1))
   SEG_B_ON;
else
   SEG_B_OFF;

if (bit_test(display_output,2))
   SEG_C_ON;
else
   SEG_C_OFF;

if (bit_test(display_output,3))
   SEG_D_ON;
else
   SEG_D_OFF;

if (bit_test(display_output,4))
   SEG_E_ON;
else
   SEG_E_OFF;

if (bit_test(display_output,5))
   SEG_F_ON;
else
   SEG_F_OFF;

if (bit_test(display_output,6))
   SEG_G_ON;
else
   SEG_G_OFF;



if(display_index==0)
   DIG_1_ON;
if(display_index==1)
   DIG_2_ON;
if(display_index==2)
   DIG_3_ON;
if(display_index==3)
   DIG_4_ON;
if(display_index==4)
   DIG_5_ON;




if(display_seg==0)     //dig0, 6
   CCP_1=500;
if(display_seg==1)     //dig1, 2
   CCP_1=150;
if(display_seg==2)     //dig2, 5
   CCP_1=400;
if(display_seg==3)     //dig3, 5
   CCP_1=400;
if(display_seg==4)     //dig4, 4
   CCP_1=350;
if(display_seg==5)     //dig5, 5
   CCP_1=400;
if(display_seg==6)     //dig6, 6
   CCP_1=450;
if(display_seg==7)     //dig7, 3
   CCP_1=200;
if(display_seg==8)     //dig8, 7
   CCP_1=500;
if(display_seg==9)     //dig9, 6
   CCP_1=450;

if(display_seg==11)     //dig9, 6
   CCP_1=200;

set_timer1(0);
ccp1if=0;


}


void main(void)
{
OSCCON=OSC_8MHZ;
   setup_wdt(WDT_144MS);

   display_data[0]=LED_MAP[11];
   display_data[1]=LED_MAP[11];
   display_data[2]=LED_MAP[11];
   display_data[3]=LED_MAP[11];
   display_data[4]=LED_MAP[11];

   display_data1[0]=11;
   display_data1[1]=11;
   display_data1[2]=11;
   display_data1[3]=11;
   display_data1[4]=11;

   setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);         // timer 1 interval
   setup_ccp1(CCP_COMPARE_INT); // Configure CCP1 to set
   enable_interrupts(INT_CCP1);         // No secondary serial interrupts

   set_timer1(0);
   CCP_1=5000;
   setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_CLK_DIV_16 | SPI_SS_DISABLED);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);         // No secondary serial interrupts
   do
   {

   restart_wdt();
//if (refresh_bit)
//   refresh_display();



if (rx_index==4)
   {
   if(check_sum==display_bits.hl.l+display_bits.hl.h+128)
      #if Display_Type==1
      sprintf(string_data,"%05lu",display_bits.L);
      #else
      sprintf(string_data,"%5lu",display_bits.L);
      #endif
      temp=0;
      rx_index=0;
      do
      {

      display_char=string_data[temp];
      if (display_char>47 && display_char<58)
         display_char=display_char-48;

      if (display_char>10)        // not within character specs
         display_char=12;       // space

      #if Display_type==1
      if (temp==2)         // for a time chip remove this line
           display_char=11;
      #endif


      display_data[temp]=LED_MAP[display_char];
      display_data1[temp]=display_char;

      temp+=1;
      }while (temp<=6);


   }


   } while (TRUE);
}

jov_damo86



Joined: 29 Nov 2007
Posts: 9
Location: Sarrat, Ilocos Norte,Philippines

View user's profile Send private message

PostPosted: Thu Nov 29, 2007 5:10 am     Reply with quote

oh yes! thanks you very much!

i'll try to examine it! Wink
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

PostPosted: Thu Nov 29, 2007 2:21 pm     Reply with quote

I have a 7seg display with a common cathode, which is connected to the PORTB (bits 0-6) of a PIC.
this is the array of 0-9 values where index is the number I want to show.
Code:
const int8 bcd27seg[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98};//BCD to 7seg

void WriteDigit(char c) //c is a value of 0 to 9
{
   if(c<10)
   {// number
      output_b(bcd27seg[c]);
   }
}


good luck
jov_damo86



Joined: 29 Nov 2007
Posts: 9
Location: Sarrat, Ilocos Norte,Philippines

View user's profile Send private message

PostPosted: Fri Nov 30, 2007 9:16 pm     Reply with quote

oh! how nice... thanks meereck for that code.. Wink
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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