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] atmega16 internal frequency set

Status
Not open for further replies.

Muhammad Faran

Newbie level 4
Joined
Apr 2, 2014
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
36
i am using atmega16 and want to set its internal frequency to 8Mhz as i am making a frequency counter using avrstudio 5 but i am having problem when i run my protues file as it does not display frequency count on lcd...

please help its urgent
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h> 

#define F_CPU 8000000UL

#define LCD_RS_PIN			5
#define LCD_RW_PIN			6
#define LCD_ENABLE_PIN			7
#define LCD_DATA_PORT	PORTA
#define LCD_DATA_DDR	DDRA
#define LCD_DATA_PIN	PINA
 
#define LCD_CNTRL_PORT	PORTC
#define LCD_CNTRL_DDR	DDRC
#define LCD_CNTRL_PIN	PINC


unsigned long int freq;  // to store value of frequency value
unsigned int i=0,dur; //i=number of overflows in one second
// dur to store the value of TCNT1 register
char buffer[8]; // to store the frequency value as a string to be displayed on lcd


// Timer1 overflow interrupt service routine
ISR(TIMER1_OVF_vect)
{
// Place your code here
i++ ;      // count the number of overflows in one second
}

int main(void)
{
_delay_ms(1000);	
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

DDRC=0xFF;
DDRA=0xFF;
_delay_ms(1000);

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;
_delay_ms(500);
// LCD module initialization
LCD_init();
_delay_ms(500);
sei();

while (1)
      {
      // Place your code here 
      _delay_ms(1000);            
      TIMSK=0x04;
      TCCR1B=0x07; 
      _delay_ms(1000);
      TCCR1B=0x00;  
      TIMSK=0x00;
      dur=TCNT1;
		freq = dur + i*65536;		//MAIN FREQUENCY VALUE IS HERE
      TCNT1=0x0000;
      i=0;
	  _delay_ms(1000); 
      LCD_goto(0,0);
	  _delay_ms(2000);
      LCD_print("freq=");
	  _delay_ms(1500);
      LCD_goto(0,1);
	  _delay_ms(1000);
	  itoa(freq,buffer,10);
	  _delay_ms(1000);
	  LCD_print(buffer);
	  _delay_ms(1000);
      };    
		
}

void LCD_goto(unsigned char y, unsigned char x)
{
	unsigned char firstAddress[] = {0x80,0xC0,0x94,0xD4};
 
	LCD_send_command(firstAddress[y-1] + x-1);
	_delay_ms(10);	
}

void LCD_send_command(unsigned char cmnd)
{
	LCD_DATA_PORT = cmnd;
	LCD_CNTRL_PORT &= ~(1<<LCD_RW_PIN);
	LCD_CNTRL_PORT &= ~(1<<LCD_RS_PIN);
 
	LCD_CNTRL_PORT |= (1<<LCD_ENABLE_PIN);
	_delay_ms(2);
	LCD_CNTRL_PORT &= ~(1<<LCD_ENABLE_PIN);
	_delay_ms(100);
}
 
void lcd_data(unsigned char data)
{
	LCD_DATA_PORT = data;
	LCD_CNTRL_PORT &= ~(1<<LCD_RW_PIN);
	LCD_CNTRL_PORT |= (1<<LCD_RS_PIN);
 
	LCD_CNTRL_PORT |= (1<<LCD_ENABLE_PIN);
	_delay_ms(2);
	LCD_CNTRL_PORT &= ~(1<<LCD_ENABLE_PIN);
	_delay_ms(100);
}

void LCD_print(char *string)
{
	unsigned char j=0;
	_delay_ms(100);
	while(string[j]!=0)
	{
		lcd_data(string[j]);
		_delay_ms(100);
		j++;
	}
}

void LCD_init()
{
	LCD_CNTRL_DDR = 0xFF;
	LCD_CNTRL_PORT = 0x00;
	LCD_DATA_DDR = 0xFF;
	LCD_DATA_PORT = 0x00;
 
	_delay_ms(100);
	LCD_send_command(0x38);
	_delay_ms(100);
	LCD_send_command(0x0C);
	_delay_ms(100);
	LCD_send_command(0x01);
	_delay_ms(100);
	LCD_send_command(0x06);
	_delay_ms(100);
}
 

Have u converted the Hex count into BCD before sending to LCD. The common mistake is on LCD ACII character displays only. So make sure after converting Hexadecimal number then Gate OR with 0x30. Then send your result to LCD.
 

use
unsigned char BCD2Bin(unsigned char data)
return ((((data & 0xF0 )>>4) * 10) + (data&0xF));

unsigned char D2BCD(unsigned char data)
return (((data/10)<< 4) | (data%10));

or else use sprintf
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top