There are many peripherals used today, including DMA, TIM1, and ADC. Channel 24 of ADC1, namely PF1, is used to collect the voltage of the potentiometer, and is transferred to the buffer by channel 0 of DMA1. At the same time, channel 2 of DMA1 transfers the buffer to the TIM1 peripheral, namely the TIM1_CCR1H register, which can change the duty cycle of the PWM wave. We connect the PWM output end to LED3 on the board, so that the brightness of LED3 can be adjusted by the potentiometer. We took such a big detour, but we were able to learn a lot. Here is the code:
void main(void)
{
//1 Enable clock
/* Enable ADC1 clock */
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);
/* Enable TIM1 clock */
CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE);
/* Enable DMA1 clock */
CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE);
//2 Set up ADC
/* Initialise and configure ADC1 */
ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_2);
ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 Channels 24 */
ADC_ChannelCmd(ADC1, ADC_Channel_24, ENABLE); /* connected to potentiometer */
//3 Set DMA channel 0 to connect to ADC1, channel 2 to connect to TIM1
/* Connect ADC1 to DMA1 channel 0 */
SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1 ToChannel0);
DMA_Init(DMA1_Channel0,
BUFFER_ADDRESS,
ADC1_DR_ADDRESS,
BUFFER_SIZE,
DMA_DIR_PeripheralToMemory, DMA_Mode_Circular
,
DMA_MemoryIncMode_Inc,
DMA_Priority_High,
DMA_MemoryDataSize_HalfWord);
/* Connect DMA1 channel 2 to TIM1 */
DMA_Init(DMA1_Channel2,
BUFFER_ADDRESS,
TIM1_CCR1_ADDRESS,
BUFFER_SIZE,
DMA_DIR_MemoryToPeripheral,
DMA_Mode_Circular,
DMA_MemoryIncMode_Inc,
DMA_Priority_High,
DMA_MemoryDataSize_HalfWord);
/* DMA1 Channel0 enable */
DMA_Cmd(DMA1_ Channel0, ENABLE);
/* DMA1 Channel2 enable */
DMA_Cmd(DMA1_Channel2, ENABLE);
/* DMA1 enable */
DMA_GlobalCmd(ENABLE);
//4Set TIM1 CH1 to output PWM wave
/* configure TIM1 channel 1 as PWM Output */
TIM1_OC1Init(TIM1_OCMode_PWM1,
TIM1_OutputState_Enable,
TIM1_Out putNState_Disable,
0x7FF/* TIM1_Pulse */,
TIM1_OCPolarity_Low,
TIM1_OCNPolarity_Low,
TIM1_OCIdleState_Reset,
TIM1_OCNIdleState_Reset);
/* Set TIM1 Autoreload value*/
TIM1_SetAutoreload(0xFFF);
//2M/4095=488.4HZ
/* Enable TIM1 */
TIM1_Cmd(ENABLE);
//5 PD2 TIM1 CH1设置
/* GPIO configuration: TIM1 channel 1 (PD2)*/
GPIO_Init(GPIOD, GPIO_Pin_2 , GPIO_Mode_Out_PP_Low_Fast);
//6 使能
/* Enable TIM1 Outputs*/
TIM1_CtrlPWMOutputs(ENABLE);
/* Enable ADC1 DMA requests*/
ADC_DMACmd(ADC1, ENABLE);
/* Enable TIM1 DMA requests*/
TIM1_DMACmd(TIM1_DMASource_Update, ENABLE);
/* Start ADC1 Conversion using Software trigger*/
ADC_SoftwareStartConv(ADC1);
while (1)
{}
}
Keywords:STM8L
Reference address:STM8L Exploration Kit Study Notes - Comprehensive Application (XVIII)
void main(void)
{
//1 Enable clock
/* Enable ADC1 clock */
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);
/* Enable TIM1 clock */
CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE);
/* Enable DMA1 clock */
CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE);
//2 Set up ADC
/* Initialise and configure ADC1 */
ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_2);
ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 Channels 24 */
ADC_ChannelCmd(ADC1, ADC_Channel_24, ENABLE); /* connected to potentiometer */
//3 Set DMA channel 0 to connect to ADC1, channel 2 to connect to TIM1
/* Connect ADC1 to DMA1 channel 0 */
SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1 ToChannel0);
DMA_Init(DMA1_Channel0,
BUFFER_ADDRESS,
ADC1_DR_ADDRESS,
BUFFER_SIZE,
DMA_DIR_PeripheralToMemory, DMA_Mode_Circular
,
DMA_MemoryIncMode_Inc,
DMA_Priority_High,
DMA_MemoryDataSize_HalfWord);
/* Connect DMA1 channel 2 to TIM1 */
DMA_Init(DMA1_Channel2,
BUFFER_ADDRESS,
TIM1_CCR1_ADDRESS,
BUFFER_SIZE,
DMA_DIR_MemoryToPeripheral,
DMA_Mode_Circular,
DMA_MemoryIncMode_Inc,
DMA_Priority_High,
DMA_MemoryDataSize_HalfWord);
/* DMA1 Channel0 enable */
DMA_Cmd(DMA1_ Channel0, ENABLE);
/* DMA1 Channel2 enable */
DMA_Cmd(DMA1_Channel2, ENABLE);
/* DMA1 enable */
DMA_GlobalCmd(ENABLE);
//4Set TIM1 CH1 to output PWM wave
/* configure TIM1 channel 1 as PWM Output */
TIM1_OC1Init(TIM1_OCMode_PWM1,
TIM1_OutputState_Enable,
TIM1_Out putNState_Disable,
0x7FF/* TIM1_Pulse */,
TIM1_OCPolarity_Low,
TIM1_OCNPolarity_Low,
TIM1_OCIdleState_Reset,
TIM1_OCNIdleState_Reset);
/* Set TIM1 Autoreload value*/
TIM1_SetAutoreload(0xFFF);
//2M/4095=488.4HZ
/* Enable TIM1 */
TIM1_Cmd(ENABLE);
//5 PD2 TIM1 CH1设置
/* GPIO configuration: TIM1 channel 1 (PD2)*/
GPIO_Init(GPIOD, GPIO_Pin_2 , GPIO_Mode_Out_PP_Low_Fast);
//6 使能
/* Enable TIM1 Outputs*/
TIM1_CtrlPWMOutputs(ENABLE);
/* Enable ADC1 DMA requests*/
ADC_DMACmd(ADC1, ENABLE);
/* Enable TIM1 DMA requests*/
TIM1_DMACmd(TIM1_DMASource_Update, ENABLE);
/* Start ADC1 Conversion using Software trigger*/
ADC_SoftwareStartConv(ADC1);
while (1)
{}
}
Previous article:STM8L Study Notes - Unique Device ID (19)
Next article:STM8L Exploration Kit Study Notes - Window Watchdog WWDG (Seventeen)
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like