ZGC
3 years ago
8 changed files with 955 additions and 0 deletions
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
#人体接近传感器示例 |
||||
|
||||
|
||||
import RPi.GPIO as GPIO |
||||
import time |
||||
import sqlite_connector |
||||
|
||||
# 初始化 |
||||
def init(): |
||||
# 设置不显示警告 |
||||
GPIO.setwarnings(False) |
||||
# 设置读取面板针脚模式 |
||||
GPIO.setmode(GPIO.BOARD) |
||||
# 设置读取针脚标号 |
||||
GPIO.setup(11, GPIO.IN) |
||||
pass |
||||
|
||||
|
||||
def detct(): |
||||
while True: |
||||
curtime = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) |
||||
# 当高电平信号输入时报警 |
||||
if GPIO.input(11) == True: |
||||
alart(curtime) |
||||
print(GPIO.input(11)) |
||||
else: |
||||
continue |
||||
time.sleep(3) |
||||
|
||||
|
||||
def alart(curtime): |
||||
print(curtime + " Someone is coming!") |
||||
|
||||
|
||||
time.sleep(2) |
||||
init() |
||||
detct() |
||||
GPIO.cleanup() |
||||
|
||||
|
@ -0,0 +1,255 @@
@@ -0,0 +1,255 @@
|
||||
# -*- coding: UTF-8 -*- |
||||
import time |
||||
import RPi.GPIO as GPIO |
||||
#一下试一下nrf24l01的C语言宏定义 |
||||
TX_ADR_WIDTH = 5 # 5 uints TX address width |
||||
RX_ADR_WIDTH = 5 # 5 uints RX address width |
||||
TX_PLOAD_WIDTH = 32 # 20 uints TX payload |
||||
RX_PLOAD_WIDTH = 32 # 20 uints TX payload |
||||
|
||||
TX_ADDRESS = [0x34,0x43,0x10,0x10,0x01] #本地地址 |
||||
RX_ADDRESS = [0x34,0x43,0x10,0x10,0x01] #接收地址 |
||||
READ_REG = 0x00 # 读寄存器指令 |
||||
WRITE_REG = 0x20 # 写寄存器指令 |
||||
RD_RX_PLOAD = 0x61 # 读取接收数据指令 |
||||
WR_TX_PLOAD = 0xA0 # 写待发数据指令 |
||||
FLUSH_TX = 0xE1 # 冲洗发送 FIFO指令 |
||||
FLUSH_RX = 0xE2 # 冲洗接收 FIFO指令 |
||||
REUSE_TX_PL = 0xE3 # 定义重复装载数据指令 |
||||
NOP = 0xFF # 保留 |
||||
#*************************************SPI(nRF24L01)寄存器地址**************************************************** |
||||
CONFIG = 0x00 # 配置收发状态,CRC校验模式以及收发状态响应方式 |
||||
EN_AA = 0x01 # 自动应答功能设置 |
||||
EN_RXADDR = 0x02 # 可用信道设置 |
||||
SETUP_AW = 0x03 # 收发地址宽度设置 |
||||
SETUP_RETR = 0x04 # 自动重发功能设置 |
||||
RF_CH = 0x05 # 工作频率设置 |
||||
RF_SETUP = 0x06 # 发射速率、功耗功能设置 |
||||
STATUS = 0x07 # 状态寄存器 |
||||
OBSERVE_TX = 0x08 # 发送监测功能 |
||||
CD = 0x09 # 地址检测 |
||||
RX_ADDR_P0 = 0x0A # 频道0接收数据地址 |
||||
RX_ADDR_P1 = 0x0B # 频道1接收数据地址 |
||||
RX_ADDR_P2 = 0x0C # 频道2接收数据地址 |
||||
RX_ADDR_P3 = 0x0D # 频道3接收数据地址 |
||||
RX_ADDR_P4 = 0x0E # 频道4接收数据地址 |
||||
RX_ADDR_P5 = 0x0F # 频道5接收数据地址 |
||||
TX_ADDR = 0x10 # 发送地址寄存器 |
||||
RX_PW_P0 = 0x11 # 接收频道0接收数据长度 |
||||
RX_PW_P1 = 0x12 # 接收频道0接收数据长度 |
||||
RX_PW_P2 = 0x13 # 接收频道0接收数据长度 |
||||
RX_PW_P3 = 0x14 # 接收频道0接收数据长度 |
||||
RX_PW_P4 = 0x15 # 接收频道0接收数据长度 |
||||
RX_PW_P5 = 0x16 # 接收频道0接收数据长度 |
||||
FIFO_STATUS = 0x17 # FIFO栈入栈出状态寄存器设置 |
||||
|
||||
TX_OK = 0x20 #TX发送完成中断 |
||||
MAX_TX = 0x10 #达到最大发送次数中断 |
||||
|
||||
#sta = 0 |
||||
#RX_DR = 0 |
||||
#树莓派各个引脚的定义(使用BCM编码) |
||||
MOSI = 17 |
||||
CSN = 21 |
||||
MISO = 27 |
||||
SCK = 22 |
||||
CE = 20 |
||||
LIGHT = 26 |
||||
IRQ = 18 |
||||
def GPIO_Init(): |
||||
GPIO.setmode(GPIO.BCM) |
||||
GPIO.setwarnings(False) |
||||
Pinlist = [MOSI,CSN,SCK,CE,LIGHT] |
||||
GPIO.setup(Pinlist, GPIO.OUT) |
||||
Pinlist_Input = [MISO,IRQ] |
||||
GPIO.setup(Pinlist_Input, GPIO.IN) |
||||
return 0 |
||||
|
||||
def LEDH(): |
||||
GPIO.output(LIGHT, GPIO.HIGH) |
||||
|
||||
def LEDL(): |
||||
GPIO.output(LIGHT, GPIO.LOW) |
||||
|
||||
#**************************************************************************************************** |
||||
#*函数:uint SPI_RW(uint dat) |
||||
#*功能:NRF24L01的SPI写时序 |
||||
#**************************************************************************************************** |
||||
def SPI_RW(dat): |
||||
bit_ctr = 8 |
||||
_MOSI = 0 |
||||
while(bit_ctr): |
||||
|
||||
bit_ctr = bit_ctr - 1 |
||||
_MOSI = dat & 0x80 #output 'dat', MSB to MOSI |
||||
if(_MOSI): |
||||
GPIO.output(MOSI, GPIO.HIGH) |
||||
else: |
||||
GPIO.output(MOSI, GPIO.LOW) |
||||
dat = (dat << 1) #shift next bit into MSB.. |
||||
GPIO.output(SCK, GPIO.HIGH) #Set SCK GPIO.high.. |
||||
dat |= GPIO.input(MISO) #capture current MISO bit |
||||
GPIO.output(SCK, GPIO.LOW) #..then set SCK GPIO.low again |
||||
return dat #return read dat |
||||
|
||||
#**************************************************************************************************** |
||||
#*函数:uchar SPI_Read(uchar reg) |
||||
#*功能:NRF24L01的SPI读时序 |
||||
#***************************************************************************************************** |
||||
def SPI_Read(reg): |
||||
reg_val = 0 |
||||
GPIO.output(CSN, GPIO.LOW) #CSN GPIO.low, initialize SPI communication... |
||||
SPI_RW(reg) #Select register to read from.. |
||||
reg_val = SPI_RW(0) #..then read registervalue |
||||
GPIO.output(CSN, GPIO.HIGH) #CSN GPIO.high, terminate SPI communication |
||||
return reg_val #return register value |
||||
|
||||
#****************************************************************************************************# |
||||
#*功能:NRF24L01读写寄存器函数 |
||||
#****************************************************************************************************# |
||||
def SPI_RW_Reg(reg,value): |
||||
status = 0 |
||||
GPIO.output(CSN, GPIO.LOW) #CSN GPIO.low, init SPI transaction |
||||
status = SPI_RW(reg) #select register |
||||
SPI_RW(value) #..and write value to it.. |
||||
GPIO.output(CSN, GPIO.HIGH) #CSN GPIO.high again |
||||
return status #return nRF24L01 status uchar |
||||
|
||||
#****************************************************************************************************# |
||||
#*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars) |
||||
#*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数 |
||||
#****************************************************************************************************# |
||||
def SPI_Read_Buf(reg, pBuf, uchars): |
||||
status = 0 |
||||
uchar_ctr = 0 |
||||
GPIO.output(CSN, GPIO.LOW) # Set CSN GPIO.low, init SPI tranaction |
||||
status = SPI_RW(reg) # Select register to write to and read status uchar |
||||
while(uchar_ctr < uchars): |
||||
pBuf[uchar_ctr] = SPI_RW(0) # |
||||
uchar_ctr = uchar_ctr + 1 |
||||
GPIO.output(CSN, GPIO.HIGH) |
||||
return(status) #return nRF24L01 status uchar |
||||
|
||||
#********************************************************************************************************* |
||||
#*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars) |
||||
#*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数 |
||||
#*********************************************************************************************************# |
||||
def SPI_Write_Buf(reg, pBuf, uchars): |
||||
status = 0 |
||||
uchar_ctr = 0 |
||||
GPIO.output(CSN, GPIO.LOW) #SPI使能 |
||||
status = SPI_RW(reg) |
||||
while(uchar_ctr < uchars): # |
||||
SPI_RW(pBuf[uchar_ctr]) |
||||
uchar_ctr = uchar_ctr + 1 |
||||
GPIO.output(CSN, GPIO.HIGH) #关闭SPI |
||||
return(status) |
||||
|
||||
|
||||
#****************************************************************************************************# |
||||
#*函数:void SetRX_Mode(void) |
||||
#*功能:数据接收配置 |
||||
#****************************************************************************************************# |
||||
def SetRX_Mode(): |
||||
GPIO.output(CE, GPIO.LOW) |
||||
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f) # IRQ收发完成中断响应,16位CRC ,主接收 |
||||
GPIO.output(CE, GPIO.HIGH) |
||||
|
||||
|
||||
#******************************************************************************************************# |
||||
#*函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf) |
||||
#*功能:数据读取后放如rx_buf接收缓冲区中 |
||||
#******************************************************************************************************# |
||||
def nRF24L01_RxPacket(rx_buf): |
||||
|
||||
revale = 0 |
||||
sta = 0 |
||||
RX_DR = 0 |
||||
sta = SPI_Read(STATUS) # 读取状态寄存其来判断数据接收状况 |
||||
RX_DR = sta&0x40 |
||||
if(RX_DR): # 判断是否接收到数据 |
||||
GPIO.output(CE, GPIO.LOW) #SPI使能 |
||||
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH) # read receive payload from RX_FIFO buffer |
||||
revale =1 #读取数据完成标志 |
||||
SPI_RW_Reg(WRITE_REG+STATUS,sta) #接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志 |
||||
return revale |
||||
|
||||
|
||||
#*********************************************************************************************************** |
||||
#*函数:void nRF24L01_TxPacket(unsigned char * tx_buf) |
||||
#*功能:发送 tx_buf中数据 |
||||
#**********************************************************************************************************# |
||||
def nRF24L01_TxPacket(tx_buf): |
||||
sta = 0 |
||||
GPIO.output(CE, GPIO.LOW) |
||||
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e) |
||||
#微控制器把CE置高(至少10us) |
||||
GPIO.output(CE, GPIO.HIGH) |
||||
time.sleep(0.0001) |
||||
GPIO.output(CE, GPIO.LOW) #StandBy I模式 |
||||
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH) # 装载数据 |
||||
GPIO.output(CE, GPIO.HIGH) #置高CE,激发数据发送 |
||||
# while(GPIO.input(IRQ)!=0) #等待发送完成 |
||||
sta=SPI_Read(STATUS) |
||||
print(sta) |
||||
if(sta & MAX_TX): #达到最大重发次数 |
||||
SPI_RW_Reg(FLUSH_TX,0xff) #清除TX FIFO寄存器 |
||||
return MAX_TX |
||||
|
||||
if(sta&TX_OK): #发送完成 |
||||
return 0 |
||||
|
||||
return 0xff #其他原因发送失败 |
||||
|
||||
|
||||
#**************************************************************************************** |
||||
#*NRF24L01初始化 |
||||
#***************************************************************************************# |
||||
def Init_NRF24L01(): |
||||
GPIO.output(CE, GPIO.LOW) # chip enable |
||||
GPIO.output(CSN, GPIO.HIGH) # Spi disable |
||||
GPIO.output(SCK, GPIO.LOW) # Spi clock line init GPIO.high |
||||
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH) # 写本地地址 |
||||
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH) # 写接收端地址 |
||||
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01) # 频道0自动 ACK应答允许 |
||||
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01) # 允许接收地址只有频道0,如果需要多频道可以参考Page21 |
||||
SPI_RW_Reg(WRITE_REG + RF_CH, 40) # 设置信道工作为2.4GHZ,收发必须一致 |
||||
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH) #设置接收数据长度,本次设置为32字节 |
||||
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f) #设置发射速率为1MHZ,发射功率为最大值0dB |
||||
return 0 |
||||
|
||||
if __name__ == "__main__": |
||||
TxBuf = [1,1,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0] |
||||
RxBuf = [0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0] |
||||
SendResult = 0 |
||||
GPIO_Init() |
||||
Init_NRF24L01() |
||||
print("Init Over") |
||||
config_ADD = [0] |
||||
config_count = 1 |
||||
firstData = 0 |
||||
secondData = 0 |
||||
while True: |
||||
#测试发送变化的数据 |
||||
if firstData>16: |
||||
firstData = 1 |
||||
else: |
||||
firstData = firstData + 1 |
||||
pass |
||||
if secondData>31: |
||||
secondData = 1 |
||||
else: |
||||
secondData = secondData + 1 |
||||
pass |
||||
TxBuf = [firstData,secondData,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0] |
||||
#print("start send") |
||||
SendResult = nRF24L01_TxPacket(TxBuf) |
||||
#print("resultCode:"+str(SendResult)) |
||||
if SendResult == 0: |
||||
LEDH() |
||||
print("send data success") |
||||
time.sleep(0.1) |
||||
LEDL() |
||||
time.sleep(0.1) |
||||
pass |
||||
|
@ -0,0 +1,233 @@
@@ -0,0 +1,233 @@
|
||||
# -*- coding: UTF-8 -*- |
||||
import time |
||||
import RPi.GPIO as GPIO |
||||
#一下试一下nrf24l01的C语言宏定义 |
||||
TX_ADR_WIDTH = 5 # 5 uints TX address width |
||||
RX_ADR_WIDTH = 5 # 5 uints RX address width |
||||
TX_PLOAD_WIDTH = 32 # 20 uints TX payload |
||||
RX_PLOAD_WIDTH = 32 # 20 uints TX payload |
||||
|
||||
TX_ADDRESS = [0x34,0x43,0x10,0x10,0x01] #本地地址 |
||||
RX_ADDRESS = [0x34,0x43,0x10,0x10,0x01] #接收地址 |
||||
READ_REG = 0x00 # 读寄存器指令 |
||||
WRITE_REG = 0x20 # 写寄存器指令 |
||||
RD_RX_PLOAD = 0x61 # 读取接收数据指令 |
||||
WR_TX_PLOAD = 0xA0 # 写待发数据指令 |
||||
FLUSH_TX = 0xE1 # 冲洗发送 FIFO指令 |
||||
FLUSH_RX = 0xE2 # 冲洗接收 FIFO指令 |
||||
REUSE_TX_PL = 0xE3 # 定义重复装载数据指令 |
||||
NOP = 0xFF # 保留 |
||||
#*************************************SPI(nRF24L01)寄存器地址**************************************************** |
||||
CONFIG = 0x00 # 配置收发状态,CRC校验模式以及收发状态响应方式 |
||||
EN_AA = 0x01 # 自动应答功能设置 |
||||
EN_RXADDR = 0x02 # 可用信道设置 |
||||
SETUP_AW = 0x03 # 收发地址宽度设置 |
||||
SETUP_RETR = 0x04 # 自动重发功能设置 |
||||
RF_CH = 0x05 # 工作频率设置 |
||||
RF_SETUP = 0x06 # 发射速率、功耗功能设置 |
||||
STATUS = 0x07 # 状态寄存器 |
||||
OBSERVE_TX = 0x08 # 发送监测功能 |
||||
CD = 0x09 # 地址检测 |
||||
RX_ADDR_P0 = 0x0A # 频道0接收数据地址 |
||||
RX_ADDR_P1 = 0x0B # 频道1接收数据地址 |
||||
RX_ADDR_P2 = 0x0C # 频道2接收数据地址 |
||||
RX_ADDR_P3 = 0x0D # 频道3接收数据地址 |
||||
RX_ADDR_P4 = 0x0E # 频道4接收数据地址 |
||||
RX_ADDR_P5 = 0x0F # 频道5接收数据地址 |
||||
TX_ADDR = 0x10 # 发送地址寄存器 |
||||
RX_PW_P0 = 0x11 # 接收频道0接收数据长度 |
||||
RX_PW_P1 = 0x12 # 接收频道0接收数据长度 |
||||
RX_PW_P2 = 0x13 # 接收频道0接收数据长度 |
||||
RX_PW_P3 = 0x14 # 接收频道0接收数据长度 |
||||
RX_PW_P4 = 0x15 # 接收频道0接收数据长度 |
||||
RX_PW_P5 = 0x16 # 接收频道0接收数据长度 |
||||
FIFO_STATUS = 0x17 # FIFO栈入栈出状态寄存器设置 |
||||
|
||||
TX_OK = 0x20 #TX发送完成中断 |
||||
MAX_TX = 0x10 #达到最大发送次数中断 |
||||
|
||||
#sta = 0 |
||||
#RX_DR = 0 |
||||
#树莓派各个引脚的定义(使用BCM编码) |
||||
MOSI = 5 |
||||
CSN = 16 |
||||
MISO = 6 |
||||
SCK = 13 |
||||
CE = 19 |
||||
LIGHT = 12 |
||||
IRQ = 17 |
||||
def GPIO_Init(): |
||||
GPIO.setmode(GPIO.BCM) |
||||
GPIO.setwarnings(False) |
||||
Pinlist = [MOSI,CSN,SCK,CE,LIGHT] |
||||
GPIO.setup(Pinlist, GPIO.OUT) |
||||
Pinlist_Input = [MISO,IRQ] |
||||
GPIO.setup(Pinlist_Input, GPIO.IN) |
||||
return 0 |
||||
|
||||
def LEDH(): |
||||
GPIO.output(LIGHT, GPIO.HIGH) |
||||
|
||||
def LEDL(): |
||||
GPIO.output(LIGHT, GPIO.LOW) |
||||
|
||||
#**************************************************************************************************** |
||||
#*函数:uint SPI_RW(uint dat) |
||||
#*功能:NRF24L01的SPI写时序 |
||||
#**************************************************************************************************** |
||||
def SPI_RW(dat): |
||||
bit_ctr = 8 |
||||
_MOSI = 0 |
||||
while(bit_ctr): |
||||
|
||||
bit_ctr = bit_ctr - 1 |
||||
_MOSI = dat & 0x80 #output 'dat', MSB to MOSI |
||||
if(_MOSI): |
||||
GPIO.output(MOSI, GPIO.HIGH) |
||||
else: |
||||
GPIO.output(MOSI, GPIO.LOW) |
||||
dat = (dat << 1) #shift next bit into MSB.. |
||||
GPIO.output(SCK, GPIO.HIGH) #Set SCK GPIO.high.. |
||||
dat |= GPIO.input(MISO) #capture current MISO bit |
||||
GPIO.output(SCK, GPIO.LOW) #..then set SCK GPIO.low again |
||||
return dat #return read dat |
||||
|
||||
#**************************************************************************************************** |
||||
#*函数:uchar SPI_Read(uchar reg) |
||||
#*功能:NRF24L01的SPI读时序 |
||||
#***************************************************************************************************** |
||||
def SPI_Read(reg): |
||||
reg_val = 0 |
||||
GPIO.output(CSN, GPIO.LOW) #CSN GPIO.low, initialize SPI communication... |
||||
SPI_RW(reg) #Select register to read from.. |
||||
reg_val = SPI_RW(0) #..then read registervalue |
||||
GPIO.output(CSN, GPIO.HIGH) #CSN GPIO.high, terminate SPI communication |
||||
return reg_val #return register value |
||||
|
||||
#****************************************************************************************************# |
||||
#*功能:NRF24L01读写寄存器函数 |
||||
#****************************************************************************************************# |
||||
def SPI_RW_Reg(reg,value): |
||||
status = 0 |
||||
GPIO.output(CSN, GPIO.LOW) #CSN GPIO.low, init SPI transaction |
||||
status = SPI_RW(reg) #select register |
||||
SPI_RW(value) #..and write value to it.. |
||||
GPIO.output(CSN, GPIO.HIGH) #CSN GPIO.high again |
||||
return status #return nRF24L01 status uchar |
||||
|
||||
#****************************************************************************************************# |
||||
#*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars) |
||||
#*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数 |
||||
#****************************************************************************************************# |
||||
def SPI_Read_Buf(reg, pBuf, uchars): |
||||
status = 0 |
||||
uchar_ctr = 0 |
||||
GPIO.output(CSN, GPIO.LOW) # Set CSN GPIO.low, init SPI tranaction |
||||
status = SPI_RW(reg) # Select register to write to and read status uchar |
||||
while(uchar_ctr < uchars): |
||||
pBuf[uchar_ctr] = SPI_RW(0) # |
||||
uchar_ctr = uchar_ctr + 1 |
||||
GPIO.output(CSN, GPIO.HIGH) |
||||
return(status) #return nRF24L01 status uchar |
||||
|
||||
#********************************************************************************************************* |
||||
#*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars) |
||||
#*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数 |
||||
#*********************************************************************************************************# |
||||
def SPI_Write_Buf(reg, pBuf, uchars): |
||||
status = 0 |
||||
uchar_ctr = 0 |
||||
GPIO.output(CSN, GPIO.LOW) #SPI使能 |
||||
status = SPI_RW(reg) |
||||
while(uchar_ctr < uchars): # |
||||
SPI_RW(pBuf[uchar_ctr]) |
||||
uchar_ctr = uchar_ctr + 1 |
||||
GPIO.output(CSN, GPIO.HIGH) #关闭SPI |
||||
return(status) |
||||
|
||||
|
||||
#****************************************************************************************************# |
||||
#*函数:void SetRX_Mode(void) |
||||
#*功能:数据接收配置 |
||||
#****************************************************************************************************# |
||||
def SetRX_Mode(): |
||||
GPIO.output(CE, GPIO.LOW) |
||||
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f) # IRQ收发完成中断响应,16位CRC ,主接收 |
||||
GPIO.output(CE, GPIO.HIGH) |
||||
|
||||
|
||||
#******************************************************************************************************# |
||||
#*函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf) |
||||
#*功能:数据读取后放如rx_buf接收缓冲区中 |
||||
#******************************************************************************************************# |
||||
def nRF24L01_RxPacket(rx_buf): |
||||
|
||||
revale = 0 |
||||
sta = 0 |
||||
RX_DR = 0 |
||||
sta = SPI_Read(STATUS) # 读取状态寄存其来判断数据接收状况 |
||||
RX_DR = sta&0x40 |
||||
if(RX_DR): # 判断是否接收到数据 |
||||
GPIO.output(CE, GPIO.LOW) #SPI使能 |
||||
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH) # read receive payload from RX_FIFO buffer |
||||
revale =1 #读取数据完成标志 |
||||
SPI_RW_Reg(WRITE_REG+STATUS,sta) #接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志 |
||||
return revale |
||||
|
||||
|
||||
#*********************************************************************************************************** |
||||
#*函数:void nRF24L01_TxPacket(unsigned char * tx_buf) |
||||
#*功能:发送 tx_buf中数据 |
||||
#**********************************************************************************************************# |
||||
def nRF24L01_TxPacket(tx_buf): |
||||
sta = 0 |
||||
GPIO.output(CE, GPIO.LOW) |
||||
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e) |
||||
GPIO.output(CE, GPIO.HIGH) |
||||
time.sleep(0.00001) |
||||
GPIO.output(CE, GPIO.LOW) #StandBy I模式 |
||||
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH) # 装载数据 |
||||
GPIO.output(CE, GPIO.HIGH) #置高CE,激发数据发送 |
||||
# while(GPIO.input(IRQ)!=0) #等待发送完成 |
||||
sta=SPI_Read(STATUS) |
||||
if(sta & MAX_TX): #达到最大重发次数 |
||||
SPI_RW_Reg(FLUSH_TX,0xff) #清除TX FIFO寄存器 |
||||
return MAX_TX |
||||
|
||||
if(sta&TX_OK): #发送完成 |
||||
return 0 |
||||
|
||||
return 0xff #其他原因发送失败 |
||||
|
||||
|
||||
#**************************************************************************************** |
||||
#*NRF24L01初始化 |
||||
#***************************************************************************************# |
||||
def Init_NRF24L01(): |
||||
GPIO.output(CE, GPIO.LOW) # chip enable |
||||
GPIO.output(CSN, GPIO.HIGH) # Spi disable |
||||
GPIO.output(SCK, GPIO.LOW) # Spi clock line init GPIO.high |
||||
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH) # 写本地地址 |
||||
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH) # 写接收端地址 |
||||
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01) # 频道0自动 ACK应答允许 |
||||
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01) # 允许接收地址只有频道0,如果需要多频道可以参考Page21 |
||||
SPI_RW_Reg(WRITE_REG + RF_CH, 40) # 设置信道工作为2.4GHZ,收发必须一致 |
||||
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH) #设置接收数据长度,本次设置为32字节 |
||||
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f) #设置发射速率为1MHZ,发射功率为最大值0dB |
||||
return 0 |
||||
|
||||
if __name__ == "__main__": |
||||
TxBuf = [1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0] |
||||
RxBuf = [0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0] |
||||
GPIO_Init() |
||||
Init_NRF24L01() |
||||
SetRX_Mode() |
||||
while True: |
||||
if nRF24L01_RxPacket(RxBuf): |
||||
if RxBuf[0]: |
||||
print(RxBuf) |
||||
LEDH() |
||||
SetRX_Mode() |
||||
time.sleep(0.1) |
||||
LEDL() |
||||
time.sleep(0.1) |
@ -0,0 +1,78 @@
@@ -0,0 +1,78 @@
|
||||
import RPi.GPIO as GPIO |
||||
import time |
||||
|
||||
channel = 16 # 引脚号16 |
||||
data = [] # 温湿度值 |
||||
j = 0 # 计数器 |
||||
|
||||
GPIO.setmode(GPIO.BCM) # 以BCM编码格式 |
||||
|
||||
time.sleep(1) # 时延一秒 |
||||
|
||||
GPIO.setup(channel, GPIO.OUT) |
||||
|
||||
GPIO.output(channel, GPIO.LOW) |
||||
time.sleep(0.02) # 给信号提示传感器开始工作 |
||||
GPIO.output(channel, GPIO.HIGH) |
||||
|
||||
GPIO.setup(channel, GPIO.IN) |
||||
|
||||
while GPIO.input(channel) == GPIO.LOW: |
||||
print("----------------low1---------------") |
||||
continue |
||||
|
||||
while GPIO.input(channel) == GPIO.HIGH: |
||||
print("----------------high---------------") |
||||
continue |
||||
|
||||
#signal get |
||||
while j < 40: |
||||
k = 0 |
||||
while GPIO.input(channel) == GPIO.LOW: |
||||
print("----------------low2---------------") |
||||
continue |
||||
|
||||
while GPIO.input(channel) == GPIO.HIGH: |
||||
k += 1 |
||||
if k > 100: |
||||
break |
||||
print("----------------k="+str(k)+"---------------") |
||||
if k < 38: |
||||
data.append(0) |
||||
else: |
||||
data.append(1) |
||||
|
||||
j += 1 |
||||
|
||||
print("sensor is working.") |
||||
print(data) # 输出初始数据高低电平 |
||||
print(len(data)) # 长度 |
||||
|
||||
humidity_bit = data[0:8] # 分组 |
||||
humidity_point_bit = data[8:16] |
||||
temperature_bit = data[16:24] |
||||
temperature_point_bit = data[24:32] |
||||
check_bit = data[32:40] |
||||
|
||||
humidity = 0 |
||||
humidity_point = 0 |
||||
temperature = 0 |
||||
temperature_point = 0 |
||||
check = 0 |
||||
|
||||
for i in range(8): |
||||
humidity += humidity_bit[i] * 2 ** (7 - i) # 转换成十进制数据 |
||||
humidity_point += humidity_point_bit[i] * 2 ** (7 - i) |
||||
temperature += temperature_bit[i] * 2 ** (7 - i) |
||||
temperature_point += temperature_point_bit[i] * 2 ** (7 - i) |
||||
check += check_bit[i] * 2 ** (7 - i) |
||||
|
||||
tmp = humidity + humidity_point + temperature + temperature_point # 十进制的数据相加 |
||||
|
||||
if check == tmp: # 数据校验,相等则输出 |
||||
print("temperature : ", temperature, ", humidity : ", humidity) |
||||
else: # 错误输出错误信息,和校验数据 |
||||
print("wrong") |
||||
print("temperature : ", temperature, ", humidity : ", humidity, " check : ", check, " tmp : ", tmp) |
||||
|
||||
GPIO.cleanup() |
@ -0,0 +1,80 @@
@@ -0,0 +1,80 @@
|
||||
import RPi.GPIO as gpio |
||||
import time |
||||
import dhtsensor |
||||
|
||||
gpio.setwarnings(False) |
||||
gpio.setmode(gpio.BOARD) |
||||
time.sleep(1) |
||||
data = [] |
||||
|
||||
|
||||
def delay(i): # 20*i usdelay |
||||
a = 0 |
||||
for j in range(i): |
||||
a + 1 |
||||
|
||||
|
||||
j = 0 |
||||
# start work |
||||
gpio.setup(12, gpio.OUT) |
||||
# gpio.output(12,gpio.HIGH) |
||||
# delay(10) |
||||
gpio.output(12, gpio.LOW) |
||||
time.sleep(0.02) |
||||
gpio.output(12, gpio.HIGH) |
||||
i = 1 |
||||
i = 1 |
||||
|
||||
# wait to response |
||||
gpio.setup(12, gpio.IN) |
||||
|
||||
while gpio.input(12) == 1: |
||||
continue |
||||
|
||||
while gpio.input(12) == 0: |
||||
continue |
||||
|
||||
while gpio.input(12) == 1: |
||||
continue |
||||
# get data |
||||
|
||||
while j < 40: |
||||
k = 0 |
||||
while gpio.input(12) == 0: |
||||
continue |
||||
|
||||
while gpio.input(12) == 1: |
||||
k += 1 |
||||
if k > 100: break |
||||
if k < 3: |
||||
data.append(0) |
||||
else: |
||||
data.append(1) |
||||
j += 1 |
||||
|
||||
print("Sensor is working") |
||||
# get temperature |
||||
humidity_bit = data[0:8] |
||||
humidity_point_bit = data[8:16] |
||||
temperature_bit = data[16:24] |
||||
temperature_point_bit = data[24:32] |
||||
check_bit = data[32:40] |
||||
|
||||
humidity = 0 |
||||
humidity_point = 0 |
||||
temperature = 0 |
||||
temperature_point = 0 |
||||
check = 0 |
||||
|
||||
for i in range(8): |
||||
humidity += humidity_bit[i] * 2 ** (7 - i) |
||||
humidity_point += humidity_point_bit[i] * 2 ** (7 - i) |
||||
temperature += temperature_bit[i] * 2 ** (7 - i) |
||||
temperature_point += temperature_point_bit[i] * 2 ** (7 - i) |
||||
check += check_bit[i] * 2 ** (7 - i) |
||||
|
||||
tmp = humidity + humidity_point + temperature + temperature_point |
||||
if check == tmp: |
||||
print("temperature is ", temperature, "wet is ", humidity, "%") |
||||
else: |
||||
print("something is worong the humidity,humidity_point,temperature,temperature_point,check is", humidity, humidity_point, temperature, temperature_point, check) |
@ -0,0 +1,222 @@
@@ -0,0 +1,222 @@
|
||||
from machine import I2C,Pin |
||||
from ssd1306 import SSD1306_I2C |
||||
import math |
||||
|
||||
chine=[ |
||||
#/*-- 文字: 执 --*/ |
||||
#/*-- 新宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/ |
||||
0x10,0x10,0x10,0xFF,0x10,0x90,0x00,0x10,0x10,0xFF,0x10,0x10,0xF0,0x00,0x00,0x00, |
||||
0x04,0x44,0x82,0x7F,0x01,0x80,0x40,0x21,0x1A,0x07,0x18,0x00,0x3F,0x40,0xF0,0x00, |
||||
|
||||
#/*-- 文字: 念 --*/ |
||||
#/*-- 新宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/ |
||||
0x40,0x40,0x20,0x20,0x90,0x88,0x94,0xE3,0x84,0x88,0x90,0x20,0x20,0x40,0x40,0x00, |
||||
0x40,0x30,0x00,0x00,0x38,0x40,0x40,0x44,0x5A,0x41,0x40,0x70,0x00,0x08,0x30,0x00, |
||||
|
||||
#/*-- 文字: 执 --*/ |
||||
#/*-- 新宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/ |
||||
0x10,0x10,0x10,0xFF,0x10,0x90,0x00,0x10,0x10,0xFF,0x10,0x10,0xF0,0x00,0x00,0x00, |
||||
0x04,0x44,0x82,0x7F,0x01,0x80,0x40,0x21,0x1A,0x07,0x18,0x00,0x3F,0x40,0xF0,0x00, |
||||
|
||||
#/*-- 文字: 战 --*/ |
||||
#/*-- 新宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/ |
||||
0x00,0x00,0x00,0xFF,0x08,0x08,0x08,0x40,0x40,0x40,0xFF,0x20,0x22,0xAC,0x20,0x00, |
||||
0x00,0x7F,0x21,0x21,0x21,0x21,0x7F,0x80,0x40,0x20,0x17,0x18,0x26,0x41,0xF0,0x00, |
||||
|
||||
|
||||
] |
||||
i2c=I2C(-1, sda=Pin(4), scl=Pin(5), freq=400000) |
||||
oled = SSD1306_I2C(128, 64, i2c) |
||||
def ByteOpera(num,dat): |
||||
byte= [0x01,0x02,0x04,0x8,0x10,0x20,0x40,0x80] |
||||
if dat&byte[num]: |
||||
return 1 |
||||
else: |
||||
return 0 |
||||
def hline(x0,y0,le,color): |
||||
for i in range(le): |
||||
oled.pixel(x0+i,y0,color) |
||||
|
||||
def shuline(x0,y0,le,color): |
||||
for i in range(le): |
||||
oled.pixel(x0,y0+i,color) |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class GUI: |
||||
#画点 |
||||
def DrawDot(x,y): |
||||
oled.pixel(x,y) |
||||
#横线 |
||||
def hline(x0,y0,le,color): |
||||
for i in range(le): |
||||
oled.pixel(x0+i,y0,color) |
||||
#竖线 |
||||
def shuline(x0,y0,le,color): |
||||
for i in range(le): |
||||
oled.pixel(x0,y0+i,color) |
||||
|
||||
########################## |
||||
#函数:Line |
||||
#功能:任意画线 |
||||
#描述: |
||||
# x0,y0:起始位置 |
||||
# x1,y1:终止位置 |
||||
# color:颜色 |
||||
# |
||||
def Line(x0,y0,x1,y1,color): |
||||
dx=x1-x0 |
||||
if(dx>0): |
||||
s1=1 |
||||
else: |
||||
s1=-1 |
||||
dy=y1-y0 |
||||
if(dy>0): |
||||
s2=1 |
||||
else: |
||||
s2=-1 |
||||
dx=math.fabs(x1-x0) |
||||
dy=math.fabs(y1-y0) |
||||
if(dy>dx): |
||||
temp=dx |
||||
dx=dy |
||||
dy=temp |
||||
status=1 |
||||
else: |
||||
status=0 |
||||
|
||||
|
||||
if(dx==0): |
||||
hline(x0,y0,y1-y0,color) |
||||
if(dy==0): |
||||
shuline(x0,y0,x1-x0,color) |
||||
|
||||
sub=2*dy-dx |
||||
for i in range(dx): |
||||
oled.pixel(x0,y0,color) |
||||
if(sub>0): |
||||
if(status==1): |
||||
x0+=s1 |
||||
else: |
||||
y0+=s2 |
||||
sub-=2*dx |
||||
if(status==1): |
||||
y0+=s2 |
||||
else: |
||||
x0+=s1 |
||||
sub+=2*dy |
||||
#oled.show() |
||||
|
||||
####################### |
||||
#画矩形函数 |
||||
#fill为是否填充,默认不填充 |
||||
def DrawBox(x0,y0,x1,y1,color=1,fill=0): |
||||
if (fill==1): |
||||
for i in range(y1-y0): |
||||
hline(x0,y0+i,x1-x0,color) |
||||
else: |
||||
hline(x0,y0,x1-x0,color) |
||||
hline(x0,y1,x1-x0,color) |
||||
shuline(x0,y0,y1-y0,color) |
||||
shuline(x1,y0,y1-y0,color) |
||||
|
||||
############################## |
||||
#画圆函数,很占CPU |
||||
#fill 为是否填充 |
||||
#画两遍,是因为只画一遍的话中间有点画不上 |
||||
def DrawCircle_math(x,y,r,color,fill=0): |
||||
if(fill==0): |
||||
for i in range(x-r,x+r+1): |
||||
oled.pixel(i,int(y-math.sqrt(r*r-(x-i)*(x-i))),color) |
||||
oled.pixel(i,int(y+math.sqrt(r*r-(x-i)*(x-i))),color) |
||||
for i in range(y-r,y+r+1): |
||||
oled.pixel(int(x-math.sqrt(r*r-(y-i)*(y-i))),i,color) |
||||
oled.pixel(int(x+math.sqrt(r*r-(y-i)*(y-i))),i,color) |
||||
else: |
||||
for i in range(x-r,x+r+1): |
||||
a=int(math.sqrt(r*r-(x-i)*(x-i))) |
||||
shuline(i,y-a,a*2,color) |
||||
|
||||
for i in range(y-r,y+r+1): |
||||
a=int(math.sqrt(r*r-(y-i)*(y-i))) |
||||
hline(x-a,i,a*2,color) |
||||
|
||||
|
||||
######################### |
||||
#画圆函数 |
||||
#网上的算法,不怎么圆 |
||||
def DrawCircle(x,y,r,color): |
||||
a=0 |
||||
b=r |
||||
di=3-2*r |
||||
while a<b: |
||||
oled.pixel(x-b,y-a,color) |
||||
oled.pixel(x+b,y-a,color) |
||||
oled.pixel(x-a,y+b,color) |
||||
oled.pixel(x-b,y-a,color) |
||||
oled.pixel(x-a,y-b,color) |
||||
oled.pixel(x+b,y+a,color) |
||||
oled.pixel(x+a,y-b,color) |
||||
oled.pixel(x+a,y+b,color) |
||||
oled.pixel(x-b,y+a,color) |
||||
a=a+1 |
||||
if(di<0): |
||||
di+=4*a+6 |
||||
else: |
||||
di+=10+4*(a-b) |
||||
b=b-1 |
||||
oled.pixel(x+a,y+b,color) |
||||
#oled.show() |
||||
############################ |
||||
#16x16中文字符函数 |
||||
def ShowChar16x16(x,y,n): |
||||
for i in range(2): |
||||
for a in range(16): |
||||
for b in range(8): |
||||
if(ByteOpera(b,chine[n*32+i*16+a])): |
||||
oled.pixel(x+a,y+i*8+b,1) |
||||
else: |
||||
oled.pixel(x+a,y+i*8+b,0) |
||||
#oled.show() |
||||
###################### |
||||
#任意大小图片显示 |
||||
def ShowPic(x,y,w,h,color,pic): |
||||
a=h/8 |
||||
if(h%8>0): |
||||
a+=1 |
||||
for i in range(a): |
||||
for n in range(w): |
||||
for z in range(8): |
||||
if(ByteOpera(z,pic[a*w+n])): |
||||
oled.pixel(w,y+a*8+z,1) |
||||
else: |
||||
oled.pixel(w,y+a*8+z,0) |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#奇数时反相显示,偶数时正常显示 |
||||
def invert(n): |
||||
oled.invert(n) |
||||
#调整亮度。0最暗,255最亮 |
||||
def contrast(n): |
||||
oled.contrast(n) |
||||
#在(x, y)处显示字符串,注意text()函数内置的字体是8x8的,暂时不能替换 |
||||
def text(char,x,y): |
||||
oled.text(char,x,y) |
||||
#n=0,清空屏幕,n大于0,填充屏幕 |
||||
def fill(n): |
||||
oled.fill(n) |
||||
####################### |
||||
#显示函数 |
||||
def show(): |
||||
oled.show() |
||||
#关屏函数 |
||||
def poweroff(): |
||||
oled.poweroff() |
||||
def poweron(): |
||||
oled.poweron() |
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
# SPDX-FileCopyrightText: Tony DiCola |
||||
# SPDX-License-Identifier: CC0-1.0 |
||||
|
||||
# Basic example of clearing and drawing pixels on a SSD1306 OLED display. |
||||
# This example and library is meant to work with Adafruit CircuitPython API. |
||||
|
||||
# Import all board pins. |
||||
from board import SCL, SDA |
||||
import busio |
||||
|
||||
# Import the SSD1306 module. |
||||
import adafruit_ssd1306 |
||||
|
||||
|
||||
# Create the I2C interface. |
||||
i2c = busio.I2C(SCL, SDA) |
||||
|
||||
# Create the SSD1306 OLED class. |
||||
# The first two parameters are the pixel width and pixel height. Change these |
||||
# to the right size for your display! |
||||
display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c) |
||||
# Alternatively you can change the I2C address of the device with an addr parameter: |
||||
# display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x31) |
||||
|
||||
# Clear the display. Always call show after changing pixels to make the display |
||||
# update visible! |
||||
display.fill(0) |
||||
display.show() |
||||
|
||||
# Set a pixel in the origin 0,0 position. |
||||
display.pixel(0, 0, 1) |
||||
# Set a pixel in the middle 64, 16 position. |
||||
display.pixel(64, 16, 1) |
||||
# Set a pixel in the opposite 127, 31 position. |
||||
display.pixel(127, 31, 1) |
||||
|
||||
|
||||
display.show() |
Loading…
Reference in new issue