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

switch VS array which one is more efficient?[SOLVED]

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



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

switch VS array which one is more efficient?[SOLVED]
PostPosted: Thu Apr 09, 2015 6:00 pm     Reply with quote

Greeting my friends,
I am working on a 7 segments driver. The segments part, I got it working good with both switch case statement 0-9 and with int array[10]. which one is more efficient for this purpose? Why?
Thanks,


Last edited by Sam_40 on Tue Apr 14, 2015 2:48 pm; edited 2 times in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 09, 2015 6:08 pm     Reply with quote

Quote:
which is faster switch() or if() else

Partial answer:

See these posts on switch-case efficiency by Ttelmah:
http://www.ccsinfo.com/forum/viewtopic.php?t=47315
http://www.ccsinfo.com/forum/viewtopic.php?t=45415

More threads on switch-case efficiency in CCS:
http://www.ccsinfo.com/forum/viewtopic.php?t=28643
http://www.ccsinfo.com/forum/viewtopic.php?t=21373
http://www.ccsinfo.com/forum/viewtopic.php?t=17868
http://www.ccsinfo.com/forum/viewtopic.php?t=20721
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Thu Apr 09, 2015 6:24 pm     Reply with quote

PCM programmer,
What is your choice for similar task?
Which one is more efficient for my purpose and why?
They both worked and I did not notice the different.
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Thu Apr 09, 2015 6:29 pm     Reply with quote

I forgot to mention I did not use the default in the switch statement!
Thanks,
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 09, 2015 6:55 pm     Reply with quote

You should take the time and effort to dump the listings of your two test programs. Within the lines of assembler, you'll soon see which is 'better'.
A program can be 'better' because it is faster or since it takes up less memory or other resources. To some speed is the main criteria, as more things' can be accomplished within a certain time frame. Others favour more compact code, when space is a cost factor.

These days it's almost a 'Ford vs. Chev' comparison.

Jay
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Thu Apr 09, 2015 7:12 pm     Reply with quote

Jay,
I am 100% chevy guy. the reason is Chevrolet clutches offer better quality at lower prices lol
I just thought there is a quick and sweet answer Smile
assembler are way over my head at the moment :(
Regards,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 10, 2015 3:18 pm     Reply with quote

Quote:
PCM programmer,
What is your choice for similar task?
Which one is more efficient for my purpose and why?
They both worked and I did not notice the different.

Are you talking about the segments used for each digit ?
I use an array for that data:
Code:
// The following array tells the CCS lcd_symbol() function which
// segments to turn on, to display the numbers from 0 to 9.
//                            0    1    2    3    4    5    6    7    8    9
byte const Digit_Map[10] = {0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xE6};

Then I use the CCS function, lcd_symbol() to turn on the segments.

This is for use with an LCD 7-segment display, with a PIC that has
an lcd segment controller built-in.
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Fri Apr 10, 2015 4:58 pm     Reply with quote

No, I am using just a simple 7 segment LED with no built in controller! The one I am using is common cathode.

The way I am controlling the 7 segments is:

1- All the segments (A, B, C, D, E, F, G) are connected to the port B pin (0-6 via 330ohm resisters).
2- All the common connected to port C (D1, D2, G3, D4) are connected to pin (2-5) via 4k7 resisters and BC547 transistors.
3- I got it working good with both switch case statement 0-9 and with int array[10].
4- This is how I done it. In the main() and using while(true) loop. and in this order:

output_b(the number I want to display on the first digit)
output_c(the first digit to turn high for example C2 as byte)
delay_ms(1)
output_b(all output low)
output_c(one digit to turn low for example C2 as byte)
delay_ms(1)

and so on for the rest of the digit.

I did this part for each digit because I had ghosting problem and that how I fixed it:
output_b(all output low)
output_c(one digit to turn low for example C2 as byte)
delay_ms(1)

Thanks,
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Tue Apr 14, 2015 12:38 pm     Reply with quote

Guys,
I will go with the array it look nicer Smile as far as code!
One more question. When first worked on this, I decided to use 2N3904 transistors for the commons. I am very sorry it is OT, But I am sure one of you know the answer and it may help others. Using the same setup. From the PIC output to 4K7 or 4K2 even tried it down to 2K2 then to B on NPN trans. it works great with BC547 and it does NOT work with 2N3904? Both made by Fairchild and have identical parameters that effect my project?
It will work with the 2N3904 if I turn it ON all the Time (the seven segments) but not in multiplexing?
I did the math for the base current and I came up with 4K2. I did try that and I got the same results. it works with BC547 and it does NOT work with 2N3904?
Any idea?
Thanks,


Last edited by Sam_40 on Tue Apr 14, 2015 12:50 pm; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Tue Apr 14, 2015 12:50 pm     Reply with quote

You did twig that the pinout is TO92R, not TO92....

e, b, c across the flat, not c, b, e

I ran into this when I had to do a substitute for the BC, because it is getting hard to get now.
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Tue Apr 14, 2015 2:42 pm     Reply with quote

Ttelmah,
You are 100% right. Thank you very much. Reading so many datasheet and I made this mistake that me troubleshot it for a while. On my final PCB I want to use the MMBT3904 which is SOT-23. Since I like to try my project physically, I built it first on predrill PCB and using through hole parts, Just in case something need to be change.
This kind problem pay off as a learning curve and save from ordering 30+ PCB that have a defective silk screen, holes, ...etc.
Thanks again for your help,
Sam
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Wed Apr 15, 2015 12:34 am     Reply with quote

Glad it was as simple as that. It's the classic 'non obvious' error that stares you in the face. Smile
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