Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

help on menu call function and it can't increment while a butten pressed

Status
Not open for further replies.
hi dear some one help me how to increment the count while inside the menu its very urgent any one plz
the count does not increment
complier ccs c
pic16f877a

this is my code

#include<lcd.c>
#bit m=0x05.2
#bit n=0x05.3
#bit o=0x05.4
#byte trisa=0x85
int i,j;
void counttime();

void counttime()
{
if(n==0)
{
j++;
if(j>59)
j=0;
printf(lcd_putc," %02d ",j);
delau_ms(10);
}
}
void main()
{
Trisa=0x3c;
m=n=o=0;

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
//setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1););
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

// TODO: USER CODE!!
lcd_init();
Lcd_gotoxy(1,1);
printf(lcd_putc," welcome ");
delay_ms(100);
while(1)
{
if(m==0)
{
delay_ms(100);
i++;
if(i==1)
{
Lcd_gotoxy(1,1);
printf(lcd_putc,"\f menu 1 ");
delay_ms(100);
counttime():
}
if(i==2)
{

Lcd_gotoxy(1,1);
printf(lcd_putc,"\f menu2");
delay_ms(100);
}


if(i>3)
i=0;
}
}
 
Last edited:
Here I am finding 2-3 things,

1. n should be declared global or in the counttime function as n is used in counttime function and not in main, and what is the use of the variable o.
2. It is continues loop so you will not be able to see the menu 1 and 2 stable on the LCD as the value of the i is continuously changing in while loop.
3. and what is the use of the menu, can you just elloborate on that...
 
thanks for reply
this is my application code i want to set time from keypad so i chose 4 keys one is menu and other 3 are hrs min sec ...here menu function working ..i need inside the menu set the value..
any help

#include<lcd.c>
#include "button.c"
#include "ds1307.c"

//#byte Trisa=0x85
void display();
Void settime();
int1 flag,toggle;
int i,k;
int8 A2=0;
int8 A3=0;
int8 A4=0;
int8 A5=0;
//int8 A2_pressed = FALSE;
//int8 A3_pressed = FALSE;

//char i,SUN,MON,TUE,WED,THU,FRI,SAT;
byte sec;
byte min;
byte hrs;
byte day;
byte month;
byte year;
byte dow;



void display()
{
//delay_ms(100);
ds1307_get_date(day,month,year,dow);
ds1307_get_time(hrs,min,sec);

lcd_gotoxy(1,1);
printf(lcd_putc,"Time: %02d:%02d:%02d",hrs,min,sec);

lcd_gotoxy(1,2);
printf(lcd_putc,"Date:%02d:%02d:%02d-%2d",day,month,year,dow);

}

void settime()
{
if(button(PIN_A3, 0, 50, 10, A3, 1))
{
delay_ms(100);
k++;
if(k>23)
k=0;
lcd_gotoxy(1,2);
printf(lcd_putc,"\:%02d ",k);
k=hrs;
ds1307_set_date_time(day,month,year,dow,hrs,min,sec);
delay_ms(100);
}
}
void main()
{
int i,j,k;
lcd_init();
ds1307_init();
set_tris_a(0x3c);
//set_rtcc(RTCC_PRELOAD);

//enable_interrupts(INT_RTCC);
//enable_interrupts(GLOBAL);

//ds1307_set_date_time(26,7,12,5,12,04,00);//day, month,year,dow,hr,min.sec

delay_ms(100);

i=0;
while(1)
{
if(button(PIN_A2, 0, 50, 10, A2, 1))
{
delay_ms(100);
i++;
if(i==1)
{
lcd_gotoxy(1,1);
printf(lcd_putc,"\fSet Time: ");
delay_ms(100);
settime();
}
if(i==2)
{
lcd_gotoxy(1,2);
printf(lcd_putc,"\fset date: ");
delay_ms(100);
}
if(i==3)
{
lcd_gotoxy(1,2);
printf(lcd_putc,"\fSet relay turn on delay: ");
delay_ms(100);
}
if(i==4)
While(1)
{
display();
if(button(PIN_A2,0,50,10,A2,1))
{
break;
}
}
if(i>4)
i=0;
}
if(i==0)
{
display();
}
}
}
 
Last edited:
this code is button.c
// Button.c
//====================================
// Differences between this function and the PicBasicPro
// compiler function:

// 1. The PBP button function requires a "GOTO" label for
// the last parameter. I didn't implement this feature
// because while in Basic it's considered OK to use
// GOTO statements, we don't normally use them in C.
// Instead, I have the button() function return the
// value of the "Action" parameter. (1 or 0).
// So if Action parameter is 1, and the button is
// pressed, the return value is 1. If the button
// isn't pressed the function will return 0.

// 2. The PBP button function has an internal debounce
// delay in the auto-repeat code which can optionally
// be disabled. This internal delay isn't necessary
// so I didn't include it in my version of the button
// function. The external delay explained in item 3
// below, takes care of all debouncing.

// 3. A delay_ms(10) statement is required in the loop
// in your main program that calls the button()
// function. Actually, PBP also requires this.
// All their examples show a PAUSE 10 or PAUSE 100
// statement in a loop. But I'm explicitly stating
// that it is required.

// 4. The PBP function allows the Pin parameter to be
// a number from 0-15 or a port.bit value. I didn't do
// it that way. With the CCS compiler, we like to use
// CCS pin numbers, which are defined in the .H file
// for your PIC. So for the pin parameter you must
// use values such as PIN_B0, PIN_C1, PIN_E0, etc.
// The pin parameter can be passed in a variable.
// It doesn't have to be passed to the function as a
// constant.

// 5. The PBP function sets the TRIS for the specified pin
// so it's an input. Currently I don't do that, so you
// have to set it with the set_tris_x() function. If I
// can decide on a clean way to do it, I may add that
// feature later.

//====================================
// Description of function parameters:

// Pin:
// This must be a CCS-style pin number, such as PIN_B0.

// Downstate:
// This is the logic level of the button when it's pressed.
// For a circuit with a Normally-Open switch and a pull-up
// resistor, this parameter will be 0.

// Delay:
// This is the initial delay before auto-repeat begins.
// Example: A Delay value of 50 means a 500 ms auto-repeat
// delay.

// Rate:
// This is the auto-repeat interval.
// Example: A Rate value of 10 gives a 100 ms interval,
// which means an auto-repeat rate of 10 keys per second.

// Bvar:
// This an 8-bit variable that must be declared in the main
// program, and it must be initialized to zero. There must
// be a separate variable for each button that you use.
// Example: Declare the variable for pin B0 as int8 B0 = 0;
// See the demo program for more examples. Note that Bvar
// is a "reference variable" (it has a "&" in front of it).
// This is intentional. It's done so the button function
// can directly access the Bvar variable which is declared
// outside the function, without having to pass a pointer
// to it. This keeps the function interface simple.
//====================================

// Action:
// This is the value that's returned when the button is
// pressed. Normally you set this = 1.

// Return value:
// When the button is pressed, the value of the Action
// parameter will be returned. When the button is not
// pressed, the opposite value will be returned.
// Normally, you set the Action parameter = 1, and so
// if the button is pressed, the function will return 1.
// If it's not pressed, it will return 0. So you just
// poll the function every 10 ms in a loop, and check the
// return value with an if() statement. If it's non-zero
// then the button is pressed and you can take appropriate
// action.


//=====================================
// The following macro is used by the Button function.
#define read_bit_var(x) bit_test(*(int8 *)(x >> 3), x & 7)


//=====================================
int8 button(int16 pin, int8 downstate, int8 delay,
int8 rate, int8 &BVar, int8 action)
{
int8 pin_value;

// Read the button pin.
pin_value = read_bit_var(pin);

// Check if the button is pressed. It's pressed if the
// pin value is the same as the "downstate". If it's not
// pressed, then zero the Bvar and return "Not pressed".
if(pin_value != downstate)
{
Bvar = 0;
return(!action);
}

// The button is pressed. Check to see if it's a new
// keypress. We can tell if it's a new keypress by
// checking if BVar = 0. If so, load the counter with
// the initial auto-repeat delay and return "Pressed".
// (If the delay has been set to 0, then load a non-zero
// value to allow the function to operate properly).
if(Bvar == 0)
{
if(delay == 0)
Bvar = 255;
else
Bvar = delay;

return(action);
}

// Decrement the auto-repeat counter.
Bvar--;

// Check if we just counted down to 0. If so, then load
// the counter with the auto-repeat interval and return
// "Pressed". If the delay is set to 0 or 255, it means
// that auto-repeat is disabled, so fall through and
// return "Not Pressed".
if(BVar == 0)
{
BVar = rate;

if((delay != 0) && (delay != 255))
return(action);
}

// If the counter is positive, then it means an auto-repeat
// is still pending, so return "Not Pressed".
return(!action);

}
 
Hi,

Here when you are moving to the function settime(), i think "if" statement is causing the problem.
Actually as per my understanding you have to stay there in that function unless and untill the value has been set.

So in your case i think what is happening is that when it is reaching to settime() function,it is checking for that button is pressed or not which in our case will not be...
wait in that function settime() unless get the button pressed... Maybe using the "while" will do it...
 
thaks again mr Dj

show me a code for just hours(hrs) to set

input keys are pulled up and oter end gnd ,while pressed gnd signal pass to uC
 
again it does not increment k where is the problem
void settime()
{
while(1)
{
if( !input(PIN_A3) )

{
delay_ms(100);
k++;



lcd_gotoxy(1,2);
printf(lcd_putc,"\:%02d ",k);
delay_ms(100);
if(k>23)
{
k=0;
}
k=hrs;
ds1307_set_date_time(day,month,year,dow,hrs,min,sec);
}
if(button(PIN_A2, 0, 50, 10, A2, 1))
{
break;
}
}
}
 
Last edited:
void settime()
{
while(1)
{
if(button(PIN_A3, 0, 50, 10, A3, 1))
{
delay_ms(100);
k++;
if(k>23)
k=0;
lcd_gotoxy(1,2);
printf(lcd_putc,"\:%02d ",k);
k=hrs;
ds1307_set_date_time(day,month,year,dow,hrs,min,se c);
delay_ms(100);
break;
}
}
}

Try this way...
 
Thanks dj now i can edit the time

i need to set relay turn on time to eeprom how..?
and compare it ..if equal set output pin

also want to set delay for relay on to off how can you help me....?
 
how you are planning to interface the EEPROM i mean which protocol need to be used is it i2c,spi, etc...
And one more question do we need to update the reference time which has been provided in the EEPROM or it is just one time job...
 
i am using pic16f877a so am using internal eeprom

eeprom tested ok working fine ...how to compare min,hr to user min,hr

now i have doubt ,relay on delay edit by user ..how to generate delay examble 1hr,2hr like that

also if power fails i need to run delay complete cycle examble 1hr ..if power fails at 20 min ,again power comes it should start from 20 ...is there any possibility
 
Dont worry about the power failure because RTC will work on the backup battery provided and eeprom value will also remain intact...
So on the power on the RTC will have the latest current value only....

But I am still not clear regarding the how will you enter the value of delay....
 
i am enter the delay constant 1,2,3,4,5,6 hour no more hour ...if i enter 1hr .. how to run the time 60min to turn on the relay also power fails at 30 min ..if power comes it will start from 31 min(not rtc) the rtc trigger the relay once min ,hr matches ..after that relay turn on runs the time by user.
 
Do one thing is that whenever the particular delay hrs is enter for e.g. if 1 hour and current RTC time 1 o'clock so now your relay should be trigerred at 2 o'clock...
So what you do is when delay time has been capture the time of the RTC and add the 1 hour or whatever hours has been entered... Store that value in EEPROM then compare the value of RTC and value store in EEPROM once it compares that value then make your relay on...
 
I am not getting your question...

But as per my understanding the comparision that you have to do is the read from RTC value and read from EEPROM value...
 
ok i found the comparison .....if((min==RMN)||(Hrs==Rhr))

now relay turned on ..next step is now power fails and returned so the relay wont turn on again..wat can i do...?

also how to go menu by long press a key, is there any command...?
 
Last edited:
Very Simple you store the status of the relay also in the EEPROM so on power failure when it restore back you just read the status of the relay in your intialization of the code and then whatever the status is act accordingly...
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top