#include "stc15.h"
#define MAIN_Fosc 24000000 //Clock frequency 24M
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
sbit P_PWM = P1^4; //PWM pin
u16 PWM_high;
void Pin_Init()
{
P1 = P3 = P5 = 0xff;
P_PWM = 0;
P1M1 &= ~(1 << 4);
P1M0 |= (1 << 4);
}
u16 cur_tune = 0; //Used to record the current frequency
void PWM_SetTune(u16 tune)
{
cur_tune = tune;
if(tune == 0)
{
TR0 = 0;
PT0 = 0;
TMOD = 00;
TMOD = 0;
INT_CLKO = 0;
P1M0 = 0;
P_PWM = 0;
}
else
{
EA = 0;
PT0 = 1;
P1M0 |= (1 << 4);
TR0 = 0;
PT0 = 1;
TMOD &= ~0x03;
TMOD &= ~0x04;
INT_CLKO |= 0x01;
PWM_high =65535UL - 1000000UL/tune;
TH0 = (u8)(PWM_high >> 8);
TL0 = (u8)PWM_high;
TR0 = 1;
}
}
char last_value[13]= {1,1,1,1,1,1,1,1,1,1,1,1,1}; //Record the last key value of 13 keys
char now_value[13] = {1,1,1,1,1,1,1,1,1,1,1,1,1}; //Record the current key values of 13 keys
//The frequency corresponding to each key uses the five-tone interface
u16 tune[13] = {262, 294, 330,392,440,523, 587, 660,784,880, 1046,1174,1318};
void main()
{
int i = 0;
Pin_Init();
PWM_SetTune(0);
while (1)
{
now_value[0] = P10;
now_value[1] = P37;
now_value[2] = P11;
now_value[3] = P36;
now_value[4] = P12;
now_value[5] = P33;
now_value[6 ] = P13;
now_value[7] = P32;
now_value[8] = P15;
now_value[9] = P31;
now_value[10] = P54;
now_value[11] = P30;
now_value[12] = P55;
for(i=0; i<13; i++)
{
if(last_value[i] != now_value[i]) //
{
if(now_value[i] == 0)
{
PWM_SetTune(tune[i]);
}
else if (cur_tune == tune[i])
{
PWM_SetTune(0);
}
last_value[i] = now_value[i];
}
}
if( (last_value[0] == 1) &&
(last_value[1] == 1) &&
(last_value[2] == 1) &&
(last_value[3] == 1) &&
(last_value[4] == 1 ) &&
(last_value[5] == 1) &&
(last_value[6] == 1) &&
(last_value[7] == 1) &&
(last_value[8] == 1) &&
(last_value[9] == 1 ) &&
(last_value[10] == 1) &&
(last_value[11] == 1) &&
(last_value[12] == 1))
{
PWM_SetTune(0);
}
}
}
All reference designs on this site are sourced from major semiconductor manufacturers or collected online for learning and research. The copyright belongs to the semiconductor manufacturer or the original author. If you believe that the reference design of this site infringes upon your relevant rights and interests, please send us a rights notice. As a neutral platform service provider, we will take measures to delete the relevant content in accordance with relevant laws after receiving the relevant notice from the rights holder. Please send relevant notifications to email: bbs_service@eeworld.com.cn.
It is your responsibility to test the circuit yourself and determine its suitability for you. EEWorld will not be liable for direct, indirect, special, incidental, consequential or punitive damages arising from any cause or anything connected to any reference design used.
Supported by EEWorld Datasheet