⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_8_7__.c

📁 利用单片机系统产生不同频率的脉冲信号,再将信号放大,推动发声器件发声( 如果用蜂鸣器的话,注意要用无源蜂鸣器,有源蜂鸣器发声频率固定,不能使用)
💻 C
字号:
/*****************************************************
File name           : demo_8_7.c
Chip type           : ATmega16L
Program type        : Application
Clock frequency     : 1.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/
#include <mega16.h>
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x40
	.EQU __sm_mask=0xB0
	.EQU __sm_powerdown=0x20
	.EQU __sm_powersave=0x30
	.EQU __sm_standby=0xA0
	.EQU __sm_ext_standby=0xB0
	.EQU __sm_adc_noise_red=0x10
	.SET power_ctrl_reg=mcucr
	#endif

flash unsigned int t[9] = {0,956,865,759,716,638,568,506,470};
flash unsigned char d[9] = {0,105,116,132,140,157,176,198,209};
#define Max_note    32
flash unsigned char music[Max_note] = {5,2,8,2,5,2,4,2,3,2,2,2,1,4,1,2,1,2,2,2,3,2,3,2,1,2,3,2,4,2,5,8};

unsigned char note_n;
unsigned int int_n;
bit play_on;

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
    if (!play_on)
    {
        TCCR1B = 0x09;
    }
}

// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
    if (!play_on)
    {
        note_n = 0;
        int_n = 1;
        play_on = 1;
    }
    else
    {
        if (--int_n == 0)
        {
            TCCR1B = 0x08;
            if (note_n < Max_note)
            {
                OCR1A = t[music[note_n]];
                int_n = d[music[note_n]];
                note_n++;
                int_n = int_n * music[note_n];
                note_n++;
                TCCR1B = 0x09;
            }
            else
                play_on = 0;
        }
    }
}

// Declare your global variables here

void main(void)
{
    PORTD=0x08;
    DDRD=0x20;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: CTC top=OCR1A
// OC1A output: Toggle
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x40;
TCCR1B=0x08;
TCNT1 = 0x00;
ICR1H = 0x00;
ICR1L = 0x00;
OCR1A = 0x00;
OCR1B = 0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x10;

// External Interrupt(s) initialization
// INT0: Off
// INT1: On
// INT1 Mode: Falling Edge
// INT2: Off
GICR|=0x80;
MCUCR=0x08;
MCUCSR=0x00;
GIFR=0x80;

// Global enable interrupts
#asm("sei")

    while (1)
    {
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -