|
完整代碼下載:
Adc顯示電位器的內(nèi)碼,模擬看門狗.rar
(142.71 KB, 下載次數(shù): 29)
2015-5-27 16:49 上傳
點(diǎn)擊文件名下載附件
- /*******************************************************************************
- STM32學(xué)習(xí)日志(9)----ADC 測試程序,顯示電位器或者內(nèi)部參考電壓的內(nèi)碼,模擬看門狗監(jiān)控
- 編譯環(huán)境: EWARM V5.30
- 硬件環(huán)境: 南京萬利 EK-STM32F
- 主芯片 : STM32F103VBT6
- STM32 FW: V3.0.0
- 作者 : szlihongtao
- 時(shí)間 : 2010-07-01
- 說明 : 1. 在 SysTick 定時(shí)中斷程序中刷新LCD的顯示
- 2. 要設(shè)定 CH_DISP 的值
- 3. 模擬看門狗監(jiān)控的功能
- *******************************************************************************/
- #include "stm32f10x.h"
- #include "stm32_m.h"
- #include "lcd.h"
- //******************************************************************************
- #define ADC1_DR_Address ((u32)0x4001244C) // ADC1->DR
- #define CH_DISP 1 // 0顯示電位器的內(nèi)碼
- // 1顯示內(nèi)部參考電壓的內(nèi)碼
- //******************************************************************************
- bit f_tb; // 基本定時(shí)標(biāo)志
- static INT16U tmr_1sec,cnt_test;
- extern bit f_adc;
- extern INT16U code_adc;
- //******************************************************************************
- static void delayms(INT16U cnt)
- {
- INT16U i;
- while(cnt--)
- for (i=0; i<7333; i++);
- }
- //******************************************************************************
- // 延時(shí)50ms
- //******************************************************************************
- static void delay(void)
- {
- INT32U i;
- static INT32U jjj=5240*50;
- for (i=0; i<jjj; i++);
- }
- //******************************************************************************
- // 時(shí)鐘設(shè)置初始化
- //******************************************************************************
- static void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus;
- /*
- RCC_AdjustHSICalibrationValue 調(diào)整內(nèi)部高速晶振(HSI)校準(zhǔn)值
- RCC_ITConfig 使能或者失能指定的RCC中斷
- RCC_ClearFlag 清除RCC的復(fù)位標(biāo)志位
- RCC_GetITStatus 檢查指定的RCC中斷發(fā)生與否
- RCC_ClearITPendingBit 清除RCC的中斷待處理位
- */
- /* RCC system reset(for debug purpose) */
- // 時(shí)鐘系統(tǒng)復(fù)位
- RCC_DeInit();
- // 使能外部的8M晶振
- // 設(shè)置外部高速晶振(HSE)
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- // 使能或者失能內(nèi)部高速晶振(HSI)
- RCC_HSICmd(DISABLE);
- // 等待HSE起振
- // 該函數(shù)將等待直到HSE就緒,或者在超時(shí)的情況下退出
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* HCLK = SYSCLK */
- // 設(shè)置AHB時(shí)鐘(HCLK)
- RCC_HCLKConfig(RCC_SYSCLK_Div1); // 72 MHz
- /* PCLK1 = HCLK/2 */
- // 設(shè)置低速AHB時(shí)鐘(PCLK1)
- RCC_PCLK1Config(RCC_HCLK_Div2); // 36 MHz
- /* PCLK2 = HCLK */
- // 設(shè)置高速AHB時(shí)鐘(PCLK2)
- RCC_PCLK2Config(RCC_HCLK_Div1); // 72 MHz
- /* ADCCLK = PCLK2/8 */
- // 設(shè)置ADC時(shí)鐘(ADCCLK)
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- // 設(shè)置USB時(shí)鐘(USBCLK)
- // USB時(shí)鐘 = PLL時(shí)鐘除以1.5
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
- // 設(shè)置外部低速晶振(LSE)
- RCC_LSEConfig(RCC_LSE_OFF);
- // 使能或者失能內(nèi)部低速晶振(LSI)
- // LSE晶振OFF
- RCC_LSICmd(DISABLE);
- // 設(shè)置RTC時(shí)鐘(RTCCLK)
- // 選擇HSE時(shí)鐘頻率除以128作為RTC時(shí)鐘
- RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
- // 使能或者失能RTC時(shí)鐘
- // RTC時(shí)鐘的新狀態(tài)
- RCC_RTCCLKCmd(DISABLE);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- // 設(shè)置PLL時(shí)鐘源及倍頻系數(shù)
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- // 使能或者失能PLL
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- // 檢查指定的RCC標(biāo)志位設(shè)置與否
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- // 設(shè)置系統(tǒng)時(shí)鐘(SYSCLK)
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- // 返回用作系統(tǒng)時(shí)鐘的時(shí)鐘源
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- // 使能或者失能AHB外設(shè)時(shí)鐘
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
- |RCC_AHBPeriph_DMA2
- |RCC_AHBPeriph_SRAM
- |RCC_AHBPeriph_FLITF
- |RCC_AHBPeriph_CRC
- |RCC_AHBPeriph_FSMC
- |RCC_AHBPeriph_SDIO,DISABLE);
- // 使能或者失能APB1外設(shè)時(shí)鐘
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);
- // 強(qiáng)制或者釋放高速APB(APB2)外設(shè)復(fù)位
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
- // 退出復(fù)位狀態(tài)
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); // adc時(shí)鐘使能
- // 強(qiáng)制或者釋放低速APB(APB1)外設(shè)復(fù)位
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
- // 強(qiáng)制或者釋放后備域復(fù)位
- RCC_BackupResetCmd(ENABLE);
- // 使能或者失能時(shí)鐘安全系統(tǒng)
- RCC_ClockSecuritySystemCmd(DISABLE);
- }
- //******************************************************************************
- // NVIC設(shè)置
- //******************************************************************************
- static void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure one bit for preemption priority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- /* enabling interrupt */
- NVIC_InitStructure.NVIC_IRQChannel=ADC1_2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- //******************************************************************************
- // SysTick設(shè)置初始化
- //******************************************************************************
- static void SysTick_Config1(void)
- {
- #define SystemFreq 72000000.0 // 單位為Hz
- #define TB_SysTick 2000.0 // 單位為uS
- INT32U ticks;
- ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
- ticks=ticks;
- SysTick_Config(ticks);
- }
- //******************************************************************************
- // GPIO設(shè)置
- //******************************************************************************
- static void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- // 使能或者失能APB2外設(shè)時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4; // key
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
- GPIO_Init(GPIOC, &GPIO_InitStructure); // lcd_comm
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; // lcd_seg
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- /* Configure PC.00 (ADC Channel10) as analog input */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- //******************************************************************************
- static void ADC_Configuration(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- ADC_DeInit(ADC1); // ADC參數(shù)復(fù)位
- ADC_StructInit(&ADC_InitStructure); // 默認(rèn)的參數(shù)
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;// ADC1與ADC2工作在獨(dú)立模式
- ADC_InitStructure.ADC_ScanConvMode = DISABLE; // 單通道
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // 連續(xù)轉(zhuǎn)換模式
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;// 軟件觸發(fā)啟動(dòng)
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;// ADC數(shù)據(jù)右對齊
- ADC_InitStructure.ADC_NbrOfChannel = 1; // 進(jìn)行規(guī)則轉(zhuǎn)換的ADC通道的數(shù)目
- ADC_Init(ADC1, &ADC_InitStructure);
- #if (CH_DISP==0)
- ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_28Cycles5);
- #else
- ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 1, ADC_SampleTime_28Cycles5);
- ADC_TempSensorVrefintCmd(ENABLE); // 打開溫度傳感器以及參考電壓通道
- /*
- Table 12. Embedded internal reference voltage
- Symbol Parameter Conditions Min Typ Max Unit
- VREFINT Internal reference voltage 1.16 1.20 1.24 V
- 典型值為1.20V
- 實(shí)測,VDD=3.310V,ADC_Channel_17 = VREFINT 對應(yīng)的內(nèi)碼為1509
- 理論上,VREFINT的計(jì)算值為 1509*3.310/4096= 1.219V
- */
- #endif
- ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE); // 開中斷
- ADC_ClearFlag(ADC1,ADC_FLAG_EOC); // 清除中斷標(biāo)志位
- ADC_DMACmd(ADC1, DISABLE);
- #if 1
- /* Configure high and low analog watchdog thresholds */
- ADC_AnalogWatchdogThresholdsConfig(ADC1, 3000, 1000);
- /* Configure channel10 as the single analog watchdog guarded channel */
- ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_Channel_10);
- /* Enable analog watchdog on one regular channel */
- // 模擬看門狗監(jiān)控單個(gè)的常規(guī)通道
- ADC_AnalogWatchdogCmd(ADC1, ADC_AnalogWatchdog_SingleRegEnable);
- /* Enable AWD interupt 開模擬看門狗監(jiān)控功能 */
- ADC_ITConfig(ADC1, ADC_IT_AWD, ENABLE);
- #endif
- ADC_Cmd(ADC1, ENABLE); // 使能ADC
- ADC_ResetCalibration(ADC1);
- while(ADC_GetResetCalibrationStatus(ADC1));
- ADC_StartCalibration(ADC1);
- while(ADC_GetCalibrationStatus(ADC1));
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }//******************************************************************************
- // LED5亮
- //******************************************************************************
- void Led_RW_ON(void)
- {
- GPIO_SetBits(GPIOC,GPIO_Pin_4);
- }
- //******************************************************************************
- // LED5滅
- //******************************************************************************
- void Led_RW_OFF(void)
- {
- GPIO_ResetBits(GPIOC,GPIO_Pin_4);
- }
- //******************************************************************************
- // pos=0-3
- //******************************************************************************
- static void myWrite_char(u8 pos,char car,u8 f_dot)
- {
- write_char(&car,f_dot,pos);
- }
- //******************************************************************************
- static void disp_adc(INT16U code_adc1)
- {
- INT8U i,ch;
- for(i=0;i<4;++i)
- {
- ch=code_adc1%10;
- code_adc1/=10;
- ch+=0x30;
- myWrite_char(3-i,ch,0);
- }
- }//******************************************************************************
- // 主程序
- //******************************************************************************
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- NVIC_Configuration();
- SysTick_Config1();
- Led_RW_ON();
- delay();
- Led_RW_OFF();
- //------------------------------------------------------------------------------
- write_string("STM ");
- delayms(333);
- write_string("ADC ");
- delayms(333);
- write_string("TEST");
- delayms(333);
- ADC_Configuration();
- //------------------------------------------------------------------------------
- for(;;)
- {
- if (f_adc)
- {
- f_adc=0;
- GPIOC->ODR ^= GPIO_Pin_4; // led5 toogle
- ++cnt_test;
- disp_adc(code_adc);
- }
- if (f_tb) // 每隔2ms設(shè)置為1
- {
- f_tb=0;
- if (++tmr_1sec>=(1000/2))
- {
- tmr_1sec=0;
- GPIOC->ODR ^= GPIO_Pin_6; // led3 toogle
- }
- }
- }
- }
- //******************************************************************************
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report 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
- /**
- * @}
- */
- //******************************************************************************
- /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
- //******************************************************************************
- /*
- LED2---------PC7
- LED3---------PC6
- LED4---------PC5
- LED5---------PC4
- KEY2---------PD3
- KEY3---------PD4
- */
復(fù)制代碼
|
|