这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » wenyangzeng的进程5:使用STM32CubeMX配置ADC

共9条 1/1 1 跳转至

wenyangzeng的进程5:使用STM32CubeMX配置ADC

工程师
2015-12-01 14:09:32     打赏

进程1:收到STM32L053 Nucleo

进程2:建立MDK编译环境

进程3:驱动段码式液晶

进程4:数字时钟

进程5:使用CubeMX配置ADC

今天终于收到EEPW寄来的STM32L053 Nucleo板。先晒一下:

NUCLEO


还附了任务卡。一定会认真完成任务。



专家
2015-12-01 21:02:16     打赏
2楼
加油

工程师
2015-12-01 22:48:45     打赏
3楼

进程2:建立MDK编译环境

MDK4不支持STM32L0系列,只能MDK5支持。

在MDK5的“Packs installer"选项的"Devices"窗口中添加STM32L0系列,然后在”packs"窗口中选择对应的STM32L0的"Action"-Install进行安装。

安装成功后,在项目选项的“Device"就能够找到STM32L053R8这片芯片了。

至此,STM32L053的编程环境已经建立完成。


工程师
2015-12-06 15:54:53     打赏
4楼

进程3:驱动LCD


准备给STM32L053 NUCLEO板配置一片LCD。STM32L053的最主要特点是低功耗,如果为其配置TFT或点阵LCD,将使得STM32L053的低功耗特性大打折扣了,体现不出本次活动对这颗芯片性能的期望。于是决定为它配置段码式LCD模块,手头刚好有一片GH08172T液晶模块,正好用上。

一开始认为调试应该不复杂,ST公司的STM32CubeFW_L0_V1.3的库是现成的,里面就有LCD例程,拿来就用应该没问题。硬件连接见下图,用1只IC插座和杜邦线将LCD和开发板连接完成。

GH08172T真值表:


实际的调试工作并不顺利,费力不少时间,走了不少弯路,STM32Cube_FW_L0__V1.3库里例程好像是从STM32L1XX系列移植过来的,STM32L0XX系列LCD引脚与STM32L1XX系列定义相差不少,GPIO引脚如果直接套用过来根本不能正常工作。修改后见下表:

在STM32L1XX基础上修改的引脚:

GPIO初始化的修改:
void HAL_LCD_MspInit(LCD_HandleTypeDef *hlcd)
{
GPIO_InitTypeDef  GPIO_InitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_BACKUPRESET_FORCE();
__HAL_RCC_BACKUPRESET_RELEASE();
RCC_OscInitStruct.OscillatorType =  RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{while(1);
}
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Pin       =  GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |GPIO_PIN_6|GPIO_PIN_7| GPIO_PIN_8 | GPIO_PIN_9 |GPIO_PIN_10 |GPIO_PIN_15;
GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull      = GPIO_NOPULL;
GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH  ;
GPIO_InitStruct.Alternate = GPIO_AF1_LCD;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin       = GPIO_PIN_0|GPIO_PIN_1 |GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 |GPIO_PIN_8 | GPIO_PIN_9 \
| GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin       = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |GPIO_PIN_4| GPIO_PIN_5;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
__HAL_RCC_LCD_CLK_ENABLE();
}
LCD初始化
void BSP_LCD_GLASS_Init(void)
{
LCDHandle.Instance = LCD;
LCDHandle.Init.Prescaler = LCD_PRESCALER_2;
LCDHandle.Init.Divider = LCD_DIVIDER_16;
LCDHandle.Init.Duty = LCD_DUTY_1_4;
LCDHandle.Init.Bias = LCD_BIAS_1_3;
LCDHandle.Init.VoltageSource = LCD_VOLTAGESOURCE_EXTERNAL;//LCD_VOLTAGESOURCE_INTERNAL;
LCDHandle.Init.Contrast = LCD_CONTRASTLEVEL_7;
LCDHandle.Init.DeadTime = LCD_DEADTIME_2;
LCDHandle.Init.PulseOnDuration = LCD_PULSEONDURATION_7;
LCDHandle.Init.HighDrive = DISABLE;
LCDHandle.Init.BlinkMode = LCD_BLINKMODE_OFF;
LCDHandle.Init.BlinkFrequency = LCD_BLINKFREQUENCY_DIV8;
HAL_LCD_Init(&LCDHandle);
}
演示部分代码:
uint8_t LCD_String[46]= " STM32L053R8 DISCOVERY DRIVE GH08172 LCD DEMO ";
int main(void)
{
HAL_Init();
SystemClock_Config();
BSP_LCD_GLASS_Init();
BSP_LCD_GLASS_ScrollSentence(LCD_String, SCROLL_NUM, SCROLL_SPEED_L);
while (1)
{
}
}
系统时钟:
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
RCC_OscInitStruct.MSICalibrationValue=0x00;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
}

 


