单片机源程序如下:
- // ************************Declaration***************************************//
- // File name: Main
- // Author: Sat_Madman //
- // Date: 2015/3/31 0:14:22 //
- // Version Number: 1.0 //
- // Abstract:
- // Freescale source
- // Modification history:[including time, version, author and abstract] //
- // 2015/3/31 0:14:22 version 1.0 xxx //
- // Abstract: Main() //
- // SystemClock = 180Mhz (修改路径 Chip -> system_MK60DZ10.c -> pllinit(); //
- // *********************************end************************************** //
- #include "common.h"
- #include "include.h"
- #include "math.h"
- #include "stdlib.h"
- // ***************define*****************//
- #define INT_COUNT 0xFFFF //LPT 产生中断的计数次数
- #define DMASTATE 1 //0 关闭 1 打开
- CommunicationDeal PrintBuf;
- // ***************declare*****************//
- extern char flag;
- extern int RecNum[2];
- extern float Angle_P ; //舵机P参数 Debu8ig
- extern float Angle_D ; //舵机D参数 Debug
- extern int AD[6]; //AD采集处理后的值
- extern char position;
- uint32 LutraDistance;
- extern char LutraConFlag;
- void SystemInit(void);
- void Delay(void);
- // ***************code*******************//
- void main()
- {
- SystemInit();
- PrintBuf.start = 's';
- PrintBuf.position = '1';
- memcpy(PrintBuf.SpeedData, "123", 3);
- memcpy(PrintBuf.distance, "2341", 4);
- memcpy(PrintBuf.end,"d\n" ,2);
- EnableInterrupts;
- OLED_Print(20,0,"By Madman");
- OLED_Print(5,2,"L1:");
- OLED_Print(60,2,"L2:");
- OLED_Print(5,4,"R1:");
- OLED_Print(60,4,"R2:");
- OLED_Print(5,6,"F1:");
- OLED_Print(60,6,"F2:");
- // MotoGoLeft(3000)
- // MotoGoRight(3000)
- while(1)
- {
- if(flag == 1)
- {
- printf(" %d %d " ,RecNum[0] , RecNum[1]);
- Angle_P = (float)RecNum[0] / 1000.0;
- Angle_D = (float)RecNum[1] / 1000.0;
- OLED_DisNum(5 , 6 , RecNum[0] , 4);
- OLED_DisNum(30 , 6 , RecNum[1] , 4);
- RecNum[0] = 0;
- RecNum[1] = 0;
- flag = 0;
- }
- Delay();
- #if 1
- if(position == 0) OLED_Print(20,0,"ZHI");
- else if(position == 1) OLED_Print(20,0,"ZUO");
- else if(position == 3) OLED_Print(20,0,"RAM");
- else OLED_Print(20,0,"YOU");
- OLED_DisNum(30 , 2 , AD[0] , 3);
- OLED_DisNum(85 , 2 , AD[1] , 3);
- OLED_DisNum(30 , 4 , AD[2] , 3);
- OLED_DisNum(85 , 4 , AD[3] , 3);
- OLED_DisNum(30 , 6 , AD[4] , 3);
- OLED_DisNum(85 , 6 , AD[5] , 3);
- #endif
-
-
- if( LutraConFlag == 1)
- {
- LutrasonicRang();
-
- LutraConFlag = 0;
- }
- // if(key_get(KEY_1) == KEY_DOWN)
- // {
- // Delay();
- // if(key_get(KEY_1) == KEY_DOWN)
- // {
- // while(key_get(KEY_1) == KEY_DOWN);
- // led_turn(LED0);
- // }
- // }
- // LutraDistance = LutrasonicRang();
- // printf("\r\n LutraDis : %d\r\n", LutraDistance);
- }
- }
- void SystemInit(void)
- {
- DisableInterrupts; // 关闭总中断
- key_init(KEY_MAX); //按键初始化
-
- led_init(LED0); //设置GPIO口
- pit_init_ms ( PIT0 , 5 ); //设置定时周期 5ms进一次中断
- set_vector_handler(PIT0_VECTORn ,
- PIT0_IRQHandler); //设置中断服务函数
- MotoPwmInit(); //电机驱动初始化 FTM0_PRECISON = 10000(精度为0.01%)
- SteerInit() ; //舵机驱动初始化 FTM1_PRECISON = 1000
- ftm_quad_init(FTM2); //正交解码
- AdcInit(); //Adc初始化
- OLED_Init(); //OLED初始化
- gpio_init(STOPBUT,GPI,1);
- port_init_NoALT (STOPBUT,PULLUP );
- led (LED0,LED_ON); //开指示灯 关蜂鸣器
- GetMaxValue(); //读取赛道最大值
- OLED_Fill(0x00); //初始清屏
- // set_vector_handler(UART3_RX_TX_VECTORn,uart0_handler);
- #if DMASTATE
- uart_init(UART3,115200);
- dma0_uart_init((char*)&PrintBuf, 13); //串口3 DMA发送
- set_vector_handler(DMA0_VECTORn, dma0_print_isr); //DMA0中断服务函数
- UART3_BASE_PTR->C5 = (1 << 7); //开触发源
- enable_irq(DMA0_IRQn); //使能DMA传输
- DMA_ERQ |= (1 << 0);
- // dma1_uart_init((char *)&GetBuff , 11); //串口3 DMA接收
- // set_vector_handler(DMA1_VECTORn,dma1_get_isr); //DMA1中断服务函数
- // UART3_BASE_PTR->C5 = (1 << 5); //开触发源
- // enable_irq(DMA1_IRQn); //使能DMA传输
- #endif
-
- MotoGo(1500)
- Delay();
- LutrasonicInit();
- NVIC_SetPriorityGrouping(0);
- NVIC_SetPriority(PIT0_IRQn,1);
- NVIC_SetPriority(PORTD_IRQn,4);
- enable_irq(PIT0_IRQn); //定时器中断使能
- enable_irq(PORTD_IRQn);
- // uart_rx_irq_en(UART3);
- }
- void Delay(void)
- {
- uint32 i , j;
- for(i = 1000; i > 0; i--)
- for(j = 1000; j > 0 ; j--);
- }
- char* mitoa(int value, char* str)
- {
- int i = 0;
- do{
- *str++ = value % 10 + '0';
- value = value / 10;
- i++;
- } while (value != 0);
- return str;
- }
- void SendPrintBuf(CommunicationDeal* PrintBuff,char position,int speed,int distance)
- {
- PrintBuff->position = position + 48;
- memset(PrintBuff->SpeedData,0,3);
- memset(PrintBuff->distance,0,6);
- mitoa(speed,PrintBuff->SpeedData);
- mitoa(distance,PrintBuff->distance);
- }
- /**********************GPIO LIST***************************/
- // ********************FTM********************************
- // PC1、PA11 ------正交解码
- // PC2 ------FTM0_CH1_PIN (m1)
- // PC3 ------FTM0_CH1_PIN (m1)
- // PC4 ------FTM0_CH3_PIN (m2)
- // PA7 ------FTM0_CH4_PIN (m2)
- // PA8 -------Steer
- // *******************UART********************************
- // PC16 、PC17 -------Rx Tx
- // *******************ADC*********************************
- // PTB6 -------LEFT1
- // PTB7 -------LEFT2
- // PTB0 -------RIGHT1
- // PTB1 -------RIGHT1
复制代码
所有资料51hei提供下载:
S(前车).7z
(600.09 KB, 下载次数: 22)
|