Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] 8 bit timer register shows values greater than 255

Status
Not open for further replies.

ANS HAFEEZ

Advanced Member level 4
Joined
Jul 25, 2013
Messages
101
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Location
LAHORE,PAKISTAN
Activity points
1,911
SALAM TO AALL;
i am using 8-bit timer in normal mode and when i continously read TCNT register of atmega32 and show values on LCD then it show values above 255 some time value reached to 900 im not getting where the problem...
 

Maybe the LCD display routine causing the problem. Show LCD related code.
 

Code:
MAIN HEADER FILE
#define DATA_BUS DDRD      //  LCD databus Direction Configuration
#define CONTROL_BITS DDRC  //  LCD Control bus Direction Configuration

#define DB	PORTD             //	LCD databus connected to PORTB
#define CB PORTC        //	LCD Control bus connected to PORTD

#define rs 0                    // Register select pin connected 6th bit(D5) Control bus
#define rw 1                   // Read Write pin connected to 7th bit(D6) Control bus
#define en 2                  // Enable pin connected to 8th bit(D7) Control bus

#define LCDMaxLines 2
#define LCDMaxChars 16
#define LineOne 0x80
#define LineTwo 0xc0
void LCD_CmdWrite( char cmd)
{
	
	DB=cmd;             // Send the command to LCD
	CB &=~(1<<rs);  // Select the Command Register by pulling RS LOW
	CB &=~(1<<rw);  // Select the Write Operation  by pulling RW LOW
	CB |=1<<en;     // Send a High-to-Low Pusle at Enable Pin
	_delay_us(1);
	CB &=~(1<<en);
	_delay_ms(1);
}
void LCD_DataWrite( char dat)
{
	
	DB=dat;            // Send the data to LCD
	CB |=1<<rs;	// Select the Data Register by pulling RS HIGH
	CB &=~(1<<rw);	// Select the Write Operation  by pulling RW LOW
	CB |=1<<en;	// Send a High-to-Low Pusle at Enable Pin
	_delay_us(1);
	CB &=~(1<<en);
	_delay_ms(1);
	
}
void LCD_Init()
{
	_delay_ms(50);
	DATA_BUS = 0xff;   // Configure both databus and controlbus as output
	CONTROL_BITS = 0xff;
	LCD_CmdWrite(0x38);   // LCD 2lines, 5*7 matrix
	LCD_CmdWrite(0x0C);	// Display ON cursor ON
	LCD_CmdWrite(0x01);	// Clear the LCD
	LCD_CmdWrite(0x80);	// Move the Cursor to First line First Position
}
void LCD_DisplayNumber(unsigned int num)
{
	if (num>10000)
	{
		LCD_DataWrite((num/10000)+0x30);
		num=num%10000;
		
		LCD_DataWrite((num/1000)+0x30);
		num=num%1000;
		
		LCD_DataWrite((num/100)+0x30);
		num=num%100;
		
		LCD_DataWrite((num/10)+0x30);
		
		LCD_DataWrite((num%10)+0x30);
	}
	else if (num>1000)
	{
		LCD_DataWrite((num/1000)+0x30);
		num=num%1000;
		
		LCD_DataWrite((num/100)+0x30);
		num=num%100;
		
		LCD_DataWrite((num/10)+0x30);
		
		LCD_DataWrite((num%10)+0x30);
	}
	else if (num>100)
	{
		LCD_DataWrite((num/100)+0x30);
		num=num%100;
		
		LCD_DataWrite((num/10)+0x30);
		
		LCD_DataWrite((num%10)+0x30);
	}
	else if (num>10)
	{
		LCD_DataWrite((num/10)+0x30);
		LCD_DataWrite((num%10)+0x30);
	}
	else
	LCD_DataWrite((num%10)+0x30);
}
MAIN C FILE
//#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "LCD.h"
void TIMER_ON()
{
	TCCR0 |= ( 1<<CS02) | (1<<CS00); //(1<<CS01);//
}
void PING()
{
	PORTA |= (1 << PINA0);
	_delay_us(10);
	PORTA &= ~(1 << PINA0);
}
int main(void)
{
	LCD_Init();
	TIMER_ON();
	char TYM=0;
	DDRA |= 1 << PINA0;//set pin B0 to be output and connect with trigger pin in the sensor
	DDRA &= ~(1 << PINA1);//set pin B5 to be input
	MCUCSR|= 1<<JTD;
	MCUCSR|= 1<<JTD;
	while(1)
	{
		PING();
		if (bit_is_set(PINA, PINA1)) TYM=TCNT0;
		else TCNT0=0;
		LCD_CmdWrite(0x80);
		LCD_DisplayNumber(TYM);
	}
	return 0;
}
 

Try this code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void LCD_DisplayNumber(unsigned int num)
{
    if (num>10000)
    {
        LCD_DataWrite((num/10000)+0x30);
        num=num%10000;
        
        LCD_DataWrite((num/1000)%10+0x30);
        num=num%1000;
        
        LCD_DataWrite((num/100)%10+0x30);
        num=num%100;
        
        LCD_DataWrite((num/10)%10+0x30);
        
        LCD_DataWrite((num%10)+0x30);
    }
    else if (num>1000)
    {
        LCD_DataWrite((num/1000)+0x30);
        num=num%1000;
        
        LCD_DataWrite((num/100)%10+0x30);
        num=num%100;
        
        LCD_DataWrite((num/10)%10+0x30);
        
        LCD_DataWrite((num%10)+0x30);
    }
    else if (num>100)
    {
        LCD_DataWrite((num/100)+0x30);
        num=num%100;
        
        LCD_DataWrite((num/10)%10+0x30);
        
        LCD_DataWrite((num%10)+0x30);
    }
    else if (num>10)
    {
        LCD_DataWrite((num/10)+0x30);
        LCD_DataWrite((num%10)+0x30);
    }
    else
    LCD_DataWrite((num%10)+0x30);
}

 


Code C - [expand]
1
2
3
4
5
6
7
8
9
while(1)
    {
        PING();
        if (bit_is_set(PINA, PINA1)) TYM=TCNT0;
        else TCNT0=0;
        /* Clear display */
        LCD_CmdWrite(0x80);
        LCD_DisplayNumber((unsigned char)TYM);
    }



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void LCD_DisplayNumber(unsigned int num)
{
char digit = 0;
 
if(x = (num/10000)) {
LCD_DataWrite( x +0x30);
num=num%10000;
digit = 1;
}
 
if(x = (num/1000) || digit) {
LCD_DataWrite( x +0x30);
num=num%1000;
digit = 1;
}
 
if(x = (num/100) || digit) {
LCD_DataWrite( x +0x30);
num=num%100;
digit = 1;
}
        
if(x = (num/10) || digit) {
LCD_DataWrite( x +0x30);
num=num%10;
}
        
LCD_DataWrite((num%10)+0x30);
 
}

 
Last edited:
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top