另外开发板上的SB13-SB14还要断开,SB62-SB63要短接,否则LCD的第1和第2位数码显示会缺笔画。




视频地址:http://player.youku.com/player.php/sid/XMTQwNTYxOTQ3Mg==/v.swf

STM32L053R8_NUCLEO.rar 点击下载HEX文件




工程师
2015-12-10 15:27:58     打赏
5楼

进程4:数字时钟

使用STM32L0低功耗系列的RTC来制作数字时钟再合适不过了,而且代码也很简单,将官方的STM32Cuble_FW_L0_V1.30\STM32L053R8-Nucleo\Example\RTC\RTC_TimeStamp演示稍加修改,结合进程3LCD代码,就能轻松实现。

主要代码:


uint8_t Time_str[6]={0};
int main(void)
{  HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
BSP_LCD_GLASS_Init();
RtcHandle.Instance = RTC;
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if(HAL_RTC_Init(&RtcHandle) != HAL_OK)
{    Error_Handler();  }
RTC_TimeStampConfig();
while (1)
{ RTC_CalendarShow(); }
}
//系统时钟
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
RCC_OscInitStruct.MSICalibrationValue = 0x00;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{  Error_Handler(); }
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{    Error_Handler();  }
}
//RTC配置
static void RTC_TimeStampConfig(void)
{  RTC_DateTypeDef sdatestructure;
RTC_TimeTypeDef stimestructure;
RTC_TamperTypeDef stamperstructure;
stamperstructure.Filter = RTC_TAMPERFILTER_DISABLE;
stamperstructure.Tamper = RTC_TAMPER_1;
stamperstructure.Trigger = RTC_TAMPERTRIGGER_FALLINGEDGE;
stamperstructure.SamplingFrequency = RTC_TAMPERSAMPLINGFREQ_RTCCLK_DIV4096;
stamperstructure.PrechargeDuration = RTC_TAMPERPRECHARGEDURATION_1RTCCLK ;
stamperstructure.TamperPullUp = RTC_TAMPER_PULLUP_DISABLE;
stamperstructure.TimeStampOnTamperDetection = RTC_TIMESTAMPONTAMPERDETECTION_ENABLE;
HAL_RTCEx_SetTamper(&RtcHandle, &stamperstructure);
HAL_RTCEx_SetTimeStamp_IT(&RtcHandle, RTC_TIMESTAMPEDGE_RISING, RTC_TIMESTAMPPIN_DEFAULT);
sdatestructure.Year = 0x14;
sdatestructure.Month = RTC_MONTH_APRIL;
sdatestructure.Date = 0x14;
sdatestructure.WeekDay = RTC_WEEKDAY_MONDAY;
if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK)
{    Error_Handler();   }
stimestructure.Hours = 0x23;
stimestructure.Minutes = 0x58;
stimestructure.Seconds = 0x00;
stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BCD ) != HAL_OK)
{  Error_Handler();  }
}
//时钟显示
void RTC_CalendarShow(void)
{  RTC_DateTypeDef sdatestructureget;
RTC_TimeTypeDef stimestructureget;
HAL_RTC_GetTime(&RtcHandle, &stimestructureget, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&RtcHandle, &sdatestructureget, RTC_FORMAT_BIN);
Time_str[0]=aShowTime[0];
Time_str[1]=aShowTime[1];
Time_str[2]=aShowTime[3];
Time_str[3]=aShowTime[4];
Time_str[4]=aShowTime[6];
Time_str[5]=aShowTime[7];
BSP_LCD_GLASS_DisplayString((uint8_t*)(Time_str));
}
void MX_GPIO_Init(void)
{
__GPIOC_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
}
//LCD初始化
void BSP_LCD_GLASS_Init(void)
{ LCDHandle.Instance = LCD;
LCDHandle.Init.Prescaler = LCD_PRESCALER_2;
LCDHandle.Init.Divider = LCD_DIVIDER_24;
LCDHandle.Init.Duty = LCD_DUTY_1_4;
LCDHandle.Init.Bias = LCD_BIAS_1_3;
LCDHandle.Init.VoltageSource = LCD_VOLTAGESOURCE_EXTERNAL;
LCDHandle.Init.Contrast = LCD_CONTRASTLEVEL_7;
LCDHandle.Init.DeadTime = LCD_DEADTIME_2;
LCDHandle.Init.PulseOnDuration = LCD_PULSEONDURATION_7;
LCDHandle.Init.HighDrive = DISABLE;
LCDHandle.Init.BlinkMode = LCD_BLINKMODE_OFF;
LCDHandle.Init.BlinkFrequency = LCD_BLINKFREQUENCY_DIV8;
HAL_LCD_Init(&LCDHandle);
}

 


