hemnath
Advanced Member level 3
I want to design stopwatch which shows milliseconds, seconds, minutes in LCD. I am using CCS C compiler, Microcontroller PIC 18F2520 and Cyrstal 4Mhz. which timer to be used??? please help
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include "18F2520.h"
#include <f2520_regs.h>
#fuses INTRC_IO
#use delay(clock=4000000)
#define RS PIN_A2
#define EN PIN_A1
void T0_init();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
unsigned int16 temp;
unsigned int16 count=0;
void main()
{
TRISA = 0x00;
LATA = 0x00;
T0CON = 0x00; // 0b00000000 Prescalar 1:2 , 16-bit mode, Internal clock.
while(1)
{
T0_init();
temp++;
lcd_data((temp/1000)+0x30);
lcd_data(((temp/100)%10)+0x30);
lcd_data(((temp/10)%10)+0x30);
lcd_data(((temp/1)%10)+0x30);
}
}
void T0_init()
{
TMR0H = 0xF8;
TMR0L = 0x2F;
TMR0ON = 1;
while(TMR0IF==0);
TMR0ON = 0;
TMR0IF =0;
}
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);
}
//Timer0
//Prescaler 1:1; TMR0 Preload = 64537; Actual Interrupt Time : 1 ms
//Place/Copy this part in declaration section
void InitTimer0(){
T0CON = 0x88;
TMR0H = 0xFC;
TMR0L = 0x19;
GIE_bit = 1;
TMR0IE_bit = 1;
}
void Interrupt(){
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0H = 0xFC;
TMR0L = 0x19;
//Enter your code here
}
}
//Timer0
//Prescaler 1:1; TMR0 Preload = 64537; Actual Interrupt Time : 1 ms
//Place/Copy this part in declaration section
unsigned long int time = 0;
sbit interrupt_flag = 0;
// time gives no of milli sec
void InitTimer0()
{
T0CON = 0x88;
TMR0H = 0xFC;
TMR0L = 0x19;
GIE_bit = 1;
TMR0IE_bit = 1;
}
void Interrupt()
{
if (TMR0IF_bit)
{
time++;
interrupt_flag = 1;
TMR0IF_bit = 0;
TMR0H = 0xFC;
TMR0L = 0x19;
}
}
void main() {
while(1) {
if(interrupt_flag == 1) {
//then do something
interrupt_flag = 0;
}
}
}
i want the interrupt to occur for every 1ms and increment a count value. for 1ms, i have calculated the value 0xF82F to load in timer0.
this is the code i have used to increment just millisecond in LCD. But its not working.
#include "18F2520.h"
#include <f2520_regs.h>
#fuses INTRC_IO
#use delay(clock=4000000)
#define RS PIN_A2
#define EN PIN_A1
void T0_init();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
unsigned int16 temp;
void InitTimer0()
{
T0CON = 0x88;
TMR0H = 0xFC;
TMR0L = 0x19;
GIE = 1;
TMR0IE = 1;
}
void Interrupt()
{
if (TMR0IF)
{
TMR0IF = 0;
TMR0H = 0xFC;
TMR0L = 0x19;
temp++;
printf(lcd_data,"%5LU",temp);
}
}
void main()
{
TRISB = 0x00;
LATB = 0x00;
T0CON = 0x00; // 0b00000000 Prescalar 1:2 , 16-bit mode, Internal clock.
while(1)
{
InitTimer0();
}
}
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);
}
printf(lcd_data,"%5LU",temp);
#include "18F2520.h"
#include <f2520_regs.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;
void main()
{
TRISA = 0x00;
LATA = 0x00;
TRISB = 0x00;
LATB = 0x00;
T0CON = 0x00; // Prescaler= 1:2, 16-bit mode, Internal Clock
lcd_init();
while(1)
{
T0_init();
lcd_cmd(0x80);
printf(lcd_data,"%5LU",temp);
}
}
void T0_init()
{
TMR0H = 0xF8; // Values calculated for 1 second delay with 4MHz crystal
TMR0L = 0x2F;
TMR0ON = 1; // Timer0 On
while(TMR0IF == 0); // Wait until TMR0IF gets flagged
temp++;
TMR0ON = 0; // Timer0 Off
TMR0IF = 0; // 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);
}
TMR0H = 0xFC;
TMR0L = 0x19;
void T0_init()
{
TMR0H = 0xF8; // Values calculated for 1 second delay with 4MHz crystal
TMR0L = 0x2F;
TMR0ON = 1; // Timer0 On
while(TMR0IF == 0); // Wait until TMR0IF gets flagged
temp++;
TMR0ON = 0; // Timer0 Off
TMR0IF = 0; // Clear Timer0 interrupt flag
}
#include "18F2520.h"
#include <f2520_regs.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;
void main()
{
TRISA = 0x00;
LATA = 0x00;
TRISB = 0x00;
LATB = 0x00;
T0CON = 0x00; // Prescaler= 1:2, 16-bit mode, Internal Clock
lcd_init();
T0_init();
while(1)
{
lcd_cmd(0x80);
if(intr_flag == 1) {
printf(lcd_data,"%5LU",temp);
intr_flag = 0;
}
}
}
void T0_init()
{
T0CON = 0x88;
TMR0H = 0xFC;
TMR0L = 0x19;
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);
}
void Interrupt()
{
if (TMR0IF)
{
TMR0IF = 0;
TMR0H = 0xFC;
TMR0L = 0x19;
temp++;
intr_flag = 1;
}
}
void Interrupt()
{
if (TMR0IF)
{
TMR0IF = 0;
TMR0H = 0xFC;
TMR0L = 0x19;
temp++;
intr_flag = 1;
}