xmc-pwm-led/dimming.c
2024-11-02 17:44:56 +01:00

48 lines
1,002 B
C
Executable file

#include <xmc_common.h>
void initCCU4(void);
void connectLED(void);
int main(void) {
initCCU4();
while (1) {
};
return 0;
}
void initCCU4() {
// step2: release reset of CCU40, all following lines are equal
//*(0x500414) = 0b100;
// SCU_RESET->PRCLR0 = 0b100;
SCU_RESET->PRCLR0 = SCU_RESET_PRCLR0_CCU40RS_Msk;
// step 3
SCU_CLK->CLKSET = SCU_CLK_CLKSET_CCUCEN_Msk;
// step 4
CCU40->GIDLC = CCU4_GIDLC_SPRB_Msk;
// step 6: period value & compare value for PWM
// choose 0xffff to be most flexible & granular
CCU40_CC42->PRS = 0xffff;
// CCU40_CC42->PSC = 0b1010;
CCU40_CC42->CRS = 0xffff * (1 - 0.1);
CCU40->GCSS = CCU4_GCSS_S2SE_Msk;
// connect led before CCU is started
connectLED();
// step 8
CCU40->GIDLC = CCU4_GIDLC_CS2I_Msk;
// step 9
CCU40_CC42->TCSET = CCU4_CC4_TCSET_TRBS_Msk;
}
void connectLED() {
const static uint8_t ALT3 = 0b10011;
PORT1->IOCR0 =
(PORT1->IOCR0 & ~PORT0_IOCR0_PC1_Msk) | (ALT3 << PORT0_IOCR0_PC1_Pos);
}