![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
ahsansag93
Joined: 09 Dec 2012 Posts: 3
|
help in digital dice, same sequence |
Posted: Mon Dec 10, 2012 8:43 am |
|
|
code:
Code: | #include <16F877a.h>
#Fuses HS,NOWDT,NOLVP,Protect
#include <stdlib.h>
#include <stdlibm.h>
#include <stdio.h>
#use delay (clock=20M)
#byte out=6
void main()
{
int a;
int seg[6]={0b0000110,0b11011011,0b01001111,0b01100110,0b01101101,0b01111101};
srand(1);
set_tris_b(0);
while(1)
{
a=rand()% 6 + 1;
{
switch(a)
{
case 1:out=seg[a];
delay_ms(100);
case 2:out=seg[a];
delay_ms(100);
case 3:out=seg[a];
delay_ms(100);
case 4:out=seg[a];
delay_ms(100);
case 5:out=seg[a];
delay_ms(100);
case 6:out=seg[a];
delay_ms(100);
}}
}}
|
plz help me, my code is having two problems.
1) i am getting the same sequence of random number in seven segment display, means 4,5,4,6,3 etc every time...how can i have a new sequence every time i start my program?
I am an engineering student. I have just started learning pic micro controller and c language, so plz make me understand in easy term. |
|
![](templates/subSilver/images/spacer.gif) |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Mon Dec 10, 2012 8:50 am |
|
|
Hmm... You are the second person to post with this problem today Same PIC, same problem. Is this a school assignment?
Look at the thread your collegue posted.
RF Developer |
|
![](templates/subSilver/images/spacer.gif) |
ahsansag93
Joined: 09 Dec 2012 Posts: 3
|
|
Posted: Mon Dec 10, 2012 8:55 am |
|
|
RF_Developer wrote: | Hmm... You are the second person to post with this problem today Same PIC, same problem. Is this a school assignment?
Look at the thread your collegue posted.
RF Developer |
yup brother i tried :( but as much i understood from that post , i made...
i have been trying to made it for 5 days :( but still not successful .i don't know what to do know now.
how to generate new sequence every time
she is my colleague.
its our assignment. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Dec 10, 2012 9:31 am |
|
|
First error is that your switch-case statements are missing the 'break' command. What now happens is that all commands 'fall through' until the end of the switch-statement and end with the last statement, 'case 6'. Look into your C study book for the correct implementation.
second error: Code: | a=rand()% 6 + 1;
{
switch(a)
{
case 1:out=seg[a]; | You nicely add 1 to 'a' so it counts from 1 to 6, but then the offset into an array always starts counting from 0. So here you should subtract 1 again.
Once you have that fixed you should be getting semi random values. A problem here is that you only have 100ms delay, for the human eye that will bee too fast and you only see rapid blinking LEDs.
Then, every time you start your dice you will get the same sequence of values. That is because computers use a 'pseudo random' generator. To make the sequence different every time you will have to give srand() a real random number just once, for how to do this a few hints were given in the other thread.
Hint: your program can be made a lot shorter when you look at how all the lines in the switch statement are the same. In fact, you can remove the whole switch statement and replace by 2 lines. |
|
![](templates/subSilver/images/spacer.gif) |
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|