STM8四路PWM配置

void pwm_color_init(void) 

//    uint16 v;

volatile uint8 c; 

/*datasheet 44,opt2 Port C5 alternate function = TIM2_CH1; port C6

 alternate function = TIM1_CH1; port C7 alternate function = TIM1_CH2.*/

/*

v = FLASH_ReadOptionByte(0x4803);

c =(uint8)( v >> 8);

if(0 == (0x01 & c))

{

c  |= 1;

FLASH_Unlock(FLASH_MEMTYPE_DATA);

FLASH_ProgramOptionByte(0x4803, c);

FLASH_Lock(FLASH_MEMTYPE_DATA);

}

*/

GPIO_Init(GPIOC, GPIO_PIN_1, GPIO_MODE_OUT_PP_LOW_SLOW); 

GPIO_Init(GPIOC, GPIO_PIN_2, GPIO_MODE_OUT_PP_LOW_SLOW); 

GPIO_Init(GPIOC, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_SLOW); 

GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_SLOW);//bin添加

c = PWRON_PSR; 

TIM1_DeInit(); 

TIM1_TimeBaseInit(0, TIM1_COUNTERMODE_UP, TIM_CLK_FREQUENCY / PWM_BULK_CLK, 0); 

/* PWM mode 2 - In up-counting, channel 1 is inactive as long as TIM1_CNT < TIM1_CCR1, 

otherwise, the channel is active. */

/* NOTE chn1 is used pwm2 mode , so the current will be more average!!*/

TIM1_OC4Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, 

 (TIM_CLK_FREQUENCY / PWM_BULK_CLK / 100) * (100 - c), 

TIM1_OCNPOLARITY_LOW, TIM1_OCIDLESTATE_RESET); //bin添加

TIM1_OC3Init(TIM1_OCMODE_PWM2, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 

 (TIM_CLK_FREQUENCY / PWM_BULK_CLK / 100) * (100 - c), 

 TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, 

 TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET); 

TIM1_OC2Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 

 (TIM_CLK_FREQUENCY / PWM_BULK_CLK / 100) * c, 

 TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, 

 TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET); 

TIM1_OC1Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, TIM1_OUTPUTNSTATE_DISABLE, 

 (TIM_CLK_FREQUENCY / PWM_BULK_CLK / 100) * c, 

 TIM1_OCPOLARITY_LOW, TIM1_OCNPOLARITY_LOW, 

 TIM1_OCIDLESTATE_RESET, TIM1_OCNIDLESTATE_RESET); 

/* TIM1 counter enable */

TIM1_Cmd(ENABLE); 

/* TIM1 Main Output Enable */

TIM1_CtrlPWMOutputs(ENABLE);

}

 

修改stm8s_time.c

#if 0

void TIM1_OC4Init(TIM1_OCMode_TypeDef TIM1_OCMode,

                  TIM1_OutputState_TypeDef TIM1_OutputState,

                  uint16_t TIM1_Pulse,

                  TIM1_OCPolarity_TypeDef TIM1_OCPolarity,

                  TIM1_OCIdleState_TypeDef TIM1_OCIdleState)

{

    /* Check the parameters */

    assert_param(IS_TIM1_OC_MODE_OK(TIM1_OCMode));

    assert_param(IS_TIM1_OUTPUT_STATE_OK(TIM1_OutputState));

    assert_param(IS_TIM1_OC_POLARITY_OK(TIM1_OCPolarity));

    assert_param(IS_TIM1_OCIDLE_STATE_OK(TIM1_OCIdleState));

 

    /* Disable the Channel 4: Reset the CCE Bit */

    TIM1->CCER2 &= (uint8_t)(~(TIM1_CCER2_CC4E | TIM1_CCER2_CC4P));

    /* Set the Output State  &  the Output Polarity */

    TIM1->CCER2 |= (uint8_t)((uint8_t)(TIM1_OutputState & TIM1_CCER2_CC4E ) |  

                             (uint8_t)(TIM1_OCPolarity  & TIM1_CCER2_CC4P ));

 

    /* Reset the Output Compare Bit  and Set the Output Compare Mode */

    TIM1->CCMR4 = (uint8_t)((uint8_t)(TIM1->CCMR4 & (uint8_t)(~TIM1_CCMR_OCM)) | 

                            TIM1_OCMode);

 

    /* Set the Output Idle state */

    if (TIM1_OCIdleState != TIM1_OCIDLESTATE_RESET)

    {

        TIM1->OISR |= (uint8_t)(~TIM1_CCER2_CC4P);

    }

    else

    {

        TIM1->OISR &= (uint8_t)(~TIM1_OISR_OIS4);

    }

 

    /* Set the Pulse value */

    TIM1->CCR4H = (uint8_t)(TIM1_Pulse >> 8);

    TIM1->CCR4L = (uint8_t)(TIM1_Pulse);

 

}

#endif

 

void pwm_color_adjust(uint8 chn, uint8 prescaler)

{

    if (prescaler > 100) prescaler = 100;

    TIM1_Cmd(DISABLE);

 

    if( 0x01 == chn)

    {//red

TIM1_SetCompare3(TIM_CLK_FREQUENCY / PWM_BULK_CLK / 100 * (100 - prescaler));

    }

    else

    if( 0x02 == chn)

    {//green

        TIM1_SetCompare2(TIM_CLK_FREQUENCY / PWM_BULK_CLK / 100 * prescaler);

    }

    else

    if( 0x03 == chn)

    {//blue

        TIM1_SetCompare1(TIM_CLK_FREQUENCY / PWM_BULK_CLK / 100 * prescaler);

    }

    else

    if( 0x04 == chn)//bin添加

    {

        TIM1_SetCompare4(TIM_CLK_FREQUENCY / PWM_BULK_CLK / 100 * prescaler);

    }

    TIM1_Cmd(ENABLE);

 

    TIM1_CtrlPWMOutputs(ENABLE);

}

 

 

Stm8s_time1.c

#if 0

void TIM1_SetCompare4(u16 Compare4)

{

    /* Set the Capture Compare4 Register value */

    TIM1->CCR4H = (u8)(Compare4 >> 8);

    TIM1->CCR4L = (u8)(Compare4);

}

#endif    

 

 

你可能感兴趣的:(STM8)