⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctest.c

📁 单片机98c51的一些程序代码
💻 C
字号:

//#include <AT89X51.H>
#include <REG51.H>
#include <stdio.h>

sbit    BEEP=P2^7; 
unsigned char th0_f; //在中断中装载的T0的值高8位
unsigned char tl0_f; //在中断中装载的T0的值低8位

void dems(unsigned int r5);

//----------------------------------
void delay400ms(void)
{
  unsigned char TempCycA = 5;
  unsigned int TempCycB;
  while(TempCycA--)
  {
  TempCycB=1000;//2000;//7269;
  while(TempCycB--);

  }
}

//----------------------

//定时中断0,用于产生唱歌频率
timer0() interrupt 1 
{
    TL0 =tl0_f ;TH0 =th0_f;  //调入预定时值
    BEEP =~BEEP;           //取反音乐输出IO
}

void delay(unsigned char times)
{unsigned char t,i =times;
  for(;i!=0;i--)
	for(t=500;t!=0;t--)
	;
 }

void twotager(void)
{
      unsigned char yuepu[33] ={0x44 ,0x54 ,0x64 ,0x44 ,
                                0x44 ,0x54 ,0x64 ,0x44 ,
                                0x64 ,0x74 ,0x88 ,
                                0x64 ,0x74 ,0x88 ,
                                0x82 ,0x92 ,0x82 ,0x72 ,0x64 ,0x44 ,
                                0x82 ,0x92 ,0x82 ,0x72 ,0x64 ,0x44 ,
                                0x44 ,0x84 ,0x48 ,
                                0x44 ,0x14 ,0x48 ,
                                0x00 };
      unsigned int yingao[15] ={64260, 64400, 64524, 64580,
                                64684, 64777, 64820, 64898,
                                64968, 65030, 65058, 65110,
                                65157, 65178, 65217 };
                                
unsigned int templ =0,temph =0,flag = 0; //定义循环用的变量
unsigned int b,loop,c =0,end = 30000;//loop1,loop2,loop3,
unsigned char p2k =0x00;
TMOD = 0x01;
TR0 = 0;
ET0 = 1;
EA = 1;
do{
  c++;
  dems(4);
  for (b=0; yuepu[b]!=0 ;b++) //b<33; b++) //逐个符号演奏
    {//TR0 =0;
     templ = yuepu[b] & 0x0f ;
     temph = yuepu[b] & 0xf0 ;
     temph = temph>>4;
     temph--;
     
     th0_f = (yingao[temph] ) / 256  ;
     tl0_f = (yingao[temph] ) % 256  ;
     //th0_f = 0xf8 ;
     //tl0_f = (65260 / 256)  ;
     //th0_f = (65260 % 256)  ;
     //templ = 4;
     TL0 = tl0_f;
     TH0 = th0_f;
     TR0 = 1;
     
     for(loop = 1;loop <= templ -1; loop++)
	   dems(templ );
     //delay(templ)  ;
     //for(loop = 1;loop<=templ  ; loop++)
     //   for(loop1=0;loop1<2 ;loop1++)
     //     for(loop2=0;loop2<125 ;loop2++)
     //       for(loop3=0;loop3< 248; loop3++)
                     ;
    TR0 =0;
    //dems();
	delay(2);
       }
   }while(c < 5);  
//TR0 = 0;    
}

//----------------------

void zyy(void)
{
 
  unsigned char p1k = 0xFE;
  unsigned char sbuf1 = 0x01;
  unsigned int derection = 1,count = 0,loop = 0,zy = 0;
 
  P1 = 0xff;
  
  while(zy < 5 )
  {
   
    P1 = 0xff;
    SBUF = sbuf1;	
    while(!TI) 
     ;
    TI = 0;
    P1 = 0xbf;
   
    if (derection == 1 )
       sbuf1 = sbuf1 << 1;
    else
       sbuf1 = sbuf1 >> 1;
    
    if (count == 8 )
      { count = 0;
        loop++;
        if (derection == 1 )
           sbuf1 = 0x01;
       	else
           sbuf1 = 0x80;
      }
         
    if (derection == 1 && loop >= 6)
       {sbuf1 = 0x01;
       	count = 0;loop = 0;zy++;
       	derection = 0;}
    else
       if(derection == 0 && loop >= 6)
         {sbuf1 = 0x80;
       	  count = 0;loop = 0;
       	  derection = 1;   }	
    
    count++ ;
    
    delay400ms();
   }
}

void hua(void)
{
//定义花样数据
const unsigned char design[32]={0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,
								0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF,
								0xFF,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x0,
								0xE7,0xDB,0xBD,0x7E,0xFF};
unsigned int a; //定义循环用的变量
unsigned int b,c =0;
do{
  c++;
  for (b=0; b<32; b++)
    {
     P1 = 0xff;
     SBUF = design[b];
     while(!TI) 
         ;
     TI = 0;P1 = 0xbf;
     for(a=0; a<30000; a++)
      {	
        ;//P1 = design[b];
	   }
     
     }
}while(c < 10);
}

void hello(void)
{
//unsigned char p1k[5] = {0xFd,0xFb,0xF7,0xeF,0xdF};
unsigned char p1k[5] = {0xbd,0xbb,0xb7,0xaF,0x9F};
const unsigned char design[5]={0x6e,0x9E,0x1c,0x1c,0xFc};
unsigned int a; //定义循环用的变量
unsigned int b,c =0,end = 30000;
do{
  c++;
  for (b=0; b<5; b++)
    {
     P1 = 0xff;
     SBUF = design[b];
     while(!TI) 
         ;
     TI = 0;P1 = p1k[b];
     
     end = 30000;
             
     for(a=0; a<end; a++)
      {	
        ;//P1 = design[b];
	   }
    //p1k = 0xFE; 
     }
  
    
}while(c < 5);

do{
  c++;
  for (b=0; b<5; b++)
    {
     P1 = 0xff;
     SBUF = design[b];
     while(!TI) 
         ;
     TI = 0;P1 = p1k[b];
     
     end = 10;
        
     for(a=0; a<end; a++)
      {	
        ;//P1 = design[b];
	   }
     }
}while(c < 2000);
}

void main(void)
{ while(1)
   {//hua();
	//delay400ms();
	//hello();
	//delay400ms();
	//zyy();
	//delay400ms();
	twotager();
	delay400ms();
   }
}

void dems(unsigned int r5)
{ 
	//#pragma asm
    //delay:  mov r7,#02h
    //d2:     mov r4,#125
    //d3:     mov r3,#248
    //        djnz r3,$
    //        djnz r4,d3
    //        djnz r7,d2
    //        djnz r5,delay
    //  end
   //#pragma endasm;
 unsigned char r7,r3,r4,rr;
  for(rr=r5; rr!=0;rr--)
	for(r7=2; r7!=0; r7--)
    	for(r4=125; r4!=0; r4--)
    	   for(r3=248; r3!=0; r3--)	  
                    ;
 }




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -