字体【
预留广告位

单片机最终篇之低电平有效 单片机低电平

分类:专题范文 日期:2022-11-04 20:01人气:加载中...


预留广告位

步进电机单片机系统设计 最终篇之外部中断

设计要求:一个正反转开关控制正转和反转, 一个速度开关控制高速和低速(高速和低速只要有明显差别),一个半圈按钮,按一下时转半圈, 一个一圈按钮, 按一下时转一圈, 一个连续转动按钮, 按一下时连续转动,再按一下时停止转动,程序要求用C语言编写。用protel画出系统原理图,要求是一个完整的单片机控制系统,电源为220V交流电。

#include

#include

#define uchar unsigned char

#define uint unsigned int

sbit ZHENG=P0^1;

sbit JIASU=P0^2;

sbit BAN=P0^3;

sbit YI=P0^4;

sbit LIAN=P3^3;

sbit A1=P2^0;

sbit B1=P2^1;

sbit C1=P2^2;

sbit D1=P2^3;

uchar code ffw[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //正转数组

uchar code rev[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //反转数组

uint rate;

uint i;

uint constant=1;

uint u;

uint s;

void delay(unsigned int m)

{

unsigned char x;

unsigned char j;

for(j=0;j

for(x=0;x

}

timer0()interrupt 1 using 1 /*定时器0中断服务程序*/

{

uint x;

x=rate;

i++;

TH0=(65536-x)/256;

TL0=(65536-x)%256;

u=0;

}

/***********半圈运行*************/

void semicircle()

{

for(s=0;s

{

i=0;

if(ZHENG)

{

while(i

{

u=1;

P2=ffw[i];

if(!JIASU)

{rate=20000;

TMOD=0x01;

TH0=(65536-20000)/256;

TL0=(65536-20000)%256;

EA=1;ET0=1;TR0=1;while(u);}

else

{rate=10000;

TMOD=0x01;

TH0=(65536-10000)/256;

TL0=(65536-10000)%256;

EA=1;ET0=1;TR0=1;while(u);}

}

}

else

{

while(i

{

u=1;

P2=rev[i];

if(!JIASU)

{rate=20000;

TMOD=0x01;

TH0=(65536-20000)/256;

TL0=(65536-20000)%256;

EA=1;ET0=1;TR0=1;while(u);}

else

{rate=10000;

TMOD=0x01;

TH0=(65536-10000)/256;

TL0=(65536-10000)%256;

EA=1;ET0=1;TR0=1;while(u);}

}

}

}

}

/************步进电机连续运行************/

void motor_turn()

{

i=0;

if(ZHENG)

{

while(i

{

u=1;

P2=ffw[i];

if(!JIASU)

{rate=20000;

TMOD=0x01;

TH0=(65536-20000)/256;

TL0=(65536-20000)%256;

EA=1;ET0=1;TR0=1;while(u);}

else

{rate=10000;

TMOD=0x01;

TH0=(65536-10000)/256;

TL0=(65536-10000)%256;

EA=1;ET0=1;TR0=1;while(u);}

}

}

else

{

while(i

{

u=1;

P2=rev[i];

if(!JIASU)

{rate=20000;

TMOD=0x01;

TH0=(65536-20000)/256;

TL0=(65536-20000)%256;

EA=1;ET0=1;TR0=1;while(u);}

else

{rate=10000;

TMOD=0x01;

TH0=(65536-10000)/256;

TL0=(65536-10000)%256;

EA=1;ET0=1;TR0=1;while(u);}

}

}

}

/************一圈运行****************/

void circle()

{for(s=0;s

motor_turn();

}

/**************主函数****************/

main()

{

P0=0xff;

while(1)

{

P2=0x00;

if(!LIAN)

{delay(20);

if(!LIAN) {constant=-1*constant;}}

if(constant==-1) motor_turn();

if(!BAN)

{delay(20);

if(!BAN) semicircle();}

if(!YI)

{delay(20);

if(!YI) circle();}

}

}

本文TAG:单片机  低电平  单片机高低电平什么意思  tcrt5000引脚  

本文永久网址

预留广告位
预留广告位