[STM32L0] 关于STM32L052采样AD问题

[复制链接]
1829|4
 楼主| 自民 发表于 2016-1-25 16:16 | 显示全部楼层 |阅读模式
用STM32L052采样AD,两个通道,其中一个通道要用过采样技术,通道的切换是在AD中断进行。请问是否每次都要校准HAL_ADCEx_Calibration_Start()?而每次AD中断中,参数设置的时候,是否都要关中断HAL_ADC_Stop_IT() ,参数设置完在开中断HAL_ADC_Start_IT()?
 楼主| 自民 发表于 2016-1-25 16:17 | 显示全部楼层
/* ADC2 init function */
void MX_ADC2_Init(void)
{
  ADC_ChannelConfTypeDef sConfig;
    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    */
  hadc.Instance = ADC1;
  hadc.Init.OversamplingMode = DISABLE;
  hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
  hadc.Init.Resolution = ADC_RESOLUTION12b;
  hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ContinuousConvMode = DISABLE;
  hadc.Init.DiscontinuousConvMode = ENABLE;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
  hadc.Init.DMAContinuousRequests = DISABLE;
  hadc.Init.EOCSelection = EOC_SINGLE_CONV;
  hadc.Init.Overrun = OVR_DATA_PRESERVED;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerFrequencyMode = ENABLE;
  hadc.Init.LowPowerAutoPowerOff = DISABLE;
  HAL_ADC_Init(&hadc);
    /**Configure for the selected ADC regular channel to be converted.
    */
  sConfig.Channel = ADC_CHANNEL_2;
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  HAL_ADC_ConfigChannel(&hadc, &sConfig);
}
/* ADC7 init function */
void MX_ADC7_Init(void)
{
  ADC_ChannelConfTypeDef sConfig;
    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    */
  hadc.Instance = ADC1;
  hadc.Init.OversamplingMode = ENABLE;
  hadc.Init.Oversample.Ratio = ADC_OVERSAMPLING_RATIO_128;
  hadc.Init.Oversample.RightBitShift = ADC_RIGHTBITSHIFT_3;
  hadc.Init.Oversample.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
  hadc.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
  hadc.Init.Resolution = ADC_RESOLUTION12b;
  hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ContinuousConvMode = DISABLE;
  hadc.Init.DiscontinuousConvMode = ENABLE;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
  hadc.Init.DMAContinuousRequests = DISABLE;
  hadc.Init.EOCSelection = EOC_SINGLE_CONV;
  hadc.Init.Overrun = OVR_DATA_PRESERVED;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerFrequencyMode = ENABLE;
  hadc.Init.LowPowerAutoPowerOff = DISABLE;
  HAL_ADC_Init(&hadc);
    /**Configure for the selected ADC regular channel to be converted.
    */
  sConfig.Channel = ADC_CHANNEL_7;
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  HAL_ADC_ConfigChannel(&hadc, &sConfig);
}
wahahaheihei 发表于 2016-1-25 18:15 | 显示全部楼层
  1. //stm32内部AD采样程序。运行通过的请大家放心使用。
  2. //有缺陷的地方请大家多多指教。本人才刚刚起步,还有劳各位大侠带一把呀!

  3. /* Includes ------------------------------------------------------------------*/
  4. #include "stm32f10x_lib.h"
  5. #include"stdio.h"


  6. /* Private typedef -----------------------------------------------------------*/
  7. /* Private define ------------------------------------------------------------*/
  8. #define ADC1_DR_Address ((u32)0x4001244C)
  9. int AD_value;


  10. /* Private macro -------------------------------------------------------------*/
  11. /* Private variables ---------------------------------------------------------*/
  12. ADC_InitTypeDef ADC_InitStructure;

  13. vu16 ADC_ConvertedValue;
  14. ErrorStatus HSEStartUpStatus;

  15. /* Private function prototypes -----------------------------------------------*/
  16. void RCC_Configuration(void);
  17. void GPIO_Configuration(void);

  18. void SysTick_Configuration(void);

  19. void SetupClock (void);


  20. #define LED1ON GPIO_SetBits(GPIOF,GPIO_Pin_0)
  21. #define LED1OFF GPIO_ResetBits(GPIOF,GPIO_Pin_0) //片选cs

  22. #define LED2ON GPIO_SetBits(GPIOF,GPIO_Pin_1)
  23. #define LED2OFF GPIO_ResetBits(GPIOF,GPIO_Pin_1)//复位rest
  24. #define LED3ON GPIO_SetBits(GPIOF,GPIO_Pin_2)
  25. #define LED3OFF GPIO_ResetBits(GPIOF,GPIO_Pin_2)//数据sdata
  26. #define LED4ON GPIO_SetBits(GPIOF,GPIO_Pin_3)
  27. #define LED4OFF GPIO_ResetBits(GPIOF,GPIO_Pin_3)//时钟sclk


  28. unsigned char Ver='A'; //驱动版本,默认为A版本,一共4个版本
  29. void delayms(unsigned int ii)//1ms延时函数
  30. {

  31. unsigned int i,x;
  32. for (x=0;x<ii;x++)
  33. {
  34. for (i=0;i<100;i++);
  35. }
  36. }

  37. void send(unsigned char value, unsigned char cd) //写8位数据
  38. {
  39. unsigned char i,dt;
  40. LED1OFF;
  41. LED4OFF;
  42. if(cd == 0)
  43. LED3OFF;
  44. else
  45. LED3ON;
  46. LED4ON;

  47. dt=value;
  48. for(i=0;i<8;i++)
  49. {
  50. LED4OFF;
  51. if(dt&0x80)
  52. LED3ON;
  53. else
  54. LED3OFF;
  55. dt=dt<<1;
  56. LED4ON;
  57. }
  58. LED1ON;//cs=1;
  59. }
  60. void readdata() //读数据
  61. {
  62. unsigned char i,j;
  63. unsigned char ch[6];
  64. send(0x09, 0); // 读数据
  65. LED1OFF;//cs=0;
  66. LED4OFF;//sclk=0;
  67. for (i=0;i<6;i++)
  68. {
  69. ch[i]=0;
  70. for (j=0;j<8;j++)
  71. {

  72. LED4ON;//sclk=1;
  73. if (!(GPIO_ReadOutputDataBit(GPIOF,GPIO_Pin_2)))//sdata==0)
  74. {
  75. ch[i]=ch[i]|(1<<(7-j));
  76. }
  77. LED4OFF;//sclk=0;
  78. }
  79. }

  80. switch(ch[0])
  81. {
  82. case 255:
  83. Ver='A';
  84. break;
  85. case 127:
  86. Ver='B';
  87. break;
  88. case 0:
  89. if(ch[1]+ch[2]+ch[3]==0)
  90. {
  91. Ver='D';
  92. }else
  93. {
  94. Ver='C';
  95. }
  96. break;
  97. }
  98. LED1ON;//cs=1;
  99. }

  100. void LCD_Initialize() //LCD初始化
  101. {
  102. unsigned char i;
  103. LED2ON;//rest=1;
  104. LED1ON;//cs=1;
  105. LED3OFF;//sdata=0;
  106. LED4ON;//sclk=1;
  107. LED2OFF;//rest=0;
  108. delayms(50);
  109. LED2ON;//rest=1;
  110. delayms(50);

  111. send(0x00, 0); // NOP
  112. delayms(5);

  113. send(0x01, 0); // LCD Software Reset
  114. delayms(5);

  115. send(0xC6, 0); // Initial Escape

  116. send(0xB9, 0); // Refresh set
  117. send(0x00, 1);

  118. readdata(); //读出数据

  119. send(0xB5, 0); // Gamma Curve Set
  120. send(0x01, 1);

  121. send(0xbd, 0); //common driver output select//很重要的
  122. if(Ver=='D')
  123. {
  124. send(0x04, 1);
  125. }else
  126. {
  127. send(0x00, 1);
  128. }

  129. send(0xBE, 0); // Power Control
  130. send(0x03, 1);


  131. send(0x11, 0); // Sleep out

  132. send(0xBA, 0); // Power Control
  133. send(0x7F, 1);
  134. send(0x03, 1);

  135. send(0x36, 0); // 扫描方式
  136. if (Ver=='A')
  137. {
  138. send(0x00|0x08,1); //RGB
  139. }
  140. else
  141. {
  142. send(0x00,1); //RGB
  143. }

  144. send(0xB7, 0); // Temperature gradient set
  145. for(i=0; i<14; i++)
  146. {
  147. send(0, 1);
  148. }


  149. send(0x29, 0); //display ON

  150. send(0x03, 0); // Booster Voltage ON

  151. delayms(20); // Booster Voltage stabilisieren lassen

  152. send(0x20, 0); //display inversion OFF

  153. send(0x25, 0); // Write contrast
  154. switch(Ver) //对比度设置
  155. {
  156. case 'A':
  157. send(70, 1); //对比度设置
  158. break;
  159. case 'B':
  160. send(67, 1); //对比度设置
  161. break;
  162. case 'C':
  163. //send(74, 1);
  164. send(66, 1);
  165. // send(64, 1); //对比度设置
  166. break;
  167. case 'D': //对比度设置
  168. send(39, 1);
  169. break;

  170. }

  171. }

  172. //************************
  173. //颜色模式设置:color=1为4096色模式
  174. // :color=0为256色模式
  175. //************************
  176. void LCD_ColorSet(unsigned char Color)
  177. {

  178. if (Color==1) {
  179. send(0x3a, 0); //interface pixel format
  180. send(0x03, 1); //0x03 为4096色,0x02为256色

  181. }
  182. else
  183. {
  184. send(0x3a, 0); //interface pixel format
  185. send(0x02, 1); //0x03 为4096色,0x02为256色



  186. send(0x2d, 0); //调色板设置
  187. if (Ver=='B'||Ver=='C')
  188. {
  189. //red
  190. send(~0x00, 1);
  191. send(~0x02, 1);
  192. send(~0x03, 1);
  193. send(~0x04, 1);
  194. send(~0x05, 1);
  195. send(~0x06, 1);
  196. send(~0x08, 1);
  197. send(~0x0f, 1);

  198. //green
  199. send(~0x00, 1);
  200. send(~0x02, 1);
  201. send(~0x03, 1);
  202. send(~0x04, 1);
  203. send(~0x05, 1);
  204. send(~0x06, 1);
  205. send(~0x08, 1);
  206. send(~0x0f, 1);
  207. //blue
  208. send(~0x00, 1);
  209. send(~0x03, 1);
  210. send(~0x06, 1);
  211. send(~0x0f, 1);
  212. }else
  213. {
  214. //red
  215. send(0x00, 1);
  216. send(0x02, 1);
  217. send(0x03, 1);
  218. send(0x04, 1);
  219. send(0x05, 1);
  220. send(0x06, 1);
  221. send(0x08, 1);
  222. send(0x0f, 1);

  223. //green
  224. send(0x00, 1);
  225. send(0x02, 1);
  226. send(0x03, 1);
  227. send(0x04, 1);
  228. send(0x05, 1);
  229. send(0x06, 1);
  230. send(0x08, 1);
  231. send(0x0f, 1);
  232. //blue
  233. send(0x00, 1);
  234. send(0x03, 1);
  235. send(0x06, 1);
  236. send(0x0f, 1);
  237. }


  238. }
  239. }
  240. void addset(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2) //坐标设置 4个参数依次为开始X,开始Y,结束x,结束Y
  241. {
  242. send(0x2a,0 );//column address set
  243. send(x1,1 );
  244. send(x2,1 );
  245. send(0x2B,0 );//page address set
  246. send(y1,1 );
  247. send(y2,1 );
  248. send(0x2C,0 );//memory write
  249. }
  250. //************************
  251. //清屏:color=1为4096色模式清屏
  252. // :color=0为256色模式清屏
  253. //************************
  254. void LCD_Clear(unsigned int value,unsigned char Color)
  255. {
  256. unsigned char x, y;
  257. addset(0,0,97,66);
  258. if (Color==1)
  259. {
  260. for(y = 0; y < 67; y ++)
  261. {
  262. for(x = 0; x < 49;x ++)
  263. { send(value>>4,1);
  264. send(((value&0x0f)<<4)|(value>>8),1);
  265. send(value,1);
  266. }
  267. }
  268. }
  269. else
  270. {
  271. for(y = 0; y < 67; y ++)
  272. {
  273. for(x = 0; x < 98; x ++)
  274. {
  275. send(value,1);
  276. }
  277. }
  278. }
  279. }
  280. //在指定位置显示一个字符(8*12大小)
  281. //dcolor为内容颜色,gbcolor为背静颜色
  282. void showdian(unsigned char x,unsigned char y,unsigned char dcolor,unsigned char bgcolor)
  283. {


  284. addset(x,y,x+1,y+1); //设置区域





  285. send(dcolor,1);


  286. send(bgcolor,1);


  287. }
  288. int main(void)
  289. {
  290. //int i,j;
  291. #ifdef DEBUG
  292. debug();
  293. #endif

  294. /* System clocks configuration ---------------------------------------------*/
  295. RCC_Configuration();
  296. SysTick_Configuration();

  297. /* GPIO configuration ------------------------------------------------------*/
  298. GPIO_Configuration();



  299. //printf("\r\n USART1 print AD_value -------------------------- \r\n");

  300. /* DMA channel1 configuration ----------------------------------------------*/
  301. /*DMA_DeInit(DMA1_Channel1);
  302. DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  303. DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
  304. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  305. DMA_InitStructure.DMA_BufferSize = 1;
  306. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  307. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  308. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  309. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  310. DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  311. DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  312. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  313. DMA_Init(DMA1_Channel1, &DMA_InitStructure);

  314. // Enable DMA channel1
  315. DMA_Cmd(DMA1_Channel1, ENABLE);
  316. */

  317. /* ADC1 configuration ------------------------------------------------------*/
  318. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  319. ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  320. ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  321. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  322. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  323. ADC_InitStructure.ADC_NbrOfChannel = 1;
  324. ADC_Init(ADC1, &ADC_InitStructure);

  325. /* ADC1 regular channel13 configuration */
  326. ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_239Cycles5);

  327. /* Enable ADC1 DMA */
  328. ADC_DMACmd(ADC1, ENABLE);

  329. /* Enable ADC1 */
  330. ADC_Cmd(ADC1, ENABLE);

  331. /* Enable ADC1 reset calibaration register */
  332. ADC_ResetCalibration(ADC1);
  333. /* Check the end of ADC1 reset calibration register */
  334. while(ADC_GetResetCalibrationStatus(ADC1));

  335. /* Start ADC1 calibaration */
  336. ADC_StartCalibration(ADC1);
  337. /* Check the end of ADC1 calibration */
  338. while(ADC_GetCalibrationStatus(ADC1));

  339. /* Start ADC1 Software Conversion */
  340. ADC_SoftwareStartConvCmd(ADC1, ENABLE);


  341. LCD_Initialize() ;
  342. LCD_ColorSet(0); //0为256色模式,1为4096色模式
  343. LCD_Clear(255,0); //清屏
  344. addset(0,0,103,81); //设置起止坐标,4个参数依次为开始X,开始Y,结束x,结束Y
  345. while(1)
  346. {
  347. AD_value=ADC_GetConversionValue(ADC1);


  348. }
  349. /*{
  350. for(i=0;i<100;i++)
  351. {for(j=0;j<20;j++)
  352. {
  353. AD_value=ADC_GetConversionValue(ADC1);
  354. // Printf message with AD value to serial port every 1 second
  355. showdian(i,65-AD_value/63 ,0x03,255);
  356. delayms(100);
  357. }
  358. }
  359. LCD_Clear(255,0); //清屏
  360. addset(0,0,103,81);
  361. }*/


  362. }
  363. /*******************************************************************************
  364. * Function Name : RCC_Configuration
  365. * Description : Configures the different system clocks.
  366. * Input : None
  367. * Output : None
  368. * Return : None
  369. *******************************************************************************/
  370. void RCC_Configuration(void)

  371. { /* RCC system reset(for debug purpose) */
  372. RCC_DeInit();

  373. /* Enable HSE */
  374. RCC_HSEConfig(RCC_HSE_ON);

  375. /* Wait till HSE is ready */
  376. HSEStartUpStatus = RCC_WaitForHSEStartUp();

  377. if(HSEStartUpStatus == SUCCESS)
  378. {
  379. /* HCLK = SYSCLK */
  380. RCC_HCLKConfig(RCC_SYSCLK_Div1);

  381. /* PCLK2 = HCLK */
  382. RCC_PCLK2Config(RCC_HCLK_Div1);

  383. /* PCLK1 = HCLK/2 */
  384. RCC_PCLK1Config(RCC_HCLK_Div2);

  385. /* ADCCLK = PCLK2/4 */
  386. RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  387. /* Flash 2 wait state */
  388. FLASH_SetLatency(FLASH_Latency_2);
  389. /* Enable Prefetch Buffer */
  390. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  391. /* PLLCLK = 8MHz * 9 = 56 MHz */
  392. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  393. /* Enable PLL */
  394. RCC_PLLCmd(ENABLE);

  395. /* Wait till PLL is ready */
  396. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  397. {
  398. }

  399. /* Select PLL as system clock source */
  400. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  401. /* Wait till PLL is used as system clock source */
  402. while(RCC_GetSYSCLKSource() != 0x08)
  403. {
  404. }
  405. }

  406. /* Enable peripheral clocks --------------------------------------------------*/
  407. /* Enable DMA clock */
  408. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  409. /* Enable ADC1 and GPIOC clock */
  410. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);

  411. /* Enable USART1 and GPIOA clock */
  412. //RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  413. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOC|
  414. RCC_APB2Periph_AFIO, ENABLE);


  415. }
  416. void SysTick_Configuration(void)
  417. {
  418. /* Select AHB clock(HCLK) as SysTick clock source */
  419. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

  420. /* Set SysTick Priority to 3 */
  421. NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);

  422. /* SysTick interrupt each 1ms with HCLK equal to 72MHz */
  423. SysTick_SetReload(72000);

  424. /* Enable the SysTick Interrupt */
  425. SysTick_ITConfig(ENABLE);
  426. }
  427. /*******************************************************************************
  428. * Function Name : GPIO_Configuration
  429. * Description : Configures the different GPIO ports.
  430. * Input : None
  431. * Output : None
  432. * Return : None
  433. *******************************************************************************/
  434. void GPIO_Configuration(void)
  435. {
  436. GPIO_InitTypeDef GPIO_InitStructure;

  437. /* Configure PC.03 (ADC Channel13) as analog input -------------------------*/
  438. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  439. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  440. GPIO_Init(GPIOC, &GPIO_InitStructure);

  441. // Configure PF. as Output push-pull
  442. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
  443. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  444. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  445. GPIO_Init(GPIOF, &GPIO_InitStructure);
  446. }



  447. #ifdef DEBUG
  448. /*******************************************************************************
  449. * Function Name : assert_failed
  450. * Description : Reports the name of the source file and the source line number
  451. * where the assert error has occurred.
  452. * Input : - file: pointer to the source file name
  453. * - line: assert error line source number
  454. * Output : None
  455. * Return : None
  456. *******************************************************************************/
  457. void assert_failed(u8* file, u32 line)
  458. {
  459. /* User can add his own implementation to report the file name and line number,
  460. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  461. /* Infinite loop */
  462. while (1)
  463. {
  464. }
  465. }
  466. #endif


  467. /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/


huangcunxiake 发表于 2016-1-25 23:00 | 显示全部楼层
Stop the ADC peripheral using HAL_ADC_Stop_IT()
大果仁儿 发表于 2016-1-26 12:52 | 显示全部楼层
学习了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

47

主题

195

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部