Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Where is the error in my code

Status
Not open for further replies.

junkie_

Junior Member level 2
Joined
Oct 9, 2005
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,476
Dear board,
I am back with another problem with the code mentioned below:
Code:
#include<pic.h>      //Define PIC Registers
__CONFIG(0x3f72);//HS oscillator, Enable(PWRTE,BOREN),                                            
          //Disable(CPD,CP,WDTEN,In-circuit Debugger)
#define CNTRL_PORT PORTA
#define DATA_PORT  PORTB
 
void hex2dec(unsigned char);
void send_seg(unsigned char,unsigned char,
unsigned char,unsigned char);
void DelayMs(unsigned int);
unsigned char x;
unsigned char hourt=0,hour=0,minten=0,minute=0 ,secten=0;
unsigned char CA[10] =
   {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//unsigned char CC[10] =  
//{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char CA_CNTRL[5]    =    {0x1F,0x2F,0x37,0x3B,0x3D};
//unsigned char CC_CNTRL[4]    =    {0x08,0x04,0x02,0x01};
unsigned char n=1;
void main()
{
//   unsigned char number;
   nRBPU =0;
   TRISB=0x00;  //PORTB configured as O/P
   ADCON1=0x07; //Configure PORTA & PORTE as Digital port
   TRISA=0x00;  //PORTA Configured as O/P
   while(1)
   {
      if(x == 200)  
      {
         x=0;
         secten++; //Inc the value upto 9 in unit place
         if(secten>9)
         {
		 minute++; //Inc the value upto 9 in unit place
         if(minute>9)
         {
            minute=0;
            minten++; //Inc the value upto 9 in Tenth place              
            if(minten>9)
            {
               minten=0;
               hour++;//Inc the value upto 9 in Hundredth                   
               if(hour>9)
               {
                  hour=0;
                  hourt++;//Inc value upto 9 in hourtsandth
                  if(hourt>9)
                     hourt=0;
               }
            }
         }
		 }
      }
      x++;
      send_seg(hourt,hour,minten,minute,secten);     
   }
}
void send_seg(unsigned char hourt,unsigned char hour,
unsigned char minten,unsigned char minute,unsigned char secten)
{
   if(n==1)
   {
      CNTRL_PORT=CA_CNTRL[0];//Enable unit place
      DATA_PORT=CA[secten];  //Display Unit Place Number
      n=2;
      DelayMs(2);
   }
   else if(n==2)
       {
         CNTRL_PORT=CA_CNTRL[1];//Enable 10th place
         DATA_PORT=CA[minute]; //Display Tenth Place Number
         n=3;
         DelayMs(2);
       }
       else if(n==3)
       {
          CNTRL_PORT=CA_CNTRL[2];                          
          //Eanble Hundredth place 7-Segment
          DATA_PORT=CA[minten];                             
          //Display Hundredth Place Number
          n=4;
          DelayMs(2);
       }
       else if(n==4)
       {
          CNTRL_PORT=CA_CNTRL[3];                       
          //Enable Thousandth place 7-Segment
          DATA_PORT=CA[hour];                              
          //Display Thousandth Place Number
          n=1;
          DelayMs(2);
       }
	   else if(n==5)
       {
          CNTRL_PORT=CA_CNTRL[4];                       
          //Enable Thousandth place 7-Segment
          DATA_PORT=CA[hourt];                              
          //Display Thousandth Place Number
          n=1;
          DelayMs(2);
       }
}
 
void DelayMs(unsigned int Ms)
{
   int delay_cnst;
   while(Ms>0)
   {
      Ms--;
      for(delay_cnst = 0;delay_cnst <120;delay_cnst++);
   }
}

Code was compiling fine before I added "secten" now I am getting the following compile error:
Code:
Error   [186] D:\Emergency Move\Personal\New Electronics\7 Segments XC8\My own Code\main.c; 56.41 too many function arguments
Error   [984] D:\Emergency Move\Personal\New Electronics\7 Segments XC8\My own Code\main.c; 61.1 type redeclared
Error   [1098] D:\Emergency Move\Personal\New Electronics\7 Segments XC8\My own Code\main.c; 61.1 conflicting declarations for variable "send_seg" (D:\Emergency Move\Personal\New Electronics\7 Segments XC8\My own Code\main.c:9)
Error   [253] D:\Emergency Move\Personal\New Electronics\7 Segments XC8\My own Code\main.c; 61.1 argument list conflicts with prototype

its is PIC16F876A and compiler is MPLAB PIC C Pro.
 

I think you should change the declaration :

Code:
void send_seg(unsigned char,unsigned char,
unsigned char,unsigned char);

by

Code:
void send_seg(unsigned char,unsigned char,
unsigned char,unsigned char, unsigned char);
 

move all the functions up before void main(void). Post the new errors.
 

move all the functions up before void main(void).
You can either use forward declarations or reverse the code order. But if you use forward declarations, they must be correct, as already mentioned by aProgrammer.
 

Well I did as "aProgrammer" suggested and corrected one error in char declaration the code is now compiling OK, but proteus simulation shows weird behavior. I know I am making a fundamental error in the code "CNTRL", "if statements" or "CA[]", but cant seem to put my finger on it. Appreciate if you guys could have a look at it.
Revised code is:
Code:
#include<pic.h>      //Define PIC Registers
__CONFIG(0x3f72);//HS oscillator, Enable(PWRTE,BOREN),                                            
          //Disable(CPD,CP,WDTEN,In-circuit Debugger)
#define CNTRL_PORT PORTA
#define DATA_PORT  PORTB
 
void hex2dec(unsigned char);
void send_seg(unsigned char,unsigned char,
unsigned char,unsigned char,
unsigned char,unsigned char);
void DelayMs(unsigned int);
unsigned char x;
unsigned char hourt=0,hour=0,minten=0,minute=0 ,secten=0,seconds=0;
unsigned char CA[10] =
   {0x40,0x79,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned char CA_CNTRL[6]    =    {0x1F,0x2F,0x37,0x3B,0x3D,0x3E};
unsigned char n=1;
void send_seg(unsigned char hourt,unsigned char hour,
unsigned char minten,unsigned char minute,
unsigned char secten,unsigned char seconds)
{
if(n==1)
   {
      CNTRL_PORT=CA_CNTRL[0];//Enable unit place
      DATA_PORT=CA[seconds];  //Display Unit Place Number
      n=2;
      DelayMs(2);
   }   
if(n==2)
   {
      CNTRL_PORT=CA_CNTRL[1];//Enable unit place
      DATA_PORT=CA[secten];  //Display Unit Place Number
      n=2;
      DelayMs(2);
   }
   else if(n==3)
       {
         CNTRL_PORT=CA_CNTRL[2];//Enable 10th place
         DATA_PORT=CA[minute]; //Display Tenth Place Number
         n=3;
         DelayMs(2);
       }
       else if(n==4)
       {
          CNTRL_PORT=CA_CNTRL[3];                          
          //Eanble Hundredth place 7-Segment
          DATA_PORT=CA[minten];                             
          //Display Hundredth Place Number
          n=4;
          DelayMs(2);
       }
       else if(n==5)
       {
          CNTRL_PORT=CA_CNTRL[4];                       
          //Enable Thousandth place 7-Segment
          DATA_PORT=CA[hour];                              
          //Display Thousandth Place Number
          n=1;
          DelayMs(2);
       }
	   else if(n==6)
       {
          CNTRL_PORT=CA_CNTRL[5];                       
          //Enable Thousandth place 7-Segment
          DATA_PORT=CA[hourt];                              
          //Display Thousandth Place Number
          n=1;
          DelayMs(2);
       }
}
 
void DelayMs(unsigned int Ms)
{
   int delay_cnst;
   while(Ms>0)
   {
      Ms--;
      for(delay_cnst = 0;delay_cnst <120;delay_cnst++);
   }
}
void main()
{
//   unsigned char number;
   nRBPU =0;
   TRISB=0x00;  //PORTB configured as O/P
   ADCON1=0x07; //Configure PORTA & PORTE as Digital port
   TRISA=0x00;  //PORTA Configured as O/P
   while(1)
   {
      if(x == 200)  
      {
         x=0;
seconds++;
if(seconds>9)
         {
		 seconds=0;      
		secten++; //Inc the value upto 9 in unit place
         if(secten>9)
         {
		 secten=0;
		 minute++; //Inc the value upto 9 in unit place
         if(minute>9)
         {
            minute=0;
            minten++; //Inc the value upto 9 in Tenth place              
            if(minten>9)
            {
               minten=0;
               hour++;//Inc the value upto 9 in Hundredth                   
               if(hour>9)
               {
                  hour=0;
                  hourt++;//Inc value upto 9 in hourtsandth
                  if(hourt>9)
                     hourt=0;
               }
            }
         }
}
		 }
      }
      x++;
     send_seg(hourt,hour,minten,minute,secten,seconds);     //(seconds,secten,minute,minten,hour,hourt); 
   }
}

Also attached is proteus simulation along hex and cof file, will highly appreciate inputs.
 

Attachments

  • 7 Segment Testing.zip
    12.2 KB · Views: 64

I do not know that the program is supposed to do; you have put comments that are hardly useful in understanding. What you are trying to display? What is the display device?
 

I'm not sure how you intend the "send_seg" function to operate, but I suspect there may be issues with the way you use the 'n' variable.
It is initialised at the start of the program to 1. The first time 'send_seg' is called it will be updated to 2. The next time 'send_seg' is called you reset the value of 'n' back to 2!
As I can't see anywhere else that you update the 'n' variable, I suspect this will not let 'send_seg' operate the what I think you intend.
Susan
 

Thanks susan,

Actually what I am trying to do is first learn 7 segment multiplexing, secondly with this program create a 6 digit 7 segment counter.
Third stage is to incorporate the multiplexing code with my RTC code of DS1307.

What/where I am doing wrong, is it syntax error or something else, it was working fine for 4 digits did run proper simulation for upto "9999" as soon as I added two more digits things have gone totally south. Now it shows same number on 6,5,4,3 and 1 digit while second digit remains blank.
 

If it is a syntax error then the compiler will have picked it up. Therefore it is a logical error.
I mentioned above the inconsistent way you are setting the 'n' variable. I'm guessing but what you have written might have been correct at one time in the program's life, but when you added the last 2 digits, you have not updated how the 'n' variable is set.
Also you are inconsistent in how you test for the value of 'n'. The first 'if' has no 'else' clause and so the 2nd 'if' statement will always be executed. However it doe shave an 'else' clause and so do the subsequent 'if' statements.
Can I suggest that you use a 'switch' statement instead of these nested (or not) 'if' statements as, from what I can see, you only ever execute one option for any call of the function.
Also, I suggest that you go through the code step by step, either with a debugger or with pencil and paper and make sure the code is doing what you think it should do.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top