找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1036|回复: 1
收起左侧

传感器控制直流电机调速程序

[复制链接]
ID:728926 发表于 2020-12-27 09:35 来自手机 | 显示全部楼层 |阅读模式
#include<reg51.h>

//---重定义关键词---//
#define uchar unsigned char
#define uint unsigned int

sbit motor=P1^0;
sbit IN2=P1^1;
sbit ENA=P1^2;         

sbit K1=P3^1;

sbit sensor=P3^3;

int count,sec=0;
char jishi;

char Time=0,state;
unsigned char ZKB1=30;  //占空比
unsigned char ZKB2=60;  //占空比
unsigned char ZKB3=90;  //占空比

//--定义全局变量--//
unsigned char code DIG_PLACE[8] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位选控制   查表的方法控制
unsigned char code DIG_CODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码

//--声明全局函数--//
void Delay10ms(unsigned int c)   //误差 0us
{
    unsigned char a,b;
    for(;c>0;c--)
        for(b=38;b>0;b--)
            for(a=130;a>0;a--);
}

void main()
{
        IN2=0;
        ENA=P1;         
        motor=0;
        TMOD=0X11;
        TH0=0x3c;               
        TL0=0xb0;
        TH1=(65536-100)/256;               
        TL1=(65536-100)%256;
        EA=1;
        ET0=1;TR0=1;
        ET1=0;TR1=0;
        motor=0;
        P0 = DIG_CODE[0];
        while(1)
        {
                while(sensor==0)
                {
                                
                }
                state=1;
                while(sensor==1)
                {
                        jishi=0;
                        ET1=1;TR1=1;
                        count=0;sec=0;
                        P0 = DIG_CODE[state];

                        if(K1==0)
                        {
                                Delay10ms(1);        
                                if(K1==0)
                                {
                                        state++;
                                        if(state>3)
                                        state=1;
                                        P0 = DIG_CODE[state];
                                        while(K1==0);
                                }
                        }

                }
                jishi=1;

        }
}

void Timer0() interrupt 1
{
        TH0=0x3c;               
        TL0=0xb0;        
        if(jishi==1)
        {
                count++;
                if(count>=20)
                {
                        count=0;
                        sec++;
                        if(sec>=5)
                        {
                                sec=0;
                                jishi=0;
                                ET1=0;TR1=0;
                                motor=0;
                                P0 = DIG_CODE[0];        
                        }
                }
        }
}

void DSY_Refresh2() interrupt 3
{
        TH1=(65536-100)/256;               
        TL1=(65536-100)%256;        
        if(state==1)
        {
                Time++;
                if(Time<ZKB1)
                {
                        motor=1;
                }
                else if(Time<100)
                {
                        motor=0;
                }
                else
                {        
                        Time=0;
                }
        }
        if(state==2)
        {
                Time++;
                if(Time<ZKB2)
                {
                        motor=1;
                }
                else if(Time<100)
                {
                        motor=0;
                }
                else
                {        
                        Time=0;
                }
        }
        if(state==3)
        {
                Time++;
                if(Time<ZKB3)
                {
                        motor=1;
                }
                else if(Time<100)
                {
                        motor=0;
                }
                else
                {        
                        Time=0;
                }
        }
}
#include<reg51.h>

//---重定义关键词---//
#define uchar unsigned char
#define uint unsigned int

sbit motor=P1^0;
sbit IN2=P1^1;
sbit ENA=P1^2;         

sbit K1=P3^1;

sbit sensor=P3^3;

int count,sec=0;
char jishi;

char Time=0,state;
unsigned char ZKB1=30;  //占空比
unsigned char ZKB2=60;  //占空比
unsigned char ZKB3=90;  //占空比

//--定义全局变量--//
unsigned char code DIG_PLACE[8] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位选控制   查表的方法控制
unsigned char code DIG_CODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码

//--声明全局函数--//
void Delay10ms(unsigned int c)   //误差 0us
{
    unsigned char a,b;
    for(;c>0;c--)
        for(b=38;b>0;b--)
            for(a=130;a>0;a--);
}

void main()
{
        IN2=0;
        ENA=P1;         
        motor=0;
        TMOD=0X11;
        TH0=0x3c;               
        TL0=0xb0;
        TH1=(65536-100)/256;               
        TL1=(65536-100)%256;
        EA=1;
        ET0=1;TR0=1;
        ET1=0;TR1=0;
        motor=0;
        P0 = DIG_CODE[0];
        while(1)
        {
                while(sensor==0)
                {
                                
                }
                state=1;
                while(sensor==1)
                {
                        jishi=0;
                        ET1=1;TR1=1;
                        count=0;sec=0;
                        P0 = DIG_CODE[state];

                        if(K1==0)
                        {
                                Delay10ms(1);        
                                if(K1==0)
                                {
                                        state++;
                                        if(state>3)
                                        state=1;
                                        P0 = DIG_CODE[state];
                                        while(K1==0);
                                }
                        }

                }
                jishi=1;

        }
}

void Timer0() interrupt 1
{
        TH0=0x3c;               
        TL0=0xb0;        
        if(jishi==1)
        {
                count++;
                if(count>=20)
                {
                        count=0;
                        sec++;
                        if(sec>=5)
                        {
                                sec=0;
                                jishi=0;
                                ET1=0;TR1=0;
                                motor=0;
                                P0 = DIG_CODE[0];        
                        }
                }
        }
}

void DSY_Refresh2() interrupt 3
{
        TH1=(65536-100)/256;               
        TL1=(65536-100)%256;        
        if(state==1)
        {
                Time++;
                if(Time<ZKB1)
                {
                        motor=1;
                }
                else if(Time<100)
                {
                        motor=0;
                }
                else
                {        
                        Time=0;
                }
        }
        if(state==2)
        {
                Time++;
                if(Time<ZKB2)
                {
                        motor=1;
                }
                else if(Time<100)
                {
                        motor=0;
                }
                else
                {        
                        Time=0;
                }
        }
        if(state==3)
        {
                Time++;
                if(Time<ZKB3)
                {
                        motor=1;
                }
                else if(Time<100)
                {
                        motor=0;
                }
                else
                {        
                        Time=0;
                }
        }
}
回复

使用道具 举报

ID:579406 发表于 2020-12-27 17:12 | 显示全部楼层
有没有仿真效果图?看一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表