Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me refine my switch function code

Status
Not open for further replies.

prasadsrkrec

Junior Member level 2
Junior Member level 2
Joined
May 18, 2012
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,421
i am using switch function below


Code:
void sw_press(void)
{
	SFRPAGE = CONFIG_PAGE; 
	
	if(incr == 0)
	{
		MSDelay(5);
		if(incr == 0)
		{
			if(Mode_flag == 0)
				Mode_flag = 1;

			else if(Cal_flag == 0)
				Cal_flag = 1;		
			
			else 
			{
				if(SW_flag == 0)
				 {
					low++;
					if(low > 20.9)
							low = 0.0;
					convert_lcd(low*10, 3);
					lcdcmd(0xC0);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(incr == 0); 
				}
				else 
				{
					high ++;
					if(high > 20.9)
							high = 0.0;
					convert_lcd(high*10, 3);
					lcdcmd(0x80);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(incr == 0); 
				}
			}
			convert_flash(low*10, 3);
			for(t = 0 ; t < 3 ; t++)
				l_flash[t] = flash[t];
								
			convert_flash(high*10, 3);
			for(t = 0 ; t < 3 ; t++)
				h_flash[t] = flash[t];
							
			MEM_erase();
			MEM_write();
		}
	}
   
	if(decr == 0)
	{
		MSDelay(5);
		if(decr == 0)
		{
			if(Mode_flag == 0)
				Mode_flag = ~Mode_flag;

		 	else if(Cal_flag == 0)
				Cal_flag = ~Cal_flag;	  
			
			else 
			{
				if(SW_flag == 0)
				 {
					low+= 0.1;
					if(low > 20.9)
						low = 0.0;
					convert_lcd(low*10, 3);
					lcdcmd(0xC0);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(decr == 0);
				}
				else
				{
					high+= 0.1;
					if(high > 20.9)
						high = 0.0;
					convert_lcd(high*10, 3);
					lcdcmd(0x80);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(decr == 0);
				}
			}
			convert_flash(low*10, 3);
			for(t= 0 ; t < 3 ; t++)
				l_flash[t] = flash[t];

			convert_flash(high*10, 3);
			for(t= 0 ; t < 3 ; t++)
				h_flash[t] = flash[t];
				
			MEM_erase();
			MEM_write();
		}
	}
	
	if(entr == 0)
	{
		MSDelay(5);
		if(entr == 0)
		{	
			SW_flag = ~SW_flag;
			buzzer = 1;
			MSDelay(100);
			buzzer = 0;
			while(entr == 0);
		}
	}
}


If i press mode flag becomes 1 then it display first condition
if i press mode flag not one then it display other condition ...so how can i give different ranges for two different modes..
I want to change range 0 -20 for both low and high if mode flag 1
i want to change range 20 -40 for both low and high if mode flag not equal to one

incr (switch 1) incremented by 1
decr (switch 2) incremented by 0.1
give me sugggestions.
 

Re: Switch Case Doubt????

buddy ...
i could not understand what you want to know
so please explain your query
 

Re: Switch Case Doubt????

Hi Prasad ,

I think you need to first state your requirement a kind of state machine that you are looking for from the code then only people can guide you some things ..... In my view you should modify the code like this ....
Code:
void sw_press(void)
{
	SFRPAGE = CONFIG_PAGE; 
	
	if(incr == 0)
	{
		MSDelay(5);
		if(incr == 0)
		{
			if(Mode_flag == 0)
				Mode_flag = 1;

			else if(Cal_flag == 0)
				Cal_flag = 1;		
			
			else 
			{
				if(SW_flag == 0)
				 {
					low++;
					if((low < 20.0)&&(Mode_flag == 1))
                                          {
							low = 0.0;
					  }
                                        else if(( low>20)&&(low<40)&& (Mode_flag == 0))
                                        {
                                            // do required
                                        }
                                        convert_lcd(low*10, 3);
					lcdcmd(0xC0);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(incr == 0); 
				}
				else 
				{
					high ++;
					if((high < 20.0)&&(Mode_flag == 1))
                                         {
							high = 0.0;
					 }
                                         else if((high>20)&&(high<40)&&(Mode_flag == 0))
                                         {
                                           // do required things here
                                         }
                                          convert_lcd(high*10, 3);
					lcdcmd(0x80);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(incr == 0); 
				}
			}
			convert_flash(low*10, 3);
			for(t = 0 ; t < 3 ; t++)
				l_flash[t] = flash[t];
								
			convert_flash(high*10, 3);
			for(t = 0 ; t < 3 ; t++)
				h_flash[t] = flash[t];
							
			MEM_erase();
			MEM_write();
		}
	}
   
	if(decr == 0)
	{
		MSDelay(5);
		if(decr == 0)
		{
			if(Mode_flag == 0)
				Mode_flag = ~Mode_flag;

		 	else if(Cal_flag == 0)
				Cal_flag = ~Cal_flag;	  
			
			else 
			{
				if(SW_flag == 0)
				 {
					low+= 0.1;
					if(low > 20.9)
						low = 0.0;
					convert_lcd(low*10, 3);
					lcdcmd(0xC0);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(decr == 0);
				}
				else
				{
					high+= 0.1;
					if(high > 20.9)
						high = 0.0;
					convert_lcd(high*10, 3);
					lcdcmd(0x80);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(decr == 0);
				}
			}
			convert_flash(low*10, 3);
			for(t= 0 ; t < 3 ; t++)
				l_flash[t] = flash[t];

			convert_flash(high*10, 3);
			for(t= 0 ; t < 3 ; t++)
				h_flash[t] = flash[t];
				
			MEM_erase();
			MEM_write();
		}
	}
	
	if(entr == 0)
	{
		MSDelay(5);
		if(entr == 0)
		{	
			SW_flag = ~SW_flag;
			buzzer = 1;
			MSDelay(100);
			buzzer = 0;
			while(entr == 0);
		}
	}
}


