找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5134|回复: 0
打印 上一主题 下一主题
收起左侧

STM32学习-Adc显示电位器的内码,模拟看门狗

[复制链接]
跳转到指定楼层
楼主
ID:81272 发表于 2015-5-27 16:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
完整代码下载: Adc显示电位器的内码,模拟看门狗.rar (142.71 KB, 下载次数: 29)
  1. /*******************************************************************************

  2. STM32学习日志(9)----ADC 测试程序,显示电位器或者内部参考电压的内码,模拟看门狗监控

  3. 编译环境:        EWARM V5.30
  4. 硬件环境:        南京万利 EK-STM32F
  5. 主芯片        :        STM32F103VBT6
  6. STM32 FW:   V3.0.0
  7. 作者        :        szlihongtao
  8. 时间        :          2010-07-01
  9. 说明        :   1. 在 SysTick 定时中断程序中刷新LCD的显示
  10.                         2. 要设定 CH_DISP 的值
  11.                         3. 模拟看门狗监控的功能
  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显示电位器的内码
  19.                                                                         // 1显示内部参考电压的内码
  20. //******************************************************************************

  21. bit f_tb;                                                        // 基本定时标志
  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. // 延时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. // 时钟设置初始化
  43. //******************************************************************************
  44. static void RCC_Configuration(void)
  45. {
  46.   ErrorStatus HSEStartUpStatus;
  47. /*
  48. RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
  49. RCC_ITConfig 使能或者失能指定的RCC中断
  50. RCC_ClearFlag 清除RCC的复位标志位
  51. RCC_GetITStatus 检查指定的RCC中断发生与否
  52. RCC_ClearITPendingBit 清除RCC的中断待处理位
  53. */
  54.           /* RCC system reset(for debug purpose) */
  55.           // 时钟系统复位
  56.           RCC_DeInit();

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

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

  63.         // 等待HSE起振
  64.         // 该函数将等待直到HSE就绪,或者在超时的情况下退出
  65.           /* Wait till HSE is ready */
  66.           HSEStartUpStatus = RCC_WaitForHSEStartUp();

  67.           if(HSEStartUpStatus == SUCCESS)
  68.           {
  69.             /* HCLK = SYSCLK */
  70.                 // 设置AHB时钟(HCLK)
  71.             RCC_HCLKConfig(RCC_SYSCLK_Div1);        // 72 MHz

  72.             /* PCLK1 = HCLK/2 */
  73.                 // 设置低速AHB时钟(PCLK1)
  74.             RCC_PCLK1Config(RCC_HCLK_Div2);        // 36 MHz

  75.             /* PCLK2 = HCLK */
  76.                 // 设置高速AHB时钟(PCLK2)
  77.             RCC_PCLK2Config(RCC_HCLK_Div1);        // 72 MHz

  78.             /* ADCCLK = PCLK2/8 */
  79.                 // 设置ADC时钟(ADCCLK)
  80.                    RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  81.                 // 设置USB时钟(USBCLK)
  82.                 // USB时钟 = PLL时钟除以1.5
  83.                 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

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

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

  89.                 // 设置RTC时钟(RTCCLK)
  90.                 // 选择HSE时钟频率除以128作为RTC时钟
  91.                 RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

  92.                 // 使能或者失能RTC时钟
  93.                 // RTC时钟的新状态
  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.                 // 设置PLL时钟源及倍频系数
  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标志位设置与否
  107.             while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  108.             {
  109.             }

  110.             /* Select PLL as system clock source */
  111.                 // 设置系统时钟(SYSCLK)
  112.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  113.             /* Wait till PLL is used as system clock source */
  114.                 // 返回用作系统时钟的时钟源
  115.             while(RCC_GetSYSCLKSource() != 0x08)
  116.             {
  117.             }
  118.   }

  119.         // 使能或者失能AHB外设时钟
  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外设时钟
  128.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);

  129.         // 强制或者释放高速APB(APB2)外设复位
  130.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
  131.         // 退出复位状态
  132.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);

  133.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);        // adc时钟使能

  134.         // 强制或者释放低速APB(APB1)外设复位
  135.         RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);

  136.         // 强制或者释放后备域复位
  137.         RCC_BackupResetCmd(ENABLE);

  138.         // 使能或者失能时钟安全系统
  139.         RCC_ClockSecuritySystemCmd(DISABLE);
  140. }
  141. //******************************************************************************
  142. // NVIC设置
  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设置初始化
  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设置
  170. //******************************************************************************
  171. static void GPIO_Configuration(void)
  172. {
  173.         GPIO_InitTypeDef GPIO_InitStructure;

  174.         // 使能或者失能APB2外设时钟
  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参数复位
  198.           ADC_StructInit(&ADC_InitStructure); // 默认的参数

  199.         ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;// ADC1与ADC2工作在独立模式
  200.           ADC_InitStructure.ADC_ScanConvMode = DISABLE;                // 单通道
  201.           ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;        // 连续转换模式
  202.           ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;// 软件触发启动
  203.           ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;// ADC数据右对齐
  204.           ADC_InitStructure.ADC_NbrOfChannel = 1;                                // 进行规则转换的ADC通道的数目
  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. 实测,VDD=3.310V,ADC_Channel_17 = VREFINT 对应的内码为1509
  217. 理论上,VREFINT的计算值为 1509*3.310/4096= 1.219V
  218. */
  219. #endif

  220.         ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);        // 开中断
  221.         ADC_ClearFlag(ADC1,ADC_FLAG_EOC);                // 清除中断标志位

  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.         // 模拟看门狗监控单个的常规通道
  230.           ADC_AnalogWatchdogCmd(ADC1, ADC_AnalogWatchdog_SingleRegEnable);

  231.           /* Enable AWD interupt  开模拟看门狗监控功能 */
  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设置为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. */
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表