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.

[SOLVED] Seven Segment with PIC16f877 code issue

Status
Not open for further replies.

WStevens_sa

Member level 2
Joined
Jan 5, 2011
Messages
47
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
South Africa
Activity points
1,695
Hi all

I have some code I created to count 0 to 9 on a 7 segment LED. Everything works fine except when it gets to 9 it shows a zero. The issue at hand is that my function returns an integer instead of the value 0x6F that I pass to PORTD. If I pass it directly to PORTA = 0x6F then it shows a 9. I would like to know if this code is efficient or is there a simpler way.

Code:
int x=0;//Counter to loop through 0 to 9

getnumber(int val) //function to return number as a byte
{
       switch (val) {

  case 0: return 0x3f; break;   // 0 = 0x3f // 1 = 0x06 // 2 = 0x5B // 3 = 0x4F // 4 = 0x66 // 5 = 0x6D
  case 1: return 0x06; break;   // 6 = 0x7D // 7 = 0x07 // 8 = 0x7F // 9 = 0x6F
  case 2: return 0x5B; break;     
  case 3: return 0x4F; break;
  case 4: return 0x66; break;
  case 5: return 0x6D; break;
  case 6: return 0x7D; break;
  case 7: return 0x07; break;
  case 8: return 0x7F; break;
  case 9: return 0x6F; break;
  //default:return 0x3f;
}

}

void main()
{
TRISD = 0; // PORTA set to output
ADCON1 = 7; //Disable ADC so that PORTA can be used for digital purpose
while(1)
{
 for (x = 0; x < 9; x++)
{
Delay_ms(1000);
PORTD = getnumber(x); //Call function with parameter

}
};
}
 

while(1)
{
for (x = 0; x <= 9; x++)
....
{
....
}

better to not leave an open buffer

so use

while(1)
{
for (x = 0; x <= 9;)
...
...
...

x++;
}
<9 = 9 states
<=9 = 10states

this is why it goes to 0 becouse <=9 is needed
 
Last edited:
no dont

use <=9 not <10
otherwise again you leave an open buffer

---------- Post added at 09:28 ---------- Previous post was at 09:25 ----------

if you do <10 and compile youll see it uses more ram than <=9
this is becouse the for statement has to count to 10 to see if its less than 10

an open buffer!!! waisted state introduced
more space and more instructions used

so stick to <=9 for 10 states

a common mistake people make

not me
 
no dont

use <=9 not <10
otherwise again you leave an open buffer

I dint get you. Can you explain in detail

---------- Post added at 13:06 ---------- Previous post was at 12:59 ----------

Thanx VSMVDD
 

im sorry but you need to study to understand logic more fully
 

Hi I have simplified my code by removing the case switch and used an array. I also left the for statement to <=9 as i realised it uses more memory.

Working code. Hope this helps somebody else.

Code:
int x=0;//Counter to loop through 0 to 9
char myarray[10]={0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // using array instead of case statement with fucntion call.
char ad;

void main()
{
TRISD = 0; // PORTA set to output
ADCON1 = 7; //Disable ADC so that PORTA can be used for digital purpose
while(1)
{
for (x = 0; x <= 9;)
{
Delay_ms(500);
PORTD = myarray[x]; // get segment byte from an array
x++;
}
};
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top