上學(xué)期做的課程設(shè)計(jì)的一部分,
以前做過基于模電的磁懸浮,誤差很大,調(diào)節(jié)不方便,需要多各元件進(jìn)行,三極管發(fā)熱的情況也不容樂觀。所以在學(xué)習(xí)stm32的adc功能之后,想使用stm32單片機(jī)取代電位比較器。 stm32的adc功能比較強(qiáng)大,根據(jù)讀的st官方技術(shù)手冊,使用多通道后,adc的采樣速度甚至能達(dá)到us級,能滿足很多設(shè)計(jì)要求很高的場景,足夠磁懸浮使用。
1. 了解掌握stm32的綜合運(yùn)用方法 2. 加強(qiáng)了解 l298模塊的使用 3. 加強(qiáng)了解霍爾傳感器的使用 4. 加強(qiáng)了解 adc的使用
二、主要儀器設(shè)備、試劑或材料
1. PC,stm32 2. 霍爾傳感器 3. L298n 4. 杜邦線 電線若干
ewewewe.jpg (2.16 MB, 下載次數(shù): 131)
下載附件
001
2017-8-22 21:24 上傳
三、實(shí)驗(yàn)方法與步驟 代碼(部分)
- /* Includes------------------------------------------------------------------*/
- //# include <stdio. h>
- //# include <stdlib. h>
- #include "stm32f10x.h"
- #include"lcd1602.h"
- //#include "misc.h"
- //#include "misc.c"
- #include<stm32f10x_adc.h>
- //#include<stm32f10x_flash.h>
- /********************************宏定義------------------------------------*/
- #define ADC1_DR_Address ((u32)0x4001244C)
- #define ADC3_DR_Address ((u32)0x40013C4C)
- /********************************變量定義------------------------------------*/
- ADC_InitTypeDefADC_InitStructure;
- DMA_InitTypeDefDMA_InitStructure;
- GPIO_InitTypeDefGPIO_InitStructure; //定義GPIO初始化結(jié)構(gòu)體
- vu16 ADC1ConvertedValue = 0,ADC3ConvertedValue = 0;
- ErrorStatus HSEStartUpStatus;
- int AD_value;
- float CountV;
- float Std = (3.3)/4096;
- int CountV1;
- int CountV2;
- int CountV3;
- int hall;
- unsigned int jj = 0;
- //此表為 LED 的字模, 共陰數(shù)碼管 0-9 -
- unsigned int Disp_Tab[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //段碼控制
- //此表為8個數(shù)碼管位選控制, 共陰數(shù)碼管 1-8個 -
- unsigned intdispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F}; //位選控制 查表的方法控制
- unsigned int LedOut[10]; //變量定義
-
- /*********************************聲明函數(shù) -----------------------------------------------*/
- void RCC_Configuration(void);
- voidGPIO_Configuration(void);
- voidNVIC_Configuration(void);
- void delay_nms(u16 time);
- void Delay(vu32 nCount);
- /*******************************************************************************
- *
- * 主函數(shù)
- *
- *******************************************************************************/
- int main(void)
- { unsigned int LedNumVal = 0 ,LedNumVal1 = 0; //變量定義
- #ifdef DEBUG
- debug();
- #endif
-
- //RCC配置
- RCC_Configuration();
- // NVIC配置
- NVIC_Configuration();
- //GPIO配置
- GPIO_Configuration();
- //DMA1 通道配置
- DMA_DeInit(DMA1_Channel1); //設(shè)置成CH1 DMA中包含了7個通道(CH1-CH7)
- DMA_InitStructure.DMA_PeripheralBaseAddr =ADC1_DR_Address; //給DMA起始地址
- DMA_InitStructure.DMA_MemoryBaseAddr =(u32)&ADC1ConvertedValue;//DMA連接在內(nèi)存中的變量地址
- DMA_InitStructure.DMA_DIR =DMA_DIR_PeripheralSRC; //設(shè)置DMA傳輸方向單向傳輸
- DMA_InitStructure.DMA_BufferSize = 1; //設(shè)置DMA在傳輸時緩沖區(qū)的長度
- DMA_InitStructure.DMA_PeripheralInc =DMA_PeripheralInc_Disable; //設(shè)置DMA的外設(shè)遞增模式
- DMA_InitStructure.DMA_MemoryInc =DMA_MemoryInc_Disable;//設(shè)置DMA的內(nèi)存遞增模式
- DMA_InitStructure.DMA_PeripheralDataSize =DMA_PeripheralDataSize_HalfWord;//DMA在訪問時每次操作的數(shù)據(jù)長度
- DMA_InitStructure.DMA_MemoryDataSize =DMA_MemoryDataSize_HalfWord;
- DMA_InitStructure.DMA_Mode =DMA_Mode_Circular;//DMA的傳輸模式,連續(xù)不斷的循環(huán)模式
- DMA_InitStructure.DMA_Priority =DMA_Priority_High; //DMA的優(yōu)先級別:可以分為4級
- DMA_InitStructure.DMA_M2M =DMA_M2M_Disable;//DMA的2個memory中的變量互相訪問的
- DMA_Init(DMA1_Channel1, &DMA_InitStructure);//DMA整個模塊初始化
- //開啟DMA通道1
- DMA_Cmd(DMA1_Channel1, ENABLE);
-
- // ADC1 配置
- ADC_InitStructure.ADC_Mode =ADC_Mode_FastInterl; //ADC1工作在獨(dú)立模式
- ADC_InitStructure.ADC_ScanConvMode =ENABLE;//使能掃描
- ADC_InitStructure.ADC_ContinuousConvMode =ENABLE;;//ADC轉(zhuǎn)換工作在連續(xù)模式
- ADC_InitStructure.ADC_ExternalTrigConv =ADC_ExternalTrigConv_None;//由軟件控制轉(zhuǎn)換
- ADC_InitStructure.ADC_DataAlign =ADC_DataAlign_Right;//轉(zhuǎn)換數(shù)據(jù)右對齊
- ADC_InitStructure.ADC_NbrOfChannel = 14;//轉(zhuǎn)換通道為通道1
- ADC_Init(ADC1, &ADC_InitStructure); //初始化ADC
- //ADC1選擇信道14,音序器等級1,采樣時間239.5個周期
- ADC_RegularChannelConfig(ADC1,ADC_Channel_14, 1, ADC_SampleTime_28Cycles5);
- //使能ADC1模塊DMA
- ADC_DMACmd(ADC1, ENABLE);
- //打開ADC1
- ADC_Cmd(ADC1, ENABLE);
- //重置ADC1校準(zhǔn)寄存器
- ADC_ResetCalibration(ADC1);
- //等待ADC1校準(zhǔn)重置完成
- while(ADC_GetResetCalibrationStatus(ADC1));
- //開始ADC1校準(zhǔn)
- ADC_StartCalibration(ADC1);
- //等待ADC1校準(zhǔn)完成
- while(ADC_GetCalibrationStatus(ADC1));
- //使能ADC1軟件開始轉(zhuǎn)換
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- //*****************************************************************************//
- L1602_init();
- L1602_Clear();
- GPIO_SetBits(GPIOC,GPIO_Pin_2);
- L1602_string(1,1," wj works ");
- L1602_string(2,1," ALL SET OK ");
- delay_nms(1000);
- while (1)
- { unsignedint i ;
-
- // L1602_Clear();
- int num = 100;
- int str[4];
- //itoa(num, str, 10);
- AD_value =ADC_GetConversionValue(ADC1);
- CountV = ADC_GetConversionValue(ADC1)* Std;
- CountV1 = CountV/1;
- str[0] = CountV1 ;
-
- // "WJWORKSVoltage "
- //L1602_string(1,1,"WJWORKS");
- //L1602_string(2,1,"Voltage is ");
- //L1602_DispFloatNum(2,12,CountV);
- // L1602_string(2,16,"V");
- if(CountV>=1.1) GPIO_SetBits(GPIOE,GPIO_Pin_5);
- elseGPIO_ResetBits(GPIOE,GPIO_Pin_5);
- //delay_nms(200);
- }
- }
- void Delay(vu32 nCount)
- {
- for(; nCount != 0; nCount--);
- }
- /*******************************************************************************
- *
- * RCC配置
- *
- *******************************************************************************/
- void RCC_Configuration(void)
-
-
- {
- //復(fù)位RCC外部設(shè)備寄存器到默認(rèn)值
- RCC_DeInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能GPIOB的時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//使能GPIOB的時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB的時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//使能GPIOA的時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);//使能GPIOD的時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//打開USART的定時器
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
- //打開外部高速晶振
- RCC_HSEConfig(RCC_HSE_ON);
- //等待外部高速時鐘準(zhǔn)備好
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- //外部高速時鐘已經(jīng)準(zhǔn)別好
- if(HSEStartUpStatus == SUCCESS)
- {
-
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
-
- FLASH_SetLatency(FLASH_Latency_2);
-
- //配置AHB(HCLK)時鐘=SYSCLK
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- //配置APB2(PCLK2)鐘=AHB時鐘
- RCC_PCLK2Config(RCC_HCLK_Div1);
- //配置APB1(PCLK1)鐘=AHB 1/2時鐘
- RCC_PCLK1Config(RCC_HCLK_Div2);
- //配置ADC時鐘=PCLK2 1/4
- RCC_ADCCLKConfig(RCC_PCLK2_Div4);
-
- //配置PLL時鐘 == 外部高速晶體時鐘*9
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
-
- //配置ADC時鐘= PCLK2/4
- RCC_ADCCLKConfig(RCC_PCLK2_Div4);
- //使能PLL時鐘
- RCC_PLLCmd(ENABLE);
- //等待PLL時鐘就緒
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) ==RESET)
- {
- }
- //配置系統(tǒng)時鐘 = PLL時鐘
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- //檢查PLL時鐘是否作為系統(tǒng)時鐘
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- // 開啟DMA1時鐘 Enable
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 ,ENABLE);
- //開啟ADC1時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
-
- //下面是給各模塊開啟時鐘
- //啟動GPIO
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOB ,ENABLE);
- //啟動AFIO
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
- //啟動DMA時鐘
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
- //啟動ADC1時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
- }
- /*************************************************
- 函數(shù): voidGPIO_Config(void)
- 功能: GPIO配置
- **************************************************/
- void GPIO_Configuration(void)
- {
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//| RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz; //引腳頻率50M
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//-------------------------------------輸出配置
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
-
-
- //配置PA0為模擬輸入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//*************指示燈配置
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
- GPIO_Init(GPIOE,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
- GPIO_Init(GPIOE,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//-------------------------------------輸出配置
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_13|GPIO_Pin_14;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//-------------------------------------輸出配置
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//-------------------------------------輸出配置
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//-------------------------------------輸出配置
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13;
- GPIO_Init(GPIOE,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//-------------------------------------輸出配置
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//-------------------------------------輸出配置d10
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- }
- void delay_nms(u16 time)
- {
- u16 i=0;
- while(time--)
- {
- i=12000;
- while(i--) ;}}
-
- /*******************************************************************************
- * Function Name : NVIC_Configuration
- * Description : Configures Vector Table base location.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- #ifdef VECT_TAB_RAM
- /* Set the Vector Table base location at0x20000000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else /* VECT_TAB_FLASH */
- /* Set the Vector Table base location at0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
- #endif
- /* Configure and enable ADC interrupt */
- NVIC_InitStructure.NVIC_IRQChannel =ADC1_2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- #ifdef DEBUG
- /*******************************************************************************
- * Function Name : assert_failed
- * Description : Reports the name of the source file andthe source line number
- * where the assert_param errorhas occurred.
- * Input : - file: pointer to the source filename
- * - line: assert_param errorline source number
- * Output : None
- * Return : None
- *******************************************************************************/
- void assert_failed(u8* file,u32 line)
- {
- /* User can add his own implementation toreport the file name and line number,
- ex: printf("Wrong parameters value:file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
復(fù)制代碼
四、實(shí)驗(yàn)總結(jié) 彌補(bǔ)了之前使用電位比較器的缺點(diǎn),因?yàn)辄c(diǎn)位比較器沒有反饋的機(jī)制,所以最后小球在空中震蕩的頻率會越來越快,最后超過閾值會吸在通電線圈上。 現(xiàn)在通過stm32可以通過程序,例如 增加一個剎車的機(jī)制,設(shè)置一個隨時間變化的比例,通過改變in1,in2,ena的電平狀態(tài),逐步增加剎車時間,完成穩(wěn)定懸浮體的作用。
|