9
10

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.

【C言語】Intel EdisonでPWM出力

Posted at

ざっとWebを見た限りでは,EdisonをC言語で扱おうという人間は少数派なのかもしれない・・.

Intel謹製のHAL(Hardware abstract layer)らしい,MRAAを使ってみました.

https://github.com/intel-iot-devkit/mraa/blob/master/docs/edison.md
のページに,Edison breakout boardのピンマップが書いてあるので,その表の中から使用したい物理ピンのMRAA Numberを調べて, mraa_hoge_init にわたすようです.hogeの部分はgpio,pwm,spi,uartなど.

MRAAには,C以外にもC++やPython,JavaScript用のインタフェースもそろっているので,お好みな言語をお使いくださいな.

pwm.c
#include <stdio.h>
#include <syslog.h>
#include <mraa.h>

int main(int argc, char *argv[])
{
	mraa_result_t ret;
	mraa_init();

	ret = mraa_set_log_level(LOG_DEBUG);
	fprintf(stdout, "Hello mraa.\nVersion: %s\n", mraa_get_version());

	mraa_pwm_context pwm;

	// The physical pin mapping is defined at 
	// https://github.com/intel-iot-devkit/mraa/blob/master/docs/edison.md
	//
	// For example,
	// MRAA Number(20) => Physical Pin(J18-7)
	pwm = mraa_pwm_init(20);
	if(pwm == NULL){
		return 1;
	}

	mraa_pwm_period_us(pwm, 15*1000);	// 15[ms]
	mraa_pwm_enable(pwm, 1);

	int i;
	for(i=0;i<20;i++){
		mraa_pwm_write(pwm, 0.05*i);
		usleep(1000*1000);	// 1[s]

		float out = mraa_pwm_read(pwm);
		fprintf(stdout, "PWM0 : %f\n", out);
	}


	mraa_deinit();

	return ret;
}

CC = gcc
MRAALIBS = -lmraa

.PHONY: all clean pwm

all: pwm

clean:
	rm -f ./*.o
	rm -f pwm

pwm: pwm.o
	$(CC) -O4 $(MRAALIBS) -o $@ $^

%.o: %.c
	$(CC) -Wall -g -c $<
9
10
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
9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?