|
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- #include "usart2.h"
- #include "string.h"
- #include "timer.h"
- #include "exti.h"
- #include "usmart.h"
- #include "led.h"
- #define RIGHT_V 350
- #define LEFT_V 385
- extern u32 time;
- u16 right_v=RIGHT_V;
- u16 left_v=LEFT_V;
- u8 last_state=0;
- u8 fseg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- u8 segbit[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
- u8 disbuf[4]={0,0,0,0};
- u8 LED[8]; //用于LED的8位显示缓存
- unsigned char LED_0F[] =
- {// 0 1 2 3 4 5 6 7 8 9 A b C d E F -
- 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0xFF,0xbf
- };
- void LED4_Display (void);
- void LED_OUT(u8 X);
- void LED4_Display (void)
- {
- unsigned char *led_table; // 查表指针
- u8 i;
- //显示第1位
- led_table = LED_0F + LED[0];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0x01);
- RCLK=0;
- RCLK=1;
- //显示第2位
- led_table = LED_0F + LED[1];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0x02);
- RCLK = 0;
- RCLK = 1;
- //显示第3位
- led_table = LED_0F + LED[2];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0x04);
- RCLK = 0;
- RCLK = 1;
- //显示第4位
- led_table = LED_0F + LED[3];
- i = *led_table;
- LED_OUT(i);
- LED_OUT(0x08);
- RCLK = 0;
- RCLK = 1;
- }
- void LED_OUT(u8 X)
- {
- u8 i;
- for(i=8;i>=1;i--)
- {
- if (X&0x80) DIO=1; else DIO=0;
- X<<=1;
- SCLK = 0;
- SCLK = 1;
- }
- }
- void zhixing()
- {
- TIM_SetCompare2(TIM3,right_v);
- TIM_SetCompare3(TIM3,left_v);
- }
- void youzhuan()
- {
- TIM_SetCompare2(TIM3,right_v+350);//youlun
- TIM_SetCompare3(TIM3,left_v-385);
- }
- void zuozhuan()
- {
- TIM_SetCompare2(TIM3,right_v-350);//youlun
- TIM_SetCompare3(TIM3,left_v+350);
- }
- void stop()
- {
- TIM_SetCompare2(TIM3,899);//youlun
- TIM_SetCompare3(TIM3,899);
- }
- int main(void)
- {
- u8 t=5;
- u32 l=0;
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- uart_init(115200);
- usart2_init(115200);
- TIM3_PWM_Init(899,0); //不分频。PWM频率=72000000/900=80Khz //初始化与LED连接的硬件接口
- TIM4_Int_Init(4000-1,2000-1);
- HONGWAI();
-
-
- LED_Init();
- LED[0]=0;
- LED[1]=0;
- LED[2]=0;
- LED[3]=0;
- LED4_Display ();
- while((zuo==0)||(zhong==0)||(you==0))
- {
- zhixing();
- }
- BEEP=0;
- delay_ms(500);
- BEEP=1;
- time=0;
- while(time!=5)
- { stop();
-
- LED[0]=(t-time)%10;
- LED[1]=0;
- LED[2]=0;
- LED[3]=0;
- LED4_Display ();
- }
- time=0;
- zhixing();
- delay_ms(300);
- while(1)
- {
- LED[0]=time%10;
- LED[1]=(time/10)%10;
- LED[2]=(time/100)%10;
- LED4_Display ();
- if((zuo==0)&&(zhong==1)&&(you==0))
- {
- zhixing();
- last_state=0;
- }
- else if((zuo==1)&&(you==0))
- {
- zuozhuan();
- last_state=1;
- }
- else if((you==1)&&(zuo==0))
- {
- youzhuan();
- last_state=2;
-
- }
- else if((zuo==1)&&(zhong==1)&&(you==1))
- {
- l=time;
- stop();
- BEEP=0;
- delay_ms(500);
- BEEP=1;
- while(1)
- {
- LED[0]=l%10;
- LED[1]=(l/10)%10;
- LED[2]=(l/100)%10;
- LED4_Display ();
- }
- }
- else if((zuo==0)&&(zhong==0)&&(you==0))
- {
- switch(last_state)
- {
- case 0:zhixing();break;
- case 1:zuozhuan();break;
- case 2:youzhuan();break;
- }
-
- }
-
- }
- }
复制代码
|
-
-
智能小车.7z
225.94 KB, 下载次数: 34, 下载积分: 黑币 -5
|