Hello, i'm new here,
using c language for a PIC16f877A and MPLABS IDE(any better free IDE's out there?)
i'm pretty new the c language!
i have written some code that simply controls 3 buttons that when pushed, a counter counts up or down by 1, when held down counter++ or -- until it reachs 0. My results are beening displayed on an LCD.
athough my code wrks perfectly i'm not sure i am going about this the right way.. take the (RB0 / minus) return to main function
here's where the simple line i dont know should go!
Code:
void rtn_main (void)
{
while (minus != 0)
{
if (plus == 0)
{
main(); /*<--------------is this right, it compiles and works fine*/
}
}
}
i am neck deep in a switch case statment when i call this so break and return do not seem to work.
any ideas????
code posted below... the lcdres.c not inclued
Code:
#include <pic.h>
#include <stdio.h>
#include "..\lcdres.c"
__CONFIG (0x3739);
#define plus RB1
#define minus RB2
const char wel[] = "----WELCOME-----";
const char numr[] = "Number = ";
const char full[] = "MaxLimit ";
const char pak[] = "--Press A Key---";
unsigned char readkeys = 0x00;
unsigned char keypressed = 0x00;
unsigned char num1 = 0x7f;
unsigned char check_keys (void);
void init (void);
void key_holdup (void);
void key_holddown (void);
void delay (int t);
void print_num (void);
void print_full (void);
void rtn_main (void);
void main (void)
{
init();
init_lcd();
cls();
printf("%s",wel);
nline();
printf("Enter Press 1&2");
while((RB0 == 1) || (RB1 == 1));
cls();
printf("!-Release Keys-!");
while((RB0 != 1), (RB1 != 1));
do
{
switch(check_keys())
{
case 0x00: {move_lcd(0x00); printf("%s",pak); print_num();break;}
case 0x01: {cls(); printf("Exit, Really??? "); nline(); printf("K2= yes / K0= no"); while(RB0 == 0); rtn_main(); cls(); break;}
case 0x02: {if (num1 != 255){(num1++; print_num(); key_holdup();}else{move_lcd(0x00); print_full(); print_num();} break;}
case 0x03: {if (num1 != 0){(num1--; print_num(); key_holddown();}else{move_lcd(0x00); print_full(); print_num();} break;}
default: {move_lcd(0x00);printf
("Careful...");print_num();break;}
}
}
while(1);
}
void rtn_main (void)
{
while (minus != 0)
{
if (plus == 0)
{
main();
}
}
}
void print_full (void)
{
printf("%s",full);
}
void print_num (void)
{
nline();
printf("%s%d ",numr, num1);
}
void delay (int t)
{
int n;
n=0x0000;
while(n++ < t);
}
void key_holdup (void)
{
unsigned int WT = 0x0000;
while (plus == 0)
{
WT++;
if (WT >26000)
{
if (num1!=255)
{
num1++;
print_num();
}
else
{
nline();
print_full();
}
}
}
}
void key_holddown (void)
{
unsigned int WT = 0x0000;
while (minus == 0)
{
WT++;
if (WT >26000)
{
if (num1!=0)
{
num1--;
print_num();
}
else
{
nline();
print_full();
}
}
}
}
void init (void)
{
TRISB = 0xFF;
OPTION = 0X00;
PORTB = 0x00;
}
unsigned char check_keys (void)
{
readkeys = (PORTB & 0x07); // & inclue only these pins
switch (readkeys)
{
case 0x07: {keypressed = 0x00; break;} //no keys pressed
case 0x06: {keypressed = 0x01; break;} //key 1 not in use
case 0x05: {keypressed = 0x02; break;} //key 2
case 0x03: {keypressed = 0x03; break;} //key 3
default: {keypressed = 0xFF; break;} // if portb doesnt match the hex value
}
return keypressed;
}