Adruino 환경에서 PWM도 정상 동작하지 않는다.
CH32V003 Arduino ADC테스트 에서와 같이 variant_CH32V003F4.h 파일을 확인해 보니 TIM_MODULE_ENABLED가 설정되어 있지 않다.
Enable 하고 컴파일 하면 에러가 발생한다.
pwm_start() 함수에서 TimerCompareFormat_t 가 정의 되어 있지 않다고 한다.
C:\Users\jhpark\AppData\Local\Temp\arduino\sketches\0CFD1DECB3444CB36B9C51B7C2E4C6C8\sketch\AnalogInOutSerial.ino.cpp:1:
C:\Users\jhpark\AppData\Local\Arduino15\packages\WCH\hardware\ch32v\1.0.3\cores\arduino/ch32/analog.h:68:66: error: 'TimerCompareFormat_t' has not been declared
void pwm_start(PinName pin, uint32_t clock_freq, uint32_t value, TimerCompareFormat_t resolution);
일단 원복하고… CH32V 로레벨 코드로 PWM을 구동해 보자
const int analogInPin = PD3; // Analog input pin that the potentiometer is attached to
const int analogOutPin = PA2; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
/* PWM Output Mode Definition */
#define PWM_MODE1 0
#define PWM_MODE2 1
/* PWM Output Mode Selection */
#define PWM_MODE PWM_MODE2
void TIM1_PWMOut_Init(u16 arr, u16 psc, u16 ccp)
{
GPIO_InitTypeDef GPIO_InitStructure={0};
TIM_OCInitTypeDef TIM_OCInitStructure={0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD | RCC_APB2Periph_TIM1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOD, &GPIO_InitStructure );
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
#if (PWM_MODE == PWM_MODE1)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
#elif (PWM_MODE == PWM_MODE2)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
#endif
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = ccp;
//TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init( TIM1, &TIM_OCInitStructure );
TIM_CtrlPWMOutputs(TIM1, ENABLE );
TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Disable );
TIM_ARRPreloadConfig( TIM1, ENABLE );
TIM_Cmd( TIM1, ENABLE );
}
#define _TIME_FREQ (4800-1) //48Mhz/4.8Khz = 10000hz
#define _TIME_PERIOD 200 //20ms
#define PWM1_OUT(Val) TIM1->CH1CVR = (Val)
void setup() {
Serial.begin(115200);
TIM1_PWMOut_Init(_TIME_PERIOD, _TIME_FREQ, 10);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 200);
PWM1_OUT(outputValue);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the Serial Monitor:
Serial.print("ADC = ");
Serial.print(sensorValue);
Serial.print("\t PWM = ");
Serial.println(outputValue);
delay(10);
}
정상적으로 PWM 출력이 나오는 것을 확인 할 수 있다.
이부분을 활용해서 pwm_start() 함수를 수정하면 될것 같다.
반응형