i'm going to realize a speed meter with an 18f452...
i spent all the day reading datasheet!
but i don't know if the registers configuration is right for my idea.
here's the code:
Code: Select all
void boot(){
INTCON = 0b10110000;
INTCON2 = 0b11000000;
INTCON3 = 0b11000000;
T0CON = 0b10001000;
ADCON1 = 0x07; // configure VDD as Vref, PORTA pins as analog, PORTB pins as digital
TRISA = 0xFF; // designate PORTA as input
TRISD = 0x00; // designate PORTA as output
TRISB = 0xFF; // designate PORTB as input
//debug
TRISE = 0b00000000;
LCD_Init(&PORTD);
Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off
Ps2_Init(&PORTC);
Delay_ms(100); // attesa inizializzazione tastiera
}
/*********************************/
unsigned speed(){
while(overflow < 23437) ; // wait 1 second : 23437 = 24 000 000 / 4 / 256, rounded up
speed_value = (((diameter / 1000) * (628 / 100))* wheel_counter); // /2 if 2 reed are used m/s and *3.6 for Km/h
longtostr(overflow, text);
LCD_Out(1,1, text );
wheel_counter = 0 ; // clear counters
overflow = 0 ;
longtostr(overflow, text);
LCD_Out(1,1, text );
//comment line ->debug speed_value = 8 ;
return speed_value;
}
/******************************************/
void interrupt(void)
{
if(INTCON.INT0IF)
{
/*
* RB0 interrupt
*/
wheel_counter++ ; // inc. transition counter
INTCON.INT0IF = 0 ; // clear interrupt flag to enable next call
}
else if(INTCON.TMR0IF)
{
/*
* TIMER 0 overflow
*/
overflow = 0; // inc. overflow counter
INTCON.TMR0IF = 0; // clear interrupt flag to enable next call on overflow
}
}
/*******************************************/
void main(){
//speed code
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,"#Speed value: #" );
while(exit_loop==0){
if((Ps2_Key_read(&key,&special,&down)) && down && (key==34)) { exit_loop=1;exit_add=1;}
InttoStr(speed(), text_speed);
LCD_Out(2,14,"m/s");
Delay_ms(150);
} //fine loop principale
}//end if key 50
}//end loop exit_add
}
this code is only an extract of whole program that works fine!
only i need to say if this registers configuration is ok because it seems to freeze!
thank to all!