标题:
步进电机+1602显示C源代码
[打印本页]
作者:
hustbiu
时间:
2016-12-14 12:41
标题:
步进电机+1602显示C源代码
步进电机+1602显示C源代码
下载:
步进电机 LCD1602显示.doc
(3.72 KB, 下载次数: 17)
2016-12-14 12:40 上传
点击文件名下载附件
下载积分: 黑币 -5
/*此程序在SP-518USB开发板上测试过 laosong */
/******************************步进电机的驱动*************************************
程序名称: 按键控制电机正反转,LCD1602显示正反转
说明:使用本程序你必须把 SE5设置为ON(2-3)短接
;FOSC = 12MHz
;---------------------------------------------------------------------------------
; 步进电机的驱动信号必须为脉冲信号!!! 转动的速度和脉冲的频率成正比!!!
; 本步进电机步进角为 5.625度 . 一圈 360 度 , 需要64个脉冲完成!!!
;---------------------------------------------------------------------------------
; A组线圈对应 P1.4
; B组线圈对应 P1.5
; C组线圈对应 P1.6
; D组线圈对应 P1.7
; 正转次序: AB组--BC组--CD组--DA组 (即一个脉冲,正转5.625度)
;----------------------------------------------------------------------------------
**********************************************************************************/
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcd_rs_port = P2^7; /*定义LCD控制端口*/
sbit lcd_rw_port = P2^6; /*定义LCD控制端口*/
sbit lcd_en_port = P2^5; /*定义LCD控制端口*/
#define lcd_data_port P0 /*定义LCD控制端口*/
#define motor P1 /*步进电机接口*/
sbit P34 = P3^4; /*控制正转按键*/
sbit P33 = P3^3; /*控制电机停止*/
sbit P11 = P1^1; /*控制电机反转*/
uchar code motor_table[][4]={{0x0f,0x0f,0x0f,0x0f},{0x3f,0x6f,0xcf,0x9f},{0x3f,0x9f,0xcf,0x6f}}; /*正反转表*/
uchar code lcd_table1[] = {"Motor Direction "};
uchar code lcd_table2[][16]={{" stop "},
{" >>>>>>>> "},
{" <<<<<<<< "}};
void lcd_delay(uchar ms) /*LCD1602 延时*/
{
uchar j;
while(ms--){
for(j=0;j<250;j++)
{;}
}
}
void lcd_busy_wait() /*LCD1602 忙等待*/
{
lcd_rs_port = 0;
lcd_rw_port = 1;
lcd_en_port = 1;
lcd_data_port = 0xff;
while (lcd_data_port&0x80);
lcd_en_port = 0;
}
void lcd_command_write(uchar command) /*LCD1602 命令字写入*/
{
lcd_busy_wait();
lcd_rs_port = 0;
lcd_rw_port = 0;
lcd_en_port = 0;
lcd_data_port = command;
lcd_en_port = 1;
lcd_en_port = 0;
}
void lcd_system_reset() /*LCD1602 初始化*/
{
lcd_delay(20);
lcd_command_write(0x38);
lcd_delay(100);
lcd_command_write(0x38);
lcd_delay(50);
lcd_command_write(0x38);
lcd_delay(10);
lcd_command_write(0x08);
lcd_command_write(0x01);
lcd_command_write(0x06);
lcd_command_write(0x0c);
}
void lcd_char_write(uchar x_pos,y_pos,lcd_dat) /*LCD1602 字符写入*/
{
x_pos &= 0x0f; /* X位置范围 0~15 */
y_pos &= 0x01; /* Y位置范围 0~ 1 */
if(y_pos==1) x_pos += 0x40;
x_pos += 0x80;
lcd_command_write(x_pos);
lcd_busy_wait();
lcd_rs_port = 1;
lcd_rw_port = 0;
lcd_en_port = 0;
lcd_data_port = lcd_dat;
lcd_en_port = 1;
lcd_en_port = 0;
}
/*1MS为单位的延时程序*/
void delay_1ms(uchar x)
{
uchar j;
while(x--) { for(j=0;j<125;j++); }
}
void main()
{
uchar i;
uchar yunzhuang=0; /*运转状况 0停止 1正转 2反转*/
uchar count=0; /*电机转动步数*/
motor = 0x0f; /*电机停止*/
lcd_system_reset(); /*LCD1602 初始化*/
for(i=0;i<16;i++) lcd_char_write(i,0,lcd_table1[i]);
while(1){
motor = motor_table[yunzhuang][count]; /*电机转动*/
delay_1ms(5);
count++; /*电机步数加1*/
if(count>=4) count = 0; /*完成一圈转动*/
for(i=0;i<16;i++) lcd_char_write(i,1,lcd_table2[yunzhuang][i]);
if(P33==0){
delay_1ms(1);
if(P33==0){
yunzhuang = 0;
}
}
else if(P34==0){
delay_1ms(5);
if(P34==0){
yunzhuang = 1;
count = 0;
}
}
else if(P11==0){
delay_1ms(1);
if(P11==0){
yunzhuang = 2;
}
}
}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1