树莓派Raspberry通过BCM2835进行红外遥控IR remote control操作,控制元件

问题来源

近期一直在自学嵌入式,自从在Arduino上实现红外遥控功能后,就琢磨的怎么把这些功能移植到树莓派Raspbian上,关于红外遥控的环境配置已经在博文:树莓派Raspberry红外遥控IR remote control环境搭建中提到了,在此不多赘述。此处主要实现将所有代码放入.c的文本中,编译运行实现遥控功能。详细过程如下

下载并安装BCM2835库

  • 下载

BCM官方网站下载最新的BCM2835库文件,下载链接http://www.airspayce.com/mikem/bcm2835/bcm2835-1.59.tar.gz。直接用树莓派下载或者其他电脑下载后,在用ftp传到树莓派中。
网站中下载的位置

  • 安装
    注意将下列代码中安装包的名字修改成与所下载的文件名称一致,及替换XX所指代的内容
    1.进入包含下载的安装包(bcm2835-1.59.tar.gz)的目录,
    2.解压文件

    tar xvzf bcm2835-1.XX.tar.gz 
    

3.进入解压文件

cd bcm2835-1.XX

4.配置

./configure

5.从源码生成新的安装包

make

6.执行检查

sudo make check

7.安装BCM2835库

sudo make install

说明:树莓派3b CPU应该是BCM2835,但为什么还是安装BCM2835的库?这个百度有说明,详细的我也搞不清,反正是在终端查CPU型号,返回的BCM2835。据说BCM2835与
BCM2837 差不多。

  • 过程图
    在这里插入图片描述

程序代码

网络上网友一般在这个程序之前都进行遥控器按下信号与实际信息匹配,红外线编码录制命令,可百度相关教程。此处我只想验证能否用遥控器控制元件功能,暂时不管各按钮与实际名称与键值对应关系。下列代码复制于树莓派系列教程:红外遥控,感谢waterseason这位网友(如有涉及版权问题,请联系我删除这段代码)。上面的注释是我加上去的。

#include <bcm2835.h>		//需要包含的库文件
#include <stdio.h>
#include <stdlib.h>
//红外接收传感器使用的引脚pin口,BCM编号
#define PIN 22				
#define IO bcm2835_gpio_lev(PIN)
unsigned char i,idx,cnt;
unsigned char count;
unsigned char data[4];

static int flag = 0;
//接收到遥控器按钮按下信号后,在终端屏幕输出文字信息
//实际控制元件时,改为驱动对应元件的函数即可
int exec_cmd(unsigned char key_val){
	switch(key_val) {
		case 0x45://CH-
		       	printf("Button CH-\n");
			break;
		case 0x46://CH
			printf("Button CH\n");
			break;
		case 0x47://CH+
			printf("Button CH+\n");
			break;
		case 0x44://PREV
			printf("Button PREV\n");
			system("mpc prev");
			break;
		case 0x40://NEXT
			printf("Button NEXT\n");
			system("mpc next");
			break;
		case 0x43://PLAY/PAUSE
			printf("Button PLAY/PAUSE\n");
			if (flag == 0) {
				flag = 1;
				printf("mpc play\n");
				//system("mpc play");
			}else{
				flag = 0;
				printf("mpc pause\n");
				//system("mpc pause");
			}
			break;
		case 0x07://VOL-
			printf("Button VOL-\n");
			system("mpc volume -1");
			break;
		case 0x15://VOL+
			printf("Button VOL+\n");
			system("mpc volume +1");
			break;
		case 0x09://EQ
			printf("Button EQ\n");
			break;
		case 0x16://0
			printf("Button 0\n");
			break;
		case 0x19://100+
			printf("Button 100+\n");
			break;
		case 0x0d://200+
			printf("Button 200+\n");
			break;
		case 0x0c://1
			printf("Button 1\n");
			break;
		case 0x18://2
			printf("Button 2\n");
			break;
		case 0x5e://3
			printf("Button 3\n");
			break;
		case 0x08://4
			printf("Button 4\n");
			break;
		case 0x1c://5
			printf("Button 5\n");
			break;
		case 0x5a://6
			printf("Button 6\n");
			break;
		case 0x42://7
			printf("Button 7\n");
			break;
		case 0x52://8
			printf("Button 8\n");
			break;
		case 0x4a://9
			printf("Button 9\n");
			break;
		default:
			break;
	}
	return 0;
}

