【单片机】单片机演奏两只老虎

2019-04-15 12:46发布

#include #include "./delay/delay.h" #define SEGPORT P0 #define KEYPORT P1 sbit seg_select = P2^1; sbit bit_select = P2^0; sbit beep = P2^7; //unsigned char seg_data[16]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //unsigned char bit_data[8]= {0x0,0x0,0x0,0x0,0x0,0x0,0xbf,0x7f}; //unsigned char time[2] = {0}; int music[40] = {1,2,3,1,1,2,3,1,3,4,5,5,3,4,5,5,5,6,5,4,3,1,5,6,5,4,3,1,1,5,1,1,1,5,1,1,}; unsigned char High,Low; unsigned char code freq[][2] = { 0xD8,0xF7,//00440HZ 1 0xBD,0xF8,//00494HZ 2 0x87,0xF9,//00554HZ 3 0xE4,0xF9,//00587HZ 4 0x90,0xFA,//00659HZ 5 0x29,0xFB,//00740HZ 6 0xB1,0xFB,//00831HZ 7 0xEF,0xFB,//00880HZ `1 }; void timer0_isr(void) interrupt 1 { TH0 = High; TL0 = Low; beep = ~beep; } void timer0_init(void) { EA = 1; TMOD |= 0x01; TH0 = 0; TL0 = 0; ET0 = 1; TR0 = 1; } void main() { unsigned char num = 0; unsigned char i = 0; int sound = 0; timer0_init(); while(1) { sound = music[i]; i++; if(40 == i) { i = 0; } switch(sound) { case 1:num = 1;break; case 2:num = 2;break; case 3:num = 3;break; case 4:num = 4;break; case 5:num = 5;break; case 6:num = 6;break; case 7:num = 7;break; case 8:num = 8;break; default : num = 0;break; } if(num) { High = freq[num - 1][1]; Low = freq[num - 1][0]; TR0 = 1; delay_ms(200); delay_ms(200); delay_ms(200); } // else // { // TR0 = 0; // } } }