0% found this document useful (0 votes)
58 views3 pages

#Define #Include #Include #Include #Include #Define #Define #Define #Define #Define #Define #Define #Define #Define Void Unsigned Char

This document contains C code to measure the frequency and duty cycle of a signal using an ATmega microcontroller. It uses input capture on timer 1 to sample the rising and falling edges of the signal. It then calculates the period from these samples and uses it to determine the frequency. It also calculates the duty cycle by comparing the high and low times. The results are displayed on an LCD screen in Hertz and percentage. The code sets up timers and input capture, samples the signal edges, calculates frequency and duty cycle, and continuously displays the results.

Uploaded by

Trần Ngọc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views3 pages

#Define #Include #Include #Include #Include #Define #Define #Define #Define #Define #Define #Define #Define #Define Void Unsigned Char

This document contains C code to measure the frequency and duty cycle of a signal using an ATmega microcontroller. It uses input capture on timer 1 to sample the rising and falling edges of the signal. It then calculates the period from these samples and uses it to determine the frequency. It also calculates the duty cycle by comparing the high and low times. The results are displayed on an LCD screen in Hertz and percentage. The code sets up timers and input capture, samples the signal edges, calculates frequency and duty cycle, and continuously displays the results.

Uploaded by

Trần Ngọc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

* ATmega 16 frequency and duty cycle measurment using input capture


* www.electronicwings.com
*/

#define F_CPU 8000000UL


#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#define LCD_DPRT PORTB
#define LCD_DDDR DDRB
#define LCD_DPIN PINB
#define LCD_CPRT PORTC
#define LCD_CDDR DDRC
#define LCD_CPIN PINC
#define LCD_RS PC3
#define LCD_RW PC4
#define LCD_EN PC5
void lcdCommand(unsigned char cmnd){
LCD_DPRT = cmnd;
LCD_CPRT &= ~ (1<<LCD_RS);
LCD_CPRT &= ~ (1<<LCD_RW);
LCD_CPRT |= (1<<LCD_EN);
_delay_us(1);
LCD_CPRT &= ~ (1<<LCD_EN);
_delay_us(100);
}

void lcdData(unsigned char data){


LCD_DPRT = data;
LCD_CPRT |= (1<<LCD_RS);
LCD_CPRT &= ~ (1<<LCD_RW);
LCD_CPRT |= (1<<LCD_EN);
_delay_us(1);
LCD_CPRT &= ~ (1<<LCD_EN);
_delay_us(100);
}

void lcd_init(){
LCD_DDDR = 0xFF;
LCD_CDDR = 0xFF;
LCD_CPRT &= ~(1<<LCD_EN);
_delay_us(2000);
lcdCommand(0x38);
lcdCommand(0x0E);
lcdCommand(0x01);
_delay_us(2000);
lcdCommand(0x06);
}

void lcd_gotoxy(unsigned char x, unsigned char y){


unsigned char firstCharAdr[] = {0x80,0xC0,0x94,0xD4};
lcdCommand(firstCharAdr[y-1]+x-1);
_delay_us(100);
}
void lcd_print(char *str){
unsigned char i = 0;
while(str[i]!=0){
lcdData(str[i]);
i++;
}
}
int main ( )
{
unsigned int a,b,c,high,period;
lcd_init();
char frequency[14],duty_cy[7];
DDRD |= (1<<PD7); //set PD7 as output
TCCR2 |= (1<<WGM20)|(1<<WGM21); //select Fast PWM mode
TCCR2 |=(1<<COM21)|(1<<CS20)|(0<<CS21)|(0<<CS22); //clear OC2 on compare match
//Clock Prescaler is 1 (no prescaling) for ease of simulation

OCR2 = 127; // Set duty cycle to 50%

for(;;);
return 0;
lcd_gotoxy(1,1);
lcd_print("Freq' ");
lcd_gotoxy(1,2);
lcd_print("Hz");
while(1)
{
TCCR1A = 0;
TCNT1=0;
TIFR = (1<<ICF1); /* clear ICP flag (Input Capture
flag) */

TCCR1B = 0x41; /* rising edge, No


prescaler */
while ((TIFR&(1<<ICF1)) == 0);
a = ICR1; /* take value of capture
register */
TIFR = (1<<ICF1); /* clear ICP flag (Input Capture
flag) */

TCCR1B = 0x01; /* falling edge, No prescaler


*/
while ((TIFR&(1<<ICF1)) == 0);
b = ICR1; /* take value of capture
register */
TIFR = (1<<ICF1); /* clear ICP flag (Input Capture
flag) */

TCCR1B = 0x41; /* rising edge, No


prescaler */
while ((TIFR&(1<<ICF1)) == 0);
c = ICR1; /* take value of capture
register */
TIFR = (1<<ICF1); /* clear ICP flag (Input Capture
flag) */

TCCR1B = 0; /* stop the timer */


if(a<b && b<c) /* check for valid
condition, to avoide timer overflow reading */
{
high=b-a;
period=c-a;

long freq= F_CPU/period; /* calculate frequency */


float duty_cycle = ((float) high/ (float) period)*100; /*calculate
duty cycle */

ltoa(freq,frequency,10);

itoa((int)duty_cycle,duty_cy,10);
lcd_gotoxy(1,1);
lcd_print("Freq' ");
lcd_gotoxy(1,2);
lcd_print("Hz");
/*LCD_Command(0x80);
LCD_String("Freq: ");
LCD_String(frequency);
LCD_String(" Hz ");

LCD_Command(0xC0);
LCD_String("Duty: ");
LCD_String(duty_cy);
LCD_String(" % ");*/

else
{

}
_delay_ms(50);
}
}

You might also like