PIC蜂鳴器教程
阿新 • • 發佈:2021-11-24
專案目標:編制一個演奏樂曲的程式,並同時使用LED打節拍
開啟MCC 進行配置 具體的配置如下(MCC配置教程在下面)
PWM6
TMR0
TMR2
中斷
IO
系統時鐘
基礎設定之後修改main.c檔案
開啟MCC 進行配置 具體的配置如下(MCC配置教程在下面)
PWM6
TMR0
TMR2
中斷
IO
系統時鐘
基礎設定之後修改main.c檔案
/** Generated Main Source File Company: Microchip Technology Inc. File Name: main.c Summary: This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs Description: This header file provides implementations for driver APIs for all modules selected in the GUI. Generation Information : Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.7 Device : PIC16F18446 Driver Version : 2.00 */ /* (c) 2018 Microchip Technology Inc. and its subsidiaries. Subject to your compliance with these terms, you may use Microchip software and any derivatives exclusively with Microchip products. It is your responsibility to comply with third party license terms applicable to your use of third party software (including open source software) that may accompany Microchip software. THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. */ #include "mcc_generated_files/mcc.h" /* Main application */ #define timer_factor 1.0016 //calibrate the offset of the timer. #define timer_2_PR_counter 64 //PR counter for timer2 is 64us. it means when PR register is 0x00, //the period of timer2 is 64us. //C、D、E、F、G、A、B分別對應簡譜1234567 #define C 260 #define D 293 #define E 329 #define F 349 #define G 392 #define A 440 #define B 494 #define H_C 523 int i =0; int j =0; int flag = 0; unsigned int a[14] = {C,D,E,F,G,A,B,H_C,B,A,G,F,E,D}; // just simple notes //unsigned int a[15] = {C,10000000,E,10000000,G,10000000,B,H_C,10000000,A,G,F,10000000,D,C}; void frequency_change(void) //change the frequency of tmr2 every 500ms/1beat { TMR2_LoadPeriodRegister(a[i]); i++; if(i == 14) { i=0; } IO_RA1_Toggle(); //LED toggle to simulate the beat IO_RA0_Toggle(); } void main(void) { SYSTEM_Initialize(); TMR2_LoadPeriodRegister(a[i]); //first set the C note to let the buzzer not scream. for(int k=0; k<=13; k++) { a[k] = (1000000/a[k]*timer_factor)/64; //this result a[k] is the values we need to write into PR Register. //e.g: 260Hz, so the period is 3.846ms, so the PR Register is } //1000000/260/1.0016/64 = 60 = 0x3C. INTERRUPT_GlobalInterruptEnable(); TMR0_SetInterruptHandler(frequency_change); //Timer0 interrupt function; using this one to change the //frequency of the timer2, as well as the period of PWM. TMR0_StartTimer(); TMR2_StartTimer(); while (1) { // Add your application code } } /** End of File */