📄 sound.c
字号:
/* SOUND.C */
/****************************************************************************
*
* STK16XSW.SOUND
* ==============
*
* Signalausgabe auf Buzzer und Testpinn
*
* Es wird eine Tonfolge auf den Buzzer und eine Sinuswelle auf dem PWM Ausgang
* ausgegeben.
* Mit dem Potentiometer wird die Lautst鋜ke vom Buzzer und die Freuenz von
* der Sinusschwingung am PWM Ausgang eingestellt
*
* TQ-Systems GmbH
* ---------------
* Customer: TQ-Components
* Project : STK16XSW
* Tools : uVision 2.05
*
* Rev: Date: Name: Modification:
* ----+---------+----------------+------------------------------------------
* 100 02.05.01 A. Lichte Beginn
*****************************************************************************/
/****************************************************************************
*
* availability summary
*
* available for Starterkit: STK16xU
* conformed for Starterkit: STK16xU
* available for Module : TQM167UL TQM167U TQM167UE
*
* conformed for Module : TQM167UL TQM167U TQM167UE
*****************************************************************************/
/*==========================================================================*
* include files (INCLUDE):
*===========================================================================*/
/*--------------------------------------------------------------------------*
* standard include files:
*---------------------------------------------------------------------------*/
#include <ctype.h> /* Typecast functions */
#include <string.h> /* String functions */
#include <intrins.h> /* Intrinsic functions */
#include <reg167.h> /* Special function register */
#include <stdio.h>
/*--------------------------------------------------------------------------*
* project specific include files:
*---------------------------------------------------------------------------*/
#include "timer.h"
#include "stkreg_u.h"
#include "lcd_u.h"
/*==========================================================================*
* module internal definitions (DEFINE):
*===========================================================================*/
/*==========================================================================*
* module internal type definitions (TYPEDEF):
*===========================================================================*/
/*==========================================================================*
* module internal constants (CONST):
*===========================================================================*/
#define SINETABLE_LENTH 20
const char sint[SINETABLE_LENTH] = {0, 39, 75, 103, 121, 127, 121, 103, 75, 39, 0, -39,
-75, -103, -121, -127, -121, -103, -75, -39 };
// for the soundtable[]: f = 1/(Reloadvalue*400ns)
#define SOUNDTABLE_LENTH 4
#define SOUNDTABLE_SPEED 25 /* Time in 10ms between frequency switching */
const UINT soundtable[SOUNDTABLE_LENTH] =
{
1250, 1000, 833, 1000
};
/*==========================================================================*
* extern available constants (CONST):
*===========================================================================*/
/*==========================================================================*
* module internal variables:
*===========================================================================*/
/*==========================================================================*
* globale external available variables (EXTERN):
*===========================================================================*/
/*==========================================================================*
* module internal functions:
*===========================================================================*/
/*-------------------------------------------------------------------------*
* void sound_init()
*--------------------------------------------------------------------------*
* FT: Timer 7: counting from 0xFF00 to 0xFFFF for PWM output on P7.5 (CC29)
* Timer 8: counting from 0xFF?? to 0xFFFF for variable Frequency
* on P7.6 (CC30/Buzzer)
* EP: -
* RW: -
* GP: -
*--------------------------------------------------------------------------*/
void sound_init()
{
T7 = 0xFEFF; /* init T1 for counting from FF00 to FFFF (=256 steps) */
T7REL = 0xFEFF;
T78CON = 0x4040; /* init T7+T8 for max. counter frequency */
// tcount = 400ns Period (8Bit) = 102.4 祍
CCM7 |= 0x0070; // Activate Compare Mode 3 on Timer T7 for CC29 (PWM)
CCM7 |= 0x0F00; // Activate Compare Mode 3 on Timer T8 for CC30 (SOUND)
DP7 |= 0x0060; // Set Buzzer + PWM to Output
}
/*-------------------------------------------------------------------------*
* void sound_setfreq(UINT period, BYTE volume)
*--------------------------------------------------------------------------*
* FT: Set the Period of Timer 8 in 400 ns Steps to set the frequency for
* the Buzzer
* The Compare Value of CC30 is set to 50% of the Reloadvalue of Timer 8
* to achieve 50% duty cycle
* EP: period: timer Period [400ns]
* volume: Volume: 0 = min; 255 = max (50% Duty cycle)
* RW: -
* GP: -
*--------------------------------------------------------------------------*/
void sound_setfreq(UINT period, BYTE volume)
{
T8REL = 0xFFFF-period; // Set Period of T8
CC30 = 0xFFFF-(long)period*volume/0x200; // Set Compare value of CC30
}
/*-------------------------------------------------------------------------*
* void sound_setpwm(unsigned char pwm_value)
*--------------------------------------------------------------------------*
* FT: PWM control via capture compare (CAPCOM) register of port 7
* for the PWM output CC29
* EP: pwm_value = duty cycle (1..255)
* RW:
* GP: -
*--------------------------------------------------------------------------*/
void sound_setpwm(unsigned char pwm_value)
{
IEN = 0; // Deaktiviere Inrterrupts f黵 die gesamte Periode (14祍)
// Grund: Interrupts w黵den die Zeit zwischen Timer-Nulldurchgang
// und dem schreiben von CC29 verl鋘gern
// Sicher, einfach aber lange Interruptblockierung
while (T7 != 0xFFF8); // Wait until begin of the PWM Period
CC29 = (0xFFFF - pwm_value); /* load COMPARE register */
IEN = 1;
}
/*-------------------------------------------------------------------------*
* void main()
*--------------------------------------------------------------------------*
* FT: main function for PWM demo (3 demos will be performed)
* EP: -
* RW: -
* GP:
*--------------------------------------------------------------------------*/
void main()
{
UINT ui, adcval;
BYTE errval, sound_ptr, pwm_ptr;
TIMER_COUNTER tval;
timer_init(NULL);
sound_init();
stkreg_init();
if (errval=lcd_init()) printf("\nError: no LCD found (init step %d)\n", errval);
lcd_center(0,"DEMO FOR");
lcd_center(1,"ACOUSTICS / PWM");
ui=0;
sound_ptr=0;
pwm_ptr=0;
tval = timer_get_10ms(0);
ADCON = 0x80+0; /* select channel in ADC-control-register */
while (1)
{
// AD-Conversation ready?
if (!(ADCON&0x100))
{
adcval=(ADDAT&0x3FF);
ADCON = 0x80+0; /* select channel in ADC-control-register */
}
// Time for next Reloadvalue for the Buzzer?
if (timer_get_10ms(tval)>SOUNDTABLE_SPEED)
{
tval+=SOUNDTABLE_SPEED;
sound_setfreq(soundtable[sound_ptr++], adcval>>2);
if (sound_ptr>=SOUNDTABLE_LENTH) sound_ptr=0; // correct pointer overflow
}
// Next PWM Value
sound_setpwm(sint[pwm_ptr++]+128);
if (pwm_ptr>=SINETABLE_LENTH) pwm_ptr=0;
// Wait lit
timer_delay_20us(0x0105-(adcval>>2));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -