레이블이 pwm인 게시물을 표시합니다. 모든 게시물 표시
레이블이 pwm인 게시물을 표시합니다. 모든 게시물 표시

2018년 11월 20일 화요일

ESP32 PWM 사용하기

ESP32에는 16개의 독립적인 LED PWM 채널을 가지고 있어 각각 다른 속성의 PWM 신호를 만들어 낼 수 있다.

1. 어떤 PWM 채널을 사용할 지 골라야 한다. 0~15 사이의 값을 사용할 수 있다.
2. PWM 주파수를 설정해 줘야 한다. LED의 경우 5000 Hz 정도면 충분하다.
3. PWM duty cycle resolution을 설정해 줘야 한다. 해상도(resolution)은 1비트에서 16비트까지 지정할 수 있다. 여기서는 8 bit 해상도를 사용하는데 그 경우 LED 밝기 값은 0에서 255가 될 수 있다. 만일 16 bit로 설정하면 좀 더 세밀하게 (0~65535) 밝기 값을 조정할 수 있게 된다.

ledcSetup(ch, freq, resolution);   // ch: PWM channel (0~15), freq: PWM 주파수, resolution: PWM  해상도


PWM 주파수는 1초에 몇번 신호를 ON/OFF 할 것인지 결정한다. 위의 그림에서 처럼 PWM 주파수를 4 Hz로 한다면 CH0에 연결되어 있는 LED는 4번 on/off를 반복할 것이다. 8 Hz인 경우 CH1에 연결되어 있는 LED는 8번 on/off를 하게 될 것이다. 아두이노 우노의 경우 PWM 주파수는490 Hz(5,6번 핀은 980 Hz)로 고정되어 있다.

Resolution은 한 주기 내에서 얼마나 세밀하게 시간을 지정할 수 있는가를 결정한다. 8-bit resolution인 경우 2^8 = 256 등분으로 나누지만 10-bit resolution이 되면 2^10 = 1024 등분으로 나뉘어 훨씬 더 정밀하게 제어가 가능해진다. ESP32에서는 1-bit 부터 16-bit까지 resolution을 지정할 수 있다. 아두이노 우노의 경우에는 analogWrite()의 resolution은 8-bit로 고정되어 있어 출력값으로 0~255 사이의 값을 사용해야 한다.

4. 설정한 채널의 출력을 어느 GPIO 핀으로 보낼것인지 지정해야 한다.

ledcAttachPin(gpio, ch);   // gpio : GPIO 핀 번호,  ch : PWM channel

5. 이제 ledcWrite() 함수를 사용해 LED 밝기를 조절할 수 있다.

ledcWrite(ch, duty); // ch : PWM channel, duty : Duty cycle

duty 값은 한 주기 내에서 ON 시간과 OFF 시간의 비율을 결정하는 값이다.


위의 그림을 보면 알 수 있듯이 PWM 주파수가 다르면 같은 duty 값이라도 ON 되어 있는 시간이 달라진다. 다만 전체적으로 ON 시간의 합과 OFF 시간의 합의 비율은 동일하다.




위와 같이 LED를 연결한 경우 LED가 점점 밝아졌다가 다시 점점 어두워지는걸 반복하는 코드는 다음과 같다.

const int ledPin = 16;  // 16 corresponds to GPIO16
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
 

void setup(){
  ledcSetup(ledChannel, freq, resolution);  
// configure LED PWM functionalitites  
  ledcAttachPin(ledPin, ledChannel);  
  // attach the channel to the GPIO to be controlled
}
 void loop(){
  // increase the LED brightness
  for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){   
    // changing the LED brightness with PWM
    ledcWrite(ledChannel, dutyCycle);
    delay(15);
  }

  // decrease the LED brightness
  for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
    // changing the LED brightness with PWM
    ledcWrite(ledChannel, dutyCycle);   
    delay(15);
  }
}

PWM 신호의 frequency를 조정할 수 있으므로 16개의 서보모터를 동시에 제어할 수도 있다.


서보모터는 위와 같은 PWM 신호로 제어를 하기 때문에 PWM frequency를 50 Hz로 해 주고 16-bit resolution을 사용하는 경우 duty값 3277이 0도, 4915가 90도, 6554가 180도가 된다.

즉 GPIO16에 연결된 서보모터를 CH0를 사용해서 제어한다고 하면 다음과 같이 할 수 있다.

void setup()
{
  ...
  ledcSetup(0, 50, 16);    // PWM CH 0, Freq. 50 Hz, 16-bit resolution
  ledcAttach(16, 0);         // PWM CH 0을 GPIO 16번으로 출력
  ...
}

// deg는 0~180도 까지
void servoWrite(int ch, int deg)
{
  int duty = deg*18.2 + 3277;

  ledcWrite(ch, duty);
}

void loop()
{
  ...
  servoWrite(0, 90);   // CH 0에 연결된 서보를 90도로
  ...
}







2010년 3월 7일 일요일

