0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WiringPiでGPIOとPWM(工事中)

Last updated at Posted at 2018-04-08

WiringPi

WiringPiについてのまとめをしておく(工事中)

概要

Raspberry PiのGPIOをC言語から扱うライブラリ
http://wiringpi.com/

ダウンロード

工事中

インストール

工事中

コマンドラインで使う

ソースコードで書く

初期化

WiringPiを使う前には初期化関数を呼び出す。

wiringPiSetup();

GPIO

GPIOで入力や出力をする。

出力は下記のように

#include <wiringPi.h>

#define WRITE_PIN0 0
#define WRITE_PIN1 1

int main(void){
  if(wiringPiSetup() == -1){
    return -1;
  }
  pinMode(WRITE_PIN0, OUTPUT);
  pinMode(WRITE_PIN1, OUTPUT);

  digitalWrite(WRITE_PIN, 0);  //0を出力
  digitalWrite(WRITE_PIN, 1);  //1を出力

  return 0;
}

PWM

PWMはPWM出力可能なポートにPWMの設定をして行う。

 #include <wiringPi.h>
 #include <stdio.h>
 
 #define PWM_PIN (1)     // PHY 12
 #define RANGE (100)     //0~100%の整数で指定
 #define FREQ (20000.0)  // 周波数
 #define CLOCK ((int)(19200000.0 / FREQ / (float)RANGE + 0.5))

int main(void){
  if(wiringPiSetup() == -1){
    printf("error\n");
    return -1;
  }
  
  pinMode(PWM_PIN, PWM_OUTPUT);

  pwmSetMode(PWM_MODE_MS);
  pwmSetClock(CLOCK);
  pwmSetRange(100);
  pwmWrite(PWM_PIN, 50);  // 50%で出力

  while(1){
    ;  //無限ループ
  }

  return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?