Altium Designer畫的原理圖和PCB圖如下:(51hei附件中可下載工程文件)
51hei.png (45.23 KB, 下載次數: 67)
下載附件
2020-12-17 18:33 上傳
以下為主函數內容:
- #include "GPIO.h"
- #include "usart.h"
- #include "config.h"
- #include "adc.h"
- #include "delay.h"
- #define Mz P24 //左轉電機端口
- #define Mf P25 //右轉電機端口
- #define LED1 P20 //左轉LED燈控制端口
- #define LED2 P21 //右轉LED燈控制端口
- #define P1n_pure_input(bitn) P1M1 |= (bitn), P1M0 &= ~(bitn)
- u16 ADC1=0,ADC2=0;
- void GPIO_Init_init() //GPIO初始化設置
- {
- GPIO_InitTypeDef GPIO_Init;
- GPIO_Init.Mode = GPIO_OUT_PP;
- GPIO_Init.Pin = P30|P31;
- GPIO_Inilize(GPIO_P3, &GPIO_Init);
- GPIO_Init.Mode = GPIO_OUT_PP;
- GPIO_Init.Pin = P20|P21;
- GPIO_Inilize(GPIO_P2, &GPIO_Init);
- }
- void COM1_Init() //串口1初始化設置
- {
- COMx_InitDefine COM1_InitDefine;
- COM1_InitDefine.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
- COM1_InitDefine.UART_BRT_Use = BRT_Timer2; //使用波特率, BRT_Timer1,BRT_Timer2
- COM1_InitDefine.UART_BaudRate = 115200ul; //波特率, ENABLE,DISABLE
- COM1_InitDefine.Morecommunicate = DISABLE; //多機通訊允許, ENABLE,DISABLE
- COM1_InitDefine.UART_RxEnable = ENABLE; //允許接收, ENABLE,DISABLE
- COM1_InitDefine.BaudRateDouble = ENABLE; //波特率加倍, ENABLE,DISABLE
- COM1_InitDefine.UART_Interrupt = ENABLE; //中斷控制, ENABLE,DISABLE
- COM1_InitDefine.UART_Polity = PolityHigh; //優先級, PolityLow,PolityHigh
- COM1_InitDefine.UART_P_SW = UART1_SW_P30_P31; //切換端口, UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17(必須使用內部時鐘)
- COM1_InitDefine.UART_RXD_TXD_Short = DISABLE; //內部短路RXD與TXD, 做中繼, ENABLE,DISABLE
- USART_Configuration(USART1, &COM1_InitDefine);
- //PrintString1("STC15F2K60S2 UART1 Test Prgramme!\r\n"); //SUART1發送一個字符串
- }
- void ADC_Init() //ADC10-11初始化
- {
- ADC_InitTypeDef ADC_InitType;
- ADC_InitType.ADC_Px = ADC_P10|ADC_P11; //設置要做ADC的IO, ADC_P10 ~ ADC_P17,ADC_P1_All
- ADC_InitType.ADC_Speed = ADC_180T; //ADC速度 ADC_90T,ADC_180T,ADC_360T,ADC_540T
- ADC_InitType.ADC_Power = ENABLE; //ADC功率允許/關閉 ENABLE,DISABLE
- ADC_InitType.ADC_AdjResult = ADC_RES_H8L2; //ADC結果調整, ADC_RES_H2L8,ADC_RES_H8L2
- ADC_InitType.ADC_Polity = PolityHigh; //優先級設置 PolityHigh,PolityLow
- ADC_InitType.ADC_Interrupt = DISABLE; //中斷允許 ENABLE,DISABLE
- ADC_Inilize(&ADC_InitType);
- P1n_pure_input(0x03);
- }
- void main()
- {
- u8 i = 0;
- Mz = Mf = 1;
- GPIO_Init_init(); //GPIO初始化
- COM1_Init(); //串口初始化
- ADC_Init(); //ADC初始化
- ADC_PowerControl(ENABLE); //使能內部工作電源ADC
- EA = 1; //打開總中斷
- while(1)
- {
- delay_ms(10);
- ADC1 = Get_ADC10bitResult(ADC_CH0)/10; //讀ADC0值;除以誤差系數10
- ADC2 = Get_ADC10bitResult(ADC_CH1)/10; //讀ADC1值;除以誤差系數10
- // TX1_write2buff(ADC1);
- if(ADC1 == 1024 || ADC2 == 1024)
- {
- PrintString1("1024\r\n");
- continue;
- }
- if(ADC1 > ADC2)
- {
- if(i == 2)
- {
- Mz = 1;
- Mf = 1;
- delay_ms(200);
- i = 1;
- }
- Mz = 0;
- Mf = 1;
- LED1 = 0;
- LED2 = 1;
- PrintString1("左側\r\n");
- }
- else if(ADC1 < ADC2)
- {
- if(i == 1)
- {
- Mz = 1;
- Mf = 1;
- delay_ms(200);
- i = 2;
- }
- PrintString1("右側\r\n");
- Mz = 1;
- Mf = 0;
- LED1 = 1;
- LED2 = 0;
-
- }
- else
- {
- if(i != 0)
- {
- Mz = 1;
- Mf = 1;
- delay_ms(200);
- i = 0;
- }
- Mz = 1;
- Mf = 1;
- LED1 = 1;
- LED2 = 1;
- PrintString1(".");
- }
-
- }
- }
復制代碼
51hei.png (10.35 KB, 下載次數: 51)
下載附件
2020-12-17 18:38 上傳
全部資料51hei下載地址(程序+PCB+原理圖):
PCB工程文件.7z
(3.91 MB, 下載次數: 126)
2020-12-17 18:40 上傳
點擊文件名下載附件
程序+電機驅動電路 下載積分: 黑幣 -5
|