int main(int argc, char const *argv[]){
	if (!bcm2835_init()){
		return 1;
	}
	bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
	bcm2835_gpio_set_pud(PIN, BCM2835_GPIO_PUD_UP);
	printf("IRM Test Program ... \n");
	while (1) {
		if (IO == 0) {
			count = 0;
			while (IO == 0 && count++ < 200)   //9ms
				delayMicroseconds(60);

			count = 0;
			while (IO == 1 && count++ < 80)    //4.5ms
				delayMicroseconds(60);

			idx = 0;
			cnt = 0;
			data[0]=0;
			data[1]=0;
			data[2]=0;
			data[3]=0;
			for (i =0;i<32;i++) {
				count = 0;
				while (IO == 0 && count++ < 15)  //0.56ms
					delayMicroseconds(60);

				count = 0;
				while (IO == 1 && count++ < 40)  //0: 0.56ms; 1: 1.69ms
					delayMicroseconds(60);

				if (count > 25)
					data[idx] |= (1<<cnt);

				if (cnt== 7) {
					cnt=0;
					idx++;
				}else{
					cnt++;
				}
			}
			if ((data[0]+data[1] == 0xFF) && (data[2]+data[3]==0xFF)) {    //check
				printf("Get the key: 0x%02x\n",data[2]);
				exec_cmd(data[2]);
			}
		}
	}

	bcm2835_close();
	return 0;
}

实验结果图
在这里插入图片描述

参考资料

https://blog.csdn.net/jh1513/article/details/90692093
https://blog.csdn.net/waterseason/article/details/84916642
http://www.waveshare.net/study/article-622-1.html
https://www.cnblogs.com/cj2014/p/3848887.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Table of Contents 1 Introduction 4 1.1 Overview 4 1.2 Address map 4 1.2.1 Diagrammatic overview 4 1.2.2 ARM virtual addresses (standard Linux kernel only) 6 1.2.3 ARM physical addresses 6 1.2.4 Bus addresses 6 1.3 Peripheral access precautions for correct memory ordering 7 2 Auxiliaries: UART1 & SPI1, SPI2 8 2.1 Overview 8 2.1.1 AUX registers 9 2.2 Mini UART 10 2.2.1 Mini UART implementation details. 11 2.2.2 Mini UART register details. 11 2.3 Universal SPI Master (2x) 20 2.3.1 SPI implementation details 20 2.3.2 Interrupts 21 2.3.3 Long bit streams 21 2.3.4 SPI register details. 22 3 BSC 28 3.1 Introduction 28 3.2 Register View 28 3.3 10 Bit Addressing 36 4 DMA Controller 38 4.1 Overview 38 4.2 DMA Controller Registers 39 4.2.1 DMA Channel Register Address Map 40 4.3 AXI Bursts 63 4.4 Error Handling 63 4.5 DMA LITE Engines 63 5 External Mass Media Controller 65 o Introduction 65 o Registers 66 6 General Purpose I/O (GPIO) 89 6.1 Register View 90 6.2 Alternative Function Assignments 102 6.3 General Purpose GPIO Clocks 105 7 Interrupts 109 7.1 Introduction 109 7.2 Interrupt pending. 110 7.3 Fast Interrupt (FIQ). 110 7.4 Interrupt priority. 110 7.5 Registers 112 8 PCM / I2S Audio 119 8.1 Block Diagram 120 8.2 Typical Timing 120 8.3 Operation 121 8.4 Software Operation 122 8.4.1 Operating in Polled mode 122 8.4.2 Operating in Interrupt mode 123 8.4.3 DMA 123 8.5 Error Handling. 123 8.6 PDM Input Mode Operation 124 8.7 GRAY Code Input Mode Operation 124 8.8 PCM Register Map 125 9 Pulse Width Modulator 138 9.1 Overview 138 9.2 Block Diagram 138 9.3 PWM Implementation 139 9.4 Modes of Operation 139 9.5 Quick Reference 140 9.6 Control and Status Registers 141 10 SPI 148 10.1 Introduction 148 10.2 SPI Master Mode 148 10.2.1 Standard mode 148 10.2.2 Bidirectional mode 149 10.3 LoSSI mode 150 10.3.1 Command write 150 10.3.2 Parameter write 150 10.3.3 Byte read commands 151 10.3.4 24bit read command 151 10.3.5 32bit read command 151 10.4 Block Diagram 152 10.5 SPI Register Map 152 10.6 Software Operation 158 10.6.1 Polled 158 10.6.2 Interrupt 158 10.6.3 DMA 158 10.6.4 Notes 159 11 SPI/BSC SLAVE 160 11.1 Introduction 160 11.2 Registers 160 12 System Timer 172 12.1 System Timer Registers 172 13 UART 175 13.1 Variations from the 16C650 UART 175 13.2 Primary UART Inputs and Outputs 176 13.3 UART Interrupts 176 13.4 Register View 177 14 Timer (ARM side) 196 14.1 Introduction 196 14.2 Timer Registers: 196 15 USB 200 15.1 Configuration 200 15.2 Extra / Adapted registers. 202
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值