【5.RGB小灯】

树梅派PWM脉冲应用2

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

#define uchar unsigned char

#define makerobo_Led_PinRed    0 // 红色LED 管脚
#define makerobo_Led_PinGreen  1 // 绿色LED 管脚
#define makerobo_Led_PinBlue   2 // 蓝色LED 管脚

// LED 初始化
void makerobo_led_Init()
{
	pinMode(makerobo_Led_PinRed, PWM_OUTPUT);
	pinMode(makerobo_Led_PinGreen, PWM_OUTPUT);
	pinMode(makerobo_Led_PinBlue, PWM_OUTPUT);
	//第三个参数是range,将pwm设置成100(全开)
	softPwmCreate(makerobo_Led_PinRed,  0, 100);
	softPwmCreate(makerobo_Led_PinGreen,0, 100);
	softPwmCreate(makerobo_Led_PinBlue, 0, 100);
}
// LED 颜色设置
void makerobo_led_Color_Set(uchar r_val, uchar g_val, uchar b_val)
{//对应不同颜色针脚的设置,如果需要红色,就只给红色r_val传对应值
 //给不同颜色输入不同值,达成混色的效果!
	softPwmWrite(makerobo_Led_PinRed,   r_val);
	softPwmWrite(makerobo_Led_PinGreen, g_val);
	softPwmWrite(makerobo_Led_PinBlue,  b_val);
}


int main()
{
    //初始化连接失败时,将消息打印到屏幕
	if(wiringPiSetup() == -1){
		printf("setup wiringPi failed !");
		return 1; 
	}
	makerobo_led_Init();
	
	int n=0;
	printf("请输入循环周期的次数>");
	scanf("%d",&n);//输入循环周期的次数
	while(n--)
	{
		makerobo_led_Color_Set(0xff,0xB6,0xc1);//3CB371
		delay(500);
		
		makerobo_led_Color_Set(0xff,0x00,0x00);   //红色	
		delay(500);   //延时500ms,使更改便于观察
		makerobo_led_Color_Set(0x00,0xff,0x00);   //绿色
		delay(500);                   
		makerobo_led_Color_Set(0x00,0x00,0xff);   //蓝色
		delay(500);

		makerobo_led_Color_Set(0xff,0xff,0x00);   //黄色
		delay(500);                 
		makerobo_led_Color_Set(0xff,0x00,0xff);   //粉色
		delay(500);     
		makerobo_led_Color_Set(0xff,0xff,0xff);   //白色
		delay(500);       

		makerobo_led_Color_Set(0x94,0x00,0xd3);   //紫色
		delay(500);
		makerobo_led_Color_Set(0x76,0xee,0x00);   //偏黄色
		delay(500);
		makerobo_led_Color_Set(0x00,0xc5,0xcd);	  //淡蓝色
		delay(500);
	}
	
	//最后循环结束时,关闭LED(如果不这么设置,LED灯会停留在最后一个颜色)
	//makerobo_led_Color_Set(0x00,0x00,0x00);	//参数都为0,相当于关灯
	delay(500);//如果不延时,效果无法展示出来
	
	return 0;
}

感谢贡献代码
代码转自:https://blog.csdn.net/muxuen/article/details/123902497

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

松桥爸(仁勇)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值