基于C#的上位机编程程序,通过上位机控制下位机实现电脑操作控制步进电机的转动 |
程序
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#include "2401.h"
#include "delay.h"
#include "ds18b20.h"
sbit IN1=P1^0;
sbit IN2=P1^1;
sbit ENA=P1^2; //位定义L298M芯片使能端ENA
sbit led0=P2^0; //正转指示灯
sbit led1=P2^1; //反转指示灯
sbit led2=P2^2; //加速指示灯
sbit led3=P2^3; //减速指示灯
sbit led4=P2^4; //开始指示灯
sbit led5=P2^5; //停止指示灯
uint v[1];
uint i,Time1num=0;
uint song[1];
uint j;
void delay_ms(uint z) //延时函数
{
uint y;
while(z--)
for(y=110;y>0;y--);
}
uchar k,m;
uint x=3,y=7;
void pwm(uint i,uint j) //pwm调速
{
ENA=1;
delay_ms(i);
ENA=0; //电机停转
delay_ms(j);
}
void UART_init (void){//UART串口初始化函数
TMOD = 0x21; //定时器T/C1工作方式2
SCON = 0x50; //串口工作方式1,允许串口接收(SCON = 0x40 时禁止串口接收)
TH1 = 0xfD; //定时器初值高8位设置
TL1 = 0xfD; //定时器初值低8位设置
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
PCON = 0; //波特率倍频
//PCON = 0;
TR1 = 1; //定时器启动
TR0 = 1; //定时器启动
EA=1;ES=1;ET0=1;PT0=1;//PS=1;
}
void main()
{
UART_init ();
while(1)
{
if(k==0xfa)
{
x=10;y=0;pwm(x,y);
}
if(k==0xc8)
{
x=8;y=2;pwm(x,y);
}
if(k==0x96)
{
x=6;y=4;pwm(x,y);
}
if(k==0x64)
{
x=4;y=6;pwm(x,y);
}
if(k==0x32)
{
x=2;y=8;pwm(x,y);
}
if(k==0x01) //正转
{
IN1=0;IN2=1;led0=0;x=2;y=8;pwm(x,y);
}
if(k==0x02) //反转
{
IN1=1;IN2=0;led1=0;x=2;y=8;pwm(x,y);
}
if(k==0x03) //停转
{
IN1=0;IN2=0;x=0;
}
if(k==0x81) //加速
{
led2=0;pwm(x,y);
}
if(k==0x82) //减速
{
led3=0;pwm(x,y);
}
if(k==0x06) //全速
{
x=10;y=0;led5=0;pwm(x,y);
}
}
}
void SIO_ISR() interrupt 4
{
k=SBUF;
RI=0;
if(k==0x81)
{
x=x+2;y=10-x;
if(x>10)
{x=10;y=0;}
if(x<0)
{x=0;y=10;}
}
if(k==0x82)
{
x=x-2;
y=10-x;
if(x>10)
{x=10;y=0;}
if(x<0)
{x=0;y=10;}
}
}
void int_1() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
Time1num++;
if(Time1num==5)