Design of a simple electronic piano system based on a single chip microcomputer (program)

Publisher:星辰耀眼Latest update time:2022-04-21 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include  

#define uint unsigned int 

#define uchar unsigned char

#define output P1

#define input P1

//Easy to transplant to other circuit boards, my circuit uses P1 port as keyboard input interface

//Define 16 buttons

enum KEY{key0,key1,key2,key3,key4,key5,key6,key7,key8,key9,key10,key11,key12,key13,key14,key15};

uchar code table[]={   //array definition, display digital tube

    0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

    0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};

unsigned int code Freqtab[] = { //initial value of timing half cycle

     64021,64103,64260,64400, //Bass 3 4 5 6

     64524,64580,64684,64777, //Bass 7, Midrange 1 2 3

     64820,64898,64968,65030, // Middle 4 5 6 7

     65058,65110,65157,65178}; //treble 1 2 3 4

uint FreqTemp;

//The buzzer sound is not very good, so we use a speaker, connected to P3.7 port

sbit SPEAK = P3^7;

void delay1ms(uint n) //error 0us, delay n milliseconds

{

    uint a,b,c;

for(c=n;c>0;c--)

    for(b=199;b>0;b--)

        for(a=1;a>0;a--);

}

 

uint key_input(void)

{

uchar temp1, temp2; //temporarily save the read row or column

uchar num = 16; //Return the value of the key    

output = 0x0f;

    temp1 = input; //Read the line first

if(0x0f != temp1)    //button pressed

{    

delay1ms(5); //delay to eliminate jitter

temp1 = input; // read line again

if(0x0f != temp1) //Indicates that the button is pressed

{

temp1 = temp1 & 0x0f; //Retain the lower 4 bits

output = 0xf0; //Ready to read the column

delay1ms(1); //Delay to prevent the next sentence from being missed

temp2 = input; //Read the column directly

temp2 = temp2 & 0xf0; //Reserve the upper 4 bits

temp1 = temp1 | temp2; //Merge the values ​​of temp1 and temp2

}

}

switch(temp1)

{

case 0xee:num=0;break;  

        case 0xde:num=1;break;  

        case 0xbe:num=2;break;   

        case 0x7e:num=3;break; 

case 0xed:num=4;break;               

        case 0xdd:num=5;break;           

        case 0xbd:num=6;break;             

        case 0x7d:num=7;break;   

case 0xeb:num=8;break;    

        case 0xdb:num=9;break;    

        case 0xbb:num=10;break;   

        case 0x7b:num=11;break;    

case 0xe7:num=12;break;   

        case 0xd7:num=13;break;   

        case 0xb7:num=14;break;    

        case 0x77:num=15;break;      

default:break;

}

return num;

}

void T0_INT(void) interrupt 1

{

     TL0 = FreqTemp; //Load the initial value of the timing half cycle

     TH0 = FreqTemp >> 8;

     SPEAK = ~SPEAK; //pronunciation

int main()

{

TMOD = 0X01; //Timer T0 is set to mode 1

EA = 1;

ET0 = 1;

TR0 = 0;

while(1)

{

uint num,temp;

num = key_input();

temp = num;

P0 = table[num];

if(16 != num)

{

FreqTemp = Freqtab[num];

TR0 = 1;

while(0xf0 != output); //Detect button release

TR0 = 0;

SPEAK = 1;

}

}

return 0;

}

Reference address:Design of a simple electronic piano system based on a single chip microcomputer (program)

Previous article:Design of temperature and humidity monitoring system based on single chip microcomputer (program)
Next article:Design of digital clock system based on single chip microcomputer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

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号