#include <reg51.h>
#include <intrins.h>
#include<math.h>
#include"stdlib.H"
#include"stdio.H"
#define uchar unsigned char
#define uint unsigned int
/**********端口分配(根据控制对象命名)*******************************/
sbit A1=P1^0; //
sbit B1=P1^1; //
sbit C1=P1^2; //
sbit D1=P1^3; //
sbit S=P1^4; //
sbit Q=P1^5; //
sbit UP_HUM=P1^6; //
sbit DN_HUM=P1^7; //
sbit UP_HIGH=P3^4; //
sbit DN_HIGH=P3^5; //
sbit DUAN=P2^6;
sbit WEI=P2^7;
sbit LED_R=P3^6;
sbit LED_G=P3^7;
sbit A_MOTOR=P2^0; //
sbit B_MOTOR=P2^1; //
sbit C_MOTOR=P2^2; //
sbit D_MOTOR=P2^3; //
sbit E_MOTOR=P2^4; //
sbit F_MOTOR=P2^5; //
/**********变量定义*******************************/
#define pwm_max 10
uchar pwm_motor[4]={3,3,3,3};//电机初始速度
uchar HUM_TMP = 17; //土壤湿度
uchar HIGH_TMP = 50; //高度
char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};//数码管段码表
#define start_led_g
/*********定义延时1ms子程序************************/
void delay1ms(uint t)//1ms延时子程序
{
uint k,j;
for(k=0;k<t;k++)
for(j=0;j<120;j++);
}
void motor_stop(void)//电机停止子程序
{
A_MOTOR=0;
B_MOTOR=0;
C_MOTOR=0;
D_MOTOR=0;
E_MOTOR=0;
F_MOTOR=0;
}
void seg_close(void)//数码管关闭子程序
{
P0 = 0;
DUAN = 1;
DUAN = 0;
WEI = 1;
WEI = 0;
}
void seg_dis(uchar dat1,uchar dat2)//数码管显示子程序
{
uchar i;
uchar dis[8];
dis[0]=0xff;
dis[1]=0xff;
dis[2]=table[dat1%100/10];
dis[3]=table[dat1%10];
dis[4]=0xff;
dis[5]=table[dat2/100];
dis[6]=table[dat2%100/10];
dis[7]=table[dat2%10];
for(i=0;i<8;i++)
{
P0 = dis[i];
DUAN = 1;
DUAN = 0;
P0=0X01<<i;
WEI = 1;
WEI = 0;
delay1ms(2);
P0= 0;
WEI = 1;
WEI = 0;
}
}
void led_close(void)//led关闭子程序
{
LED_G =0;
LED_R = 0;
}
/*************定时器初始化程序*******************/
void timer_init(void)
{
TMOD=0x10; //设置定时器1为16位模式
TH1=0xFc; //1000us定时初值
TL1=0x18;
ET1=1; //定时器1中断开
EA=1; //总中断开
}
/***********定时器1中断服务程序*******************/
void timer1(void) interrupt 3
{
static uchar count = 0;
static uint count1 = 0;
TH1=0xFc; //1000us定时初值
TL1=0x18;
count++;
//电机A控制PWM
if(count==pwm_motor[0])
A_MOTOR=0;
if(count==pwm_max)
A_MOTOR=1;
//电机B控制PWM
if(count==pwm_motor[1])
B_MOTOR=0;
if(count==pwm_max)
B_MOTOR=1;
//电机C控制PWM
if(count==pwm_motor[2])
C_MOTOR=0;
if(count==pwm_max)
C_MOTOR=1;
//电机D控制PWM
if(count==pwm_motor[3])
D_MOTOR=0;
if(count==pwm_max)
D_MOTOR=1;
if(count==pwm_max)
count=0;
//S弹开后进入10s倒计时
if(S==1)
{
count1++;
if(count1>=10000)
{
count1=0;
LED_R = 0;
motor_stop();
TR1 = 0;
}
}
}
/**********************主函数*********************/
void main()
{
motor_stop();
seg_close();
led_close();
timer_init(); //定时器初始化程序
while(1)
{
if(S==0)//S开关闭合
{
TR1=1;
LED_R=1;
if(A1==0)//A1开关闭合
pwm_motor[0]=10;
else
pwm_motor[0]=3;
if(B1==0)//A1开关闭合
pwm_motor[1]=10;
else
pwm_motor[1]=3;
if(C1==0)//A1开关闭合
pwm_motor[2]=10;
else
pwm_motor[2]=3;
if(D1==0)//A1开关闭合
pwm_motor[3]=10;
else
pwm_motor[3]=3;
if(Q==0)//Q开关闭合
{
E_MOTOR=1;
F_MOTOR=1;
LED_G=1;
}
else
{
LED_G = 0;
E_MOTOR=0;
F_MOTOR=0;
}
seg_dis(HUM_TMP,HIGH_TMP);
//按键调节HUM_TMP
if(!UP_HUM)
{
while(!UP_HUM);
if(HUM_TMP<20)
HUM_TMP++;
}
if(!DN_HUM)
{
while(!DN_HUM);
if(HUM_TMP>7)
HUM_TMP--;
}
//按键调节HIGH_TMP
if(!UP_HIGH)
{
while(!UP_HIGH);
if(HIGH_TMP<100)
HIGH_TMP++;
}
if(!DN_HIGH)
{
while(!DN_HIGH);
if(HIGH_TMP>50)
HIGH_TMP--;
}
}
else
{
LED_G = 0;
E_MOTOR=0;
F_MOTOR=0;
}
}
}
/**************************************************/
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
51多电机同步控制关系.zip (18个子文件)
51多电机同步控制关系
keil
motor.c 4KB
motor.lnp 42B
motor.plg 4KB
motor.uvgui.huanglangchezai 69KB
motor 10KB
motor.M51 12KB
motor.uvproj 13KB
motor.LST 11KB
motor.OBJ 11KB
motor.uvopt 55KB
motor.hex 2KB
motor_uvopt.bak 55KB
Proteus
Last Loaded motor.pdsbak 29KB
Backup Of motor.pdsbak 2KB
motor.pdsprj 20KB
motor.pdsprj.DESKTOP-BMT37B0.Administrator.workspace 2KB
Last Loaded motor.fv8.21.pdsbak 37KB
motor.pdsprj.LAPTOP-PTIQMMET.31344.workspace 328B
共 18 条
- 1
资源评论
不知何时归家
- 粉丝: 161
- 资源: 112
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功