標(biāo)題: 求寫一段單片機(jī)定時(shí)中斷程序 使其 正轉(zhuǎn)一圈 過(guò)24小時(shí)反轉(zhuǎn)一圈。一直這樣循環(huán) [打印本頁(yè)]

作者: plkio6613    時(shí)間: 2018-1-24 16:33
標(biāo)題: 求寫一段單片機(jī)定時(shí)中斷程序 使其 正轉(zhuǎn)一圈 過(guò)24小時(shí)反轉(zhuǎn)一圈。一直這樣循環(huán)
求一個(gè)定時(shí)中斷程序添加到此代碼中,使其 正轉(zhuǎn)一圈  過(guò)24小時(shí)反轉(zhuǎn)一圈。一直這樣循環(huán)。謝謝了。

接線方式:
IN1 ---- P00
IN2 ---- P01
IN3 ---- P02
IN4 ---- P03
+   ---- +5V
-   ---- GND
*********************/
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int
#define MotorData P0                    //步進(jìn)電機(jī)控制接口定義
uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正轉(zhuǎn) 電機(jī)導(dǎo)通相序 D-C-B-A
uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反轉(zhuǎn) 電機(jī)導(dǎo)通相序 A-B-C-D
//ms延時(shí)函數(shù)
void Delay_xms(uint x)
{
uint i,j;
for(i=0;i<x;i++)
  for(j=0;j<112;j++);
}
//順時(shí)針轉(zhuǎn)動(dòng)
void MotorCW(void)
{
uchar i;
for(i=0;i<4;i++)
  {
   MotorData=phasecw;
   Delay_xms(4);//轉(zhuǎn)速調(diào)節(jié)
  }
}
//逆時(shí)針轉(zhuǎn)動(dòng)
void MotorCCW(void)
{
uchar i;
for(i=0;i<4;i++)
  {
   MotorData=phaseccw;
   Delay_xms(4);//轉(zhuǎn)速調(diào)節(jié)
  }
}
//停止轉(zhuǎn)動(dòng)
void MotorStop(void)
{
MotorData=0x00;
}
//主函數(shù)
void main(void)
{
uint i;
Delay_xms(50);//等待系統(tǒng)穩(wěn)定
while(1)
{
  for(i=0;i<500;i++)
  {
   MotorCW();   //順時(shí)針轉(zhuǎn)動(dòng)
  }
  MotorStop();  //停止轉(zhuǎn)動(dòng)
  Delay_xms(500);
  for(i=0;i<500;i++)
  {
   MotorCCW();  //逆時(shí)針轉(zhuǎn)動(dòng)
  }
  MotorStop();  //停止轉(zhuǎn)動(dòng)
  Delay_xms(500);  
}
}



作者: cjjcjj1    時(shí)間: 2018-1-24 17:26
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽
作者: 村風(fēng)    時(shí)間: 2018-1-24 22:27
這個(gè)可以完全用定時(shí)器寫一個(gè)時(shí)分秒的定時(shí)累加程序,定時(shí)時(shí)間到,改變程序運(yùn)行狀態(tài)標(biāo)志位就可以了。集體還是樓主自己想辦法琢磨,拒絕伸手黨&拿來(lái)主義。




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1