51单片机控制步进电机正反转的程序
扫描二维码
随时随地手机看文章
这是一款51单片机控制步进电机正反转的程序,同时还能实现调速。
#include”reg51.h“
#include “intrins.h”
#define uchar unsigned char
#define uint unsigned int
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
unsigned char code FFW[8]={0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7,0xf6}; //??
unsigned char code REV[8]={0xf6,0xf7,0xf3,0xfb,0xf9,0xfd,0xfc,0xfe}; //??
//unsigned char code FFW[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //??
//unsigned char code REV[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //??
sbit K1 = P3^2; //?????
sbit K2 = P3^3; //????
sbit K3 = P3^4; //????
sbit K4 = P3^5; //????
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
bit on_off=0; //???????
bit direction=1; //????
bit rate_dr=1; //????
bit snum_dr=1; //????
uchar code cdis1[ ] = {"NO.0738230126 "};
uchar code cdis2[ ] = {" 2012/5/24"};
uchar code cdis3[ ] = {" STOP "};
uchar code cdis4[ ] = {"NUM: RATE: "};
uchar code cdis5[ ] = {" RUNNING "};
uchar m,v=0,q=0;
uchar number=0,number1=0;
uchar snum=20,snum1=20; //?????
uchar rate=3; //?????
uchar data_temp,data_temP0,data_temp2;
void delay(uint t)
{
uchar k;
while(t--)
{
for(k=0; k<124; k++)
{ }
}
}
void delayB(uchar x) //x*0.14MS
{
uchar i;
while(x--)
{
for (i=0; i<13; i++)
{ }
}
}
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = cmd;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
void lcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
void lcd_init()
{
delay(30);
lcd_wcmd(0x38); //16*2??,5*7??,8???
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x0c); //???,???
delay(5);
lcd_wcmd(0x06); //????
delay(5);
lcd_wcmd(0x01); //??LCD?????
delay(5);
}
void lcd_pos(uchar pos)
{
lcd_wcmd(pos | 0x80); //????=80+????
}
void LCD_init_DIS()
{
delay(10); //??
lcd_init(); //???LCD
lcd_pos(0); //????????????1???
m = 0;
while(cdis1[m] != '