37种传感器-树莓派开发-1-双色LED

C编程源文件

程序以初始化GPIO口,充当LED等的初始化,利用PWM来实现LED灯的不同控制。

#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>

#define uchar unsigned char

#define LedPinRed    0//对应GPIO0口也对应转接板上的GPIO17
#define LedPinGreen  1//对应GPIO1口也对应转接板上的GPIO18

void ledInit(void)
{
	softPwmCreate(LedPinRed,  0, 100);
	softPwmCreate(LedPinGreen,0, 100);
}

void ledColorSet(uchar r_val, uchar g_val)
{
	softPwmWrite(LedPinRed,   r_val);
	softPwmWrite(LedPinGreen, g_val);
}

int main(void)
{
	int i;

	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}
	//printf("linker LedPin : GPIO %d(wiringPi pin)\n",LedPin); //when initialize wiring successfully,print message to screen

	ledInit();

	while(1){
		ledColorSet(0xff,0x00);   //red	
		delay(500);
		ledColorSet(0x00,0xff);   //green
		delay(500);
		ledColorSet(0xff,0x45);	
		delay(500);
		ledColorSet(0xff,0xff);	
		delay(500);
		ledColorSet(0x7c,0xfc);	
		delay(500);
	}

	return 0;
}

代码解释

1.头文件wiringPi.h

wiringPi是一个很棒的树莓派IO控制库,使用C语言开发,提供了丰富的接口:GPIO控制,中断,多线程。

2.头文件softPwm.h

2.1 ----softPwmCreate 函数
原型为: int softPwmCreate(int pin, int initialValue, int pwmRange);
该函数将会创建一个软件控制的 PWM 管脚。可以使用任何一个 GPIO 管脚,pwmRange 参数可以为 0(关) ~100(全开)。

2.2 ----softPwmWrite 函数
原型为: void softPwmWrite(int pin, int value);
该函数将会更新指定管脚的 PWM 值。 value 参数的范围将会被检查,如果指定的管脚之前没有通过 softPwmCreate 初始化,将会被忽略。

Python程序源文件

python也是类似C,定义引脚,设置输出,拉高,设置PWM波的频率,启动线程,最后利用 def 函数来实现颜色变换。

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

colors = [0xFF00, 0x00FF, 0x0FF0, 0xF00F]
pins = {'pin_R':11, 'pin_G':12}  # pins is a dict

GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
for i in pins:
	GPIO.setup(pins[i], GPIO.OUT)   # Set pins' mode is output
	GPIO.output(pins[i], GPIO.HIGH) # Set pins to high(+3.3V) to off led

p_R = GPIO.PWM(pins['pin_R'], 2000)  # set Frequece to 2KHz
p_G = GPIO.PWM(pins['pin_G'], 2000)

p_R.start(0)      # Initial duty Cycle = 0(leds off)
p_G.start(0)

def map(x, in_min, in_max, out_min, out_max):
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

def setColor(col):   # For example : col = 0x112233
	R_val = (col & 0x1100) >> 8
	G_val = (col & 0x0011) >> 0
	
	R_val = map(R_val, 0, 255, 0, 100)
	G_val = map(G_val, 0, 255, 0, 100)
	
	p_R.ChangeDutyCycle(R_val)     # Change duty cycle
	p_G.ChangeDutyCycle(G_val)

def loop():
	while True:
		for col in colors:
			setColor(col)
			time.sleep(0.5)

def destroy():
	p_R.stop()
	p_G.stop()
	for i in pins:
		GPIO.output(pins[i], GPIO.HIGH)    # Turn off all leds
	GPIO.cleanup()

if __name__ == "__main__":
	try:
		loop()
	except KeyboardInterrupt:
		destroy()

代码解释

1.RPi.GPIO模块

使用python进行变编程要使用RPi.GPIO库。树莓派没人在python2版本时默认安装,python3要自行安装。

2.函数

2.1 ----pins = {‘pin_R’:11, ‘pin_G’:12}
作用是定义管脚,分别对应Header里面的11|12脚也就是转接出来的17|18脚
2.2 ----GPIO.setmode(GPIO.BOARD)
GPIO.setmode(GPIO.BOARD) //设置GPIO引脚编号的模式,这里引脚采用BOARD编码方式。
2.3----GPIO.setmode(GPIO.BCM)
GPIO.setmode(GPIO.BCM) //设置GPIO引脚编号的模式,这里引脚编号采用BCM编码方式;
不同模式下对应的管脚号不同
树莓派管脚图
2.4 -----设置引脚

设置引脚为输入:GPIO.setup(pin,GPIO.IN)

设置引脚为输出:GPIO.setup(pin,GPIO.OUT)

设置初始化高电平:GPIO.setup(pin,GPIO.OUT,initial=GPIO.HIGH)

设置初始化为低电平:GPIO.setup(pin,GPIO.OUT,initial=GPIO.LOW)

2.5 -----代码中 for i in pins 的意思
将pins里定义的数组下标依次赋给i,再调用pins[i]将引脚的电频拉高或者降低。

2.6 -----代码中的 def 标示定义一个函数

2.7 代码 R_val = map(R_val, 0, 255, 0, 100) 函数
原函数为 map(function, iterable, ...)
其中
function–函数
iterable–一个或多个序列

2.8 ----start()函数
其中
run() 方法并不启动一个新线程,就是在主线程中调用了一个普通函数而已。
start() 方法是启动一个子线程,线程名就是自己定义的name。也就可以直接用之前定义的引脚启动。
PS:如果你想启动多线程,就必须使用start()方法。

发布了5 篇原创文章 · 获赞 5 · 访问量 124

猜你喜欢

转载自blog.csdn.net/qq_44752226/article/details/104565905
今日推荐