...

반응형




#include<reg51.h>

sbit speaker = P2^0;
unsigned char time_data, tone_data;
code unsigned char tone_tbl[] = {0x33,0x3d,0x3d,0x33,0x3d,0x4d,0x44,0x35,0x44,
        0x4d,0x3d,0x33,0x25,0x33,0x25,0x33,0x25,0x33,
        0x3d,0x33,0x44,0x39,0x3d,0x44,0x4d,0x00};
code unsigned char time_tbl[] = {18,8,8,8,8,16,16,8,8,8,8,16,12,4,12,4,8,8,16,16,8,8,8,8,16,0};


void delay(unsigned char i)
{
 while(i--);
}


void display(void)
{
 switch(tone_data)
 {
  case 0x4d:  
   P1 = 0xff;
   P3 = 0xfd;
   break;
  case 0x44:  
   P1 = 0xff;
   P3 = 0xfc;
   break;
  case 0x3d:  
   P1 = 0x7f;
   P3 = 0xfc;
   break;
  case 0x39:  
   P1 = 0x3f;
   P3 = 0xfc;
   break;
  case 0x33:  
   P1 = 0x1f;
   P3 = 0xfc;
   break;
  case 0x2d:  
   P1 = 0x0f;
   P3 = 0xfc;
   break;
   case 0x28:  
   P1 = 0x07;
   P3 = 0xfc;
   break;
   case 0x25:  
   P1 = 0x03;
   P3 = 0xfc;
   break;
   case 0x21:  
   P1 = 0x01;
   P3 = 0xfc;
   break;
   default:
   P1 = 0x00;
   P3 = 0xfc;
   break;
 }
}


void sound(void)
{
 do
 {
  speaker = 1;
  delay(tone_data);
  speaker = 0;
  delay(tone_data);
 }while(time_data);
}


void no_sound(void)
{
 speaker = 0;
 P1 = 0xff;
 P3 = 0xff;
 while(time_data);
}

void TMR0_int(void) interrupt 1
{
 time_data--;
 TH0 = 0x59;
 TL0 = 0x8b;
 return;
}

main()
{
 unsigned char i;

 TMOD = 0x01;
 TH0 = 0x59;
 TL0 = 0x8b;
 ET0 = 1;
 EA = 1;
 PT0 = 1;
 TR0 = 1;

 do
 {
  for(i=0;;i++)
  {
   tone_data = tone_tbl[i];
   time_data = time_tbl[i];
   if(time_data ==0) break;
   if(tone_data ==0) no_sound();
   else
   {
    display();
    sound();
   }
  }
 }while(1);
}


    

반응형