- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- #include "mpu6050.h"
- #include "inv_mpu.h"
- #include "inv_mpu_dmp_motion_driver.h"
- #include "Motorinit.h"
- #include "usart_170.h"
- #include "pid.h"
- #include "timer.h"
- #include "motor_pid.h"
- #include "math.h"
- #include "control.h"
- #include "adc.h"
- #include "relay.h"
- /*
- *********************************************************************
- Design dy: GDOU-Lin桑
- 2017-9-2
- *********************************************************************
- */
- //Ëٶȣ¬·½Ïò£¬¿ª¹ØÊý¾Ý
- u8 rate,derection,key,redata;;
- int flag=1;
- float pitch,roll,yaw; //Å·À½Ç
- int t=1;
- short aacx,aacy,aacz; //¼ÓËٶȴ«¸ÐÆ÷ÔʼÊý¾Ý
- short gyrox,gyroy,gyroz; //ÍÓÂÝÒÇÔʼÊý¾Ý
- u16 adc1,adc2;
- float data1,data2;
- int main(void)
- {
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
- uart_init(115200);
- Usart_170_Init(9600);
- delay_init(); //ÑÓʱ³õʼ»¯
- LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
- MPU_Init(); //³õʼ»¯MPU6050
- Adc_Init(); //ADC³õʼ»¯
-
- TIM3_PWM_Init(19999,71); //ʹÓõçµ÷½øÐпØÖÆʱµÄ·ÖƵ
- TIM2_PWM_Init(199,7199); //ʹÓÃË®±ÃÊǵÄPWM¿ØÖÆ
- TIM4_Int_Init(5000-1,71); //ʹÓÃÖжϱ£³Öƽºâ
- Motor_init(); //µç»úµçµ÷³õʼ»¯
- // while(mpu_dmp_init())
- // ;
- while(1)
- {
- //printf("Start£¡");
- //´®¿Ú½ÓÊÕÖ¸ÁîÒÔ¼°Êý¾ÝµÄ´«Êä
-
- //Ïà¹ØADCÊýÖµµÄ»ñÈ¡
- data_get();
- // printf("%d ",redata);
- printf("%d %d %dn",rate,derection,key);
- // ratecontrol(rate); //ËٶȿØÖÆ
- // keycontrol(key); //¿ª¹Ø¿ØÖÆ
- // derectioncontrol(derection);//·½Ïò¿ØÖÆ
-
- }
- }
- #include "usart_170.h"
- #include "usart.h"
- #include "led.h"
- /**
- ************************************
- * @brief ³õʼ»¯´®¿Ú2
- * @param baudrate£º²¨ÌØÂÊ
- * @retval None
- ************************************
- */
- extern u8 rate,derection,key;
- extern u8 redata;
- extern u16 adc1,adc2;
- extern float data1,data2;
- //Ëٶȣ¬·½Ïò£¬¿ª¹ØÊý¾Ý
- #if EN_USART2_RX //Èç¹ûʹÄÜÁ˽ÓÊÕ
- //´®¿Ú1ÖжϷþÎñ³ÌÐò
- //×¢Òâ,¶ÁÈ¡USARTx->SRÄܱÜÃâĪÃûÆäÃîµÄ´íÎó
- u8 USART_RX_BUF[USART2_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.
- //½ÓÊÕ״̬
- //bit15£¬ ½ÓÊÕÍê³É±êÖ¾
- //bit14£¬ ½ÓÊÕµ½0x0d
- //bit13~0£¬ ½ÓÊÕµ½µÄÓÐЧ×Ö½ÚÊýÄ¿
- u16 USART_RX_STA=0; //½ÓÊÕ״̬±ê¼Ç
- void Usart_170_Init(u32 baudrate)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //ʹÄÜUSART1£¬GPIOAʱÖÓ
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
-
- //USART1_TX GPIOA.9
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.9
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
- GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.9
-
- //USART1_RX GPIOA.10³õʼ»¯
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA10
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
- GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.10
- //Usart1 NVIC ÅäÖÃ
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇÀÕ¼ÓÅÏȼ¶3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
- NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷
-
- //USART ³õʼ»¯ÉèÖÃ
- USART_InitStructure.USART_BaudRate = baudrate;//´®¿Ú²¨ÌØÂÊ
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
- USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæżУÑéλ
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½
- USART_Init(USART2, &USART_InitStructure); //³õʼ»¯´®¿Ú1
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿ªÆô´®¿Ú½ÓÊÜÖжÏ
- // USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//¿ªÆô´®¿Ú½ÓÊÜÖжÏ
- USART_Cmd(USART2, ENABLE);
- }
- void usart_170_send_char(u8 c)
- {
- while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); //Ñ»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
- USART_SendData(USART2, c);
-
- }
- //Êý¾Ý½ÓÊÕ²¢´¦Àí
- u8 com_data;
- u8 len;
- char Reveive_Buff[6];
- int i=0;
- void USART2_IRQHandler(void) //´®¿Ú1ÖжϷþÎñ³ÌÐò
- {
- #if SYSTEM_SUPPORT_OS //Èç¹ûSYSTEM_SUPPORT_OSΪÕ棬ÔòÐèÒªÖ§³ÖOS.
- OSIntEnter();
- #endif
- // if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ)
- // {
- //
- // USART_ReceiveData(USART2);
- // redata=USART2->DR; //¶Á³öÊý¾Ý
- //// redata=USART_ReceiveData(USART2);
- //// //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
- ////
- // }
- //
- //// while(!(USART2->SR & (1<<5)));//µÈ´ý½ÓÊÕµ½Êý¾Ý
- // rate=(redata&0x80);
- // derection=(redata&0x7c)>>2;
- // key=(redata&0x03);
- //
- //
- // LED1=!LED1;
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ)
- {
-
- USART_ReceiveData(USART2);
- //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
- Reveive_Buff[i] = USART2->DR;
- i=i+1;
-
- }
- if (i == 6)
- {
- USART1->DR = 0;
- USART1->SR = 0;
- i=0;
- rate=Reveive_Buff[2];
- key=Reveive_Buff[3];
- derection=3;
- LED0=!LED0;
- }
- #if SYSTEM_SUPPORT_OS //Èç¹ûSYSTEM_SUPPORT_OSΪÕ棬ÔòÐèÒªÖ§³ÖOS.
- OSIntExit();
- #endif
- }
- #endif
复制代码
实用!!!
0
|
|
|
|