已阅读5页,还剩25页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Copyright c Guangzhou ZHIYUAN electronics Co LTD 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 Description 包含头文件 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 左方传感器输出的信号 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 相 define 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 端口定义传感器红外发射驱动左前右正向 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 TURNBACK 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 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 定义并初始化右电机状态 static MOTOR GmLeft MOTORSTOP MOTORGOAHEAD 0 0 0 定义并初始化左电机状态 static uint8 GucMouseState STOP 保存电脑鼠当前运行状态 static uint32 GuiAccelTable 300 0 电机加减速各阶段定时器值 static int32 GiMaxSpeed 70 保存允许运行的最大速度 static uint8 GucDistance 5 0 记录传感器状态 Function name delay Descriptions 延时函数 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 cStep 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 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 A1B1 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 GPIOPinWrite 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 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 PORTD 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 PHLA1 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 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 GmRight iSpeed else 非减速区间 则加速到最大值 if GmRight iSpeed GiMaxSpeed GmRight iSpeed else GmRight iSpeed if GmRight iSpeed 0 设置速度下限 GmRight iSpeed 0 TimerLoadSet TIMER0 BASE TIMER A GuiAccelTable GmRight iSpeed 设置定时时间 Function name speedContrL Descriptions 左电机速度调节 input parameters 无 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 GmRight 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 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 根据传感器状态微调电机位置 if GucDistance FRONTR n n 2 else n 0 if GucDistance LEFT 1 m m 6 else m 0 leftMotorContr 电机驱动程序 break default break 是否完成任务判断 if GmLeft 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 选择需要设置的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 irCheck Descriptions 红外线传感器检测 input parameters 无 output parameters 无 Returned value 无 void irCheck void static uint8 ucState 0 static uint8 ucIRCheck switch ucState case 0 irSendFreq 32200 2 探测左右两侧近距 irSendFreq 35000 1 驱动斜角上的传感器检测 break case 1 ucIRCheck GPIOPinRead GPIO PORTB BASE 0 x3e 读取传感器状态 PWMGenDisable PWM BASE PWM GEN 2 禁止PWM 发生器2 PWMGenDisable PWM BASE PWM GEN 1 禁止PWM 发生器1 if ucIRCheck else GucDistance RIGHT 0 x02 if ucIRCheck else GucDistance LEFT 0 x02 if ucIRCheck else GucDistance FRONTR 0 x01 if ucIRCheck else GucDistance FRONTL 0 x01 break case 2 irSendFreq 36000 2 驱动检测左前右三个方向远距 break case 3 ucIRCheck GPIOPinRead GPIO PORTB BASE 0 x2a 读取传感器状态 PWMGenDisable PWM BASE PWM GEN 2 禁止PWM 发生器2 break case 4 irSendFreq 36000 2 重复检测左前右三个方向远距 break case 5 ucIRCheck 读取传感器状态 PWMGenDisable PWM BASE PWM GEN 2 禁止PWM 发生器2 if ucIRCheck else GucDistance RIGHT 0 x01 if ucIRCheck else GucDistance LEFT 0 x01 if ucIRCheck else GucDistance FRONT 0 x01 break default break ucState ucState 1 6 循环检测 Function name mazeSearch Descriptions 迷宫搜索 若发现分支路或者前方有挡板则退出 input parameters 无 output parameters 无 Returned value 无 void mazeSearch void int8 cL 0 cR 0 设定运行任务 GucMouseState GOAHEAD GmRight cDir MOTORGOAHEAD GmLeft cDir MOTORGOAHEAD GmRight uiPulse MAZETYPE ONEBLOCK GmLeft uiPulse MAZETYPE ONEBLOCK GmRight cState MOTORRUN GmLeft cState MOTORRUN while GmLeft cState MOTORSTOP if GucDistance FRONT 前方有墙 设置继续前进的步数 以实现能停止在单元格中心的目的 GmRight uiPulse GmRight uiPulseCtr 70 GmLeft uiPulse GmLeft uiPulseCtr 70 反复检测前方传感器的状态 以消除误判 while GucDistance FRONT if GmLeft uiPulseCtr 50 GmLeft uiPulse goto End 跳出循环 跳到程序结束处 程序执行到此步表明出现误判 则重新设定电机运行任务 GmRight uiPulse MAZETYPE ONEBLOCK GmLeft uiPulse MAZETYPE ONEBLOCK if cL 是否允许检测左边 if GucDistance LEFT GmLeft uiPulse GmLeft uiPulseCtr 74 反复检测前方传感器的状态 以消除误判 while GucDistance LEFT 跳出循环 跳到程序结束处 程序执行到此步表明出现误判 则重新设定电机运行任务 GmRight uiPulse MAZETYPE ONEBLOCK GmLeft uiPulse MAZETYPE ONEBLOCK else 左边有墙时开始允许检测左边 if GucDistance LEFT if cR 是否允许检测右边 if GucDistance RIGHT GmLeft uiPulse GmLeft uiPulseCtr 74 反复检测前方传感器的状态 以消除误判 while GucDistance RIGHT 跳出循环 跳到程序结束处 程序执行到此步表明出现误判 则重新设定电机运行任务 GmRight uiPulse MAZETYPE ONEBLOCK GmLeft uiPulse MAZETYPE ONEBLOCK else if GucDistance RIGHT End Function name mouseTurnright Descriptions 右转 input parameters 无 output parameters 无 Returned value 无 void mouseTurnright void 等待停止 while GmLeft cState MOTORSTOP while GmRight cState MOTORSTOP GucMouseState TURNRIGHT 标记电脑鼠的运行状态 开始右转 GmRight cDir MOTORGOBACK 控制右轮向后转动 GmRight uiPulse 41 设定右轮转动的步数 GmLeft cDir MOTORGOAHEAD 控制左轮向前转动 GmLeft uiPulse 41 设定左轮转动的步数 GmRight cState MOTORRUN 使能右轮转动 GmLeft cState MOTORRUN 使能左轮转动 等待右转完成 while GmLeft cState MOTORSTOP while GmRight cState MOTORSTOP Function name mouseTurnleft Descriptions 左转 input parameters 无 output parameters 无 Returned value 无 void mouseTurnleft void 等待停止 while GmLeft cState MOTORSTOP while GmRight cState MOTORSTOP GucMouseState TURNLEFT 标记电脑鼠的运行状态 开始左转 GmRight cDir MOTORGOAHEAD 控制右轮向前转动 GmRight uiPulse 41 设定右轮转动的步数 GmLeft cDir MOTORGOBACK 控制左轮向后转动 GmLeft uiPulse 41 设定左轮转动的步数 GmRight cState MOTORRUN 使能右轮转动 GmLeft cState MOTORRUN 使能左轮转动 等待左转完成 while GmLeft cState MOTORSTOP while GmRight cState MOTORSTOP Function name mouseTurnback Descriptions 后转 input parameters 无 output parameters 无 Returned value 无 void mouseTurnback void 等待停止 while GmLeft cState MOTORSTOP while GmRight cState MOTORSTOP GucMouseState TURNBACK 开始后转 GmRight cDir MOTORGOBACK GmRight uiPulse 81 GmLeft cDir MOTORGOAHEAD GmLeft uiPulse 81 GmLeft cState MOTORRUN GmRight cState MOTORRUN 等待后转完成 while GmLeft cState MOTORSTOP while GmRight cState MOTORSTOP Function name keyCheck Descriptions 读取按键 input parameters 无 output parameters 无 Returned value true 按键已按下 false 按键未按下 uint8 keyCheck void if GPIOPinRead GPIO PORTC BASE KEY 0 delay 50 while GPIOPinRead GPIO PORTC BASE KEY 0 return true else return false Function name SysTick ISR Descriptions 定时中断扫描 input parameters 无 output parameters 无 Returned value 无 void SysTick ISR void static int32 iL 0 iR 0 如果左电机长时间停止 则断电 if GmLeft cState MOTORSTOP iL else iL 0 if iL 500 GPIOPinWrite GPIO PORTD BASE PHLA1 PHLA2 PHLB1 PHLB2 0 x00 如果右电机长时间停止 则断电 if GmRight cState MOTORSTOP iR else iR 0 if iR 500 GPIOPinWrite GPIO PORTD BASE PHRA1 PHRA2 PHRB1 PHRB2 0 x00 红外线检测 irCheck Function name wallCheck Descriptions 根据传感器检测结果判断是否存在墙壁 input parameters 无 output parameters 无 Returned value ucWall 低三位从左到右一次代表左前右 1 为有墙 0 为没墙 uint8 wallCheck void uint8 ucWall 0 if GucDistance LEFT else ucWall if GucDistance FRONT else ucWall if GucDistance RIGHT else ucWall return ucWall Function name sensorInit Descriptions 传感器控制初始化 input parameters 无 output parameters 无 Returned value 无 void sensorInit void 设置连接到传感器信号输出脚的I O 口为输入模式 GPIODirModeSet GPIO PORTB BASE LEFTSIDE FRONTSIDE L FRONTSIDE FRONTSIDE R RIGHTSIDE GPIO DIR MODE IN 用PWM 驱动红外线发射头产生调制的红外线信号 SysCtlPeripheralEnable SYSCTL PERIPH PWM 使能PWM 模块 SysCtlPWMClockSet SYSCTL PWMDIV 1 PWM 时钟配置 不分频 初始化PWM2 该PWM 驱动斜角红外发射头 GPIOPinTypePWM GPIO PORTB BASE IRSEND BEVEL PB0 配置为PWM 功能 PWMGenConfigure PWM BASE PWM GEN 1 配置PWM 发生器1 PWM GEN MODE UP DOWN PWM GEN MODE NO SYNC 加计数 立即更 新 PWMOutputState PWM BASE PWM OUT 2 BIT true 使能PWM2 输出 PWMGenDisable PWM BASE PWM GEN 1 禁止PWM 发生器1 初始化PWM4 该PWM 驱动左前右正方向红外发射头 GPIOPinTypePWM GPIO PORTE BASE IRSEND SIDE PE0 配置为PWM 功能 PWMGenConfigure PWM BASE PWM GEN 2 配置PWM 发生器2 PWM GEN MODE UP DOWN PWM GEN MODE NO SYNC 加计数 立即更新 PWMOutputState PWM BASE PWM OUT 4 BIT true 使能PWM4 输出 PWMGenDisable PWM BASE PWM GEN 2 禁止PWM 发生器2 Function name stepMotorIint Descriptions 步进电机控制初始化 input parameters 无 output parameters 无 Returned value 无 void stepMotorIint void uint16 n 0 设置驱动步进电机的八个I O 口为输出模式 GPIODirModeSet GPIO PORTD BASE PHRA1 PHRA2 PHRB1 PHRB2 PHLA1 PHLA2 PHLB1 PHLB2 GPIO DIR MODE OUT 对左右电机转动的位置初始化 GPIOPinWrite GPIO PORTD BASE PHRA1 PHRA2 PHRB1 PHRB2 PHRA1 PHRA2 PHRB1 PHRB2 GPIOPinWrite GPIO PORTD BASE PHLA1 PHLA2 PHLB1 PHLB2 PHLA1 PHLA2 PHLB1 PHLB2 初始化加速 减速时定时器加载值的数据表 GuiAccelTable 0 2236068 GuiAccelTable 1 926179 for n 2 n 300 n GuiAccelTable n GuiAccelTable n 1 2 GuiAccelTable n 1 4 n 1 初始化定时器0 用来控制右电机的转速 SysCtlPeripheralEnable SYSCTL PERIPH TIMER0 使能定时器0 模块 TimerConfigure TIMER0 BASE TIMER CFG 32 BIT PER 配置为32 位周期计数模式 TimerLoadSet TIMER0 BASE TIMER A GuiAccelTable 0 设置定时时间 TimerIntEnable TIMER0 BASE TIMER TIMA TIMEOUT 设置为溢出中断 IntEnable INT TIMER0A 使能定时器0 中断 TimerEnable TIMER0 BASE TIMER A 使能定时器0 初始化定时器1 用来控制电机的转速 SysCtlPeripheralEnable SYSCTL PERIPH TIMER1 使能定时器1 模块 TimerConfigure TIMER1 BASE TIMER CFG 32 BIT PER 配置为32 位周期计数模式 TimerLoadSet TIMER1 BASE TIMER A GuiAccelTable 0 设置定时时间 TimerIntEnable TIMER1 BASE TIMER TIMA TIMEOUT 设置为溢出中断 IntEnable INT TIMER1A 使能定时器1 中断 TimerEnable TIMER1 BASE TIMER A 使能定时器1 Function name keyInit Descriptions 对连接按键的GPIO 口初始化 input parameters 无 output parameters 无 Returned value 无 void keyInit void GPIODirModeSet GPIO PORTC BASE KEY GPIO DIR MODE IN 设置按键口为输入 Function name sysTickInit Descriptions 系统节拍定时器初始化 input parameters 无 output parameter
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年湖北省肿瘤医院编制岗位招聘笔试真题附答案
- 2025年口腔医学模拟题与答案
- 2025年煤矿防灭火试题及答案
- (2025年)场(厂)内专用机动车辆安全驾驶技术考试题附答案
- 2025制药行业外部环境变化对内部经营影响全查及市场动态分析产品主体竞争力发展分册
- 2025制药医药行业市场供需分析及投资评估规划分析研究报告
- 2025制药中间体市场供需分析及投资策略规划发展研究报告
- 项痹病护理方案
- 2025再生资源循环行业政策支持分析及产业规模研究报告
- 2025共享经济行业共享出行与资源优化研究报告
- 2025下半年四川泸州合江县人力资源和社会保障局考调事业单位工作人员7人笔试考试备考试题及答案解析
- 护林员考试题库及答案
- 病人转运工作流程图解
- 习题课件:等腰三角形中的分类讨论思想
- 无人履带车辆的鲁棒轨迹跟踪控制研究
- 2025年 石家庄市市属国有企业招聘笔试考试试卷附答案
- 2025及未来5年中国丙烯醇市场分析及数据监测研究报告
- STEAM背景下小学劳动课程设计
- Y染色体微缺失机制-第2篇-洞察与解读
- 电气应急知识培训内容课件
- 2025年中级电工证考试题库(附答案)
评论
0/150
提交评论