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

📄 dd.c

📁 PWM电机正反转C程序!包括电路图!很受欢迎的!
💻 C
字号:
#include <reg52.h>
#include <intrins.h>
#include <math.h>
#include <string.h>
#include"AT24c02.h"
#include"MAX7219.h"

#define uchar unsigned char
#define uint unsigned int 
#define num_circle 10   	  //圈数
#define second     2          //秒

sbit motor1=P1^5;
sbit motor2=P1^6;
sbit bee=P1^7;
sbit start_key=P1^3;
sbit stop_zero_key=P1^4;

uint c;
uchar twice=0,num50ms=0;
bit start_flag=1,circle_flag=0,cry_flag=1;

void circle_read(void)
{
   c=read_random(0x01)*256+read_random(0x00);
}

void circle_write(uint cir)
{
   write_byte(0x00,cir%256); write_byte(0x01,cir/256);
}

void Delay1ms(uint count)
{
	uint i,j;
	for(i=0;i<count;i++)
	for(j=0;j<120;j++);
}

void buzzer(uchar b)
{	
   uchar k;
   for(k=0;k<b;k++)
   {  
      bee=0;
      Delay1ms(50);
	  bee=1;
      Delay1ms(50);
   } 
}
void McuInitial(void)
{
   InitialMax7219();
   TMOD=0x01;
   TH0=0X4C;
   TL0=0;
   EA=1;
   ET0=1;
   start_flag=1;                                                                                
   circle_read();
   display(c);

   buzzer(1);
}

main()
{
   McuInitial();
   while(1)
   {
///////////////////////////键1继续///////////////////////////////////////////////////////	  
	  if(start_key==0&&start_flag==1)
		{
		   Delay1ms(10);
		   if(!start_key)
		   {
		      start_flag=0;
			  cry_flag=1;
		      buzzer(1);
			  circle_read();
		      if(c>=num_circle)
		      {buzzer(3);motor1=1;motor2=1;}
		      else
		      {TR0=1;motor1=1;motor2=0;}
		   }
		}
////////////////////////////键2暂停并清零///////////////////////////////////////////////////////
		 if(!stop_zero_key)
		 {
		    Delay1ms(10);
			if(!stop_zero_key)
			{
			   TR0=0;
			   motor1=1;motor2=1;
		       start_flag=1;
			   cry_flag=1;
			   buzzer(1);
			   Delay1ms(2000);
		       if(!stop_zero_key)
		         {
		            fill_byte(0);

                    circle_read();
                    display(c);

				    buzzer(1);
				    while(!stop_zero_key);
		         }
			}
		 }
///////////////////////////每圈显示,保存数据//////////////////////////////////////////////////////
        if(circle_flag)
	     {
		    circle_read();
			c++;
			circle_write(c);
			circle_read();
			display(c);
		    circle_flag=0;
	     }
////////////////////////////判断是否计数到////////////////////////////////////////////////////////
        circle_read();
		if(c>=num_circle)
		{
		   if(cry_flag)
		   {
		     TR0=0;
		     motor1=1;motor2=1;
		     start_flag=1;
		     buzzer(50);
			 cry_flag=0;
		    }
		   
		}
//////////////////////////////////////////////////////////////////////////////////////////////////
	}
}

void time0(void)interrupt 1 using 1
{
   num50ms++;
   TH0=0X4C;
   TL0=0;
   if(num50ms==20*second)
   {
     motor1=~motor1;
	 motor2=~motor2;
	 twice++;
	 num50ms=0;
   }
   if(twice==2)
   {
     circle_flag=1;
	 twice=0;
   }
}

⌨️ 快捷键说明

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