51单片机,无法进入串行口中断,求教
- /*
- agreement = 0xff + ID_servo + ANGLE1 + ANGLE2 +0xaa
- */
- #include<reg51.h>
- #include<intrins.h>
- #include<stdio.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar count,angle;
- uchar ANGLE1,ANGLE2,ID_servo;
- uchar receive,step;
- uint flag_translate;
- sbit servo_01=P0^0;
- sbit servo_02=P0^1;
- sbit plus=P1^4;
- sbit minus=P1^5;
- //sbit dula=P2^6;
- //sbit wela=P2^7;
- /*void delayms(uint t)
- {
- uint i,j;
- for(i=t;t>0;t--)
- for(j=110;j>0;j--);
- } */
- void check_gorge();
- void init()
- {
- TMOD=0x21;
- SCON=0x50;
- TH1=0xfd;
- TL1=0xfd;
- TH0=(65536-23)/256;
- TL0=(65536-23)/256;
- RI=1;
- TR0=1;
- TR1=1;
- ET0=1;
- ES=1;
- EA=1;
- }
- void servo_time0() interrupt 1
- {
- TH0=(65536-23)/256;
- TL0=(65536-23)/256;
- switch(ID_servo==1)
- {
- case 1 :
- angle=ANGLE1*256+ANGLE2;
- if(count<angle)
- servo_01=1;
- else
- servo_01=0;
- count++;
- count=count%800;
- break;
- case 2 :
- angle=ANGLE1*256+ANGLE2;
- if(count<angle)
- servo_02=1;
- else
- servo_02=0;
- count++;
- count=count%800;
- break;
- }
- }
- void re_send(uchar receive)
- {
- SBUF=receive;
- while(!TI);
- TI=0;
- ES=1;
- }
- void check_gorge()
- {
- ES=0;
- TI=1;
- puts("It's OK!\n");
- while(TI!=0);
- TI=0;
- ES=1;
- }
- //agreement = 0xff + ID_servo + ANGLE1 + ANGLE2 +0xaa
- void ser() interrupt 4
- {
- check_gorge();
- while(RI!=0);
- RI=0;
- receive=SBUF;
- flag_translate=1;
- }
- void main()
- {
- init();
- check_gorge();
- while(1)
- {
- if(flag_translate==1)
- {
- ES=0;
- flag_translate=0;
- switch(step)
- {
- case 0 :
- if(receive==0xff)
- {
- step++;
- re_send(receive);
- }
- else
- step=0;
- break;
- case 1 :
- ID_servo=receive;
- re_send(receive);
- step++;
- break;
- case 2 :
- ANGLE1=receive;
- re_send(receive);
- step++;
- break;
- case 3 :
- ANGLE2=receive;
- re_send(receive);
- step++;
- break;
- case 4 :
- if(receive!=0xaa)
- puts("error\n");
- else
- re_send(receive);
- step=0;
- break;
- }
- ES=1;
- }
- }
- }
收藏啦,感觉好好的
程序太复杂了,建议先到网上下个简单的程序,串口收到数据就点一个灯就行,你看看人家是怎么编的
程序挺多的,设置断点慢慢排查、、、、、、
求问小编为什么我的只能调到0xf3 2400波特率才能正常传输,0xfc 9600就没用
#include<reg51.h>
void UartInit(void) //9600bps@11.0592MHz
{
SCON = 0x50;
PCON=0;
TMOD=0x20;
TL1 = 0xFD;
TH1 = 0xFD;
TR1 = 1;
EA=1;
ES=1;
}
void ys(int ms)
{
int k,d;
for(k=0;k<ms;k++)for(d=0;d<123;d++);
}
main()
{
unsigned char v;
UartInit();
while(1)
{
{
while(RI==0);
v=SBUF;
RI=0;
P1=v;
SBUF=v;
while(TI==0);
TI=0;
//ys(10);
}
}
}这个是输入1,串口反馈一个1回来,你的程序长而且习惯差没有标注。我这个程序很短没有标注。