hope this helps....

Good Luck
 

Re: Switch Case Doubt????

Thank You Kulakarni...I hope u can understand my task..If u cant understand again i wll tell clearly..

Actually On display first showing 2 modes for 2 options
if u press 1st display 1st
if u press 2nd display 2nd ..but i wrote common range for both modes..
But i want different ranges for 2 different modes.Give me some ideas...
 

Re: Switch Case Doubt????

I think you mean to say if you press '1' key( on the key board) to display mode 1 on the LCD display which have 0 to 20 range and press key '2' ( on the key board) to display 2 on the LCD display which has 20 to 40 range....Did i got you right ?...

with regards,

milind
 

Re: Switch Case Doubt????

yes......Exactly
 

Re: Switch Case Doubt????

that doubt was cleared kulakarni but i want to stor those values in flash memory

Code:
if(flag == 1)
{
if(condition)
}
else
{
}
like that i did
but finally i want to stor those values in flash memory we can take 4 bytes

intially two bytes i did .but problem with flag 

if i press 1 store 0 to 20 range values in flash memory 
if i press other one 20 to 40 range values store in flash memory give me idea?

- - - Updated - - -

Code:
if(incr == 0)
	{
		MSDelay(5);
		if(incr == 0)
		{
			if(Mode_flag == 0)
				Mode_flag = 1;		
			else 
			{
				if(SW_flag == 0)
				 {
					low++;

					if(low > 20.9)
							low = 0.0;
					convert_lcd(low*10, 3);
					lcdcmd(0xC0);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(incr == 0); 
				}
				else 
				{
					high ++;
					if(high > 20.9)
							high = 0.0;
					convert_lcd(high*10, 3);
					lcdcmd(0x80);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(incr == 0); 
				}
			}
			convert_flash(low*10, 3);
			for(t = 0 ; t < 3 ; t++)
				l_flash[t] = flash[t];
								
			convert_flash(high*10, 3);
			for(t = 0 ; t < 3 ; t++)
				h_flash[t] = flash[t];
							
			MEM_erase();
			MEM_write();

see this above code in that code previously i store ranges of common for both
now i want to store 4 bytes ...

l_flash low range
h-flash high range
but i want to more for diffrent ranges
 

Re: Switch Case Doubt????

It will be very simple just create the unsigned char variable.....then you can write the value to that character variable....

Good Luck
 

Re: Switch Case Doubt????

I tried but not coming.........give me some idea..
i put if(flag == 1)
for(t =0 ; t< 3 ; t++)
l_flash[t] = flash[t];
else
for(t =0 ; t < 3 ; t++)
p_flash[t] = flash[t];

like wise same in h_flash[] and q_flash[]

i took 4
l-flash[]
h_flash[]
p_flash[]
q_flash[]

but how to assign ?//


initially How can i assign for those to


Simply i am telling

1 / 0
1 == some flag and save values low and high
0 == other flag and save different values in low and high
I hope u can understand this
 

Re: Switch Case Doubt????

You need to have this variable to be Global....declare it out side the main() function ....and better to make this variable volatile so that compiler will not optimize it....

Good Luck
 

Re: Switch Case Doubt????

Code:
low = l[2]+l[1]+l[0];
low = p[2]+p[1]+p[0];

high = h[2]+h[1]+h[0];
high = q[2]+q[1]+q[0];

like this i am giving so i want to store low values and high values in 2 types so for mem_read operation i want either or operation..
How can i give either l[x] or p[x]
give me idea pls..
I was not that much of proficient in this
 

Re: Switch Case Doubt????

Code:
if(incr == 0)
	{
		MSDelay(5);
		if(incr == 0)
		{
			if(Mode_flag == 0)
				Mode_flag = 1;		
			else 
			{
				if(SW_flag == 0)
				 {
					low++;
                                        If(Mode_flag == 1) 
{					
if(low > 20.9)
							low = 0.0;
}
else
{
if(low > 40.0 || low < 20.0)
 low = 20.0
}
					convert_lcd(low*10, 3);
					lcdcmd(0xC0);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(incr == 0); 
				}
				else 
				{
					high ++;
					if(high > 20.9)
							high = 0.0;
					convert_lcd(high*10, 3);
					lcdcmd(0x80);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(incr == 0); 
				}
			}
			convert_flash(low*10, 3);
			for(t = 0 ; t < 3 ; t++)
				l_flash[t] = flash[t];
								
			convert_flash(high*10, 3);
			for(t = 0 ; t < 3 ; t++)
				h_flash[t] = flash[t];
							
			MEM_erase();
			MEM_write();
In the above code i want to save if low > 20 ...in l_flash and similarly if low > 40 store in another flash.
similarly high also..
give me suggestion

- - - Updated - - -

very urgent..
low > 20.0
store in l_flash
low < 20
store in P-flash
similarly
high > 20.0
store in h_flash
high < 20.0
store in q_flash
like that i want ..pls modify in that code or give me idea...

i hope u understand what i am saying


i gave L-flash h_flash p_flash q_flash








how can i give flash for p_flash and q_flash..\

And if u dont mind tell me which book is best for embedded C








high ++;
if(Mode_flag == 1)
{
if(high > 20.9)
high = 0.0;
}
else
{
if(high > 40.0 || high < 20.0)
high = 20.0
}
Code:
convert_flash(low*10, 3);
			for(t = 0 ; t < 3 ; t++)
				l_flash[t] = flash[t];
								
			convert_flash(high*10, 3);
			for(t = 0 ; t < 3 ; t++)
				h_flash[t] = flash[t];


similarly hiw can i stor for p_flash and q_flash..


variable is same low and high \

but memory values stor different for 2 variables
 

Re: Switch Case Doubt????

Code:
l = l[2] + l[1] +l[0];
h = h[2] + h[1] +h[0];

l= p[2] +p[1]+p[0];
h = q[2] +q[1] + q[0];

i want either or operation or both ....for above code
 

Re: Switch Case Doubt????

Kulakarni If u dont mind please help me .i hope u understood this problem.
Code:
void sw_press(void)
{
	SFRPAGE = CONFIG_PAGE; 
	
	if(incr == 0)
	{
		MSDelay(5);
		if(incr == 0)
		{
			if(Mode_flag == 0)
				Mode_flag = 1;

			else if(Cal_flag == 0)
				Cal_flag = 1;		
			
			else 
			{
				if(SW_flag == 0)
				 {
					low++;
					if((low < 20.0)&&(Mode_flag == 1))
                                          {
							low = 0.0;
					  }
                                        else if(( low>20)&&(low<40)&& (Mode_flag == 0))
                                        {
                                            // do required
                                        }
                                        convert_lcd(low*10, 3);
					lcdcmd(0xC0);
					lcddata(a_lcd[2]);
					lcddata(a_lcd[1]);
					lcddata('.');
					lcddata(a_lcd[0]);
					while(incr == 0); 
				}
				}
			}

                        if(Mode_flag == 1)
			convert_flash(low*10, 3);
			for(t = 0 ; t < 3 ; t++)
				l_flash[t] = flash[t];
                          else
                         convert_flash(low*10, 3);
			for(t = 0 ; t < 3 ; t++)
				p_flash[t] = flash[t];
                  
								
							
			MEM_erase();
			MEM_write();
		}
	}
i want to store low ranges in two different flashes ...
if i press 1 it storeone low value
otherwise stroe in other value
 
Last edited by a moderator:

Re: Switch Case Doubt????

I want to store two values in different locations of flash memory of microcontroller c8051f120.

I reffered application notes in si labs..but i did nt get ..i am somewhat confusing at addressing modes of flash memory..
kindly help me how to give those flash read , flash write , flash erase techniques for different locations of flash mmeory.

PLs..
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top