2023년 4월 25일 화요일

PWM --> map()

 

void setVoltage0(float voltage) {
      //VpwmValue = voltage / Vslope; // 전압값을 PWM 듀티 사이클 값으로 환산
    int dutyCycle = map(voltage, 0, 330, 0, 255); // Map duty cycle to 0-255 range
    analogWrite(5, dutyCycle); // Output PWM signal using pin 5
}

전압값 116.9V log값 1.7752V    
116.9/1.7752 = 65.851
기울기값을 이용해 map()함수에서 값을 변환해도 가능.
전압값 x 기울기 = log값 형태의 전압값
116.9 x 65.851 = 1.7752V
map(1.7752, 0, 5, 0, 255)

=====================================================
void setVoltage0(float voltage) {
  unsigned int dutyCycle = map(voltage, 0, 335, 0, 65535); // Map duty cycle to 0-65535 range
  analogWrite(5, dutyCycle); // Output PWM signal using pin 5

  Serial.print("dutyCycle: ");
  Serial.println(dutyCycle); // Print duty cycle value  
  Serial.print("number0 ");
  Serial.println(number0);
}

void setVoltage1(float voltage) {
  //int dutyCycle1 = map(voltage, 0, 615, 82, 255); // 83 // Map duty cycle to 0-255 range
  unsigned int dutyCycle1 = map(voltage, 0, 615, 20000, 65535); // 83 // Map duty cycle to 0-255 range
  analogWrite(6, dutyCycle1); // Output PWM signal using pin 5

  Serial.print("dutyCycle1 ");
  Serial.println(dutyCycle1);
}
void setVoltage2(float voltage) {
  //unsigned int dutyCycle2 = map(voltage, 0, 330, 0, 65535); // Map duty cycle to 0-65535 range
  unsigned int dutyCycle2 = map(voltage, 0, 30, 21242, 65535);
  analogWrite(7, dutyCycle2); // Output PWM signal using pin 7

  Serial.print("dutyCycle2: ");
  Serial.println(dutyCycle2); // Print duty cycle value
}

setup------------------------------ 타이머 16bit
  TCCR1A = _BV(COM1A1) | _BV(WGM11); // Fast PWM, non-inverting mode
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // Fast PWM, prescaler 1
  ICR1 = 0xFFFF; // Set the top value for 16-bit PWM

  // Set timer3 for 16-bit fast PWM on PWM_PIN2
  TCCR3A = _BV(COM3A1) | _BV(WGM31); // Fast PWM, non-inverting mode
  TCCR3B = _BV(WGM33) | _BV(WGM32) | _BV(CS30); // Fast PWM, prescaler 1
  ICR3 = 0xFFFF; // Set the top value for 16-bit PWM

  // Set timer4 for 16-bit fast PWM on PWM_PIN3
  TCCR4A = _BV(COM4A1) | _BV(WGM41); // Fast PWM, non-inverting mode
  TCCR4B = _BV(WGM43) | _BV(WGM42) | _BV(CS40); // Fast PWM, prescaler 1
  ICR4 = 0xFFFF; // Set the top value for 16-bit PWM

댓글 없음:

댓글 쓰기