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

📄 power_control.c

📁 很好的单片机控制继电器代码
💻 C
字号:
#include <reg52.h>
#include<string.h>
#include<stdio.h>

#define CONTROL 'B'   //控制器
sfr  WDT_CONTR=0xE1;
sbit OE=P3^7;


unsigned char code sbuf_sign[3]={'P','K','Z'};


unsigned char flag_se,time_count,sbuf_data[6],state_h,state_l;


unsigned char rx_char()//接受串口数据
{
while(RI!=1);
RI=0;
return(SBUF);
}

tx_char(unsigned char ch)//发送字节
{

	SBUF=ch;
    while(TI==0);
    TI=0;   
}

//延迟
void delay(unsigned int num)
{
unsigned char j;
while(num!=0)
{
  num--;
  for(j=0;j<200;j++);
}
}


tx_str(unsigned char *str)//发送字符串
{
unsigned int i;
for(i=0;i<strlen(str);i++)
tx_char(str[i]);
}


/*串口初始化函数*/
void initial_UART()
  {
    TR1=0;
    SCON=0x50;          /*方式1,8位UART,1位起始位,1位停止位;允许接收 */                  
	TL1=0XFD;
	TH1=0XFD;            
	TR1=1;              /*定时器1启动*/
  }

#if 0
//校验和
bit sum_check(char * s1)
{
 bit ok;
 unsigned char m;
 unsigned char temple=sbuf_sign[0]+sbuf_sign[1]+sbuf_sign[2];
 for(m=0;m<5;m++)
   {
   temple=temple+*s1;
   s1++;
   }
//      tx_char(temple);
   ok=temple-*s1;
   return(ok);
}


void array_reversal()//颠倒P0口状态顺序
{
  unsigned char  i,p;  
  p=P0;
  state_l=0;
  for(i=0;i<8;i++)
  {
    if(p>=0x80)
    {
      state_l=state_l+(0x01<<i);  
    }
    p=p<<1;  
  }
}
void tx_fun()
{
   unsigned char sum;
   array_reversal();    //P0口状态
   state_h=P1|0xc0;     //P1口状态,高位置1
   sum='P'+'K'+'Z'+state_h+state_l;
   tx_str("PKZ");
   tx_char(state_h);
   tx_char(state_l);
   tx_char(sum);

}

#endif
//串口中断
#if 0

void int_uart() interrupt 4 
{
  unsigned char sum,j;
  ES=0;
  time_count=0;
  if(flag_se<3)
  {
    sbuf_data[flag_se]=rx_char();
    if(sbuf_data[flag_se]!=sbuf_sign[flag_se])
      flag_se=0;
    else flag_se++;
  }
  else
  {
    sbuf_data[flag_se-3]=rx_char();//接受数据
    flag_se++;
  }
  if(flag_se==9)
  {
    flag_se=0;
    if(sum_check(sbuf_data)==0)//校验和
    {
//  	  if(sbuf_data[0]==CONTROL)
//	  {
	     if(sbuf_data[1]=='1')//查询功能
         {
           array_reversal();    //P0口状态
		   state_h=P1&0x3f;     //P1口状态,高位置0
           sum='P'+'K'+'Z'+state_h+state_l;
		   tx_str("PKZ");
		   tx_char(state_h);	
		   tx_char(state_l);	
	   	   tx_char(sum);	
		 }
		 else if(sbuf_data[1]=='0')
		 {
		    if(sbuf_data[2]=='1')//断开所有继电器
		    {

               for(j=0;j<14;j++)
               {
                 if(j<8)
                   P0=P0&0xff>>(j+1);
                 else 
                   P1=P1&0xff<<(j-7);
                 tx_fun();
                 WDT_CONTR=0x3f;
                 delay(200);
				 delay(200);
                 
               }			 
		    }
		    else if(sbuf_data[2]=='0')
		    {
              if(sbuf_data[3]<0x09)
              {
                j=0x80>>(sbuf_data[3]-1);
                if(sbuf_data[4]=='1')
                  P0=P0|j;
                else if(sbuf_data[4]=='0')
                  P0=P0&(0xff-j);
              }
              else if(sbuf_data[3]<0x0f)
              {
                j=0x01<<(sbuf_data[3]-9);
                if(sbuf_data[4]=='1')
                  P1=P1|j;
                else if(sbuf_data[4]=='0')
                  P1=P1&(0xff-j); 
              }
			}
           tx_fun();	 
		 }

//      }
    }  

  }
  ES=1;
}

#else

void int_uart() interrupt 4 
{
  ES=0;
  time_count=0;
  if(flag_se<3)
  {
    sbuf_data[flag_se]=rx_char();
    if(sbuf_data[flag_se]!=sbuf_sign[flag_se])
      flag_se=0;
    else flag_se++;
  }
  else
  {
    sbuf_data[flag_se-3]=rx_char();//接受数据
    flag_se++;
  }

  if (flag_se==4 )
  { 
     flag_se=0;
     if(sbuf_data[0]=='1')
	 {
	  P0=0x00;
	  P0=P0|0x03;//00000000和00000011或运算结果为00000011  前刘个灯亮 后两个灭
	 }
     else
	 {
	 //P0=0x00;
     P0=P0&0xfc;//00000000和11111100与运算结果为00000000   八个灯都亮
	 }

  }
  
  ES=1;
}



#endif

void main()
{


P0=0x00;
P1=0x00;
//初始化串口
TMOD=0x21; 
initial_UART();
//初始化端口
EA=1;
OE=0;
flag_se=0;
while(1)
{
  ES=1;
  for(time_count=0;time_count<100;time_count++)//计时结束命令,关掉灯
  {
   TR0=1;
   while(TF0==0);
   TR0=0;
   TF0=0;
   WDT_CONTR=0x3f;
  }
  if(time_count==100)
  {
    flag_se=0;
    time_count=0;
  }

}

}

⌨️ 快捷键说明

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