The ATmega128 microcontroller source program is as follows:
//ICC-AVR application builder: 2018/2/23 22:40:20
// Target : M128
// Crystal: 8.0000Mhz
#include #include //#include "delay.h" //This program generates 7 scales from 1 to 7 by pressing keys. The frequencies corresponding to the scales are 523hz, 587hz, and 698hz respectively. . . // tone_freq[7]={523,587,659,698,784,880,987} //The member values in tone_cnt[] are the setting values of the timer register OCR1A that generates different frequencies. For example, the value of the timer OCR1A corresponding to 523hz is 0x3BC, and so on. unsigned int tone_cnt[7]={0x03BC,0x0353,0x02F6,0x02CC,0x027D,0x0238,0x01FA}; void port_init(void) { PORTB = 0x01; //PORTB.0 is input with pull-up DDRB = 0x00; //PORTB.0 is the input port PORTC = 0x00; //PORTC outputs 0 level DDRC = 0xFF; //PORTC is the output port } //Use the CTC mode (mode 4) of timer 1 and enable the compare match A interrupt. That is, if you want to generate a 523 Hz frequency signal, calculate the period of 523 Hz = 1.9 ms, half period = 0.95 ms. Set the OCR1A value of the timer so that the timer will be interrupted every 0.95 ms. //An interrupt occurs, and the high and low levels are processed in the interrupt to obtain a 523hz square wave signal. You can use the application wizard to help calculate the value of OCR1A. There are two methods: 1. Calculate the half cycle time as above 2. Use 523*2HZ for one cycle time //This example uses the second method. Note that it uses 8-frequency division. //TIMER1 initialize - prescale:8 8 division // WGM: 4) CTC, TOP=OCR1A The maximum value of TCNT1 count = OCR1A. Once they are equal, TCNT1 is automatically cleared to 0 and restarts counting. The value of OCR1A remains unchanged unless the program modifies it. // desired value: 1046Hz One cycle time of 1046hz = high level time or low level time of 523hz // actual value: 1046.025Hz (0.0%) Note: The timing interval is the time of one cycle of the frequency void timer1_init(void) { TCCR1B = 0x00; //stop turns off the timer, and the timer is not supplied with a clock TCNT1H = 0xFC; //setup TCNT1L = 0x45; OCR1AH = 0x03; OCR1AL = 0xBB; TCCR1A = 0x00; //TCCR1B = 0x0A; //start Timer } #pragma interrupt_handler timer1_compa_isr:iv_TIM1_COMPA void timer1_compa_isr(void) { //compare occurred TCNT1=OCR1A //There is another way to initialize the timer. The signal is directly output from OC1A, so this interrupt is not needed. PORTC=PORTC^0xFF; //Each time an interrupt is entered, high and low levels are output in turn } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts XDIV = 0x00; //xtal divider XMCRA = 0x00; //external memory port_init(); timer1_init(); MCUCR = 0x00; EICRA = 0x00; //extended ext ints EICRB = 0x00; //extended ext ints EIMSK = 0x00; TIMSK = 0x10; //timer interrupt sources ETIMSK = 0x00; //extended timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } // void main(void) { unsigned char key_num; init_devices(); key_num=0; //delay_nms(5); //insert your functional code here... while(1) { if ((PINB&0x01)==0x00) { if ((PINB&0x01)==0x00); //Wait for key press to end OCR1A=tone_cnt[key_num]; key_num=(key_num+1)%7; //7 scales are played in turn TCCR1B = 0x0A; //Start the timer delay_nms(500); TCCR1B = 0x00; //Turn off the timer. If you need to accurately control the sounding time of each scale, you can use another timer to determine the opening and closing of this timer in the timer interrupt } } } ……………………
Previous article:SHT10 temperature and humidity sensor application, including microcontroller source code
Next article:Single chip computer full-field positioning system program
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- EEWORLD University Hall----Live Replay: Introduction to the Deep Learning Platform Based on TI Jacinto?
- What should I do if an IDE is warned about copyright issues?
- 【Qinheng】Evaluation board review and sharing——Summary of the problems I encountered
- About the boost and buck issues of DCDC chips
- Compatible with Raspberry Pi pin definition, adaptable to a variety of modules--Milan STM32MP1 development board demonstration
- A detail of MSP430 interrupt
- About "Coupled SEPIC"
- Schematic diagram - Is there any difference or consideration between choosing MOS and transistor for Q2 in this power switch circuit?
- ESP8266 adds support for VfsLfs1 and VfsLfs2
- TI - Signal Chain Design Considerations for Ultrasound Systems