본문 바로가기

WCH/CH32V035

CH32X035 SSM - Arduino PWM 테스트

CH32X035는 저렴하면서 플래시 용량도 64K이므로 Arduino에서 다양한 예제를 구동 해 볼 수 있다. 하지만 Arduno 예제중 아직 몇가지 기능은 안되는것 같다. CH32V003와 같이 PWM출력은 아직 안되는것 같다.

PWM은 라이브러리를 직접 만들어 사용해야 한다.

 

PA1에 연결되어 있는 가변저항의 전압값을 ADC로 읽어서 PA6에 연결된 PWM으로 출력해서 서보 모터를 구동할 수 있는 간단한 예제를 테스트 해 보았다.

 

const int analogInPin = PA1;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = PA6;  // 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)




#define _TIME_FREQ            (2400-1)     //48Mhz/4.8Khz = 10000hz
#define _TIME_PERIOD          200          //20ms
//#define PWM1_OUT(Val)         TIM1->CH1CVR = (Val)

#define PWM31_OUT(Val)        TIM3->CH1CVR = (Val)
#define _TIME_INSTANCE        TIM3


void TIM_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_GPIOA, ENABLE );
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOA, &GPIO_InitStructure );

    TIM_TimeBaseInitStructure.TIM_Period = arr;
    TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV2;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit( _TIME_INSTANCE, &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_OC1Init( _TIME_INSTANCE, &TIM_OCInitStructure );

#if (PWM_SPE_MODE == PWM_SPE_MODE_ENABLE)
    TIM_OC12_SupersedeModeCmd( _TIME_INSTANCE, TIM_Supersede_Mode_OC1_H, TIM_Supersede_Mode_OC2_H, ENABLE);
#endif

    TIM_CtrlPWMOutputs(_TIME_INSTANCE, ENABLE );
    TIM_OC1PreloadConfig( _TIME_INSTANCE, TIM_OCPreload_Disable );
    TIM_ARRPreloadConfig( _TIME_INSTANCE, ENABLE );
    TIM_Cmd( _TIME_INSTANCE, ENABLE );
}

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(115200);

  //GPIO_PinRemapConfig(AFIO_PartialRemap2_TIM1_ENABLE, ENABLE);
  //GPIO_PinRemapConfig(AFIO_PCFR1_PA12_REMAP, DISABLE)
    
  TIM_PWMOut_Init( 100, 48000-1, 50 );    
  //analogWrite(analogOutPin, 100);

  //TIM1_PWMOut_Init(_TIME_PERIOD, _TIME_FREQ, 10);

  //TIM1->CH1CVR = 30;
}

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);


  PWM31_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);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(10);
  }
반응형