标签:
/******************************************************************* ** 文件名:擂台赛非标准平台1V1程序_检测原理AVR单片机示例程序,详细资料请购买产品后索要 ** Copyright (c) KXCT雷電科技 技术部 ** 创建人:KXCT ** 日 期:2015年11月 ** 修改人: ** 日 期: ** 描 述:仅供参考,擂台机器人调试能用的程序,包含了上台、边缘检测、推棋子、检测对手等功能 ** 版 本:V1.0.1 **********************************************************************/ #include<iom128v.h> #include<macros.h> #include<math.h> #define uint unsigned int #define uchar unsigned char #define IN1 {PORTC&=~BIT(0);} #define _IN1 {PORTC|=BIT(0);} #define IN2 {PORTC&=~BIT(1);} #define _IN2 {PORTC|=BIT(1);} int abs(int); char key=0,tim=0,dat,a; void delay_1ms(void) //1ms延时函数 { unsigned int i; for (i=0;i<1140;i++); } void delay(uint n) //N ms延时函数 { unsigned int i=0; for (i=0;i<n;i++) { delay_1ms(); if(key!=0)break; } } void delay_1(uint n) //N ms延时函数 { unsigned int i=0; for (i=0;i<n;i++) { delay_1ms(); if(key!=a)break; if((PIND&0x0f)==0) if(!((PINA&0x7f)==0x7f))break; } } void delay_2(uint n) //N ms延时函数 { unsigned int i=0; for (i=0;i<n;i++) { delay_1ms(); if(key!=0)break; if((PINA&0x7f)!=0x7f)break; } } void init(void) { DDRC|=0x03;//电机控制IO口初始化 DDRB|=0x60;//电机PWMIO口初始化 PORTB&=~0x60; DDRA&=0x80;//周边红外检测IO输入初始化 PORTA|=0x7f; DDRD&=0xf0;//红外边缘检测IO输入初始化 PORTD|=0x0f; DDRC&=~0xf0;//检测棋子端口 PORTC|=0xf0; } void zhongduan_init(void)/***************外部中断初始化****************/ { CLI(); //disable all interrupts EICRA = 0xFF; //extended ext ints EIMSK = 0x0F; SEI(); //re-enable interrupts } void Dj(int m,int n) { if((m)>0){IN1}//电机方向控制 else {_IN1} if((n)>0){IN2} else {_IN2} TCCR1A=0xa1; TCCR1B=0x09; m=abs(m); n=abs(n); OCR1A=m; OCR1B=n; } void bianyuan(void) { uint s2; s2=PIND|0xf0; switch(s2) { case 0xf1:Dj(-180,-90);//1检测到;动作:右转后退 delay(1000); Dj(180,-180); delay(800); break; case 0xf2:Dj(-90,-180);//2检测到;动作:右转后退 delay(1000); Dj(-180,180); delay(800); break; case 0xf4:Dj(180,100);//3检测到;动作:右转前进 delay(1000); Dj(-180,180); delay(800); break; case 0xf8:Dj(180,100);//4检测到;动作:右转前进 delay(1000); Dj(-180,180); delay(800); break; case 0xf3:Dj(-150,-150);//1、2检测到;动作:后退 delay(1000); break; case 0xf9:Dj(180,100);//1、4检测到;动作:右转前进 delay(2000); break; case 0xfc:Dj(150,150);//3、4检测到;动作:前进 delay(1000); break; case 0xf6:Dj(100,180);//2、3检测到;动作:左转前进 delay(2000); break; default :Dj(150,150); break; } } void qizi(void) { uint s3,n; s3=PINC&0xf0; if((PINC&0x30)!=0x30)//1或2检测到;动作:全速前进 Dj(220,220); else if((PINC&0x40)!=0x40)//3检测到;动作:右转检测 { Dj(-180,-180); for(n=150;n>0;n--) { if(key!=0)break; } Dj(220,-220); for(n=400;n>0;n--) { if((PINC&0x30)!=0x30)break; if(key!=0)break; delay_1ms(); } } else if((PINC&0x80)!=0x80)//4检测到;动作:左转检测 { Dj(-180,-180); for(n=150;n>0;n--) { if(key!=0)break; } Dj(-220,220); for(n=400;n>0;n--) { if((PINC&0x30)!=0x30)break; if(key!=0)break; delay_1ms(); } } else Dj(200,200); } void qidong(void) { Dj(0,0); delay(200); Dj(-240,-240); delay(300); Dj(-200,-210); delay(300); Dj(-200,-220); delay(300); delay(300); delay(300); Dj(-210,-230); delay(300); Dj(-220,220); delay(300); Dj(180,180); } void ruanqidong(void) { uint s1; do { s1=PINA; s1=s1&0x20; }while(!(s1==0)); } int zhoubian_scan(void) { uint s1; s1=PINA; s1=s1&0x7f; if(s1==0x7f) return 0; else return 1; } #pragma interrupt_handler int0_isr:iv_INT0 void int0_isr(void) { uint s1; s1=PINA; s1=s1&0x7f; key=10; } #pragma interrupt_handler int1_isr:iv_INT1 void int1_isr(void) { uint s1; s1=PINA; s1=s1&0x7f; key=20; } #pragma interrupt_handler int2_isr:iv_INT2 void int2_isr(void) { uint s1; s1=PINA; s1=s1&0x7f; key=30; } #pragma interrupt_handler int3_isr:iv_INT3 void int3_isr(void) { uint s1; s1=PINA; s1=s1&0x7f; key=40; } void bianyuan_scan(void) { int s2; s2=PIND&0x0f; if(s2==0) key=0; } void main(void) { init(); delay(2000); ruanqidong(); qidong(); zhongduan_init(); while(1) { a=key; switch(key) { case 0 : qizi(); break; case 10://1产生中断,四周未检测到,执行动作:后退+转向延时 Dj(-200,-200); delay(300); Dj(-180,180); delay(300); Dj(180,-180); delay(300); Dj(-200,-200); delay_1(200); Dj(200,-200); delay_1(300); Dj(150,150); bianyuan_scan(); break; case 20://2产生中断,四周未检测到,执行动作:后退+延时转向 Dj(-200,-200); delay(300); Dj(180,-180); delay(300); Dj(-180,180); delay(300); Dj(-200,-200); delay_1(200); Dj(-200,200); delay_1(350); Dj(150,150); bianyuan_scan(); break; case 30://3产生中断,四周未检测到,执行动作:前进+延时左转 Dj(200,200); delay_1(300); Dj(-200,200); delay_1(350); Dj(150,150); bianyuan_scan(); break; case 40://4产生中断,四周未检测到,执行动作:前进+延时转向 Dj(200,200); delay_1(300); Dj(200,-200); delay_1(300); Dj(150,150); bianyuan_scan(); break; default : bianyuan(); qizi(); break; } } }
最后请允许我插入一则广告,本人开的网店:KXCT雷電科技 主要经营机器人和智能车相关的灰度传感器和直流电机以及直流电机驱动等相关配件,欢迎大家前来选购和交流。
淘宝店铺链接:https://shop128260623.taobao.com/?spm=2013.1.1000126.d21.tDinKz
数字灰度传感器链接:https://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-11970373710.2.Eg1Umi&id=521915489173
模拟灰度传感器连接:https://item.taobao.com/item.htm?spm=2013.1.w4004-11970373722.23.D3sGjR&id=523220787646
标签:
原文地址:http://blog.csdn.net/zfy865628361/article/details/51329610