#define LCD_CLR 0x01 // Clear Display
#define LCD_HOME 0x02 // Cursor to Home position
unsigned long cntr ; // number of RB0 transitionunsigned
int ovrflw ; // number of timer0 overflowsunsigned
int IFR_ITR;
int cntoscurato;
int OSCURATO;
int RB5_PREC;
void interrupt isr(void)
{
GIE=0;
if(INTF)
{ // RB0 interrupt
cntr++ ; // inc. transition counter
INTF = 0 ; // clear interrupt flag to enable next call
}
else if(T0IF)
{ //TIMER 0 overflow
ovrflw++ ; // inc. overflow counter
T0IF = 0 ; // clear interrupt flag to enable next call on overflow
}
else if (TMR1IF){
cntoscurato++;
TMR1IF=0;
}
else if(RBIF)
{
if (RB5==0)
{
//IFR_ITR=1 ; // inc. overflow counter
RB5_PREC=RB5;
}
RBIF = 0 ; // clear interrupt flag to enable next call on overflow
}
GIE=1;
}
int getTimeToDisplay(int timeIn,char unit)
{
int sec=0;
int decSec=0;
sec=(timeIn/(76.6));
decSec=(timeIn-(sec*(76.6)))/(7.6);
if (unit=='s')
return sec;
else
return decSec;
}
/************************************************************************/
#if (LCD_COLS==20)
#define LCD_line1 0x80 // Line 1 position 1
#define LCD_line2 0xC0 // Line 2 position 1
#define LCD_line3 0x94 // Line 3 position 1 (20 char LCD)
#define LCD_line4 0xD4 // Line 4 position 1 (20 char LCD)
#else
#define LCD_line1 0x80 // Line 1 position 1
#define LCD_line2 0xC0 // Line 2 position 1
#define LCD_line3 0x90 // Line 3 position 1 (16 char LCD)
#define LCD_line4 0xD0 // Line 4 position 1 (16 char LCD)
#endif
/************************************************************************/
/****************************************/
/* Enable LCD to read data */
/****************************************/
void LCD_STROBE (void)
{
LCD_EN = 1;
//DelayUs(1);
DelayUs(5);
LCD_EN=0;
}
/****************************************/
/* Write a nibble to the LCD */
/****************************************/
void LCD_NIBBLE_OUT (unsigned char c )
{
if ( c & 0b10000000 )
LCD_D7=1;
else LCD_D7=0;
if ( c & 0b01000000 )
LCD_D6=1;
else LCD_D6=0;
if ( c & 0b00100000 )
LCD_D5=1;
else LCD_D5=0;
if ( c & 0b00010000 )
LCD_D4=1;
else LCD_D4=0;
LCD_STROBE();
}
/****************************************/
/* Write a byte to the LCD (4 bit mode) */
/****************************************/
void LCD_WRITE (unsigned char c)
{
LCD_NIBBLE_OUT(c);
c <<= 4;
LCD_NIBBLE_OUT(c);
//DelayUs(50);
DelayUs(250);
}
/****************************************/
/* send a command to the LCD */
/****************************************/
void LCD_CMD (char c)
{
LCD_RS = 0; // write command
LCD_WRITE(c);
}
/****************************************/
/* GoTO specified line and position */
/****************************************/
void LCD_GOTO (char line,char pos)
{
switch(line)
{
case 1: LCD_CMD((LCD_line1-1)+pos);
break;
case 2: LCD_CMD((LCD_line2-1)+pos);
break;
case 3: LCD_CMD((LCD_line3-1)+pos);
break;
case 4: LCD_CMD((LCD_line4-1)+pos);
}
}
/****************************************/
/* Clear and Home LCD */
/****************************************/
void LCD_CLEAR (void)
{
LCD_CMD(LCD_CLR);
//Delay(3);
Delay(15);
}
/****************************************/
/* Write one character to the LCD */
/****************************************/
void LCD_PUTCH (char c)
{
LCD_RS = 1; // write characters
LCD_WRITE(c);
}
/****************************************/
/* Write numbers to the LCD */
/****************************************/
void LCD_PUTUN (unsigned int c)
{
unsigned char t1,i,wrote;
unsigned int k;
wrote=0;
for (i=4;i>=1;i--)
{
switch(i){
case 4: k=10000;
break;
case 3: k=1000;
break;
case 2: k=100;
break;
case 1: k=10;
}
t1=c/k;
if((wrote)||(t1!=0))
{
LCD_PUTCH(t1+'0');
wrote=1;
}
c-=(t1*k);
}
LCD_PUTCH(c+'0');
}
/****************************************/
void LCD_PUTSN (signed int c)
{
if(c<0)
{
LCD_PUTCH('-');
c*=(-1);
}
LCD_PUTUN(c);
}
/****************************************/
/* Write a string to the LCD */
/****************************************/
void LCD_PUTS (const char * s)
{
LCD_RS = 1; // write characters
while(*s)
LCD_WRITE(*s++);
}
/****************************************/
/* Initialize LCD */
/****************************************/
void LCD_INIT (void)
{
LCD_RS = 0; // write control bytes
//Delay(15); // power on delay
Delay(75); // power on delay
LCD_D4=1;
LCD_D5=1;
LCD_D6=0;
LCD_D7=0;
LCD_STROBE();
//Delay(5);
Delay(25);
LCD_STROBE();
//DelayUs(100);
DelayUs(500);
LCD_STROBE();
//Delay(5);
Delay(25);
LCD_D4=0; // set 4 bit mode
LCD_STROBE();
//DelayUs(40);
DelayUs(200);
#if (LCD_ROWS==1)
LCD_WRITE(0b00100000); // 4 bit mode, 1 line, 5x8 font
#else
LCD_WRITE(0b00101000); // 4 bit mode, 2 or more lines, 5x8 font
#endif
LCD_WRITE(0b00001000); // display off
LCD_WRITE(0b00001100); // display on, curson off, blink off
LCD_WRITE(0b00000110); // shift entry mode, display not shifted
}
/************************************************************************/
#undef LCD_ROWS
#undef LCD_COLS
/************************************************************************/
/* !!! END !!! */
/* THANKS FOR EXAMINING MY CODE ;) */
/************************************************************************/
/* */
/* Please report any bug or suggestion at zypkin@inwind.it */
/* */
/************************************************************************/
void main(void)
{
int tempo_giro;
int tempo_giro_hold;
int tempo_giro_best;
int cnt;
initadc();
init();
Delay(500); // serve per stabilizzare l'alimentazione dell'lcd (l'lcd è molto lento)
LCD_INIT(); // INDISPENSABILE serve ad inizializzare l'LCD
Delay(1250); // piccolo ritardo per completare l'inizializzazione (facoltativo)
LCD_CLEAR(); // Cancella il display
DelayUs(200); // Altra piccola pausa (facoltativo)
cnt=0;
tempo_giro=0;
tempo_giro_hold=0;
tempo_giro_best=10000;
RB5_PREC=1;
while(1) // inizio ciclo
{
ovrflw=0;
cntr=0;
IFR_ITR=0;
while(ovrflw<78){} //attesa 1 secondo
tempo_giro=tempo_giro+ovrflw;
if (IFR_ITR==1)
{
if (cntoscurato==0)
{
tempo_giro_hold=tempo_giro;
if ((tempo_giro!=0)&&(tempo_giro<tempo_giro_best))
{
tempo_giro_best=tempo_giro;
}
tempo_giro=0;
TMR1ON=1;
}
else if(cntoscurato>0&&cntoscurato<200)//tempo di oscuramento
{
TMR1ON=1; //se sono dentro non faccio nulla
}
else
{
tempo_giro_hold=tempo_giro;
if ((tempo_giro!=0)&&(tempo_giro<tempo_giro_best))
{
tempo_giro_best=tempo_giro;
}
cntoscurato=0; //se sono fuori è il primo fronte di INFRAROSSO e aggiorno i tempi
//tempo_giro_hold=3;
tempo_giro=0;
TMR1ON=0;
}
}
if(cntoscurato>200){cntoscurato=0;TMR1ON=0;}
LCD_CLEAR();
LCD_CMD(LCD_line1);
//LCD_PUTUN(tempo_giro_hold);
//LCD_PUTS(" ");
LCD_PUTUN(cntoscurato);
LCD_PUTS(" ");
LCD_PUTUN(tempo_giro);
LCD_PUTS(" ");
LCD_PUTUN(RB5);
LCD_PUTS(" ");
LCD_PUTUN(IFR_ITR);
LCD_CMD(LCD_line2);
LCD_PUTS("LAP ");
LCD_PUTUN(getTimeToDisplay(tempo_giro_hold,'s'));
//LCD_PUTUN(tempo_giro_hold);
LCD_PUTS(".");
LCD_PUTUN(getTimeToDisplay(tempo_giro_hold,'d'));
LCD_PUTS("| BEST ");
if (tempo_giro_best==10000)
LCD_PUTS(" ");
else
LCD_PUTUN(tempo_giro_best);
}
}