STM8L探索套件学习笔记-综合应用(十八)
时间:11-24
来源:互联网
点击:
今天使用到的外设比较多,有DMA,TIM1,ADC。使用ADC1的通道24即PF1采集电位器的电压,由DMA1的通道0传输到Buffer。同时由DMA1的通道2将Buffer传输到TIM1外设上,也就是TIM1_CCR1H寄存器,能够改变PWM波的占空比。我们将PWM输出端连接到板上LED3上,这样就实现了用电位器调节LED3的亮度的作用。我们转了这么大弯,但能够学到很多,下面是代码:
void main(void)
{
//1使能时钟
/* 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设置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设置DMA通道0连接ADC1,通道2连接TIM1
/* Connect ADC1 to DMA1 channel 0 */
SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1ToChannel0);
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);
//4设置TIM1 CH1输出PWM波
/* configure TIM1 channel 1 as PWM Output */
TIM1_OC1Init(TIM1_OCMode_PWM1,
TIM1_OutputState_Enable,
TIM1_OutputNState_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)
{}
}
void main(void)
{
//1使能时钟
/* 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设置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设置DMA通道0连接ADC1,通道2连接TIM1
/* Connect ADC1 to DMA1 channel 0 */
SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1ToChannel0);
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);
//4设置TIM1 CH1输出PWM波
/* configure TIM1 channel 1 as PWM Output */
TIM1_OC1Init(TIM1_OCMode_PWM1,
TIM1_OutputState_Enable,
TIM1_OutputNState_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)
{}
}
STM8L学习笔记综合应 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)