DIAMEX DXM 명령어 목록

DIAMEX DXM module은 시리얼 포트를 통해 PC와 통신을 하고 AT명령어 셋을 사용하기 때문에 이미 나와있는 대다수의 OBD-II 소프트웨어와 호환된다. 지원하는 AT명령어 목록은 다음과 같다.
--

DIAMEX DXM command list

COMMAND

Explanation

ATZ

Reboot the controller

ATWS

Same as ATZ but faster

ATI

Identification text is reported

ATD

All parameters are stored in the ground state as after a cold or warm state

ATE0/1

Toggle the serial echo on/off

ATL0/1

Toggle the transmission of the linefeed(L/F)

ATM0/1

Toggle the memory function of the final protocol

ATH0/1

Toggle if the OBD2 header answers and checksum byte shall be displayed

ATBD

Causes the display of the OBD2 receive buffer

ATB

Display of the OBD2 reception buffer

ATSRxx

Input of the RX ECU OBD2 filter address for OBD2 responses

ATN

Display of the current protocol used as HEX value F0..F9

  • F0 – no active protocol  

  • F1 – PWM protocl 

  • F2 – VPWM protocol 

  • F3 – ISO9141 protocol 

  • F4 – KWP2000 protocol 

  • F5 – KWP2000 protocol 

  • F6 – CAN 11-bit ID, 500 kBaud 

  • F7 – CAN 29-bit ID, 500 kBaud 

  • F8 – CAN 11-bit ID, 250 kBaud 

  • F9 – CAN 29-bit ID, 250 kBaud 

ATDP

Display of the currently used protocol in plaintext

  • NOT CONNECTED 

  • SAE J1850/PWM 

  • SAE J1850/VPWM 

  • ISO9141-2 

  • ISO14230-4, KWP2000 (5 BAUD Init) 

  • ISO14230-4, KWP2000 (Fast Init) 

  • ISO15765-4, CAN(11/500) 

  • ISO15765-4, CAN(29/500) 

  • ISO15765-4, CAN(11/250) 

  • ISO15765-4, CAN(29/250) 

ATK

Display the current keywords on ISO9141 and KWP2000

ATKW0/1,

For a slow, init keywords are distinguished between protocol 3 (ISO9141) and protocol 4 (KWP2000). This automatic detection can be disabled with ATKW0

ATP[A]x

Manual setting of the current protocol or the automatic protocol search

  • ATP1 – PWM 

  • ATP2 – VPWM 

  • APT3 – ISO9141-2 

  • ATP4 – KWP2000 5 BAUD Init 

  • ATP5 – KWP2000 Fast Init 

  • ATP6 – CAN 11/500 

  • ATP7 – CAN 29/500 

  • ATP8 – CAN 11/250 

  • ATP9 – CAN 29/250 

ATV

Overview of all the modifiable parameters of the DXM1 are displayed

ATSBx

Set baud rate of serial interface

  • ATSB0 – 9600 Baud (Standard) 

  • ATSB1 – 19200 Baud 

  • ATSB2 – 38400 Baud 

  • ATSB3 – 57600 Baud 

  • ATSB4 – 115200 Baud 

  • ATSB5 – 125000 Baud 

  • ATSB6 – 250000 Baud 

ATSHxxyyzz

Manual setting of the header bytes for ISO9141, KWP2000, PWM and VPWM protocol

  • xx = priority/type-byte 

  • yy = target-address 

  • zz = source-address 

ATWMxxyyzzaa[bb][cc]

Manual setting of the wakeup message bytes for ISO9141 and KWP2000

  • xx = priority/type-byte (with length indication in KWP) 

  • yy = target-address 

  • zz = source-address 

  • aa, bb, cc = command bytes 1~3 

ATSWxx

The length of time between automatic wakeup commands in existing ISO9141 and KWP2000 can be set

ATCA0/1

The automatic formatting of the transmitted and received CAN data can be switched on/off

ATCC0/1

Multi-frame answers have to be sent flow control messages from the tester, which indicate the controller, that following packages have to be accepted

ATCDxx

Flow control data packages which has to be sent in multi frame responses, contain beside the status byte (FS), a block size byte (BS) and also a byte for the duration (ST), which have to be added between the following response packages

ATCIxxx/xxxxxxxx

CAN-ID which has to be sent, is set with this command

  • 11-bit ID – 7DF (default) 

  • 29-bit ID – 18 DD 33 F1 (default) 

ATCFxxx/xxxxxxxx

Set CAN RX-filter

  • 11-bit ID – 7E8 (default) 

  • 29-bit ID – 18 DA F1 00 (default) 

ATCMxxx/xxxxxxxx

Set CAN RX-mask

  • 11-bit ID – 7F8 (default) 

  • 29-bit ID – 1F FF FF 00 (default) 

AT!00

Output of the serial number of the DXM controller

AT!01

Output of the controller type and the BIOS version number

AT!10

Mesaurement of 12-volt vehicle power