hemnath
Advanced Member level 3
thank you very much @internetuser2k12 .. It really helped me. i know for 1ms increment, counter variable will be incrementing very fast and could not see the increment value in LCD and looks like blur. But the problem is, i am not sure whether i'm calculating the timer value to correct. Please check it once,
using internal oscillator, 4Mhz
so f = 4Mhz/4 => 1Mhz
using prescaler 1:2, so dividing by 2, => 1Mhz/2 => 0.5Mhz
t = 1/0.5Mhz =>2us
to increment for 1msec, 1msec/2us => 500 which is 1F4(in Hex)
this is the value I have loaded in timer = FFFF-1F4 => FE0B
Is it correct??? Please help
finally my code looks like this.
using internal oscillator, 4Mhz
so f = 4Mhz/4 => 1Mhz
using prescaler 1:2, so dividing by 2, => 1Mhz/2 => 0.5Mhz
t = 1/0.5Mhz =>2us
to increment for 1msec, 1msec/2us => 500 which is 1F4(in Hex)
this is the value I have loaded in timer = FFFF-1F4 => FE0B
Is it correct??? Please help
finally my code looks like this.
HTML:
#include "18F2520.h"
#include <f2520_registers.h>
#fuses INTRC_IO
#use delay(clock=4000000)
#define RS PIN_A2
#define EN PIN_A1
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
void T0_init();
void lcd_init();
unsigned int16 temp;
unsigned char intr_flag = 0;
#INT_TIMER0
void Interrupt()
{
if (TMR0IF)
{
TMR0IF = 0;
TMR0H = 0xFE;
TMR0L = 0x0B;
temp++;
intr_flag = 1;
}
}
void main()
{
TRISA = 0x00;
LATA = 0x00;
TRISB = 0x00;
LATB = 0x00;
lcd_init();
T0_init();
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(1)
{
lcd_cmd(0x80);
if(intr_flag == 1) {
printf(lcd_data,"%LU",temp);
intr_flag = 0;
}
}
}
void T0_init()
{
T0CON = 0x80;
TMR0H = 0xFE;
TMR0L = 0x0B;
GIE = 1;
TMR0IE = 1; // Clear Timer0 interrupt flag
}
void lcd_init()
{
lcd_cmd(0x30); // Configure the LCD in 8-bit mode, 1 line and 5x7 font
lcd_cmd(0x0C); // Display On and Cursor Off
lcd_cmd(0x01); // Clear display screen
lcd_cmd(0x06); // Increment cursor
lcd_cmd(0x80); // Set cursor position to 1st line, 1st column
}
void lcd_cmd(unsigned char c)
{
output_b (c);
output_low(RS);
output_high (EN);
delay_ms (10);
output_low (EN);
}
void lcd_data(unsigned char z)
{
output_b(z);
output_high(RS);
output_high(EN);
delay_ms(10);
output_low(EN);
}