找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5138|回復(fù): 0
收起左側(cè)

STM32學(xué)習(xí)-Adc顯示電位器的內(nèi)碼,模擬看門狗

[復(fù)制鏈接]
ID:81272 發(fā)表于 2015-5-27 16:50 | 顯示全部樓層 |閱讀模式
完整代碼下載: Adc顯示電位器的內(nèi)碼,模擬看門狗.rar (142.71 KB, 下載次數(shù): 29)
  1. /*******************************************************************************

  2. STM32學(xué)習(xí)日志(9)----ADC 測試程序,顯示電位器或者內(nèi)部參考電壓的內(nèi)碼,模擬看門狗監(jiān)控

  3. 編譯環(huán)境:        EWARM V5.30
  4. 硬件環(huán)境:        南京萬利 EK-STM32F
  5. 主芯片        :        STM32F103VBT6
  6. STM32 FW:   V3.0.0
  7. 作者        :        szlihongtao
  8. 時(shí)間        :          2010-07-01
  9. 說明        :   1. 在 SysTick 定時(shí)中斷程序中刷新LCD的顯示
  10.                         2. 要設(shè)定 CH_DISP 的值
  11.                         3. 模擬看門狗監(jiān)控的功能
  12. *******************************************************************************/
  13. #include "stm32f10x.h"
  14. #include "stm32_m.h"
  15. #include "lcd.h"
  16. //******************************************************************************
  17. #define ADC1_DR_Address    ((u32)0x4001244C)        // ADC1->DR
  18. #define CH_DISP                                1                // 0顯示電位器的內(nèi)碼
  19.                                                                         // 1顯示內(nèi)部參考電壓的內(nèi)碼
  20. //******************************************************************************

  21. bit f_tb;                                                        // 基本定時(shí)標(biāo)志
  22. static INT16U tmr_1sec,cnt_test;

  23. extern bit f_adc;
  24. extern INT16U code_adc;
  25. //******************************************************************************
  26. static void delayms(INT16U cnt)
  27. {
  28.         INT16U i;

  29.         while(cnt--)
  30.                 for (i=0; i<7333; i++);
  31. }
  32. //******************************************************************************
  33. // 延時(shí)50ms
  34. //******************************************************************************
  35. static void delay(void)
  36. {
  37.         INT32U i;
  38.     static INT32U jjj=5240*50;

  39.         for (i=0; i<jjj; i++);
  40. }
  41. //******************************************************************************
  42. // 時(shí)鐘設(shè)置初始化
  43. //******************************************************************************
  44. static void RCC_Configuration(void)
  45. {
  46.   ErrorStatus HSEStartUpStatus;
  47. /*
  48. RCC_AdjustHSICalibrationValue 調(diào)整內(nèi)部高速晶振(HSI)校準(zhǔn)值
  49. RCC_ITConfig 使能或者失能指定的RCC中斷
  50. RCC_ClearFlag 清除RCC的復(fù)位標(biāo)志位
  51. RCC_GetITStatus 檢查指定的RCC中斷發(fā)生與否
  52. RCC_ClearITPendingBit 清除RCC的中斷待處理位
  53. */
  54.           /* RCC system reset(for debug purpose) */
  55.           // 時(shí)鐘系統(tǒng)復(fù)位
  56.           RCC_DeInit();

  57.         // 使能外部的8M晶振
  58.         // 設(shè)置外部高速晶振(HSE)
  59.           /* Enable HSE */
  60.           RCC_HSEConfig(RCC_HSE_ON);

  61.         // 使能或者失能內(nèi)部高速晶振(HSI)
  62.         RCC_HSICmd(DISABLE);

  63.         // 等待HSE起振
  64.         // 該函數(shù)將等待直到HSE就緒,或者在超時(shí)的情況下退出
  65.           /* Wait till HSE is ready */
  66.           HSEStartUpStatus = RCC_WaitForHSEStartUp();

  67.           if(HSEStartUpStatus == SUCCESS)
  68.           {
  69.             /* HCLK = SYSCLK */
  70.                 // 設(shè)置AHB時(shí)鐘(HCLK)
  71.             RCC_HCLKConfig(RCC_SYSCLK_Div1);        // 72 MHz

  72.             /* PCLK1 = HCLK/2 */
  73.                 // 設(shè)置低速AHB時(shí)鐘(PCLK1)
  74.             RCC_PCLK1Config(RCC_HCLK_Div2);        // 36 MHz

  75.             /* PCLK2 = HCLK */
  76.                 // 設(shè)置高速AHB時(shí)鐘(PCLK2)
  77.             RCC_PCLK2Config(RCC_HCLK_Div1);        // 72 MHz

  78.             /* ADCCLK = PCLK2/8 */
  79.                 // 設(shè)置ADC時(shí)鐘(ADCCLK)
  80.                    RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  81.                 // 設(shè)置USB時(shí)鐘(USBCLK)
  82.                 // USB時(shí)鐘 = PLL時(shí)鐘除以1.5
  83.                 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

  84.                 // 設(shè)置外部低速晶振(LSE)
  85.                 RCC_LSEConfig(RCC_LSE_OFF);

  86.                 // 使能或者失能內(nèi)部低速晶振(LSI)
  87.                 // LSE晶振OFF
  88.                 RCC_LSICmd(DISABLE);

  89.                 // 設(shè)置RTC時(shí)鐘(RTCCLK)
  90.                 // 選擇HSE時(shí)鐘頻率除以128作為RTC時(shí)鐘
  91.                 RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

  92.                 // 使能或者失能RTC時(shí)鐘
  93.                 // RTC時(shí)鐘的新狀態(tài)
  94.                 RCC_RTCCLKCmd(DISABLE);

  95.             /* Flash 2 wait state */
  96.             FLASH_SetLatency(FLASH_Latency_2);

  97.             /* Enable Prefetch Buffer */
  98.             FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  99.             /* PLLCLK = 8MHz * 9 = 72 MHz */
  100.                 // 設(shè)置PLL時(shí)鐘源及倍頻系數(shù)
  101.             RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  102.             /* Enable PLL */
  103.                 // 使能或者失能PLL
  104.             RCC_PLLCmd(ENABLE);

  105.             /* Wait till PLL is ready */
  106.                 // 檢查指定的RCC標(biāo)志位設(shè)置與否
  107.             while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  108.             {
  109.             }

  110.             /* Select PLL as system clock source */
  111.                 // 設(shè)置系統(tǒng)時(shí)鐘(SYSCLK)
  112.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  113.             /* Wait till PLL is used as system clock source */
  114.                 // 返回用作系統(tǒng)時(shí)鐘的時(shí)鐘源
  115.             while(RCC_GetSYSCLKSource() != 0x08)
  116.             {
  117.             }
  118.   }

  119.         // 使能或者失能AHB外設(shè)時(shí)鐘
  120.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
  121.                                                         |RCC_AHBPeriph_DMA2
  122.                                                         |RCC_AHBPeriph_SRAM
  123.                                                         |RCC_AHBPeriph_FLITF
  124.                                                         |RCC_AHBPeriph_CRC
  125.                                                         |RCC_AHBPeriph_FSMC
  126.                                                         |RCC_AHBPeriph_SDIO,DISABLE);
  127.         // 使能或者失能APB1外設(shè)時(shí)鐘
  128.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);

  129.         // 強(qiáng)制或者釋放高速APB(APB2)外設(shè)復(fù)位
  130.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
  131.         // 退出復(fù)位狀態(tài)
  132.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);

  133.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);        // adc時(shí)鐘使能

  134.         // 強(qiáng)制或者釋放低速APB(APB1)外設(shè)復(fù)位
  135.         RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);

  136.         // 強(qiáng)制或者釋放后備域復(fù)位
  137.         RCC_BackupResetCmd(ENABLE);

  138.         // 使能或者失能時(shí)鐘安全系統(tǒng)
  139.         RCC_ClockSecuritySystemCmd(DISABLE);
  140. }
  141. //******************************************************************************
  142. // NVIC設(shè)置
  143. //******************************************************************************
  144. static void NVIC_Configuration(void)
  145. {
  146.         NVIC_InitTypeDef  NVIC_InitStructure;

  147.           /* Configure one bit for preemption priority */
  148.           NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  149.           /* enabling interrupt */
  150.         NVIC_InitStructure.NVIC_IRQChannel=ADC1_2_IRQn;
  151.           NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  152.           NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  153.           NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  154.           NVIC_Init(&NVIC_InitStructure);
  155. }
  156. //******************************************************************************
  157. // SysTick設(shè)置初始化
  158. //******************************************************************************
  159. static void SysTick_Config1(void)
  160. {
  161.         #define SystemFreq                72000000.0        // 單位為Hz
  162.         #define TB_SysTick                2000.0                        // 單位為uS

  163.         INT32U ticks;

  164.         ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
  165.         ticks=ticks;
  166.         SysTick_Config(ticks);
  167. }
  168. //******************************************************************************
  169. // GPIO設(shè)置
  170. //******************************************************************************
  171. static void GPIO_Configuration(void)
  172. {
  173.         GPIO_InitTypeDef GPIO_InitStructure;

  174.         // 使能或者失能APB2外設(shè)時(shí)鐘
  175.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE, ENABLE);

  176.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  177.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  178.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  179.           GPIO_Init(GPIOC, &GPIO_InitStructure);

  180.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4; // key
  181.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  182.           GPIO_Init(GPIOD, &GPIO_InitStructure);

  183.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  184.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
  185.           GPIO_Init(GPIOC, &GPIO_InitStructure);                        // lcd_comm

  186.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;                // lcd_seg
  187.           GPIO_Init(GPIOE, &GPIO_InitStructure);

  188.           /* Configure PC.00 (ADC Channel10) as analog input  */
  189.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  190.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  191.         GPIO_Init(GPIOC, &GPIO_InitStructure);
  192. }
  193. //******************************************************************************
  194. static void ADC_Configuration(void)
  195. {
  196.         ADC_InitTypeDef ADC_InitStructure;

  197.         ADC_DeInit(ADC1);                                        // ADC參數(shù)復(fù)位
  198.           ADC_StructInit(&ADC_InitStructure); // 默認(rèn)的參數(shù)

  199.         ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;// ADC1與ADC2工作在獨(dú)立模式
  200.           ADC_InitStructure.ADC_ScanConvMode = DISABLE;                // 單通道
  201.           ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;        // 連續(xù)轉(zhuǎn)換模式
  202.           ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;// 軟件觸發(fā)啟動(dòng)
  203.           ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;// ADC數(shù)據(jù)右對齊
  204.           ADC_InitStructure.ADC_NbrOfChannel = 1;                                // 進(jìn)行規(guī)則轉(zhuǎn)換的ADC通道的數(shù)目
  205.           ADC_Init(ADC1, &ADC_InitStructure);

  206. #if (CH_DISP==0)
  207.         ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_28Cycles5);
  208. #else
  209.         ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 1, ADC_SampleTime_28Cycles5);
  210.         ADC_TempSensorVrefintCmd(ENABLE);                // 打開溫度傳感器以及參考電壓通道
  211. /*
  212. Table 12. Embedded internal reference voltage
  213. Symbol     Parameter Conditions          Min    Typ    Max    Unit
  214. VREFINT    Internal reference voltage    1.16   1.20   1.24    V
  215. 典型值為1.20V

  216. 實(shí)測,VDD=3.310V,ADC_Channel_17 = VREFINT 對應(yīng)的內(nèi)碼為1509
  217. 理論上,VREFINT的計(jì)算值為 1509*3.310/4096= 1.219V
  218. */
  219. #endif

  220.         ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);        // 開中斷
  221.         ADC_ClearFlag(ADC1,ADC_FLAG_EOC);                // 清除中斷標(biāo)志位

  222.         ADC_DMACmd(ADC1, DISABLE);


  223. #if 1
  224.         /* Configure high and low analog watchdog thresholds */
  225.         ADC_AnalogWatchdogThresholdsConfig(ADC1, 3000, 1000);

  226.         /* Configure channel10 as the single analog watchdog guarded channel */
  227.           ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_Channel_10);

  228.           /* Enable analog watchdog on one regular channel */
  229.         // 模擬看門狗監(jiān)控單個(gè)的常規(guī)通道
  230.           ADC_AnalogWatchdogCmd(ADC1, ADC_AnalogWatchdog_SingleRegEnable);

  231.           /* Enable AWD interupt  開模擬看門狗監(jiān)控功能 */
  232.           ADC_ITConfig(ADC1, ADC_IT_AWD, ENABLE);
  233. #endif

  234.           ADC_Cmd(ADC1, ENABLE);                                        // 使能ADC

  235.           ADC_ResetCalibration(ADC1);
  236.           while(ADC_GetResetCalibrationStatus(ADC1));

  237.           ADC_StartCalibration(ADC1);
  238.           while(ADC_GetCalibrationStatus(ADC1));

  239.           ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  240. }//******************************************************************************
  241. // LED5亮
  242. //******************************************************************************
  243. void Led_RW_ON(void)
  244. {
  245.           GPIO_SetBits(GPIOC,GPIO_Pin_4);
  246. }
  247. //******************************************************************************
  248. // LED5滅
  249. //******************************************************************************
  250. void Led_RW_OFF(void)
  251. {
  252.           GPIO_ResetBits(GPIOC,GPIO_Pin_4);
  253. }
  254. //******************************************************************************
  255. // pos=0-3
  256. //******************************************************************************
  257. static void myWrite_char(u8 pos,char car,u8 f_dot)
  258. {
  259.         write_char(&car,f_dot,pos);
  260. }
  261. //******************************************************************************
  262. static void disp_adc(INT16U code_adc1)
  263. {
  264.         INT8U i,ch;

  265.         for(i=0;i<4;++i)
  266.         {
  267.                 ch=code_adc1%10;
  268.                 code_adc1/=10;

  269.                 ch+=0x30;
  270.                 myWrite_char(3-i,ch,0);
  271.         }
  272. }//******************************************************************************
  273. // 主程序
  274. //******************************************************************************
  275. int main(void)
  276. {
  277.         RCC_Configuration();
  278.           GPIO_Configuration();
  279.         NVIC_Configuration();
  280.           SysTick_Config1();
  281.         Led_RW_ON();
  282.         delay();
  283.         Led_RW_OFF();
  284. //------------------------------------------------------------------------------
  285.     write_string("STM ");
  286.            delayms(333);
  287.     write_string("ADC ");
  288.            delayms(333);
  289.     write_string("TEST");
  290.            delayms(333);

  291.         ADC_Configuration();
  292. //------------------------------------------------------------------------------
  293.         for(;;)
  294.         {
  295.                 if (f_adc)
  296.                 {
  297.                         f_adc=0;
  298.                         GPIOC->ODR ^= GPIO_Pin_4;                // led5 toogle

  299.                         ++cnt_test;
  300.                         disp_adc(code_adc);
  301.                 }

  302.                 if (f_tb)                // 每隔2ms設(shè)置為1
  303.                 {
  304.                         f_tb=0;
  305.                         if (++tmr_1sec>=(1000/2))
  306.                         {
  307.                                 tmr_1sec=0;
  308.                                 GPIOC->ODR ^= GPIO_Pin_6;                // led3 toogle
  309.                         }
  310.                 }
  311.         }
  312. }
  313. //******************************************************************************
  314. #ifdef  USE_FULL_ASSERT

  315. /**
  316.   * @brief  Reports the name of the source file and the source line number
  317.   *   where the assert_param error has occurred.
  318.   * @param file: pointer to the source file name
  319.   * @param line: assert_param error line source number
  320.   * @retval : None
  321.   */
  322. void assert_failed(uint8_t* file, uint32_t line)
  323. {
  324.   /* User can add his own implementation to report the file name and line number,
  325.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  326.   /* Infinite loop */
  327.   while (1)
  328.   {
  329.   }
  330. }
  331. #endif

  332. /**
  333.   * @}
  334.   */
  335. //******************************************************************************
  336. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
  337. //******************************************************************************
  338. /*
  339.         LED2---------PC7
  340.         LED3---------PC6
  341.         LED4---------PC5
  342.         LED5---------PC4

  343.         KEY2---------PD3
  344.         KEY3---------PD4
  345. */
復(fù)制代碼



相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表