aljamri
Member
Hi'
I've tried my luck to do an electronics Dice and got it, but faced a problem could not overcome. The generated Random numbers had the same sequence every time I restart the circuit ( 4,4,2,6,3,1,1,4,3,6,3 .... ). Read somewhere that using srand() makes some difference, but when used it, and error message appeared. It is clear that I don't know how to use it. Any body does?
Following is the code:
I've tried my luck to do an electronics Dice and got it, but faced a problem could not overcome. The generated Random numbers had the same sequence every time I restart the circuit ( 4,4,2,6,3,1,1,4,3,6,3 .... ). Read somewhere that using srand() makes some difference, but when used it, and error message appeared. It is clear that I don't know how to use it. Any body does?
Following is the code:
Code:
unsigned short (a);
int rand(void);
delay1;
delay2;
int main(){
delay1= 1000;
delay2= 200;
TRISA = 0b11110000; // RA5 as input (SWT)
TRISB = 0b00000000; // PORTB Output
// ********* Start of LED Test ********
Test:
PORTB = 0b00000001; Vdelay_ms(delay2);
PORTB = 0b00000010; Vdelay_ms(delay2);
PORTB = 0b00000100; Vdelay_ms(delay2);
PORTB = 0b00001000; Vdelay_ms(delay2);
PORTB = 0b00010000; Vdelay_ms(delay2);
PORTB = 0b00100000; Vdelay_ms(delay2);
PORTB = 0b01000000; Vdelay_ms(delay2);
PORTB = 0b10000000; Vdelay_ms(delay2);
PORTB = 0b00000000; Vdelay_ms(delay2);
// ********* End of LED Test ********
Loop:
switch (PORTA.F4) {
case 0 : PORTA.F0=1; // SW1 Indicator ON
a = rand();
while (a > 6) {
a = a-6;
};switch (a) {
case (1) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b00001000 ;
Vdelay_ms(delay2); break;
case (2) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b00010100 ;
Vdelay_ms(delay2); break;
case (3) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b01001001 ;
Vdelay_ms(delay2); break;
case (4) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b01100011 ;
Vdelay_ms(delay2); break;
case (5) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b01101011 ;
Vdelay_ms(delay2); break;
case (6) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b01110111 ;
Vdelay_ms(delay2); break;
}; break;
case 1 : PORTA.F0=0; // SW1 Indicator ON
Delay_ms(200);
}goto Loop;
}