通過采用C#語言實現的上位機控制單片機的步進電機模塊、LED燈和蜂鳴器模塊,使步進電機進行正、反轉和停止並控制轉速;LED燈模塊進行有選擇的呼吸式表達;蜂鳴器模塊的開始和終止。
上位機通過串口和自定義的通信協議(8字節)控制單片機的步進電機、LED燈和蜂鳴器模塊。其中在控制步進電機的過程中,為了使操作能夠及時響應,使用了INT0中斷來進行及時性速度響應;LED燈使用位運算控制燈的閃爍位置,合理利用了單片機的模塊和操作。
注意:由於定時器個數的限制,沒能控制更多的模塊。
#includesbit WEI=P2^7; sbit DUAN=P2^6; #define DataPort P0 unsigned char code dofly_DuanMa[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};// 顯示段碼值0~9 unsigned char code dofly_WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//分別對應相應的數碼管點亮,即位碼 unsigned char TempData[8]; //存儲顯示值的全局變量 sbit SPK1=P1^4; void delay(int t) { while(t--); } sbit A1=P1^0; //定義步進電機連接端口 sbit B1=P1^1; sbit C1=P1^2; sbit D1=P1^3; #define Coil_AB1 {A1=1;B1=1;C1=0;D1=0;}//AB相通電,其他相斷電 #define Coil_BC1 {A1=0;B1=1;C1=1;D1=0;}//BC相通電,其他相斷電 #define Coil_CD1 {A1=0;B1=0;C1=1;D1=1;}//CD相通電,其他相斷電 #define Coil_DA1 {A1=1;B1=0;C1=0;D1=1;}//D相通電,其他相斷電 #define Coil_A1 {A1=1;B1=0;C1=0;D1=0;}//A相通電,其他相斷電 #define Coil_B1 {A1=0;B1=1;C1=0;D1=0;}//B相通電,其他相斷電 #define Coil_C1 {A1=0;B1=0;C1=1;D1=0;}//C相通電,其他相斷電 #define Coil_D1 {A1=0;B1=0;C1=0;D1=1;}//D相通電,其他相斷電 #define Coil_OFF {A1=0;B1=0;C1=0;D1=0;}//全部斷電 unsigned char Speed; unsigned char dir=0; unsigned char code rst[]={0xe4,0xc0,0xe0,0xc0,0xe0,0x32}; // 復位代碼 void Init_Timer0(void); void DelayUs2x(unsigned char t) { while(--t); } void DelayMs(unsigned char t) { while(t--) { //大致延時1mS DelayUs2x(245); DelayUs2x(245); } } void Display(unsigned char FirstBit,unsigned char Num) { static unsigned char i=0; DataPort=0; //清空數據,防止有交替重影 DUAN=1; //段鎖存 DUAN=0; DataPort=dofly_WeiMa[i+FirstBit]; //取位碼 WEI=1; //位鎖存 WEI=0; DataPort=TempData[i]; //取顯示數據,段碼 DUAN=1; //段鎖存 DUAN=0; i++; if(i==Num) i=0; } void Rorate() { unsigned int i=512;//旋轉一周時間 Init_Timer0(); EA=1; //全局中斷開 EX0=1; //外部中斷0開 IT0=1; //1表示邊沿觸發 //Speed=speed; TempData[0]=dofly_DuanMa[Speed/10];//分解顯示信息,如要顯示68, TempData[1]=dofly_DuanMa[Speed%10];//則68/10=6 68%10=8 Coil_OFF while(i--&&dir==0) //正向 { Coil_A1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_AB1 //遇到Coil_AB1 用{A1=1;B1=1;C1=0;D1=0;}代替 DelayMs(Speed); //改變這個參數可以調整電機轉速 , P3=0xeb; P3=0xff; //數字越小,轉速越大,力矩越小 Coil_B1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_BC1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_C1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_CD1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_D1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_DA1 DelayMs(Speed); P3=0xeb; P3=0xff; } Coil_OFF i=512; while((i--)&&dir)//反向 { Coil_A1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_DA1 //遇到Coil_AB1 用{A1=1;B1=1;C1=0;D1=0;}代替 DelayMs(Speed); //改變這個參數可以調整電機轉速 , P3=0xeb; P3=0xff; //數字越小,轉速越大,力矩越小 Coil_D1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_CD1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_C1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_BC1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_B1 DelayMs(Speed); P3=0xeb; P3=0xff; Coil_AB1 DelayMs(Speed); P3=0xeb; P3=0xff; } } /*------------------------------------------------ 串口初始化 ------------------------------------------------*/ void InitUART (void) { SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收 TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重裝 TH1 = 0xFD; // TH1: 重裝值 9600 波特率 晶振 11.0592MHz TR1 = 1; // TR1: timer 1 打開 EA = 1; //打開總中斷 ES = 1; //打開串口中斷 } /*------------------------------------------------ 主函數 ------------------------------------------------*/ unsigned int judge[8]; int cnt=0; void main (void) { InitUART(); ES= 1;//打開串口中斷 while (1) { if(judge[0]==0xFF&&judge[1]==0xFE&&judge[6]==0xFD&&judge[7]==0xFC) { //(*((void (*)())(rst)))(); // ,將rst數組當函數調用,進行復位 if(judge[2]==0x00)//指定步進電機 { P3=0xeb; P3=0xff; if(judge[5]!=0) Rorate(); } else if(judge[2]==0x01)//指定LED { unsigned int CYCLE=600,PWM_LOW=0;//定義周期並賦值 while (1) //主循環 { WEI=0; //位鎖存置0電平,防止LED亮的時候數碼管亮 DUAN=0; //段鎖存置0電平,防止LED亮的時候數碼管亮 P0=0xFF; delay(60000); //特意加延時,可以看到熄滅的過程 for(PWM_LOW=1;PWM_LOW 0;PWM_LOW--){ //與逐漸變亮相反的過程 P0=judge[3]; delay(PWM_LOW); P0=0xFF; delay(CYCLE-PWM_LOW); } } } else if(judge[2]==0x02) { while(1) { DelayMs(1); //發出大約500Hz的方波 頻率越大聲音越尖 SPK1=!SPK1; } } } } } /*------------------------------------------------ 串口中斷程序 ------------------------------------------------*/ void UART_SER (void) interrupt 4 //串行中斷服務程序 { unsigned char Temp; //定義臨時變量 if(RI) //判斷是接收中斷產生 { RI=0; //標志位清零 Temp=SBUF; //讀入緩沖區的值 judge[cnt++]=Temp; if(cnt==8||judge[0]!=0xFF) cnt=0; SBUF=Temp; //把接收到的值再發回電腦端 } if(TI) //如果是發送標志位,清零 TI=0; } /*------------------------------------------------ 定時器初始化子程序 ------------------------------------------------*/ void Init_Timer0(void) { TMOD |= 0x01; //使用模式1,16位定時器,使用"|"符號可以在使用多個定時器時不受影響 //TH0=0x00; //給定初值 //TL0=0x00; EA=1; //總中斷打開 ET0=1; //定時器中斷打開 TR0=1; //定時器開關打開 PT0=1; //優先級打開 } /*------------------------------------------------ 定時器中斷子程序 ------------------------------------------------*/ void Timer0_isr(void) interrupt 1 { TH0=(65536-2000)/256; //重新賦值 2ms TL0=(65536-2000)%256; Display(0,8); } //外部中斷程序 void ISR_INT0(void) interrupt 0 { Speed=judge[4]; dir=judge[3]; TempData[0]=dofly_DuanMa[Speed/10];//分解顯示信息,如要顯示68, TempData[1]=dofly_DuanMa[Speed%10];//則68/10=6 68%10=8 }
C#端:

