Arduino의 SoftPWM 정보
                                            
                                                
                                                
                                                
                                                
                                                
                                                 14386 단어  Arduino
                    
예
13pin을 $duty=100/255$로 pwm한다. 프로그램
#include <SoftPWM.h>
void setup()
{
  SoftPWMBegin();  
  SoftPWMSet(13, 0);
}
void loop()
{
  SoftPWMSet(13, 255);  
}
 
LOW : 66μs
HIGH:16692μs
조금 LOW가 들어가는 것 같습니다.
 SoftPWMSet (13, 256);
ー>모두 LOW
 SoftPWMSet (13, 100);
 
1주기 = 16.7ms
HIGH=6.53ms
LOW=10.2ms
 요약
#include <SoftPWM.h>
void setup()
{
  SoftPWMBegin();  
  SoftPWMSet(13, 0);
}
void loop()
{
  SoftPWMSet(13, 255);  
}
설치는
스케치 -> 라이브러리 포함 -> 라이브러리 관리 -> softPWM
로 설치할 수 있습니다.
더 고주파를 원하는 분
나는 200Hz 정도는 최소로 갖고 싶기 때문에 좀 더 생각합니다.
소프트 인터럽트는 Arduino 리소스를 상당히 소모하기 때문에주의가 필요합니다.
분해능을 낮추는 것으로 대응은 일단 가능합니다.
TimerOne을 사용합니다.
해상도 10,
#include <TimerOne.h>
volatile byte duty1=0;
volatile byte duty2=0;
volatile byte duty3=0;
volatile byte duty4=0;
volatile byte pwm_count = 0;
void soft_pwm(){
  pwm_count++;
  if(pwm_count>=10){
    pwm_count = 0;
    PORTD |= _BV(6);    //pin6 HIGH
    PORTD |= _BV(7);    //pin7
    PORTB |= _BV(0);    //pin8
    PORTB |= _BV(1);    //pin9
  }
  if(pwm_count>=duty1){
    PORTD &= ~_BV(6);    //pin6 LOW
  }
  if(pwm_count>=duty2){
    PORTD &= ~_BV(7);    //pin7 LOW
  }
  if(pwm_count>=duty3){
    PORTB &= ~_BV(0);    //pin8 LOW
  }
  if(pwm_count>=duty4){
    PORTB &= ~_BV(1);    //pin9 LOW
  }
}
void setup() {
  Timer1.initialize(200);           //0.2ms
  Timer1.attachInterrupt(soft_pwm);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
}
void loop() {
}
 
주기 0.2ms*10=2ms(500Hz), 분해능 10
4 출력에서 duty0으로 하면 순간 1이 프로그램의 사정상 들어갑니다.
 
인터럽트에는 2μs 사용하고 있습니다.
분해능을 256으로 하면(byte)
주기 0.2ms*256=2ms(19.5Hz), 분해능 256
늦어집니다.
분해능을 20 정도가 딱 좋은 것 같습니다.
주기 0.2ms*20=2ms(250Hz), 분해능 20
#include <TimerOne.h>
volatile byte duty1 = 0;
volatile byte duty2 = 1;
volatile byte duty3 = 2;
volatile byte duty4 = 3;
volatile byte pwm_count = 0;
void soft_pwm() {
  pwm_count++;
  if (pwm_count >= 20) {
    pwm_count = 0;
    if (duty1 != 0) {
      PORTD |= _BV(6);    //pin6 HIGH
    }
    if (duty2 != 0) {
      PORTD |= _BV(7);    //pin7
    }
    if (duty3 != 0) {
      PORTB |= _BV(0);    //pin8
    }
    if (duty4 != 0) {
      PORTB |= _BV(1);    //pin9
    }
  } else {
    if (pwm_count >= duty1) {
      PORTD &= ~_BV(6);    //pin6 LOW
    }
    if (pwm_count >= duty2) {
      PORTD &= ~_BV(7);    //pin7 LOW
    }
    if (pwm_count >= duty3) {
      PORTB &= ~_BV(0);    //pin8 LOW
    }
    if (pwm_count >= duty4) {
      PORTB &= ~_BV(1);    //pin9 LOW
    }
  }
}
void setup() {
  Timer1.initialize(200);           //0.2ms
  Timer1.attachInterrupt(soft_pwm);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  Serial.begin(250000);
}
void loop() {
  Serial.println("hello");
}
이것이라면
 
이런 느낌이 듭니다.
duty1->200μs
duty2->400μs
duty3->600μs
주기 4ms=250Hz
됩니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(Arduino의 SoftPWM 정보), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/hiRina/items/02e0a3a4009e1392b082
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
#include <TimerOne.h>
volatile byte duty1=0;
volatile byte duty2=0;
volatile byte duty3=0;
volatile byte duty4=0;
volatile byte pwm_count = 0;
void soft_pwm(){
  pwm_count++;
  if(pwm_count>=10){
    pwm_count = 0;
    PORTD |= _BV(6);    //pin6 HIGH
    PORTD |= _BV(7);    //pin7
    PORTB |= _BV(0);    //pin8
    PORTB |= _BV(1);    //pin9
  }
  if(pwm_count>=duty1){
    PORTD &= ~_BV(6);    //pin6 LOW
  }
  if(pwm_count>=duty2){
    PORTD &= ~_BV(7);    //pin7 LOW
  }
  if(pwm_count>=duty3){
    PORTB &= ~_BV(0);    //pin8 LOW
  }
  if(pwm_count>=duty4){
    PORTB &= ~_BV(1);    //pin9 LOW
  }
}
void setup() {
  Timer1.initialize(200);           //0.2ms
  Timer1.attachInterrupt(soft_pwm);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
}
void loop() {
}
#include <TimerOne.h>
volatile byte duty1 = 0;
volatile byte duty2 = 1;
volatile byte duty3 = 2;
volatile byte duty4 = 3;
volatile byte pwm_count = 0;
void soft_pwm() {
  pwm_count++;
  if (pwm_count >= 20) {
    pwm_count = 0;
    if (duty1 != 0) {
      PORTD |= _BV(6);    //pin6 HIGH
    }
    if (duty2 != 0) {
      PORTD |= _BV(7);    //pin7
    }
    if (duty3 != 0) {
      PORTB |= _BV(0);    //pin8
    }
    if (duty4 != 0) {
      PORTB |= _BV(1);    //pin9
    }
  } else {
    if (pwm_count >= duty1) {
      PORTD &= ~_BV(6);    //pin6 LOW
    }
    if (pwm_count >= duty2) {
      PORTD &= ~_BV(7);    //pin7 LOW
    }
    if (pwm_count >= duty3) {
      PORTB &= ~_BV(0);    //pin8 LOW
    }
    if (pwm_count >= duty4) {
      PORTB &= ~_BV(1);    //pin9 LOW
    }
  }
}
void setup() {
  Timer1.initialize(200);           //0.2ms
  Timer1.attachInterrupt(soft_pwm);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  Serial.begin(250000);
}
void loop() {
  Serial.println("hello");
}
Reference
이 문제에 관하여(Arduino의 SoftPWM 정보), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/hiRina/items/02e0a3a4009e1392b082텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)