视频地址:http://player.youku.com/player.php/sid/XMTQwOTIyNzgzMg==/v.swf

点击下载STM32L053R8_数字时钟.rar


院士
2015-12-10 21:01:20     打赏
6楼

这个真棒!

话说楼主下次一定要用发帖第一行里的 第6个图标 “插入代码”来发布代码哟~~


工程师
2015-12-10 21:39:57     打赏
7楼
谢谢6楼的指教

工程师
2015-12-20 17:21:02     打赏
8楼

进程5:使用STM32CubeMX配置ADC

1、在STM32CubeMX中选择芯片型号STM32L053R8Tx,在Pinout里选择ADC_IN0打勾:


2、RCC的 系统时钟选项的Low Speed Clock(LSE)打勾:


3、SYS的Serial  WIRE dEBUG(SWD)打勾,使能仿真调试:


4、在Configuration对ADC进行配置:


//ADC的配置
void MX_ADC_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_239CYCLES_5;
  hadc.Init.ScanDirection = ADC_SCAN_DIRECTION_BACKWARD;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ContinuousConvMode = ENABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  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.LowPowerAutoOff = DISABLE;
  HAL_ADC_Init(&hadc);

    /**Configure for the selected ADC regular channel to be converted. 
    */
  sConfig.Channel = ADC_CHANNEL_0;
  HAL_ADC_ConfigChannel(&hadc, &sConfig);

}

在主函数中添加ADC的启动和采样:


int main(void)
{

  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  BSP_LCD_GLASS_Init();

  MX_ADC_Init();
  MX_LCD_Init();

		HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED);
		HAL_ADC_Start(&hadc);
		BSP_LCD_GLASS_DisplayString((uint8_t*)(DispBuf));
  while (1)
  {
		
		 HAL_ADC_PollForConversion(&hadc, 10);

			if ((HAL_ADC_GetState(&hadc) & HAL_ADC_STATE_EOC) == HAL_ADC_STATE_EOC)
				{											    
				uwADCxConvertedValue += HAL_ADC_GetValue(&hadc);
					 Count++;
					if(Count==64)
					{uwADCxConvertedValue>>=6;
						To_buf();
						BSP_LCD_GLASS_DisplayString((uint8_t*)(DispBuf));	
						Count=0;
						uwADCxConvertedValue=0;
					}
				}

		}
}
static void To_buf(void)
{uint16_t TMP;
	TMP=uwADCxConvertedValue*8/10;

	DispBuf[0]=' ';
	DispBuf[1]=' ';
	DispBuf[2]=TMP/1000+0x30;
	DispBuf[3]=TMP%1000/100+0x30;
	DispBuf[4]=TMP%100/10+0x30;
	DispBuf[5]=TMP%10+0x30;


}

外接1只电位器在+3.3V与DNG间,抽头连接ADC_IN0输入端:


开发板的ADC部分可以正常采样转换了


视频地址:http://player.youku.com/player.php/sid/XMTQxODk2MzQxNg==/v.swf


助工
2015-12-29 15:59:04     打赏
9楼
璐璐就是主打贴心

共9条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]