Markdem
Joined: 24 Jun 2005 Posts: 206
|
what do you think about my code?? |
Posted: Thu Mar 23, 2006 4:35 am |
|
|
Hi All, the following code works OK, but i just want your input in case i am doing somthing the wrong way. It is a menu system where a arrow moves down the page using a switch. If i press another switch, the state will change between off, on and auto. The state will be displayed on the screen. Is there anything that i could do in a better way??
Code: |
#include <16f877a.h>
#fuses NOWDT, HS, NOPROTECT, BROWNOUT, NOPUT, NODEBUG, NOLVP
#use delay(clock=20000000)
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3)
#use rs232(baud=57600,parity=N,bits=8,xmit=PIN_C6,rcv=PIN_C7)
#define downarrow PIN_C1
#define select PIN_C2
#include <ds1307.c>
#include <HDM64GS12.c>
#include <graphics.c>
char lcddata[20];
int sec, min, hrs, day, mth, year, dow;
int outputforce[8];
int p, arrowstate;
void drawstates()
{
int forcestat;
int ypos = 0;
for(p=0;p<8;p++)
{
forcestat = outputforce[p]; //move force state to var
if (arrowstate == p) //check if arrow position state is on the same line as we are printing too.
{
sprintf(lcddata,"<<");
glcd_text57(115,ypos,lcddata,1,on);
}
else
{
glcd_rect(115, ypos, 128, ypos + 8, yes, off); //clears all other positions
}
switch (forcestat)
{
case 0: //force is off
sprintf(lcddata,"Off");
glcd_text57(85,ypos,lcddata,1,on);
break;
case 1: //force is On
sprintf(lcddata,"On");
glcd_text57(85,ypos,lcddata,1,on);
break;
case 2: //force is off
sprintf(lcddata,"Auto");
glcd_text57(85,ypos,lcddata,1,on);
break;
}
ypos = ypos + 8;
}
}
void main()
{
glcd_init(ON); //turn of LCD
arrowstate = 0;
sprintf(lcddata,"Light Bank 1"); //setup non-changing data
glcd_text57(1,0,lcddata,1,on);
sprintf(lcddata,"Light Bank 2");
glcd_text57(1,8,lcddata,1,on);
sprintf(lcddata,"Light Bank 3");
glcd_text57(1,16,lcddata,1,on);
sprintf(lcddata,"Light Bank 4");
glcd_text57(1,24,lcddata,1,on);
sprintf(lcddata,"Light Bank 5");
glcd_text57(1,32,lcddata,1,on);
sprintf(lcddata,"Heaters");
glcd_text57(1,40,lcddata,1,on);
sprintf(lcddata,"PH Valve");
glcd_text57(1,48,lcddata,1,on);
sprintf(lcddata,"Hood Fan");
glcd_text57(1,56,lcddata,1,on);
while(1)
{
drawstates();
if (input(downarrow)) //down switch
{
if (arrowstate >= 7)
{
arrowstate = 0;
}
else
{
arrowstate ++;
}
}
if (input(select)) //select switch, set forcestate if pushed
{
if (outputforce[arrowstate] >= 2)
{
outputforce[arrowstate] = 0;
glcd_rect(85, arrowstate * 8, 112, arrowstate * 8 + 8, yes, off);
}
else
{
outputforce[arrowstate] ++;
glcd_rect(85, arrowstate * 8, 112, arrowstate * 8 + 8, yes, off);
}
}
}
}
|
Thanks for you input
Mark |
|