📄 step_motor_led.c
字号:
/*****************************************************************************************************************
Copyright (C), 2006, Shenzhen University,college of information engneering.
File Name: led_motor.c
Author: Iceant and Zhoudy
Version: Version 1.0
Start: Aug 17,2006
Last update: xxx xx,2006
Description: LCD serial display and motor drive
******************************************************************************************************************/
#include <reg51.h>
#include <intrins.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned char
sbit motor0=P2^0;
sbit motor1=P2^1;
sbit motor2=P2^2;
sbit motor3=P2^3;
//pins for LCD enable
sbit LCD_E=P0^2;
sbit LCD_RW=P0^1;
sbit LCD_RS=P0^0;
sbit LCD_BUSY_CHECK = P1^7;
//pins for keys as bps selecting
sbit b1=P3^3;
sbit b2=P3^4;
sbit b3=P3^5;
//Menu display
code char examp1[]="Baud rate selecting:";
code char examp2[]="1:4800 bps";
code char examp3[]="2:9600 bps";
code char examp4[]="3:19200 bps";
code char examp5[]="Error! Can not display so much characters!";
int count=0;
int dly=100;//define the parameter of delay time
int direction=1; //turn left or right
///////////type define the struct data received and the size of the buffer /////
typedef struct CMD
{
uchar buf[20];
uchar cnt;
}cmdstr;
cmdstr data cmd;
////////////
void serial(void);
void Lcd_Dis(uchar dis) ;
void buffer_clear(void);
void Lcd_Check(void);
void Lcd_Cmd(uint cmd);
void Lcd_Ini(void);
void Lcd_String(char *str);
void Delayms(int dly) ;
void Serial_Init1(void) ;
void Serial_Init2(void);
void Serial_Init3(void);
void led_dispaly(void);
void motor_control(void);
/******************************************************************************
Function: LCD serial display and motor drive
Description: main
Calls: - None
Input: - None
Output: - None
Return:
******************************************************************************/
void main(void)
{
cmd.cnt = 0;
//lcd initial
Lcd_Ini();
//menu display
Lcd_Cmd(0x80);
Lcd_String(examp1);
Lcd_Cmd(0xC0);
Lcd_String(examp2);
Lcd_Cmd(0x94);
Lcd_String(examp3);
Lcd_Cmd(0xD4);
Lcd_String(examp4);
do{ //wait for key press down
;
}while(b1&&b2&&b3);
Delayms(30);//waiting for normal state
//select baud rate
do{
if(b1==0)
{
Lcd_Cmd(0x01);
Serial_Init1();
}
else if(b2==0)
{
Lcd_Cmd(0x01);
Serial_Init2();
}
else if(b3==0)
{
Lcd_Cmd(0x01);
Serial_Init3();
}
}while(b1&&b2&&b3);
while(1)
{/*
if(direction)
{
motor0=1; //1000
motor1=0;
motor2=0;
motor3=0;
motor_control();
motor0=1; //1100
motor1=1;
motor2=0;
motor3=0;
motor_control();
motor0=0; //0100
motor1=1;
motor2=0;
motor3=0;
motor_control();
motor0=0; //0110
motor1=1;
motor2=1;
motor3=0;
motor_control();
motor0=0; //0010
motor1=0;
motor2=1;
motor3=0;
motor_control();
motor0=0; //0011
motor1=0;
motor2=1;
motor3=1;
motor_control();
motor0=0; //0001
motor1=0;
motor2=0;
motor3=1;
motor_control();
motor0=1; //1001
motor1=0;
motor2=0;
motor3=1;
motor_control();
}
else
{
motor0=1; //1001
motor1=0;
motor2=0;
motor3=1;
motor_control();
motor0=0; //0001
motor1=0;
motor2=0;
motor3=1;
motor_control();
motor0=0; //0011
motor1=0;
motor2=1;
motor3=1;
motor_control();
motor0=0; //0010
motor1=0;
motor2=1;
motor3=0;
motor_control();
motor0=0; //0110
motor1=1;
motor2=1;
motor3=0;
motor_control();
motor0=0; //0100
motor1=1;
motor2=0;
motor3=0;
motor_control();
motor0=1; //1100
motor1=1;
motor2=0;
motor3=0;
motor_control();
motor0=1; //1000
motor1=0;
motor2=0;
motor3=0;
motor_control();
} */
}
}
/******************************************************************************
Function: motor_control
Description: motor_control
Calls: - None
Input: - None
Output: - None
Return:
******************************************************************************/
void motor_control(void)
{
if((!b1)||(!b2)||(!b3)) //whether press the button
{
Delayms(3);
if(!b1) //button_direction is pressed dwon
{
while(!b1);
if(direction)
direction=0;
else
direction=1;
}
else if(!b2) // for button_left
{
while(!b2);
if(dly>15&&dly<350)
{
dly+=6;
}
else
{
dly-=12;
}
}
else if(!b3) //for button_right
{
while(!b3);
if(dly>=15&&dly<=350)
{
dly-=6;
}
else
{
dly+=12;
}
}
}
Delayms(dly);
}
/******************************************************************************
Function: buffer_clear
Description: buffer_clear
Calls: - None
Input: - None
Output: - None
Return:
******************************************************************************/
void buffer_clear(void)
{
int i;
for(i=0;i<20;i++)
{
cmd.buf[i]='\0';
}
cmd.cnt=0;
}
/******************************************************************************
Function: Lcd initial
Description: Initialize LCD IO
Calls: - None
Input: - None
Output: - None
Return:
******************************************************************************/
void Lcd_Cmd(uint cmd)
{
P1=cmd;
LCD_RS=0;
LCD_RW=0;
LCD_E=0;
Lcd_Check();
LCD_E=1;
}
//--------------Lcd check busy----------------/
void Lcd_Check(void)
{
/* volatile uchar i;
do{
P0 = 0xff;
LCD_RS = 0;
LCD_RW = 1;
LCD_E = 1;
for(i=0;i<6;i++)
;
LCD_E = 1;
}while(LCD_BUSY_CHECK == 1); */
Delayms(2);
}
//-----------Lcd initial
void Lcd_Ini(void)
{
Lcd_Cmd(0x06);
Delayms(5);
Lcd_Cmd(0x38);
Delayms(5);
Lcd_Cmd(0x0c);//0f close cursor
Delayms(5);
Lcd_Cmd(0x01); //06
Delayms(5);
}
//----------charater LCD-Display
void Lcd_Dis(uint dis)
{
P1=dis;
LCD_RS=1;
LCD_RW=0;
LCD_E=0;
Lcd_Check();
LCD_E=1;
}
//---------tips string-display------
void Lcd_String(char *str)
{
uint i=0;
while(str[i]!=0)
{
Lcd_Dis(str[i]);
if(i==19)
Lcd_Cmd(0xc0);
else if(i==39)
Lcd_Cmd(0x94);
else if(i==59)
Lcd_Cmd(0xD4);
i++;
}
}
void Delayms(int dly)
{
int i,j;
for(i=0;i<dly;i++)
for(j=0;j<120;j++); //50
}
////////////4800bps/////////////enable the interrupt serial port////////initialize
void Serial_Init1(void) //8 bit data ; 1 bit stop ; no checking bit
{
SCON=0X50;//serial mode 1; 8 bits uart, enable ucvr
TMOD|=0x20;//time1; mode 1 , 8 bits reload
PCON=0X0;//SMOD=1;
TH1=0xFA;//baudrate:4800bps fosc=11.0592MHz
TL1=0xFA;
IE|=0x90;// EA=0; enable serial port interrupt
TR1=1; //timer1 run
cmd.cnt=0; //initialize
buffer_clear();
}
////////////9600bps/////////////enable the interrupt serial port/////////initialize
void Serial_Init2(void) //8 bit data ; 1 bit stop ; no checking bit
{
SCON=0X50;//serial mode 1; 8 bits uart, enable ucvr
TMOD|=0x20;//time1; mode 1 , 8 bits reload
PCON=0X00;//SMOD=1;
TH1=0xFD;//baudrate:9600bps fosc=11.0592MHz
TL1=0xFD;
IE|=0x90;// EA=0; enable serial port interrupt
TR1=1; //timer1 run
cmd.cnt=0; //initialize
buffer_clear();//clear the buffer
}
////////////19200bps/////////////enable the interrupt serial port ///////initialize
void Serial_Init3(void)
{
SCON=0X50;//serial mode 1; 8 bits uart, enable ucvr
TMOD|=0x20;//time1; mode 1 , 8 bits reload
PCON=0X80;//SMOD=1;
TH1=0xFD;//baudrate:9600bps fosc=11.0592MHz
TL1=0xFD;
IE|=0x90;// EA=0; enable serial port interrupt
TR1=1; //timer1 run
cmd.cnt=0; //initialize
buffer_clear();//clear the buffer
}
///////////////led character dispaly///////////
void led_dispaly(void)
{
int i;
// Lcd_Cmd(0x01);
//display line select
if(count==0)
{
Lcd_Cmd(0x01);
Lcd_Cmd(0x80);
}
else if(count==1)
Lcd_Cmd(0xc0);
else if(count==2)
Lcd_Cmd(0x94);
else if(count==3)
Lcd_Cmd(0xD4);
//content display
for(i=0;i<cmd.cnt-2;i++)
{
if(i>59)
Lcd_String(examp5);
else
{
if(cmd.buf[i]!= 0x0a&&cmd.buf[i]!=0x0d)
{
Lcd_Dis(cmd.buf[i]);
if(i==19) //lines enter
Lcd_Cmd(0xc0);
else if(i==39)
Lcd_Cmd(0x94);
else if(i==59)
Lcd_Cmd(0xD4);
}
Delayms(5);
}
}
count++;
if(count==4)
count=0;
cmd.cnt=0; //initialize
buffer_clear();//clear the buffer
}
////////////////interrupt programe////////
void serial(void) interrupt 4
{
if(RI)
{
RI=0;
cmd.buf[cmd.cnt++]=SBUF;
}
if(cmd.buf[cmd.cnt-1]==0x0d);
else
{
if(cmd.buf[cmd.cnt-1]==0x0a)
{
led_dispaly();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -