学习用stm8做个2路AD电位器调节2个LED灯PWM调光的程序,使用程序里其中一路可以调节LED光,2路一起用就搞不定了,不知道是哪里写的不对,请兄弟们帮忙看看要改哪里呢?谢了。
/*2个电位器分别PWM调节2个LED*/
#include<iostm8s103f3.h>
unsigned char x;
unsigned int ADCData;
unsigned int PWM1;
unsigned int PWM2;
void PORTInit() //PWM输出端口设置
{
PC_DDR_bit.DDR3 = 1; //set out
PC_CR1_bit.C13 = 1; //推挽输出
PC_CR2_bit.C23 = 0; //输出速度最大为2M
PD_DDR_bit.DDR2 = 1; //set out
PD_CR1_bit.C12 = 1; //PD_CR1 |= 0x0d;
PD_CR2_bit.C22 = 0;
}
/*******************************************************************************
**函数名称:void TIM1_PWM_Init()
**功能描述:定时器1 PWM初始化
**入口参数:无
**输出:无
*******************************************************************************/
void TIM1_PWM_Init()
{
TIM1_CR1_bit.CEN = 0; //停止计数
TIM1_PSCRH = 0x00; //16MHZ/
TIM1_PSCRL = 0x00;
TIM1_ARRH = 1000 / 256;
TIM1_ARRL = 1000 % 256;
TIM1_BKR_bit.MOE = 1;
TIM1_CCMR3_bit.OC3M = 7; //PWM2 MODEL
TIM1_CCMR3_bit.OC3PE = 1;
TIM1_CCER2_bit.CC3E = 1; //output enable
TIM1_CCER2_bit.CC3P = 0; //OC1 hi
TIM1_CCR3H = 0x00; //50%
TIM1_CCR3L = 0x00;
TIM1_CR1 = 0x01;
}
/*******************************************************************************
**函数名称:void TIM2_PWM_Init()
**功能描述:定时器2PWM初始化
**入口参数:无
**输出:无
*******************************************************************************/
void TIM2_PWM_Init()
{
TIM2_CR1_bit.CEN = 0; //禁能定时器2
TIM2_CCMR3_bit.OC3M = 7; //定时器通道3 PWM模式 PWM2 MODEL
TIM2_CCMR3_bit.OC3PE = 1; //输出比1预装载使能
TIM2_CCER2_bit.CC3E = 1; //定时器2通道3设置为输出
TIM2_CCER2_bit.CC3P = 1; //定时器2通道3为低电平为有效电平
TIM2_PSCR_bit.PSC = 0; //设置预分频
TIM2_ARRH = 1000 / 256;
TIM2_ARRL = 1000 % 256;
TIM2_CCR3H = 0x00; //50%
TIM2_CCR3L = 0x00;
TIM2_CR1 = 0x01;
}
/*******************************************************************************
**函数名称:void ADC_Init()
**功能描述:初始化ADC
**入口参数:无
**输出:无
*******************************************************************************/
void ADC_Init()
{
PC_DDR_bit.DDR4 = 0; //设置PC->4 为输入
PC_CR1_bit.C14 = 0; //设置为悬空输入
PC_CR2_bit.C24 = 0; //设置中断禁止
PD_DDR_bit.DDR3 = 0; //设置PD->3 为输入
PD_CR1_bit.C13 = 0; //设置为悬空输入
PD_CR2_bit.C23 = 0; //设置中断禁止
ADC_CR1_bit.SPSEL = 1; //fmaster / 3 = 16MHZ / 3 = 5.333MHZ
ADC_CR2_bit.ALIGN = 1; //RIGHT ALIGN
ADC_CR1_bit.ADON = 1; //启动ADC
}
/*******************************************************************************
**函数名称:void ADC_Data_Read(unsigned int *AD_Value)
**功能描述:读取ADC完成一次模数转换结果
**入口参数:unsigned int *AD_Value
*AD_Value ->读取ADC采样数据的指针
**输出:无
*******************************************************************************/
void ADC_Data_Read(unsigned int *AD_Value)
{
ADC_CSR_bit.CH = x; //选择通道
ADC_CR1_bit.ADON = 1; //启动ADC
while(ADC_CSR_bit.EOC == 0); //等待转换结束
*AD_Value = ADC_DRH; //先读取高8位
*AD_Value = (unsigned int)((*AD_Value << 8) + ADC_DRL); //高8位与低8位相加,凑成16位数据
}
/* 主函数 */
int main(void)
{
CLK_CKDIVR = 0x00; //CPUDIV = 1 HSIDIV = 1 内部时钟 = 16Mhz
PORTInit();
TIM1_PWM_Init();
TIM2_PWM_Init();
ADC_Init(); //调用ADC初始化函数
while(1)
{
x = 2;
ADC_Data_Read(&ADCData); //读取ADC的采样数值
PWM1=ADCData;
TIM1_CCR3H = PWM1 / 256;
TIM1_CCR3L = PWM1 % 256;
x = 4;
ADC_Data_Read(&ADCData); //读取ADC的采样数值
PWM2=ADCData;
TIM2_CCR3H = PWM2 / 256;
TIM2_CCR3L = PWM2 % 256;
}
}
此帖出自
小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
x = 2;
ADC_Data_Read(&ADCData); //读取ADC的采样数值
PWM1=ADCData;
TIM1_CCR3H = PWM1 / 256;
TIM1_CCR3L = PWM1 % 256;
x = 4;
ADC_Data_Read(&ADCData); //读取ADC的采样数值
PWM2=ADCData;
TIM2_CCR3H = PWM2 / 256;
TIM2_CCR3L = PWM2 % 256;
一周热门 更多>