TA的每日心情 | 开心 2015-4-13 00:03 |
---|
签到天数: 3 天 连续签到: 1 天 [LV.2]偶尔看看I
|
/*
MK 2014-7-25 ÉÂÎ÷.ÏÌÑô
ÉÂÎ÷Àí¹¤Ñ§Ôº2014Äêµç×ÓÉè¼Æ¾ºÈü
×Ô¶¯Í£²´ÖÇÄܳµ(EÌâ)£¨1£©--³µÌå¿ØÖƲ¿·Ö
Ó²¼þƽ̨£º»ùÓÚSTM32f103RB×îСϵͳ
*/
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "timer.h"
#include "lcd.h"
#include "key.h"
void RUN_RIGHT(u16 right_temp,u16 left_temp);
void RUN_LEFT(u16 right_temp,u16 left_temp);
void RUN_GO(u16 right_temp,u16 left_temp);
void RUN_GO_Steady(u16 go_temp);
void RUN_BACK(u16 right_temp,u16 left_temp);
void Init_LCD_ShowString(void);
void Init_LCD_ShowNum(void);
void Dis_count(void);
void Reback(void);
void Sound_Control(void);
void Light_Control(void);
extern u32 x;
extern u32 y;
extern u32 flag;
u32 temp,Dis,Dis1,Dis2;
u16 t,i=1,zuo_ccr=470,you_ccr=470;
extern u8 TIM2CH1_CAPTURE_STA; //ÊäÈ벶»ñ״̬
extern u16 TIM2CH1_CAPTURE_VAL; //ÊäÈ벶»ñÖµ
int main(void)
{
Stm32_Clock_Init(9); //ϵͳʱÖÓÉèÖÃ
uart_init(72,9600); //´®¿Ú³õʼ»¯Îª9600
delay_init(72); //ÑÓʱ³õʼ»¯
LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
TIM2_Cap_Init(0XFFFF,36-1); //ÒÔ1MhzµÄƵÂʼÆÊý .³¬Éù²¨ÊäÈ벶»ñ
TIM3_PWM_Init(899,0);// ²»·ÖƵ¡£PWMƵÂÊ=72000/(899+1)=80Khz.¿ØÖƵç»úËÙ¶È
TIM4_Int_Init(9999,7199);// ʱ¼äÏÔʾ
// LCD_Init();
delay_ms(10);
// Init_LCD_ShowString();
delay_ms(20);
RUN_GO_Steady(100);
while(1)
{
RUN_GO(570,560);
// Trlg=0;
// delay_us(60);
// Trlg=1;
// delay_us(15);
// Trlg=0;
//
// if(TIM2CH1_CAPTURE_STA&0X80)//³É¹¦²¶»ñµ½ÁËÒ»´Î¸ßµçƽ
// {
// temp=TIM2CH1_CAPTURE_STA&0X3F;
// temp*=65536; //Òç³öʱ¼ä×ܺÍ
// temp+=TIM2CH1_CAPTURE_VAL; //µÃµ½×ܵĸߵçƽʱ¼ä
// Dis_count();
// Sound_Control();
//// Init_LCD_ShowNum();
//// delay_ms(50);
// TIM2CH1_CAPTURE_STA=0; //¿ªÆôÏÂÒ»´Î²¶»ñ
// }
Light_Control();
}
}
/***********************************************Dis_count**********************************************/
void Dis_count(void)
{
Dis=temp*340/4000+15;
Dis2=Dis%10;
Dis1=Dis/10;
printf("HIGH:%d cm\r\n",Dis); //´òÓ¡×ܵĸߵãƽʱ¼ä
if(Dis1>=400||Dis1<=2.5) {Dis=-0;Dis2=-0;}
}
/***********************************************Sound_Control**********************************************/
void Sound_Control(void)
{
//u32 Dis1;
if(Dis1<=35)
{
if(Dis1<=17)
{
RUN_BACK(550,550);
delay_ms(300);
}
else
{
RUN_RIGHT(570,560);
delay_ms(400);
RUN_GO(570,560);
}
}
else RUN_GO(570,560);
}
/***********************************************Light_Control**********************************************/
void Light_Control(void)
{
if((TCRT_IN1==1)&(TCRT_IN2==0)&(TCRT_IN3==0)&(TCRT_IN4==0)) RUN_RIGHT(570,560),delay_ms(5);
else if((TCRT_IN1==0)&(TCRT_IN2==1)&(TCRT_IN3==0)&(TCRT_IN4==0)) RUN_RIGHT(520,510),delay_ms(5);
else if((TCRT_IN1==0)&(TCRT_IN2==0)&(TCRT_IN3==0)&(TCRT_IN4==1)) RUN_LEFT(570,560),delay_ms(5);
else if((TCRT_IN1==0)&(TCRT_IN2==0)&(TCRT_IN3==1)&(TCRT_IN4==0)) RUN_LEFT(520,510),delay_ms(5);
else if((TCRT_IN1==0)&(TCRT_IN2==1)&(TCRT_IN3==1)&(TCRT_IN4==0)) RUN_GO(570,560),delay_ms(5);
else if((TCRT_IN1==1)&(TCRT_IN2==1)&(TCRT_IN3==1)&(TCRT_IN4==1)) RUN_BACK(520,510),delay_ms(5);
else if((TCRT_IN1==0)&(TCRT_IN2==0)&(TCRT_IN3==1)&(TCRT_IN4==1))
{RUN_LEFT(570,560);delay_ms(30);}
else if((TCRT_IN1==1)&(TCRT_IN2==1)&(TCRT_IN3==0)&(TCRT_IN4==0))
{RUN_RIGHT(570,560);delay_ms(30);}
}
/***********************************************RUN_RIGHT**********************************************/
void RUN_RIGHT(u16 right_temp,u16 left_temp)
{
// u16 i;
L298_OUT1=1;
L298_OUT3=0;
L298_OUT2=0;
L298_OUT4=1;
zuo_ccr=left_temp;
you_ccr=right_temp;
ZUO_CCR=899-zuo_ccr;
YOU_CCR=899-you_ccr;
}
/***********************************************RUN_LEFT**********************************************/
void RUN_LEFT(u16 right_temp,u16 left_temp)
{
// u16 i;
L298_OUT1=0;
L298_OUT3=1;
L298_OUT2=1;
L298_OUT4=0;
zuo_ccr=left_temp;
you_ccr=right_temp;
ZUO_CCR=899-zuo_ccr;
YOU_CCR=899-you_ccr;
}
/***********************************************RUN_GO**********************************************/
void RUN_GO(u16 right_temp,u16 left_temp)
{
// u16 i;
L298_OUT1=1;
L298_OUT3=1;
L298_OUT2=0;
L298_OUT4=0;
zuo_ccr=left_temp;
you_ccr=right_temp;
ZUO_CCR=899-zuo_ccr;
YOU_CCR=899-you_ccr;
}
/***********************************************RUN_GO_Steady**********************************************/
void RUN_GO_Steady(u16 go_temp)
{
u16 i;
L298_OUT1=1;
L298_OUT3=1;
L298_OUT2=0;
L298_OUT4=0;
for(i=0;i<go_temp;i++)//510£¬500 150
{
ZUO_CCR=899-zuo_ccr;
YOU_CCR=899-you_ccr;
zuo_ccr++;
you_ccr++;
delay_ms(1);
}
}
/***********************************************RUN_BACK**********************************************/
void RUN_BACK(u16 right_temp,u16 left_temp)
{
L298_OUT1=0;
L298_OUT3=0;
L298_OUT2=1;
L298_OUT4=1;
zuo_ccr=left_temp;
you_ccr=right_temp;
ZUO_CCR=899-zuo_ccr;
YOU_CCR=899-you_ccr;
}
/***********************************************Init_LCD_ShowString**********************************************/
void Init_LCD_ShowString(void)
{
POINT_COLOR=RED; //ÉèÖÃ×ÖÌåÑÕÉ« WHITE°×£¬BLACK£¬BLUE£¬RED£¬MAGENTA£¬GREEN£¬CYAN
//(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)
LCD_ShowString(45,90,200,24,24, "--MK-XinChi--");
LCD_ShowString(70,130,200,12,24,"2014-7-18");
LCD_ShowString(20,190,200,24,24,"SHOW TIME:");
LCD_ShowString(170,190,200,24,24,":");
LCD_ShowString(20,230,200,24,24,"SHOW Dis:");
LCD_ShowString(195,230,200,24,24,"cm");
LCD_ShowString(165,230,200,24,24,".");
}
/***********************************************Init_LCD_ShowNum**********************************************/
void Init_LCD_ShowNum(void)
{
// u32 x,y,Dis,Dis1,Dis2;
LCD_ShowNum(185,190,x,2,24);
LCD_ShowNum(140,190,y,2,24);
LCD_ShowNum(130,230,Dis1,3,24);
LCD_ShowNum(180,230,Dis2,1,24);
// LCD_ShowNum(130,260,Dis,5,24);
LCD_ShowNum(60,260,zuo_ccr,4,24);
LCD_ShowNum(130,260,you_ccr,4,24);
}
|
|