視頻教程:https://www.bilibili.com/video/BV1fF411K79x
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define LCD1602_DATAPINS P0
sbit LCD1602_E=P2^2;
sbit LCD1602_RW=P2^1;
sbit LCD1602_RS=P2^0;
sbit MA_1 = P1^0;
sbit MA_2 = P1^1;
sbit MA_3 = P1^2;
sbit MA_4 = P1^3;
sbit MB_1 = P1^4;
sbit MB_2 = P1^5;
sbit MB_3 = P1^6;
sbit MB_4 = P1^7;
uchar flag;
unsigned char code M_A[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //X反轉順序
unsigned char code M_B[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正轉順序
/*******************************************************************************
* 函 數(shù) 名 : Delay
* 函數(shù)功能 : 延時
*******************************************************************************/
void DDelay(unsigned int t)
{
unsigned int k;
while(t--)
{
for(k=0; k80; k++);
}
}
/*******************************************************************************
* 函 數(shù) 名 : Lcd1602_Delay1ms
* 函數(shù)功能 : 延時函數(shù),延時1ms
* 輸 入 : c
* 輸 出 : 無
* 說 名 : 該函數(shù)是在12MHZ晶振下,12分頻單片機的延時。
*******************************************************************************/
void Lcd1602_Delay1ms(uint c) //誤差 0us
{
uchar a,b;
for (; c?>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
/*******************************************************************************
* 函 數(shù) 名 : LcdWriteCom
* 函數(shù)功能 : 向LCD寫入一個字節(jié)的命令
* 輸 入 : com
* 輸 出 : 無
*******************************************************************************/
void LcdWriteCom(uchar com) //寫入命令
{
LCD1602_E = 0; //使能
LCD1602_RS = 0; //選擇發(fā)送命令
LCD1602_RW = 0; //選擇寫入
LCD1602_DATAPINS = com; //放入命令
Lcd1602_Delay1ms(1); //等待數(shù)據(jù)穩(wěn)定
LCD1602_E = 1; //寫入時序
Lcd1602_Delay1ms(5); //保持時間
LCD1602_E = 0;
}
/*******************************************************************************
* 函 數(shù) 名 : LcdWriteData
* 函數(shù)功能 : 向LCD寫入一個字節(jié)的數(shù)據(jù)
* 輸 入 : dat
* 輸 出 : 無
*******************************************************************************/
void LcdWriteData(uchar dat) //寫入數(shù)據(jù)
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 1; //選擇輸入數(shù)據(jù)
LCD1602_RW = 0; //選擇寫入
LCD1602_DATAPINS = dat; //寫入數(shù)據(jù)
Lcd1602_Delay1ms(1);
LCD1602_E = 1; //寫入時序
Lcd1602_Delay1ms(5); //保持時間
LCD1602_E = 0;
}
/*******************************************************************************
* 函 數(shù) 名 : LcdInit()
* 函數(shù)功能 : 初始化LCD屏
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void LcdInit() //LCD初始化子程序
{
LcdWriteCom(0x38); //開顯示
LcdWriteCom(0x0c); //開顯示不顯示光標
LcdWriteCom(0x06); //寫一個指針加1
LcdWriteCom(0x01); //清屏
LcdWriteCom(0x80); //設置數(shù)據(jù)指針起點
}
//按指定位置顯示一個字符
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //當要顯示第二行時地址碼+0x40;
X |= 0x80; // 算出指令碼
LcdWriteCom(X); //這里不檢測忙信號,發(fā)送地址碼
LcdWriteData(DData);
}
//按指定位置顯示一串字符
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
while (DData[ListLength]>=0x20) //若到達字串尾則退出
{
if (X <= 0xF) //X坐標應小于0xF
{
DisplayOneChar(X, Y, DData[ListLength]); //顯示單個字符
ListLength++;
X++;
}
}
}
//unsigned char code M_A[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //X反轉順序
//unsigned char code M_B[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正轉順序
void Motor_A1()
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2 = 1;
MB_3 = 1;
MB_4 = 0;
DDelay(3);
MB_1 = 0;
MB_2 = 1;
MB_3 = 0;
MB_4 = 0;
DDelay(3);
MB_1 = 1;
MB_2 = 1;
MB_3 = 0;
MB_4 = 0;
DDelay(3);
MB_1 = 1;
MB_2 = 0;
MB_3 = 0;
MB_4 = 0;
DDelay(3);
MB_1 = 1;
MB_2 = 0;
MB_3 = 0;
MB_4 = 1;
}
void Motor_B2()
{
DDelay(3);
MB_1 = 1;
MB_2 = 0;
MB_3 = 0;
MB_4 = 1;
DDelay(3);
MB_1 = 1;
MB_2 = 0;
MB_3 = 0;
MB_4 = 0;
DDelay(3);
MB_1 = 1;
MB_2 = 1;
MB_3 = 0;
MB_4 = 0;
DDelay(3);
MB_1 = 0;
MB_2 = 1;
MB_3 = 0;
MB_4 = 0;
DDelay(3);
MB_1 = 0;
MB_2 = 1;
MB_3 = 1;
MB_4 = 0;
DDelay(3);
MB_1 = 0;
MB_2 = 0;
MB_3 = 1;
MB_4 = 0;
DDelay(3);
MB_1 = 0;
MB_2 = 0;
MB_3 = 1;
MB_4 = 1;
DDelay(3);
MB_1 = 0;
MB_2 = 0;
MB_3 = 0;
MB_4 = 1;
}
void M_Con(uchar id,uchar dir,uint step)
{
uchar i;
if(id == 0)
{
if(dir == 0)
{
for(i=0;i=step2)
{
num = step1 - step2;
for(i=0;i
審核編輯:湯梓紅
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報投訴
-
Proteus
+關注
關注
79文章
1692瀏覽量
106573 -
電動機
+關注
關注
74文章
4110瀏覽量
96292 -
步進電機
+關注
關注
150文章
3110瀏覽量
147482
發(fā)布評論請先 登錄
相關推薦
基于單片機和proteus的步進電機控制
發(fā)表于 03-02 22:10
?132次下載
基于proteus的步進電機仿真簡介
發(fā)表于 12-02 10:50
?15次下載
Proteus步進電機控制資料
發(fā)表于 06-19 14:33
?9次下載
proteus步進電機仿真
步進電機是一種能夠將電脈沖信號轉換成角位移或線位移的機電元件,它實際上是一種單相或多相同步電動機。單相步進電動機有單路電脈沖驅動,輸出功率一般很小,其用途為微小功率驅動。多相步進電動機
發(fā)表于 02-26 09:56
?5.4w次閱讀
評論