1895 views|0 replies

6587

Posts

0

Resources
The OP
 

Programming examples to learn DSP timer and interrupt system [Copy link]

Example 1: Use a buzzer to simulate basic sound levels
1.1 Working principle of a buzzer
The principle of a buzzer is that current passes through an electromagnetic coil, which generates a magnetic field to drive the vibration membrane to make a sound. Therefore, a certain current is required to drive it. The current output by the IO pin of the microcontroller is small, and the TTL level output by the microcontroller basically cannot drive the buzzer. Therefore, a current amplification circuit is required. The experimental board uses a transistor Q1 to amplify and drive the buzzer.

1.2 Classification of Buzzers
Buzzers are divided into two types: active and passive, depending on whether they have a signal source. An active buzzer only needs to add a rated DC voltage to its power supply end, and its internal oscillator can generate a fixed frequency signal to drive the buzzer to make a sound. A passive buzzer can be understood as a speaker, which needs to add a high and low constantly changing electrical signal to its power supply end to drive it to make a sound. The driving methods of active and passive are slightly different. Let's first explain the driving method of active buzzers. Because active buzzers contain a signal source, they can make a fixed frequency sound as long as the rated working voltage is added. For passive buzzers, it is more complicated to drive them to make a sound, because they do not have a signal source themselves. Therefore, just turning on the power supply will not make a sound. It is necessary to repeat "power on-off" to make it make a sound. We can write a program to control the GPIO port to continuously set to high level-low level-high level..., so that the buzzer can be continuously powered on and off to make a sound. The different times of power on and power off are equivalent to different oscillation periods, so sounds of different frequencies can be obtained.

1.3 Buzzer hardware circuit diagram
As can be seen from the circuit diagram below, the high and low level input signal interface for controlling the buzzer is GPIO7. By controlling the different opening and closing times of GPIO7, the buzzer can be controlled to obtain sound signals of different frequencies.

1.4 How does DSP output group frequency?
In order to make DSP emit sounds of different frequencies, timer interrupt is used to calculate the delay time. Different timing values can be realized by simply presetting the timer. Take standard pitch A as an example: the frequency of A is f=440Hz, and its corresponding period is: T=1/f=1/440=2272us.

The waveform diagram of the DSP-controlled buzzer is to output a fixed frequency waveform by cyclically setting and clearing the GPIO port. As shown in the figure, T=2272us relative to the A-frequency of 440Hz, then t=T/2=2272/2=1136us. Therefore, we only need to set the GPIO to a high level in the program, delay for 1136us, then set it to a low level, delay for 1136us, and repeat this cycle to get a sound with a frequency of 440Hz.


1.5 Code Examples
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
/****************Port Macro Definition********************/
#define BUZZ_CLK_GENER GpioDataRegs.GPATOGGLE.bit.GPIO7 = 1; //Buzzer control IO level flip
#define BELL_DIR GpioCtrlRegs.GPADIR.bit.GPIO7 //Define IO output direction
#define BELL_DAT GpioDataRegs.GPADAT.bit.GPIO7 //Define IO register value
#define DISABLE_TIMER1_INT IER &= 0xFFFE; //CPU interrupt enable flag clear
#define ENABLE_TIMER1_INT IER |= M_INT1; //CPU interrupt enable flag enable
#define BUZZ_OFF GpioDataRegs.GPACLEAR.bit.GPIO7 = 1; //IO output clear
Uint16 Musi[23]=
{ //Unit is us. At different frequencies, the buzzer emits different tones.
0,
3816, //L_do
3496, //L_re
3215, //L_mi
2865, //L_fa
2551, //L_so
2272, //L_la
2024, //L_xi
1912, //do
1703, //re
1517, //mi
1432, //fa
1275, //so
1136, //la
1013, //xi
956, //H_do
851, //H_re
758, //H_mi
716, //H_fa
638, //H_so
568, //H_la
506, //H_xi
0xFF //STOP
};
Uint16 Song[]={1,2,3,4,5,6,7}; //Music score: do,re,mi,fa,so,la,xi

/****************Function declaration*******************/
void Init_Bell(void);
interrupt void cpu_timer0_isr(void);
void Delay(Uint16 t);
void main(void)
{
Uint16 addr=0;
Uint16 k;
InitSysCtrl(); // Initialize system control:
BELL_DAT=0; //Set the port to low
levelInit_Bell(); //Set the buzzer port output
DINT; // Disable CPU global interruptsInitPieCtrl
(); // Initialize PIE control registers to their default states, that is, disable PIE interrupts and clear all PIE interrupt flagsIER
= 0x0000; // Disable CPU interrupts and clear all CPU interrupt flagsIFR
= 0x0000;
InitPieVectTable(); //Initialize the PIE interrupt vector table and point it to the interrupt service routine (ISR)
EALLOW; //Remove register protectionPieVectTable.TINT0
= &cpu_timer0_isr;// The interrupts in this example are remapped to the interrupt service subroutine in this fileEDIS
; // Add register protectionInitCpuTimers
(); // Initialize on-chip peripherals. In this example, only the CPU timer needs to be initializedConfigCpuTimer
(&CpuTimer0, 150, 1000000);// Configure CPU timer 1 to interrupt once per
secondStartCpuTimer0(); // Start timerIER
|= M_INT1; // Enable CPU interrupt line INT1, connected to CPU timer 0;
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;// Enable PIE group 1 interrupt 7, TINT0 interrupt;
EINT; // Enable global interrupt INTM
ERTM; // Enable global real-time interrupt DBGM
for(k=0;k<7;k++) // Step 6. The interrupt controls the switching frequency of the buzzer. The delay function defines the rhythm. The 7 cycles make the buzzer sound 7 different tones:
{
StopCpuTimer0(); //Stop countingDISABLE_TIMER1_INT
; //Disable timer
interruptConfigCpuTimer(&CpuTimer0, 150, Musi[Song[addr]+14]/2); //Set the timing time according to the frequency of the syllableStartCpuTimer0
(); //Restart the
timerENABLE_TIMER1_INT; //Enable timer interruptDelay
(8); //Music beat
delayStopCpuTimer0(); //Stop countingDISABLE_TIMER1_INT ; //Disable timer interruptBUZZ_OFF; //
Turn off
the buzzerDelay
(2); //Music pauseaddr
++; //Continuously send out music scores: do,re,mi,fa,so,la,xi
}
while(1);
}

/*-----------------------------------------*/
/*Formal parameter: void */
/*Return value: void */ /
*Function description: Initialize the buzzer port as output */
/*-----------------------------------------*/
void Init_Bell(void)
{
EALLOW; //Remove register protection
BELL_DIR=1;//Bell port output
EDIS; //Register protection
}
/*------------------------------------------*/
/*Formal parameter: void */
/*Return value: void */
/*Function description: Timer CPU0 interrupt service subroutine */
/*------------------------------------------*/

interrupt void cpu_timer0_isr(void)
{
CpuTimer0.InterruptCount++;
BUZZ_CLK_GENER; //GPIO port output level flip
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; //PIEACK flag is set to tell PIE that it can accept other interrupts in this group
}

void Delay(Uint16 t) //Delay function
{
Uint32 i=0;
Uint32 gain = 300000;
Uint32 base=0;
base=gain*t;
for(i=0;i<=base;i++);
}

This post is from DSP and ARM Processors
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>
Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list