[SOLVED] Problem with CAVR coding for stopwatch

Status
Not open for further replies.

fauzan

Newbie level 3
Joined
Sep 4, 2010
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,306
I need help for my stopwatch project, i have problem to show the time like "02:04.89", the problem is when i start the microcontroller, the program can't display "0" beside "2", it's just show number "2_" when number become tens, it will show... can u fix it? i hope anybody can help me... thanks

this is my program

Code:
Chip type               : ATmega8535
Program type            : Application
AVR Core Clock frequency: 4,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 128


#include <mega8535.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <stdio.h>
#include <stdlib.h>

unsigned int count=0,kali=0;
unsigned char detik=0, menit=0;
unsigned char cdetik[10],cmenit[10],ccount[10];

// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer1 value
TCNT1H=0xFF64 >> 256;
TCNT1L=0xFF64 & 0xff;

if(++count==100)
    {
    count=0;
    lcd_clear();
        if(++detik==60)
            {
            detik=0;
            lcd_clear();
                if(++menit==20)
                {
                menit=0;
                lcd_clear();
                }
             }
     }                                
    kali=0;                           
    itoa(count,ccount);         
    itoa(detik,cdetik);           
    itoa(menit,cmenit);               
    lcd_gotoxy(11,0);
    lcd_puts(ccount);
    lcd_gotoxy(10,0);
    lcd_puts(".");
    lcd_gotoxy(8,0);
    lcd_puts(cdetik);
    lcd_gotoxy(7,0);
    lcd_puts(":");
    lcd_gotoxy(5,0);
    lcd_puts(cmenit);

// Place your code here

}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization

PORTA=0x00;
DDRA=0x00;


PORTB=0x00;
DDRB=0x00;

PORTC=0x00;
DDRC=0x00;

PORTD=0x00;
DDRD=0x00;

TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

TCCR1A=0x00;
TCCR1B=0x01;
TCNT1H=0xFF;
TCNT1L=0x64;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

MCUCR=0x00;
MCUCSR=0x00;


TIMSK=0x04;


ACSR=0x80;
SFIOR=0x00;

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")

while (1)
      {
};
};
 

you can use sprintf() from <stdio.h> to create a string with the time formatted as required then write it to the LCD
e.g. this displays the result to the screen but you can display data to the LCD
Code:
        unsigned int hours=2, minutes=4, seconds=59;
        char data[25];
	sprintf(data,"%02u:%02u:%02u",hours, minutes, seconds);
	printf("time %s\n", data);
the conversion specification %02u converts an unsigned int to field width 2 digits with 0 specifing print leading zeros
the above program displays
Code:
02:04:59
 
Reactions: fauzan

    fauzan

    Points: 2
    Helpful Answer Positive Rating
Thanks horace1, your reply for my thread is very usefull.. now i can make my own stopwatch program.

here is my final code
Code:
Chip type               : ATmega8535
Program type            : Application
AVR Core Clock frequency: 4,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 128
*****************************************************/

#include <mega8535.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <stdio.h>
#include <stdlib.h>

unsigned char count=0, detik=0, menit=0;
char data[25];

interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer1 value
TCNT1H=0xFF64 >> 256;
TCNT1L=0xFF64 & 0xff;

if(++count==100)
    {
    count=0;
    lcd_clear();
        if(++detik==60)
            {
            detik=0;
            lcd_clear();
                if(++menit==20)
                {
                menit=0;
                lcd_clear();
                }
             }
     }

sprintf(data,"%02u:%02u.%02u",menit,detik,count);
lcd_gotoxy(0,0);
lcd_puts(data);

}

void main(void)
{

PORTA=0x00;
DDRA=0x00;

PORTB=0x00;
DDRB=0x00;

PORTC=0x00;
DDRC=0x00;


PORTD=0x00;
DDRD=0x00;

TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

TCCR1A=0x00;
TCCR1B=0x01;
TCNT1H=0xFF;
TCNT1L=0x64;

ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

MCUCR=0x00;
MCUCSR=0x00;

TIMSK=0x04;

ACSR=0x80;
SFIOR=0x00;

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")

while (1)
      {
}
};

here is the display
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…