Design of frequency meter based on ATMEGA16 single chip microcomputer

Publisher:清新微笑Latest update time:2012-08-06 Source: 51heiKeywords:ATMEGA16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Click to browse the next page

Click to browse the next page

Complete program download address: http://www.51hei.com/f/avrplj.rar

Main function (main()): /*

Program function: frequency meter

Author: Zhu Bo

Date: January 28, 2012

Note: PB1 is the input port

Definition: Frequency meter - frequency is the number of times a signal changes within 1 second. A digital frequency meter measures the number of times a signal changes within a standard time of 1 second and then displays it in digital form.

Principle: Use timer 0 to get 1 second, timer 1 enters technical mode, and the count value is obtained in the interrupt of timer 0, which is the frequency value

*/

#include

#include

#include "delay.h"

#include "show.h"

#include "HD_init.h"

#define uchar unsigned char

#define uint unsigned int

unsigned int FREQ=0; //define global variables for calculation


void main()

{

PORT_Init(); //Port initialization

timer1_init(); //Timer 1 initialization

timer0_init(); //Timer 0 initialization

SEI(); // Enable general interrupt

while(1)

{

display();

}

}[page]

Hardware initialization function HD_init.c:
#include
#include
#define uchar unsigned char
#define uint unsigned int
#include "HD_init.h"
extern unsigned int FREQ; //define global variables for calculation
uint time_num=250;//250*4ms=1s

//Port initialization function
void PORT_Init()
{
DDRB = 0xFD; //PB1 as input port
}
//T0 overflow interrupt service routine
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x83; //Reassign value
if(!--time_num)
{
FREQ=TCNT1L;
FREQ|=TCNT1H<<8; //This is how to read the value of the lower eight bits and the upper eight bits
TCNT1H=0; //Count value reset to zero
TCNT1L=0;
time_num=250; //Timer 0 sets the timer to 4ms, multiply by 250 to get 1s
}
}
//Timer T1 initializes and enters counting mode
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00 /*INVALID SETTING*/; //setup
TCNT1L = 0x00 /*INVALID SETTING*/;
TCCR1A = 0x00;
TCCR1B = 0x06; //start Timer
}
//Timer T0 initializes the timing to 4ms
void timer0_init(void)
{
TCCR0 = 0x00; //Stop the timer
TCNT0 = 0x83; //initial value
OCR0 = 0x82; //match value
TIMSK |= 0x01; //Interrupt enable
TCCR0 = 0x04; //Start the timer
}
Delay function (delay.c):
#define uchar unsigned char
#define uint unsigned int
#include "delay.h"
void delay(uint ms)
{
uint i,j;
for(i=0;i
{
for(j=0;j<1141;j++);
}
}
Display function (show.c)
#include
#include
#define uchar unsigned char
#define uint unsigned int
#include "show.h"
extern unsigned int FREQ; //define global variables for calculation
const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void show(uchar j,uchar k)
{
DDRA|=BIT(3);
DDRA|=BIT(4);
DDRD=0XFF;
PORTA|=BIT(3);
PORTD=table[j];
PORTA&=~BIT(3);
delay(3); //This delay is very important in simulation
PORTD=0XFF;
PORTD&=~BIT(k);
PORTA|=BIT(4);
PORTA&=~BIT(4);
}
void display()//Actually only the last one is enough, keep so many
//Just to keep an algorithm
{
if(FREQ<10)
{
show(FREQ%10,1);
show(0,2);
show(0,3);
show(0,0);
}
else if(FREQ<100)
{
show(FREQ%10,1);
show(FREQ%100/10,2);
show(0,3);
show(0,0);
}
else if(FREQ<1000)
{
show(FREQ%10,1);
show(FREQ%100/10,2);
show(FREQ%1000/100,3);
show(0,0);
}
else
{
show(FREQ%10,1);
show(FREQ%100/10,2);
show(FREQ%1000/100,3);
show(FREQ/1000,0);
}
}
The following are the interface functions:

Click to browse the next page

show.h:
#define uchar unsigned char
#define uint unsigned int
#ifndef delay_h
#define delay_h
extern void show(uchar j,uchar k);
extern void display();
#endif

HD_init.h:
#ifndef HD_init_H
#define HD_init_H
extern void PORT_Init();
extern void timer0_init(void);
extern void timer1_init(void);
#endif

delay.h:
#define uchar unsigned char
#define uint unsigned int
#ifndef delay_h
#define delay_h
extern void delay(uint ms);
#endif
Keywords:ATMEGA16 Reference address:Design of frequency meter based on ATMEGA16 single chip microcomputer

Previous article:DS18B20 temperature measurement program based on ATMEGA16
Next article:DS1302 adjustable perpetual calendar program based on ATMEGA16

Recommended ReadingLatest update time:2024-11-16 15:54

ATMEGA16 drives internal EEPROM program
// 8MHZ crystal running on ATMEGA16 // Display effect: Display the number set by the button #include iom16v.h #include macros.h #define uchar unsigned char #define uint unsigned int #pragma data:code             const uchar table ="I LIKE AVR !"; const uchar table1 ="WWW*FUMANCHE*COM"; const uchar table_num
[Microcontroller]
STM8L timer2 timing interrupt
Introduction This article introduces how to use timer2 to generate timer interrupts in the STM8L series. experiment platform Compiler software: IAR for STM8 1.42.2 Hardware platform: stm8l101f3p6 development board Emulator: ST-LINK Library version: STM8L_STMTouch_Lib_V1.1.0 Experimental procedures 1. A
[Microcontroller]
STM8L timer2 timing interrupt
Design of electric vehicle lithium battery pack based on ATmega16
As electric bicycles become more popular, lithium batteries, the main energy source of electric bicycles, have also become the focus of everyone's attention. Lithium batteries are different from nickel-cadmium and nickel-metal hydride batteries. Because of their high energy density, they have very high requirements
[Microcontroller]
Design of electric vehicle lithium battery pack based on ATmega16
B001-Atmega16-Assembly implementation of bit fields
------------------------------------------------------------------------------------------------------------------------------------- Development environment: AVR Studio 4.19 + avr-toolchain-installer-3.4.1.1195-win32.win32.x86 Chip model: ATmega16 Chip frequency: 8MHz -------------------------------------------------
[Microcontroller]
TQ2416 development board TIMER interrupt application control device
       This issue introduces the control device code application of the new generation ARM9 development board TQ2416, using TIMER code to control the time period of the device. Timer syntax, timer code, the function is to repeatedly trigger the timer event of the specified window within the specified time interval.
[Microcontroller]
DS1302 adjustable perpetual calendar program based on ATMEGA16
program: main function: /* Program function: DS1302 real-time clock display Author: Zhu Bo Date: February 3, 2012 Communication mode: 3-wire serial communication Instructions: This program imitates a household perpetual calendar and can set the time. Time setting method: 1. Press the setting key to enter t
[Microcontroller]
DS1302 adjustable perpetual calendar program based on ATMEGA16
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号