namespace 單片機
{
public partial class frm : Form
{
public frm()
{
InitializeComponent();
}
private SerialPort com;
//通信協議
///
/// FF FE 00(電機) 00(方向) 00(速度) 00(停止) FD FC
///
///
///
private void btn_LED_Click(object sender, EventArgs e)
{
com = new SerialPort();
com.BaudRate = 9600;
com.PortName = "COM4";
com.DataBits = 8;
com.Open();
Byte[] data = new Byte[8];
data[0] = 0xFF;
data[1] = 0xFE;
if(rb_R.Checked==true)
{
data[2] = 0x00;
if (rb_clock.Checked == true)
{
data[3] = 0x00;
}
else if (rb_anclock.Checked == true)
{
data[3] = 0x01;
}
if (cmb_speed.Text.ToString() == "停止")
{
data[4] = 0x00;
data[5] = 0x00;
}
else
{
data[4] = Byte.Parse(cmb_speed.Text);
data[5] = 0x01;
}
}
else if (rb_LED.Checked == true)
{
data[2] = 0x01;
uint num = 255;
if (checkBox1.Checked == true)
{
num = num & 254;
}
if (checkBox2.Checked == true)
{
num = num & 253;
}
if (checkBox3.Checked == true)
{
num = num & 251;
}
if (checkBox4.Checked == true)
{
num = num & 247;
}
if (checkBox5.Checked == true)
{
num = num & 239;
}
if (checkBox6.Checked == true)
{
num = num & 223;
}
if (checkBox7.Checked == true)
{
num = num & 191;
}
if (checkBox8.Checked == true)
{
num = num & 127;
}
byte[] c = System.BitConverter.GetBytes(num);
data[3] = c[0];
if (led_s.Text.ToString() == "啟用")
data[2] = 0x02;
data[4] = 0x00;
data[5] = 0x00;
}
data[6] = 0xFD;
data[7] = 0xFC;
com.Write(data, 0, 8);
com.Close();
}
}
}