Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to measure frequency with ATmega8 single chip microcomputer?
How to measure frequency with ATmega8 single chip microcomputer?
Form a square wave before sending the sine wave to M8 for interruption, otherwise it is easy to make mistakes.

Examples; For your reference, is there an external interruption? Just write down the program to be executed after you interrupt in the external interrupt function. It's that simple. The duty cycle and period of PWM can be set by the initial value of timer register.

//ICC-AVR? Apply? A builder? :? 2009-09- 1 1? 19:02:05

//? Target? :? M8

//? Crystal:? 8.0000 MHz

# Contains? & ltiom8v.h & gt

# Contains? & ltmacros.h & gt

Invalid? Port_init (empty)

{

PORTB? =? 0x00

DDRB=? 0x00

PORTC? =? 0x00? //m 103? Output? only

DDRC=? 0x00

Porter? =? 0x00

DDRD=? 0x00

}

//TIMER 1? Initialize? -? Prescale: 64

//? WGM:? 0)? Is it normal? TOP=0xFFFF

//? Desire? Value:? 1 ms

//? Actual? Value: 1.000 ms? (0.0%)

Invalid? Timer 1_init(void)

{

TCCR 1B? =? 0x00? //Stop

TCNT 1H? =? 0xFF? //Settings

TCNT 1L? =? 0x83

OCR 1AH? =? 0x00

OCR 1AL? =? 0x7D

OCR 1BH? =? 0x00

OCR 1BL? =? 0x7D

ICR 1H=? 0x00

ICR 1L=? 0x7D

TCCR 1A? =? 0x00

TCCR 1B? =? 0x03? //Start? timer

}

#pragma? Interrupt handler? Timer1_ OVF _ ISR: iv _ Tim1_ OVF

Invalid? Timer 1_ovf_isr(void)

{

//TIMER 1? Really? spill over

TCNT 1H? =? 0xFF? //reinstall? Counter? Tall? value

TCNT 1L? =? 0x83? //reinstall? Counter? Low? value

}

#pragma? Interrupt handler? int0_isr:iv_INT0

Invalid? int0_isr(void)

{

//External? Interrupt? Open? INT0

}

//Talk? This? Routine? Where to? Initialize? All of them? ancillary equipment

Invalid? Initialize device (invalid)

{

//Stop? Go astray? Interrupt? Until? Settings? up

CLI(); ? //Disabled? All of them? suspend

port _ init();

timer 1 _ init();

MCUCR? =? 0x00

GICR=? 0x40

Timsk? =? 0x04? //timer? Interrupt? source

SEI(); ? //Re-enable? suspend

//All? Peripheral? Is it? Now? Initialized

}

//

Invalid? Major (invalid)

{

init _ devices();

//Insert? Yours Functional? Code? here ...

}?

Supplement:

I suggest you read the information of M 16 carefully? About timer and PWM? This is irreplaceable by others.

Output comparison register 1A? -? OCR 1AH? With what? OCR 1AL

Output comparison register 1B? -? OCR 1BH? With what? OCR 1BL

16? Bit timer/? Description of counter register? T/C 1? Control register a? -? TCCR 1A

T/C 1? Control register b? -? TCCR 1B

TCCR 1A is used to select the mode? Details depend on information. Too much information?

TCCR 1B frequency division selection

Such as TCCR 1A:

7? 6? 5? 4? 3? 2? 1? 0

COM 1A 1? COM 1A0? COM 1B 1? COM 1B0? FOC 1A? FOC 1B? WGM 1 1? WGM 10

Among them? draw