本程序,是自动机器人的一个程序,融合了外部中断、定时器、PWM和利用SPI显示LCD等模块。
/*主程序*/
#include "option.h"
#include "def.h"
#include "44b.h"
#include "44blib.h"
U32 TimerCnt=0,maichong=5000;
unsigned int led=0; unsigned char TempData1,TempData2;
U32 tmp=0;
U32 timer=0;
U32 time=0;
U32 speed=0;
int out2=0,out3=0,time1=0,time0=0,time2=0,time3=0,time5=20000;//定时器计数
char flag1=0,flag2=0,flag=0;
int int1=0,int2=0,int3=0,int4=0;
static int flg=0,flg1=0,flg2=0;
int temp=0;
int which_int;
int jd=0;
void Main(void)
{
rSYSCFG=CACHECFG; // Using 8KB Cache
Port_Init();
interrupt_Init();
init_LCD();
clear_LCD();
Printf(1,0," ABU ROBOCON");
delay(150);
Printf(2,0,"THE SECOND ROBOT");
Uart_Init(0,4800);
Delay(10);
timems(10); //设置定时器10ms定时
rINTMSK=~(BIT_GLOBAL+BIT_EINT4567+BIT_URXD0+BIT_URXD1+BIT_TIMER5+BIT_EINT2);
Printf(2,0,"测试开始");
while(1)
{
/* Printf(3,0,"机器人启动");
Uart_Printf1("机器人启动");
delay(200);
Printf(4,0,"是否进入测试");
Uart_Printf1("是否进入测试");
flg1=0;
while(!flg1){delay(1);}
flg1=0;
flg=0;
clear_LCD();
delay(200);
Printf(1,0,"是否测试0");
Uart_Printf1("是否测试0");
while(!flg1){delay(1);}
jd=0;
while(flg) //测试电机0 记录脉冲数
{
clear_LCD();
flg1=0;
Printf(1,0,"是否开始测试0");
Uart_Printf1("是否开始测试0");
while(!flg1){delay(1);}
Printf(1,0,"速度1k方向正");
Uart_Printf1("速度1k方向正");
pw0(1,1);
flg1=0;
while(!flg1){delay(1);}
pw0off();
Printf(1,0," 停止 ");
Uart_Printf1(" 停止");
flg1=0;
while(!flg1){delay(1);}
Printf(1,0,"是否重试");
Uart_Printf1("是否重试");
flg1=0;
while(!flg1){delay(1);}
clear_LCD();
if(flg==0)
break;
}
flg=0;
flg1=0;
clear_LCD();
delay(200);
Printf(1,0,"是否测试1");
Uart_Printf1("是否测试1");
while(!flg1){delay(1);}
clear_LCD();
jd=1;
while(flg)
{
Printf(1,0,"test 1");
delay(200);
flg1=0;
Printf(2,0,"是否开始1");
Uart_Printf1("是否开始1");
while(!flg1){delay(1);}
Printf(2,0,"开始伸杆");
Uart_Printf1("开始伸杆");
time5=0;
rPDATD&=~(1<<3); //伸杆
rPDATD|=(1<<1);
flg1=0;
while(!flg1){delay(1);}
rPDATD|=(1<<3); //停杆
rPDATD|=(1<<1);
Printf(2,0,"time=%d ",time5);
Uart_Printf1("time=%d ",time5);
delay(200);
Printf(3,0,"停杆");
Uart_Printf1("停杆");
flg1=0;
while(!flg1){delay(1);}
delay(200);
flg1=0;
Printf(3,0,"是否收起抓手");
Uart_Printf1("是否收起抓手");
while(!flg1){delay(1);}
Printf(3,0," 收起抓手");
Uart_Printf1("收起抓手");
rPDATD&=~(1<<2); //收起抓手
rPDATD|=(1<<0);
time5=0;
flg1=0;
while(!flg1){delay(1);}
rPDATD|=(1<<2); //暂停抓手
rPDATD|=(1<<0);
Printf(3,0,"收起抓手time=%d",time5);
Uart_Printf1("收起抓手time=%d",time5);
delay(200);
Printf(4,0,"暂停抓手");
Uart_Printf1("暂停抓手");
flg1=0;
while(!flg1){delay(1);}
clear_LCD();
delay(200);
Printf(1,0,"是否收杆");
Uart_Printf1("是否收杆");
flg1=0;
while(!flg1){delay(1);}
Printf(1,0," 收杆");
Uart_Printf1("收杆");
rPDATD|=(1<<3); //收杆
rPDATD&=~(1<<1);
time5=0;
flg1=0;
while(!flg1){delay(1);}
rPDATD|=(1<<3); //停杆
rPDATD|=(1<<1);
Printf(2,0,"收杆time=%d",time5);
Uart_Printf1("收杆time=%d",time5);
flg1=0;
delay(200);
Printf(3,0,"是否重试");
Uart_Printf1("是否重试");
while(!flg1){delay(1);}
clear_LCD();
if(flg==0)
break;
}
clear_LCD();
delay(200);
flg1=0;
Printf(1,0,"是否测试2");
Uart_Printf1("是否测试2");
while(!flg1){delay(1);}
clear_LCD();
jd=2;
while(flg)
{
flg1=0;
Printf(1,0,"测试2 开始");
Uart_Printf1("测试2 开始");
rPDATF|=(1<<6);
rPDATF&=~(1<<5);
while(!flg1){delay(1);}
flg1=0;
delay(200);
Printf(3,0,"是否重试");
Uart_Printf1("是否重试");
while(!flg1){delay(1);}
clear_LCD();
if(flg==0)
break;
}
clear_LCD();
delay(200);
flg1=0;
Printf(1,0,"是否测试3");
Uart_Printf1("是否测试3");
while(!flg1){delay(1);}
clear_LCD();
jd=3;
while(flg)
{
Printf(1,0,"test 3");
delay(200);
flg1=0;
Printf(2,0,"是否开始3");
Uart_Printf1("是否开始3");
while(!flg1){delay(1);}
Printf(2,0,"开始伸杆");
Uart_Printf1("开始伸杆");
time5=0;
rPDATC&=~(1<<6); //伸杆
rPDATC|=(1<<4);
flg1=0;
while(!flg1){delay(1);}
rPDATC|=(1<<6); //停杆
rPDATC|=(1<<4);
Printf(2,0,"time=%d ",time5);
Uart_Printf1("time=%d ",time5);
delay(200);
Printf(3,0,"停杆");
Uart_Printf1("停杆");
flg1=0;
while(!flg1){delay(1);}
delay(200);
flg1=0;
Printf(3,0,"是否收起抓手");
Uart_Printf1("是否收起抓手");
while(!flg1){delay(1);}
Printf(3,0," 收起抓手");
Uart_Printf1("收起抓手");
rPDATC&=~(1<<2); //收起抓手
rPDATC|=(1<<0);
time5=0;
flg1=0;
while(!flg1){delay(1);}
rPDATC|=(1<<2); //暂停抓手
rPDATC|=(1<<0);
Printf(3,0,"收起抓手time=%d",time5);
Uart_Printf1("收起抓手time=%d",time5);
delay(200);
Printf(4,0,"暂停抓手");
Uart_Printf1("暂停抓手");
flg1=0;
while(!flg1){delay(1);}
clear_LCD();
delay(200);
Printf(1,0,"是否收杆");
Uart_Printf1("是否收杆");
flg1=0;
while(!flg1){delay(1);}
Printf(1,0," 收杆");
Uart_Printf1(" 收杆");
rPDATC|=(1<<6); //收杆
rPDATC&=~(1<<4);
time5=0;
flg1=0;
while(!flg1){delay(1);}
rPDATC|=(1<<6); //停杆
rPDATC|=(1<<4);
Printf(2,0,"收杆time=%d",time5);
Uart_Printf1("收杆time=%d",time5);
flg1=0;
delay(200);
Printf(3,0,"是否重试");
Uart_Printf1("是否重试");
while(!flg1){delay(1);}
clear_LCD();
if(flg==0)
break;
}
Printf(1,0,"测试结束祝你好运");
Uart_Printf1("测试结束祝你好运");
delay(200);
Printf(2,0," 工大必胜");
Uart_Printf1(" 工大必胜");
flg1=0;
while(!flg1){delay(1);} */
}
}
void __irq ex4567(void)//主要做调试用
{
which_int=rEXTINTPND;//rEXTINTPND外部中断挂起寄存器 P316
Uart_Printf0("n中断为%d", which_int);
Delay(10);
if(which_int==rEXTINTPND)
{
switch(which_int)
{
case 1:
WrUart1('a');
Delay(200);
break;
case 2:
WrUart1('b');
Delay(200);
break;
case 4:
WrUart1('c');
Delay(200);
break;
case 8:
WrUart1('d');
Delay(200);
break;
default:
break;
}
}
Delay(5000);
rEXTINTPND=0XF; //作用是清楚中断标志
rI_ISPC=BIT_EINT4567; //清楚中断申请位,很重要
}
void motor0(int num,int k)
{
time0=0;
pw0(k,1);
while((time0<=num)&&flag){delay(1);}
pw0off();
}
void motor1(int time1,int time2)//95 D3; 97 D1; 96 D2;98 D0;第一层
{
clear_LCD();
Printf(1,0,"第一层开始");
time5=0;
Printf(2,0,"伸杆");
rPDATD&=~(1<<3); //伸杆
rPDATD|=(1<<1);
while((time5
rPDATD|=(1<<3); //停杆
rPDATD|=(1<<1);
Printf(2,0,"停杆");
time5=0;
while(time5<10){delay(1);} //延时一段1s
Printf(2,0,"收起抓手");
rPDATD&=~(1<<2); //收起抓手
rPDATD|=(1<<0);
while((time5
rPDATD|=(1<<2); //暂停抓手
rPDATD|=(1<<0);
Printf(2,0,"收起抓手收杆");
rPDATD|=(1<<3); //收杆
rPDATD&=~(1<<1);
time5=0;
while((time5
rPDATD|=(1<<3); //停杆
rPDATD|=(1<<1);
Printf(2,0,"停杆");
}
void motor2(int time1,int time2)//PC6 112; PC4 114; PC2 116; PC0 118;//第二层
{
clear_LCD();
Printf(1,0,"第二层开始");
time5=0;
Printf(2,0,"伸杆");
rPDATC&=~(1<<6); //伸杆
rPDATC|=(1<<4);
while((time5
rPDATC|=(1<<6); //停杆
rPDATC|=(1<<4);
Printf(2,0,"停杆");
time5=0;
while(time5<10){delay(1);} //延时一段1s
Printf(2,0,"收起抓手");
rPDATC&=~(1<<2); //收起抓手
rPDATC|=(1<<0);
while((time5
rPDATC|=(1<<2); //暂停抓手
rPDATC|=(1<<0);
Printf(2,0,"收起抓手收杆");
rPDATC|=(1<<6); //收杆
rPDATC&=~(1<<4);
time5=0;
while((time5
rPDATC|=(1<<6); //停杆
rPDATC|=(1<<4);
Printf(2,0,"停杆");
}
void motor3(int time1)//F6 58;59 ;第三组一个电机
{
clear_LCD();
Printf(1,0,"第二层开始");
time5=0;
rPDATD&=~(1<<1);
rPDATD|=(1<<0);
Printf(2,0,"分离");
while((time5
rPDATD|=(1<<1);
rPDATD|=(1<<0);
Printf(2,0,"分离完毕");
}
/*void motor3(int time1,int time2)
{
time5=0;
rPDATF&=~(1<<7); //伸杆
rPDATF|=(1<<6);
while((time5
rPDATF&=~(1<<7); //停杆
rPDATF&=~(1<<6);
time5=0;
while(time5<10); //延时一段1s
rPDATF&=~(1<<5); //收起抓手
rPDATF|=(1<<4);
while((time5
rPDATF&=~(1<<5); //暂停抓手
rPDATF&=~(1<<4);
rPDATF|=(1<<7); //收杆
rPDATF&=~(1<<6);
time5=0;
while((time5
rPDATF&=~(1<<7); //停杆
rPDATF&=~(1<<6);
}*/
void motor4(int time1,int time2)
{
time5=0;
rPDATF&=~(1<<3); //伸杆
rPDATF|= (1<<2);
while((time5
rPDATF&=~(1<<3); //停杆
rPDATF&=~(1<<2);
time5=0;
while(time5<10); //延时一段1s
rPDATF&=~(1<<1); //收起抓手
rPDATF|=(1<<0);
while((time5
rPDATF&=~(1<<1); //暂停抓手
rPDATF&=~(1<<0);
rPDATF|=(1<<3); //收杆
rPDATF&=~(1<<2);
time5=0;
while((time5
rPDATF&=~(1<<3); //停杆
rPDATF&=~(1<<2);
}
void __irq Timer5(void) //定时器
{
rI_ISPC=BIT_TIMER5;
timer++;
if(timer>=50)
{
time5++;
timer=0;
}
}
void delayms(int i)
{
int j;
timer=0;
time5=0;
while(1)
{
j++;
if(time5>=i)
break;
}
}
void __irq Timer4(void)
{
rI_ISPC=BIT_TIMER4
;
}
void __irq Timer3(void)
{
rI_ISPC=BIT_TIMER3;
}
void __irq Timer2(void)
{
rI_ISPC=BIT_TIMER2;
}
void __irq Timer1(void)
{
rI_ISPC=BIT_TIMER5;
}
void __irq Timer0(void)
{
time0++;
rI_ISPC=BIT_TIMER0;
}
void __irq ex3(void)
{
rI_ISPC=BIT_EINT3; //清楚中断申请位,很重要
out2++;
}
void __irq ex2(void)
{
out2++;
//Printf(4,0,"out2=%d",out2);
rI_ISPC=BIT_EINT2; //清楚中断申请位,很重要
}
void __irq ex1(void)
{
rI_ISPC=BIT_EINT1; //清楚中断申请位,很重要
}
void __irq ex0(void)
{
rI_ISPC=BIT_EINT0; //清楚中断申请位,很重要
}
void __irq Uart0_RxFifoInt(void)
{
rI_ISPC=BIT_URXD0;
while(!(rUFSTAT0&0x1));
TempData1=rURXH0;
// Uart_Printf1("我:");
if(TempData1=='r')
{
Uart_Printf0("n");
Uart_Printf1("n");
}
if(TempData1=='b')
{
Uart_Printf0("b");
Uart_Printf1("b");
}
if(TempData1!='r')
{
Uart_Printf1("%c",TempData1);
Uart_Printf0("%c",TempData1);
}
}
void __irq Uart1_RxFifoInt(void)
{
static char string[20];
static int i=0;
int j,k;
// static char x,y;
rI_ISPC=BIT_URXD1;
while(!(rUFSTAT1&0x1));
TempData2=rURXH1;
// WrUart0(TempData2);
if(TempData2!=' |