//int main_app(IN u16 argc, IN u8 *argv[]) void main(void) { int i; for (i=0; i<4; i++) { pwmout_init(&pwm_led[i], pwm_led_pin[i]); pwmout_period_us(&pwm_led[i], PWM_PERIOD); } while (1) { #if USE_FLOAT for (i=0; i<4; i++) { pwmout_write(&pwm_led[i], pwms[i]); pwms[i] += steps[i]; if (pwms[i] >= 1.0) { steps[i] = -PWM_STEP; pwms[i] = 1.0; } if (pwms[i] <= 0.0) { steps[i] = PWM_STEP; pwms[i] = 0.0; } } #else for (i=0; i<4; i++) { pwmout_pulsewidth_us(&pwm_led[i], pwms[i]); pwms[i] += steps[i]; if (pwms[i] >= PWM_PERIOD) { steps[i] = -PWM_STEP; pwms[i] = PWM_PERIOD; } if (pwms[i] <= 0) { steps[i] = PWM_STEP; pwms[i] = 0; } } #endif // wait_ms(20); // RtlMsleepOS(25); pwm_delay(); } }
// Right now, PWM output only works on the pins with // hardware support. These are defined in the appropriate // pins_*.c file. For the rest of the pins, we default // to digital output. void analogWrite(uint32_t ulPin, uint32_t ulValue) { pwmout_t *pObj; if ( ulPin < 0 || ulPin > 13 ) return; /* Handle */ if ( g_APinDescription[ulPin].ulPinType != PIO_PWM ) { pwmout_init(&pwm_pins[ulPin], g_APinDescription[ulPin].pinname); g_APinDescription[ulPin].ulPinType = PIO_PWM; } pObj = &pwm_pins[ulPin]; pObj->pulse = pObj->period * ulValue / 256; HAL_Pwm_SetDuty(pObj->pwm_idx, pObj->period, pObj->pulse); }