stuckkk
Newbie level 6
- Joined
- Dec 18, 2012
- Messages
- 13
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,383
input=PORTA; //read PORTA again
switch(input)
{
case 0x01:
function ();
function_1 ();
PORTB= PORTB | 0x01;
break;
case 0x02:
function ();
function_1 ();
PORTB= PORTB | 0x02;
break;
case 0x04:
function ();
function_1 ();
PORTB= PORTB | 0x04;
break;
case 0x08:
function ();
function_1 ();
PORTB= PORTB | 0x08;
break;
case 0x10:
function ();
function_1 ();
PORTB= PORTB | 0x10;
break;
void function_1 ()
{
while(1)
{
delay(1000);
PORTB = PORTB | 0x40;
delay(1000);
PORTB = PORTB & 0xBF;
}
}
}
unsigned char flag1 = 0;
void function_1()
{
static unsigned int counter = 0;
if((flag1 == 1) && (counter++ == 1000)) // execute every 1000 calls and when flag is 1
{
PORTB ^= 0x40; // invert pin state
counter = 0; // clear counter
}
else
{
delay(1); // 1ms delay
}
}
void main(void)
{
unsigned char input=0;
while(1)
{
input = PORTA; //read PORTA again
switch(input)
{
case 0x01:
function();
flag1=1; //function_1();
PORTB = PORTB | 0x01;
break;
case 0x02:
function();
flag1=1; //function_1();
PORTB = PORTB | 0x02;
break;
case 0x04:
function();
flag1=1; //function_1();
PORTB = PORTB | 0x04;
break;
case 0x08:
function();
flag1=1; //function_1();
PORTB = PORTB | 0x08;
break;
case 0x10:
function();
flag1=1; //function_1();
PORTB = PORTB | 0x10;
break;
}
function_1();
}
}
void function_1()
{
if(flag == 1)
{
PORTB = PORTB | 0x40;
__delay_ms (20);
PORTB = PORTB & 0xBF;
__delay_ms (20);
}
}
I have one more q, when I select one case and it loops back to the switch case, I have to deselect the selected case before selecting the next case. I have tried using 'input =0'. When I run the PIC simulator and deselect the selected case and select another case the program works fine. Is there a way I can erase the last selected case from input.
I think function_1 will be executed infintely if I use alexan_e's code as it is called after the switch case.
I want to clear input variable because if one case is selected and I want to go to another case, I need to deselect the previously selected case for the second case to execute. Now the inputs act as pushbutton switches.
If I had selected case0x04 and I want to go to case0x02, I have to change case0x02 to 0x06. Is there a way I can use this same cases and the program be able to go to another case even if one case is already active
input = PORTA; //read PORTA again
if(input & 0x01)
{
function();
function_1();
PORTB = PORTB | 0x01;
}
if(input & 0x02)
{
function();
function_1();
PORTB = PORTB | 0x02;
}
if(input & 0x04)
{
function();
function_1();
PORTB = PORTB | 0x04;
}
if(input & 0x08)
{
function();
function_1();
PORTB = PORTB | 0x08;
}
if(input & 0x10)
{
function();
function_1();
PORTB = PORTB | 0x10;
}
unsigned char flag1 = 0;
void function_1()
{
static unsigned int counter = 0;
if((flag1 == 1) && (counter++ == 1000)) // execute every 1000 calls and when flag is 1
{
PORTB ^= 0x40; // invert pin state
counter = 0; // clear counter
}
else
{
delay(1); // 1ms delay
}
}
void main(void)
{
unsigned char input = 0;
while(1)
{
input = PORTA; //read PORTA again
if(input & 0x01)
{
function();
flag1 = 1; //function_1();
PORTB = PORTB | 0x01;
}
if(input & 0x02)
{
function();
flag1 = 1; //function_1();
PORTB = PORTB | 0x02;
}
if(input & 0x04)
{
function();
fflag1 = 1; //function_1();
PORTB = PORTB | 0x04;
}
if(input & 0x08)
{
function();
flag1 = 1; //function_1();
PORTB = PORTB | 0x08;
}
if(input & 0x10)
{
function();
flag1 = 1; //function_1();
PORTB = PORTB | 0x10;
}
function_1();
}
}
I have a if-else with five options and if any of the options is selected it lights one LED and make another LED blink infinitely
unsigned char flag1 = 0;
void function_1()
{
static unsigned int counter = 0;
if((flag1 == 1) && (counter++ == 1000)) // execute every 1000 calls and when flag is 1
{
PORTB ^= 0x40; // invert pin state
counter = 0; // clear counter
}
else
{
delay(1); // 1ms delay
}
}
void main(void)
{
unsigned char input = 0;
while(1)
{
input = PORTA; //read PORTA again
if(input & 0x01)
{
function();
flag1 = 1; //function_1();
PORTB = PORTB | 0x01;
}
if(input & 0x02)
{
function();
flag1 = 1; //function_1();
PORTB = PORTB | 0x02;
}
if(input & 0x04)
{
function();
fflag1 = 1; //function_1();
PORTB = PORTB | 0x04;
}
if(input & 0x08)
{
function();
flag1 = 1; //function_1();
PORTB = PORTB | 0x08;
}
if(input & 0x10)
{
function();
flag1 = 1; //function_1();
PORTB = PORTB | 0x10;
}
function_1();
[COLOR="#FF0000"]flag1 = 0;[/COLOR] // clear flag after each scan.
}
}
Are you saying that if toggle switch 1 is ON and case 1 is running then if you toggle ON switch 2 case 1 should stop and case 2 should run?
void function_1()
{
static unsigned int counter = 0;
if(flag1 == 1)
{
if(counter++ == 1000) // execute every 1000 calls and when flag is 1
{
PORTB ^= 0x40; // invert pin state
counter = 0; // clear counter
}
else
{
delay(1); // 1ms delay
}
}
else
{
counter = 0; // clear caounter
PORTB = PORTB & 0xBF; // turn off led
}
}
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?