ATmega128 MCU syllable display simulation and source code

Publisher:SparklingMoonLatest update time:2019-10-30 Source: 51heiKeywords:ATmega128 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

          }


}

}





……………………



Keywords:ATmega128 Reference address:ATmega128 MCU syllable display simulation and source code

Previous article:SHT10 temperature and humidity sensor application, including microcontroller source code
Next article:Single chip computer full-field positioning system program

Latest Microcontroller Articles
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号