太白金星

kalimba

 
Overview

Project rendering:

 

kPFJdxuFH6RQoBumGvChZKFtTFa4lDxWQO82W3vA.png

 

Software source code

 

 

#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);
        }
    }
}

 

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-06-19 21:57:24
  • How to control the color of CTabCtrl border?
  • Request e-book
  • LED flickering problem
  • Why is the competition section so quiet recently?
  • Can the influence of the test cable be ignored?
  • EEWORLD University - NXP LPC80x Microcontroller Series Tutorials

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号