请各位大神帮忙解决个问题~~小弟弄了两天没搞出来~郁闷,将小弟仅有的芯币拿出来~求个解答
代码如下,是通过51串口控制步进电机的程序。其中串口通过串口精灵给指令控制电机,定时器0是给电机精准调速用的。发现打开串口中断和定时器0中断后,貌似有冲突,定时器0中断进不去。特来求教~
#include <reg51.h> #include <stdio.h> #define uchar unsigned char #define uint unsigned int uint i,j,k=750; uchar key; uint dir; unsigned char dat; sbit LED = P1^4; //一个灯做检测用,看是否进入定时器中断 sbit T6560_EN = P0^0; //使能 sbit T6560_CW = P0^1; //方向 sbit T6560_CLK = P0^2; //脉冲
static void delay(unsigned int s) { unsigned int i; for(i=0; i<s; i++); }
void drive_6560_Inital(void) { T6560_CLK = 1; T6560_EN = 1; }
void main () { drive_6560_Inital();
//串口初始化 SCON = 0x50; /* uart in mode 1 (8 bit), REN=1 */ TMOD = TMOD | 0x21 ; /* Timer 1 in mode 2 */ TH1 = 0xFD; /* 9600 Bds at 11.059MHz */ TL1 = 0xFD; /* 9600 Bds at 11.059MHz */ TR1 = 1; /* Timer 1 run */ ET1 =0; ES=1; //允许串行口中断 IP=0x10; //设计串行口中断优先级
//定时器0初始化 TL0 = (65536-k) % 256; TH0 = (65536-k) / 256; ET0 = 1; EA =1;
while(1) { switch (dat) //通过判断串口信息执行不同动作 { case 0x01: { T6560_CW = 1; T6560_EN = 1; TR0=1; //开定时器0中断 // SBUF=dat; break; } case 0x02: { T6560_CW = 0; T6560_EN = 1; TR0=1; //开定时器0中断 // SBUF=dat; break; }
} }
}
void uart_interrupt(void) interrupt 4 using 3 { RI=0; EA=0; dat=SBUF; //接收串口数据 EA=1; }
void timer0() interrupt 1 //定时器0中断,通过定时时间控制电机的转速 { TL0 = (65536-k) % 256; //初值设定,可控制转速 TH0 = (65536-k) / 256; T6560_CLK = ! T6560_CLK; //脉冲信号,使得电机进一次中断走一个布距角
LED=0; //通过一个灯观察是否进入中断 delay(200); LED=1;
}
[ 本帖最后由 springmissgod 于 2012-6-27 13:12 编辑 ] |