Simple (not really accurate but working) mikroC solutions:...may I know how to set the delay time as a constant like make it as an integer?
i tried to make it like this
delay_ms (i);
but it end up become error.
void DelayV_ms(unsigned ms) {
if (ms) {
do {
Delay_us(988); // experimental value
} while (--ms);
}
}
// OR (and if 255 ms is also enough as max delay):
void DelayV_ms(char ms) {
while (ms) {
Delay_us(988);
--ms;
};
}
// OR use the VDelay_ms(unsigned ms) library function (much larger code !)
#define _XTAL_FREQ 18432000 // 18.432 MHz Osc.
__delay_ms(100);
Hi,
What error?
Klaus
...and which compiler?
Please show your code.
Brian.
#define _XTAL_FREQ 4000000
signed int i= 0;
void main() {
i = 500;
TRISA = 0x00100000;
TRISB = 0X00;
CMCON = 0x07; // To turn off comparators
PORTB = 0x01;
do // To set infinite loop
{
Delay_ms(300); // 300 mili seconds delay
PORTB = PORTB<<1; //Shifting right by one bit
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
Delay_ms(i); // 350 mili seconds delay
PORTB = 1;
}
}while(1); // To set infinite loop}while(1); // To set infinite loop
}
Simple (not really accurate but working) mikroC solutions:
Code:void DelayV_ms(unsigned ms) { if (ms) { do { Delay_us(988); // experimental value } while (--ms); } } // OR (and if 255 ms is also enough as max delay): void DelayV_ms(char ms) { while (ms) { Delay_us(988); --ms; }; } // OR use the VDelay_ms(unsigned ms) library function (much larger code !)
If using HiTech-C, the delay macros have 2 under-scores in front of the function. You'll also need to have the clock speed defined (_XTAL_FREQ) so the macro can calculate the delay. Now that I look at your request, this probably won't work using a variable (i in your case).
Code:#define _XTAL_FREQ 18432000 // 18.432 MHz Osc. __delay_ms(100);
#define _XTAL_FREQ 4000000
unsigned int i= 0; // 0 to 65535
void Variable_Delay_mS(unsigned int T)
{
unsigned int t1;
t1=T;
While(t1>0)
{
Delay_ms(1);
t1--;
}
}
void main() {
i = 512; // starting value
TRISA = 0x00100000;
TRISB = 0X00;
CMCON = 0x07; // To turn off comparators
PORTB = 0x01;
do // To set infinite loop
{
PORTB = PORTB<<1; //Shifting[B] LEFT[/B] ! by one bit
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
PORTB = 1;
i=512;
}
Variable_Delay_ms(i); // 500 mili seconds delay
i=i>>1; // divide by 2 => log step
// or i=i-32 ; // => linear step
}while(1); // To set infinite loop}while(1); // To set infinite loop
}
hello,
you can use VDelay from MikroC or build you own ...
delay between each step can be linear or logaritmic
Code:#define _XTAL_FREQ 4000000 unsigned int i= 0; // 0 to 65535 void Variable_Delay_mS(unsigned int T) { unsigned int t1; t1=T; While(t1>0) { Delay_ms(1); t1--; } } void main() { i = 512; // starting value TRISA = 0x00100000; TRISB = 0X00; CMCON = 0x07; // To turn off comparators PORTB = 0x01; do // To set infinite loop { PORTB = PORTB<<1; //Shifting[B] LEFT[/B] ! by one bit if(PORTB >= 0b10000000) //To reset to 00000001 { //when the count becomes 10000000 PORTB = 1; i=512; } Variable_Delay_ms(i); // 500 mili seconds delay i=i>>1; // divide by 2 => log step // or i=i-32 ; // => linear step }while(1); // To set infinite loop}while(1); // To set infinite loop }
#define _XTAL_FREQ 4000000
void main() {
unsigned int i = 0;
int result;
TRISA = 0x00100000;
TRISB = 0X00;
CMCON = 0x07; // To turn off comparators
PORTA = 0x00;
PORTB = 0x01;
do // To set infinite loop
{
start:
// fast running
for(i = 0; i<20; i++){
delay_ms(100);
PORTB = PORTB<<1; //Shifting right by one bit
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
PORTB = 1;
}
}
// medium speed running
for(i=20; i<30; i++){
delay_ms(200);
PORTB = PORTB<<1;
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
PORTB = 1;
}
}
// slow running
for(i=30; i<35; i++){
delay_ms(300);
PORTB = PORTB<<1;
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
PORTB = 1;
}
}
// slowest speed
for(i=45; i<48; i++){
delay_ms(450);
PORTB = PORTB<<1;
PORTB = 0x00;
goto RANDOMLIGHT;
}
RANDOMLIGHT:{
result = rand(); // generate random value from 0 to 32767
result = result/1000;
switch (result){
case 0:PORTB = 0x01;
delay_ms(3000);
i== 1;
goto start; // restart
case 1:PORTB = 0x02;
delay_ms(3000);
i== 1;
goto start; // restart
case 2:PORTB = 0x04;
delay_ms(3000);
i== 1;
goto start; // restart
case 3:PORTB = 0x08;
delay_ms(3000);
i== 1;
goto start; // restart
case 4:PORTB = 0x10;
delay_ms(3000);
i== 1;
goto start; // restart
case 5:PORTB = 0x20;
delay_ms(3000);
i== 1;
goto start; // restart
case 6:PORTB = 0x40;
delay_ms(3000);
i== 1;
goto start; // restart
case 7:PORTB = 0x80;
delay_ms(3000);
i== 1;
goto start; // restart
case 8:PORTB = 0x08;
delay_ms(3000);
i== 1;
goto start; // restart
case 9:PORTB = 0x04;
delay_ms(3000);
i== 1;
goto start; // restart
}
}
}while(1); // To set infinite loop}while(1); // To set infinite loop
}
PORTB = PORTB<<1
Why in the digital roulette wheel spinning is required ? Why not just display the final random led position instantly ?
I think the original problem and error is due to the fact that the delay 'function' the OP used may not actually be a "function" but a "macro" which didn't like the use of a variable. I've seen this sort of thing before where the preprocessor expects to be able to calculate the value to be used for the delay to a single number and cannot handle using a variable in that calculation.
Just a guess...
Susan
Why do the "drums" on slot machines spin when the outcome of the pull is predetermined? Aesthetics. Suspense.
I have to admit that I'm not totally aware of the implications of the following construct, whether if falls on the "read-modify-write" category for an access to the register of an I/O or not, but I would be carefull on using this :
Code:PORTB = PORTB<<1
Consider using variables, just updating the result to the port after calculating.
Why in the digital roulette wheel spinning is required ? Why not just display the final random led position instantly ?
Ugh!
Please remove every instance of 'goto' and add 'break' to the end of each 'case' comparison. Also remove the 'goto RANDOMLIGHT' and replace it with 'RANDOMLIGHT()' and turn ther RANDOMLIGHT code into a function.
Why not use a simple array to hold the PORTB values and remove the switch() altogether?
What are all the lines 'i== 1;' supposed to do?
To be honest, it needs restructuring and rewriting, it can be done in half the lines and be much easier to follow.
Brian.
#define _XTAL_FREQ 4000000
unsigned int i = 0;
void RANDOM_LIGHT() {
int result;
result = rand(); // generate random value from 0 to 32767
result = result/1000;
switch (result){
case 0:
PORTB = 0x01;
delay_ms(3000);
i== 1; // restart
// case 1
case 1:
PORTB = 0x02;
delay_ms(3000);
i== 1;
// restart
break;
// case 2
case 2:
PORTB = 0x04;
delay_ms(3000);
i== 1;
// restart
break;
// case 3
case 3:
PORTB = 0x08;
delay_ms(3000);
i== 1;
// restart
break;
// case 4
case 4:
PORTB = 0x10;
delay_ms(3000);
i== 1;
// restart
break;
// case 5
case 5:
PORTB = 0x20;
delay_ms(3000);
i== 1;
// restart
break;
//case 6
case 6:
PORTB = 0x40;
delay_ms(3000);
i== 1;
// restart
break;
// case 7
case 7:
PORTB = 0x80;
delay_ms(3000);
i== 1;
break;
// case 8
case 8:
PORTB = 0x08;
delay_ms(3000);
i== 1;
break;
// case 9
case 9:
PORTB = 0x04;
delay_ms(3000);
i== 1;
break;
}
}
void main() {
TRISA = 0x00100000;
TRISB = 0X00;
CMCON = 0x07; // To turn off comparators
PORTA = 0x00;
PORTB = 0x01;
do // To set infinite loop
{
start:
// fast running
for(i = 0; i<20; i++){
delay_ms(100);
PORTB = PORTB<<1; //Shifting right by one bit
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
PORTB = 1;
}
}
// medium speed running
for(i=20; i<30; i++){
delay_ms(200);
PORTB = PORTB<<1;
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
PORTB = 1;
}
}
// slow running
for(i=30; i<35; i++){
delay_ms(300);
PORTB = PORTB<<1;
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
PORTB = 1;
}
}
// slowest speed
for(i=45; i<48; i++){
delay_ms(450);
PORTB = PORTB<<1;
PORTB = 0x00;
RANDOM_LIGHT();
}
}while(1); // To set infinite loop}while(1); // To set infinite loop
}
my function still cant perform like what i want. Does anyone know which part is having problem?
This is a fairly common mistake - confusing the assignment operator ('=') with the equality boolean comparison operator ('==') although it is usually confused the other way around (making an assignment in an 'if' statement rather than a useless comparison when an assignment is intended).i == 1 function is to reset the "i" so that when a full cycle is finish, the LED will run like a roulette wheel again.
(i am using value "i" to set the LED run in different speed as you can see in void main function)
i = 1;
The part 'having [a] problem' is that you have not described the problem! What is it doing that you do not want it to do? What is is NOT doing that you intend.but my function still cant perform like what i want. Does anyone know which part is having problem?
"i == 1" does not set the value to 1.
change it to "i = 1" !
Do you realize you can remove all those "delay_ms(3000)" lines and replace then with a single one at the end of the switch() section.
Brian.
#define _XTAL_FREQ 4000000
unsigned int i= 0; // 0 to 65535
void Variable_Delay_mS(unsigned int T)
{
unsigned int t1;
t1=T;
While(t1>0)
{
Delay_ms(1);
t1--;
}
}
void main() {
i = 512; // starting value
TRISA =0x00100000; //THIS LINE!!
TRISB = 0X00;
CMCON = 0x07; // To turn off comparators
PORTB = 0x01;
do // To set infinite loop
{
PORTB = PORTB<<1; //Shifting LEFT ! by one bit
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
PORTB = 1;
i=512;
}
Variable_Delay_ms(i); // 500 mili seconds delay
i=i>>1; // divide by 2 => log step
// or i=i-32 ; // => linear step
}while(1); // To set infinite loop}while(1); // To set infinite loop
}
2. there is a very long delay after the LED running stop and the LED lights up. is it because of the random instruction require a lot of scan time?
result = rand(); // generate random value from 0 to 32767
result = result/1000;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?