51单片机方波发生器

源代码在线查看: pwm.lst

软件大小: 18 K
上传用户: ghfchsu2
关键词: 51单片机 方波发生器
下载地址: 免注册下载 普通下载 VIP

相关代码

				C51 COMPILER V7.07   PWM                                                                   09/07/2010 13:40:22 PAGE 1   
				
				
				C51 COMPILER V7.07, COMPILATION OF MODULE PWM
				OBJECT MODULE PLACED IN PWM.OBJ
				COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE PWM.c BROWSE DEBUG OBJECTEXTEND
				
				stmt level    source
				
				   1          
				   2          //单片机产生方波、锯齿波、三角波程序
				   3          
				   4          #include 
				   5          
				   6          #define uchar unsigned char 
				   7          #define uint unsigned int 
				   8          
				   9          unsigned char x=0,m=0,y=50; 
				  10          
				  11          sbit WR_DA=P2^1; 
				  12          sbit CS_DA=P2^0;//DA与单片机的接口 
				  13          sbit S0=P3^2; //波形选择,每次按下将产生不同的波形 
				  14          sbit S1=P3^3; //频率减 
				  15          sbit S2=P3^4; //频率加 
				  16          sbit S3=P3^5; //调节方波的占空比 
				  17          sbit led=P2^2; //调节方波的占空比 
				  18          uchar code SinTab[256]={0x80,0x83,0x86,0x89,0x8d,0x90,0x93,0x96,0x99,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,0x
				             -b1,0xb4,0xb7,0xba,0xbc,0xbf,0xc2,0xc5 
				  19          
				  20          ,0xc7,0xca,0xcc,0xcf,0xd1,0xd4,0xd6,0xd8,0xda,0xdd,0xdf,0xe1,0xe3,0xe5,0xe7,0xe9,0xea,0xec,0xee,0xef,0xf1,
				             -0xf2,0xf4,0xf5 
				  21          
				  22          ,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
				             -0xff,0xfe,0xfd 
				  23          
				  24          ,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf2,0xf1,0xef,0xee,0xec,0xea,0xe9,0xe7,0xe5,0xe3,0xe1,
				             -0xde,0xdd,0xda 
				  25          
				  26          ,0xd8,0xd6,0xd4,0xd1,0xcf,0xcc,0xca,0xc7,0xc5,0xc2,0xbf,0xbc,0xba,0xb7,0xb4,0xb1,0xae,0xab,0xa8,0xa5,0xa2,
				             -0x9f,0x9c,0x99 
				  27          
				  28          ,0x96,0x93,0x90,0x8d,0x89,0x86,0x83,0x80,0x80,0x7c,0x79,0x76,0x72,0x6f,0x6c,0x69,0x66,0x63,0x60,0x5d,0x5a,
				             -0x57,0x55,0x51 
				  29          
				  30          ,0x4e,0x4c,0x48,0x45,0x43,0x40,0x3d,0x3a,0x38,0x35,0x33,0x30,0x2e,0x2b,0x29,0x27,0x25,0x22,0x20,0x1e,0x1c,
				             -0x1a,0x18,0x16 
				  31          
				  32          ,0x15,0x13,0x11,0x10,0x0e,0x0d,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x02,0x01,0x00,0x00,0x00,
				             -0x00,0x00,0x00 
				  33          
				  34          ,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02 ,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0d,0x0e,0x10
				             -,0x11,0x13,0x15 
				  35          
				  36          ,0x16,0x18,0x1a,0x1c,0x1e,0x20,0x22,0x25,0x27,0x29,0x2b,0x2e,0x30,0x33,0x35,0x38,0x3a,0x3d,0x40,0x43,0x45,
				             -0x48,0x4c,0x4e 
				  37          
				  38          ,0x51,0x55,0x57,0x5a,0x5d,0x60,0x63,0x66 ,0x69,0x6c,0x6f,0x72,0x76,0x79,0x7c,0x80 }; 
				  39          
				  40          void delayms(uint t) 
				  41          { 
				  42   1          uint i; 
				  43   1          while(t--) 
				  44   1          { 
				  45   2             for (i=0;i				C51 COMPILER V7.07   PWM                                                                   09/07/2010 13:40:22 PAGE 2   
				
				  46   2          } 
				  47   1      } 
				  48          
				  49          void delay(uint t) 
				  50          { 
				  51   1          uint i; 
				  52   1          for (i=t;i>0;i--); 
				  53   1      } 
				  54          
				  55          
				  56          void time0_init() 
				  57          { 
				  58   1          TMOD|=0x01;   //定时器设置 16位 
				  59   1          TH0=-50000/256; 
				  60   1          TL0=-50000%256;//初始化值 
				  61   1          ET0=1; 
				  62   1          TR0=1; 
				  63   1          EA=1; 
				  64   1      } 
				  65          
				  66          void time0_int(void) interrupt 1 
				  67          {  
				  68   1          TR0=0;//关闭定时器 
				  69   1          TH0=-50000/256; 
				  70   1          TL0=-50000%256;//重装初值 
				  71   1      
				  72   1      if(S0==0) 
				  73   1      { 
				  74   2           delayms(10);//按键消抖 
				  75   2           if(S0==0)
				  76   2                {
				  77   3                m++; 
				  78   3                  if(m==5)
				  79   3                          m=0;
				  80   3                  led=0;
				  81   3                }
				  82   2         while(!S0);//松手检测 
				  83   2      } 
				  84   1      
				  85   1      if(S1==0) 
				  86   1      { 
				  87   2           delayms(10); 
				  88   2           if(S1==0)
				  89   2                {
				  90   3                x=x+1; 
				  91   3                led=1;
				  92   3                }
				  93   2         while(!S1);//松手检测 
				  94   2      } 
				  95   1      
				  96   1      if(S2==0) 
				  97   1      { 
				  98   2           delayms(10); 
				  99   2           if(S2==0) x=x-1; 
				 100   2         while(!S2);//松手检测 
				 101   2      } 
				 102   1      
				 103   1      if(S3==0) 
				 104   1      { 
				 105   2           delayms(10); 
				 106   2           if(S3==0) y=y+5; 
				 107   2         while(!S3);//松手检测 
				C51 COMPILER V7.07   PWM                                                                   09/07/2010 13:40:22 PAGE 3   
				
				 108   2      } 
				 109   1      
				 110   1      if(y>254) y=0; 
				 111   1      if(m>5) m=0; 
				 112   1      if(x>50) x=0; 
				 113   1      TR0=1;//启动定时器 
				 114   1      } 
				 115          
				 116          void fangbo()    //方波 
				 117          {  
				 118   1         uchar i,j; 
				 119   1         j=255-y; 
				 120   1         CS_DA=0; 
				 121   1         WR_DA=0; 
				 122   1      for(i=y;i>0;i--) 
				 123   1      { 
				 124   2          P0=0; 
				 125   2          delay(x); 
				 126   2      } 
				 127   1      
				 128   1      while(j--) 
				 129   1      { 
				 130   2          P0=255; 
				 131   2          delay(x); 
				 132   2      }    
				 133   1      } 
				 134          
				 135          void juchi()    //锯齿波 
				 136          { 
				 137   1          uchar i; 
				 138   1          CS_DA=0; 
				 139   1          WR_DA=0; 
				 140   1      for(i=255;i>0;i--) 
				 141   1      {    
				 142   2           P0=i; 
				 143   2           delay(x); 
				 144   2      } 
				 145   1      
				 146   1      } 
				 147          void sanjiao()      //三角波 
				 148          { 
				 149   1      uchar i; 
				 150   1          CS_DA=0; 
				 151   1          WR_DA=0; 
				 152   1      for(i=0;i				 153   1      { 
				 154   2           P0=i; 
				 155   2           delay(x); 
				 156   2      } 
				 157   1      while(i--) 
				 158   1      { 
				 159   2          P0=i; 
				 160   2          delay(x); 
				 161   2      } 
				 162   1      } 
				 163          
				 164          void tixing()         //梯形波 
				 165          { 
				 166   1      uchar i=255,j,k; 
				 167   1      CS_DA=0; 
				 168   1      WR_DA=0; 
				 169   1      while(i--) 
				C51 COMPILER V7.07   PWM                                                                   09/07/2010 13:40:22 PAGE 4   
				
				 170   1      { 
				 171   2         P0=i; 
				 172   2         delay(x); 
				 173   2      } 
				 174   1      
				 175   1      for(j=0;j				 176   1      { 
				 177   2         P0=0; 
				 178   2         delay(x); 
				 179   2      } 
				 180   1      
				 181   1      for(k=0;k				 182   1      { 
				 183   2         P0=k; 
				 184   2         delay(x); 
				 185   2      } 
				 186   1      } 
				 187          
				 188          void sin()      //正弦波 
				 189          {  
				 190   1      uchar a;  
				 191   1      CS_DA=0; 
				 192   1      WR_DA=0; 
				 193   1      for(a=0;a				 194   1      { 
				 195   2         P0=SinTab[a]; 
				 196   2         delay(x); 
				 197   2      } 
				 198   1      } 
				 199          
				 200          main() 
				 201          { 
				 202   1      time0_init(); 
				 203   1      while(1) 
				 204   1      { 
				 205   2         switch(m) 
				 206   2         { 
				 207   3           case 0: 
				 208   3           fangbo();    //方波 
				 209   3           break; 
				 210   3           case 1: 
				 211   3           juchi();    //锯齿波 
				 212   3           break; 
				 213   3           case 2: 
				 214   3           sanjiao();   //三角波 
				 215   3           break; 
				 216   3           case 3: 
				 217   3           sin();    //正弦波 
				 218   3           break; 
				 219   3           case 4: 
				 220   3           tixing();    //梯形波 
				 221   3           break; 
				 222   3           default: fangbo(); 
				 223   3         } 
				 224   2      } 
				 225   1      } 
				 226          
				 227          
				 228          
				 229          //参考链接:http://www.picavr.com/news/2010-08/2297.htm
				
				
				C51 COMPILER V7.07   PWM                                                                   09/07/2010 13:40:22 PAGE 5   
				
				MODULE INFORMATION:   STATIC OVERLAYABLE
				   CODE SIZE        =    413    ----
				   CONSTANT SIZE    =    256    ----
				   XDATA SIZE       =   ----    ----
				   PDATA SIZE       =   ----    ----
				   DATA SIZE        =      3    ----
				   IDATA SIZE       =   ----    ----
				   BIT SIZE         =   ----    ----
				END OF MODULE INFORMATION.
				
				
				C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
							

相关资源