1、/*Copyright(c)* Guangzhou ZHIYUAN electronics Co.,LTD.* http:/*-File Info-* File Name: main.c* Last modified Date: 2008/02/14* Last Version: V1.00* Description: MicroMouse615 上的无记忆功能的走迷宫实验*-* Created By: 廖茂刚* Created date:* Version:* Descriptions:*-* Modified by:* Modified date:* Version:* Descripti
2、on:*/*包含头文件*/#include hw_memmap.h#include hw_ints.h#include hw_types.h#include interrupt.h#include gpio.h#include sysctl.h#include Systick.h#include Timer.h#include Pwm.h#include Type.h#include Zlg7289.h/*PB 端口定义红外线传感器*/#define IRSEND_BEVEL GPIO_PIN_0 /* 驱动斜角红外的调制信号*/#define LEFTSIDE GPIO_PIN_1 /* 左
3、方传感器输出的信号*/#define FRONTSIDE_L GPIO_PIN_2 /* 左前方传感器输出的信号*/#define FRONTSIDE GPIO_PIN_3 /* 前方传感器输出的信号*/#define FRONTSIDE_R GPIO_PIN_4 /* 右前方传感器输出的信号*/#define RIGHTSIDE GPIO_PIN_5 /* 右方传感器输出的信号*/*PC 端口定义KEY 键*/#define KEY GPIO_PIN_4 /* 按键连接的端口*/*PD 端口定义步进电机*/#define PHRA1 GPIO_PIN_0 /* 右侧步进电机的A1 相*/#d
4、efine PHRA2 GPIO_PIN_1 /* 右侧步进电机的A2 相*/#define PHRB1 GPIO_PIN_2 /* 右侧步进电机的B1 相*/#define PHRB2 GPIO_PIN_3 /* 右侧步进电机的B2 相*/#define PHLA1 GPIO_PIN_4 /* 左侧步进电机的A1 相*/#define PHLA2 GPIO_PIN_5 /* 左侧步进电机的A2 相*/#define PHLB1 GPIO_PIN_6 /* 左侧步进电机的B1 相*/#define PHLB2 GPIO_PIN_7 /* 左侧步进电机的B2 相*/*PE 端口定义传感器红外发射
5、驱动左前右正向*/#define IRSEND_SIDE GPIO_PIN_0 /* 驱动左前右正向红外发射*/*常量宏定义- 迷宫类型*/#define MAZETYPE 8 /* 8: 四分之一迷宫;16: 全迷宫*/*常量宏定义-前进一个迷宫格步进电机需要走的步数*/#define ONEBLOCK 125/*常量宏定义-电脑鼠状态*/#define STOP 0 /* 电脑鼠停止*/#define GOAHEAD 1 /* 电脑鼠前进*/#define TURNLEFT 3 /* 电脑鼠向左转*/#define TURNRIGHT 4 /* 电脑鼠向右转*/#define TURNBA
6、CK 5 /* 电脑鼠向后转*/*常量宏定义-传感器*/#define LEFT 0 /* 左方传感器*/#define FRONTL 1 /* 左前方传感器*/#define FRONT 2 /* 前方传感器*/#define FRONTR 3 /* 右前方传感器*/#define RIGHT 4 /* 右方传感器*/*常量宏定义-电机状态*/#define MOTORSTOP 0 /* 电机停止*/#define WAITONESTEP 1 /* 电机暂停一步*/#define MOTORRUN 2 /* 电机运行*/*常量宏定义-电机运行方向*/#define MOTORGOAHEAD
7、0 /* 电机前进*/#define MOTORGOBACK 1 /* 电机后退*/*结构体定义*/struct motor int8 cState; /* 电机运行状态*/int8 cDir; /* 电机运行方向*/uint32 uiPulse; /* 电机需要转动的步数*/uint32 uiPulseCtr; /* 电机已转动的步数*/int32 iSpeed; /* 电机转动速度*/;typedef struct motor MOTOR;/*定义全局变量*/static MOTOR GmRight = MOTORSTOP, MOTORGOAHEAD, 0, 0, 0; /* 定义并初始化
8、右电机状态*/static MOTOR GmLeft = MOTORSTOP, MOTORGOAHEAD, 0, 0, 0; /* 定义并初始化左电机状态*/static uint8 GucMouseState = STOP; /* 保存电脑鼠当前运行状态*/static uint32 GuiAccelTable300 = 0; /* 电机加减速各阶段定时器值*/static int32 GiMaxSpeed = 70; /* 保存允许运行的最大速度*/static uint8 GucDistance5 = 0; /* 记录传感器状态*/* Function name: delay* Desc
9、riptions: 延时函数* input parameters: uiD :延时参数,值越大,延时越久* output parameters: 无* Returned value: 无*/void delay (uint32 uiD)for (; uiD; uiD-);/* Function name: rightMotorContr* Descriptions: 右步进电机驱动时序* input parameters: 无* output parameters: 无* Returned value: 无*/void rightMotorContr (void)static int8 cSt
10、ep = 0; /* 保存电机当前位置*/switch (GmRight.cDir) case MOTORGOAHEAD: /* 向前步进*/cStep = (cStep + 1) % 8;break;case MOTORGOBACK: /* 向后步进*/cStep = (cStep + 7) % 8;break;default:break;switch (cStep) case 0: /* A2B2*/GPIOPinWrite(GPIO_PORTD_BASE,PHRA1 | PHRA2 | PHRB1 | PHRB2,PHRA1 | PHRA2 | PHRB1 | PHRB2);break;
11、case 1: /* A2*/GPIOPinWrite(GPIO_PORTD_BASE,PHRA1 | PHRA2 | PHRB1 | PHRB2,PHRA1 | PHRA2);break;case 2: /* A2B1*/GPIOPinWrite(GPIO_PORTD_BASE,PHRA1 | PHRA2 | PHRB1 | PHRB2,PHRA1 | PHRA2 | PHRB2);break;case 3: /* B1*/GPIOPinWrite(GPIO_PORTD_BASE,PHRA1 | PHRA2 | PHRB1 | PHRB2,PHRB2);break;case 4: /* A1
12、B1*/GPIOPinWrite(GPIO_PORTD_BASE,PHRA1 | PHRA2 | PHRB1 | PHRB2,PHRA2 | PHRB2);break;case 5: /* A1*/GPIOPinWrite(GPIO_PORTD_BASE,PHRA1 | PHRA2 | PHRB1 | PHRB2,PHRA2);break;case 6: /* A1B2*/GPIOPinWrite(GPIO_PORTD_BASE,PHRA1 | PHRA2 | PHRB1 | PHRB2,PHRA2 | PHRB1 | PHRB2);break;case 7: /* B2*/GPIOPinWr
13、ite(GPIO_PORTD_BASE,PHRA1 | PHRA2 | PHRB1 | PHRB2,PHRB1 | PHRB2);break;default:break;/* Function name: leftMotorContr* Descriptions: 左步进电机驱动时序* input parameters: GmLeft.cDir :电机运行方向* output parameters: 无* Returned value: 无*/void leftMotorContr (void)static int8 cStep = 0; /* 保存电机当前位置*/switch (GmLeft
14、.cDir) case MOTORGOAHEAD: /* 向前步进*/cStep = (cStep + 1) % 8;break;case MOTORGOBACK: /* 向后步进*/cStep = (cStep + 7) % 8;break;default:break;switch (cStep) case 0: /* A2B2*/GPIOPinWrite(GPIO_PORTD_BASE,PHLA1 | PHLA2 | PHLB1 | PHLB2,PHLA1 | PHLA2 | PHLB1 | PHLB2);break;case 1: /* B2*/GPIOPinWrite(GPIO_POR
15、TD_BASE,PHLA1 | PHLA2 | PHLB1 | PHLB2,PHLB1 | PHLB2);break;case 2: /* A1B2*/GPIOPinWrite(GPIO_PORTD_BASE,PHLA1 | PHLA2 | PHLB1 | PHLB2,PHLA2 | PHLB1 | PHLB2);break;case 3: /* A1*/GPIOPinWrite(GPIO_PORTD_BASE,PHLA1 | PHLA2 | PHLB1 | PHLB2,PHLA2);break;case 4: /* A1B1*/GPIOPinWrite(GPIO_PORTD_BASE,PHL
16、A1 | PHLA2 | PHLB1 | PHLB2,PHLA2 | PHLB2);break;case 5: /* B1*/GPIOPinWrite(GPIO_PORTD_BASE,PHLA1 | PHLA2 | PHLB1 | PHLB2,PHLB2);break;case 6: /* A2B1*/GPIOPinWrite(GPIO_PORTD_BASE,PHLA1 | PHLA2 | PHLB1 | PHLB2,PHLA1 | PHLA2 | PHLB2);break;case 7: /* A2*/GPIOPinWrite(GPIO_PORTD_BASE,PHLA1 | PHLA2 |
17、PHLB1 | PHLB2,PHLA1 | PHLA2);break;default:break;/* Function name: speedContrR* Descriptions: 右电机速度调节* input parameters: 无* output parameters: 无* Returned value: 无*/void speedContrR (void)int32 iDPusle;iDPusle = GmRight.uiPulse - GmRight.uiPulseCtr; /* 统计电机还剩余的步数*/if (iDPusle = GmRight.iSpeed) GmRig
18、ht.iSpeed-; else /* 非减速区间,则加速到最大值*/if (GmRight.iSpeed GiMaxSpeed) GmRight.iSpeed+; else GmRight.iSpeed-;if (GmRight.iSpeed 0) /* 设置速度下限*/GmRight.iSpeed = 0;TimerLoadSet(TIMER0_BASE, TIMER_A, GuiAccelTableGmRight.iSpeed); /* 设置定时时间*/* Function name: speedContrL* Descriptions: 左电机速度调节* input parameter
19、s: 无* output parameters: 无* Returned value: 无*/void speedContrL (void)int32 iDPusle;iDPusle = GmLeft.uiPulse - GmLeft.uiPulseCtr; /* 统计电机还剩余的步数*/if (iDPusle = GmLeft.iSpeed) GmLeft.iSpeed-; else /* 非减速区间,则加速到最大值*/if (GmLeft.iSpeed GiMaxSpeed) GmLeft.iSpeed+;if (GmLeft.iSpeed = GmRight.uiPulse) GmRig
20、ht.cState = MOTORSTOP;GmRight.uiPulseCtr = 0;GmRight.uiPulse = 0;GmRight.iSpeed = 0;/* Function name: Timer1A_ISR* Descriptions: Timer1 中断服务函数* input parameters: GmLeft.cState :驱动步进电机的时序状态* GmLeft.cDir :步进电机运动的方向* output parameters: 无* Returned value: 无*/void Timer1A_ISR(void)static int8 n = 0, m =
21、0;TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT); /* 清除定时器1 中断。*/switch (GmLeft.cState) case MOTORSTOP: /* 停止,同时清零速度和脉冲值*/GmLeft.iSpeed = 0;GmLeft.uiPulse = 0;GmLeft.uiPulseCtr = 0;break;case WAITONESTEP: /* 暂停一步*/GmLeft.cState = MOTORRUN;break;case MOTORRUN: /* 电机运行*/if (GucMouseState = GOAHEAD) /*
22、 根据传感器状态微调电机位置*/if (GucDistanceFRONTR &(GucDistanceFRONTL=0) if (n = 1) GmLeft.cState = WAITONESTEP;n+;n %= 2; else n = 0;if (GucDistanceLEFT = 1) & (GucDistanceRIGHT = 0) if(m = 3) GmLeft.cState = WAITONESTEP;m+;m %= 6; else m = 0;leftMotorContr(); /* 电机驱动程序*/break;default:break;/* 是否完成任务判断*/if (Gm
23、Left.cState != MOTORSTOP) GmLeft.uiPulseCtr+; /* 运行脉冲计数*/speedContrL(); /* 速度调节*/if (GmLeft.uiPulseCtr = GmLeft.uiPulse) GmLeft.cState = MOTORSTOP;GmLeft.uiPulseCtr = 0;GmLeft.uiPulse = 0;GmLeft.iSpeed = 0;/* Function name: irSendFreq* Descriptions: 发送红外线。* input parameters: uiFreq: 红外线调制频率* cNumber
24、: 选择需要设置的PWM 模块* output parameters: 无* Returned value: 无*/void irSendFreq (uint32 uiFreq, int8 cNumber)uiFreq = SysCtlClockGet() / uiFreq;switch (cNumber) case 1:PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, uiFreq); /* 设置PWM 发生器1 的周期*/PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, uiFreq / 2); /* 设置PWM2 输出的脉冲宽度*/PWMGenEnable(PWM_BASE, PWM_GEN_1); /* 使能PWM 发生器1*/break;case 2:PWMGenPeriodSet(PWM_BASE, PWM_GEN_2, uiFreq); /* 设置PWM 发生器2 的周期*/PWMPulseWidthSet(PWM_BASE, PWM_OUT_4, uiFreq / 2); /* 设置PWM4 输出的脉冲宽度*/PWMGenEnable(PWM_BASE, PWM_GEN_2); /* 使能PWM 发生器2*/break;default:break;/* Function name: irCh