Stm32c8t6制作遥控器

 

 

 

基本概述

硬件组成

摇杆 RKJXV1224005     OLED屏    STLINK    

功能

传输和接收数据

硬件部分

摇杆 RKJXV1224005

1f31158687c68cd0f826b9c5c90f0f79.jpeg

摇杆中有6个引脚来读取电路板的AD值,分别用摇杆上下左右的偏移程度得到不同的AD值,以达到读取摇杆状态的目的

NPF24L01

4ecd72700a1c6e6cf90243d0699f05ee.jpeg

传输和接收数据

 

代码模块

传输模块

NPF24L01.c

 

# include "NRF24L01.h"
#include "NRF24L01_Ins.h"
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

/***************************************************************************

׷Ҫگ˽ʹԃ˵ķú

NRF24L01_Pin_Init()		ӽޅԵʼۯ, Ϟӎ˽ìϞ׵ܘֵìһѣһַԃìԵʼۯֱޓַԃNRF24L01_Init()ܴࠉ

*---------------------------------------------------------------------------*
NRF24L01_Write_Reg(uint8_t Reg, uint8_t Value)		дNRF24L01݄զǷ

ӎ˽úReg, ݄զǷַ֘
	  ValueúҪдɫք˽ߝ
׵ܘֵú״ֵ̬
*---------------------------------------------------------------------------*
NRF24L01_Read_Reg(uint8_t Reg)		ׁNRF24L01݄զǷ

ӎ˽úRegú݄զǷַ֘
׵ܘֵú״ֵ̬
*---------------------------------------------------------------------------*
NRF24L01_Read_Buf(uint8_t Reg, uint8_t *Buf, uint8_t Len)	һՎׁNRF24L01݄զǷנٶؖޚ

ӎ˽úRegú݄զǷַ֘û*Bufú݄զǷؖޚׁԶ۳զԢք˽ةûLenúҪׁԶքؖޚٶ˽
׵ܘֵú״ֵ̬
*---------------------------------------------------------------------------*
NRF24L01_Write_Buf(uint8_t Reg, uint8_t *Buf, uint8_t Len)		һՎдNRF24L01݄զǷנٶؖޚ

ӎ˽úRegú݄զǷַ֘û*Bufú݄զǷдɫؖޚքզ؅˽ةûLenúҪׁԶքؖޚٶ˽
׵ܘֵú״ֵ̬
*---------------------------------------------------------------------------*
NRF24L01_GetRxBuf(uint8_t *Buf)		ׁԶޓ˕սք˽ߝìΪנٶؖޚ

ӎ˽ú*Bufנٶؖޚզ؅ք˽ة
׵ܘֵú״ֵ̬
*---------------------------------------------------------------------------*
NRF24L01_SendTxBuf(uint8_t *Buf)	ע̍נٶؖޚ

ӎ˽ú*BufҪע̍քؖޚզ؅ք˽ة
׵ܘֵú״ֵ̬
*---------------------------------------------------------------------------*
NRF24L01_Check(void)				ݬҩNRF24L01ˇرզ՚

׵ܘֵú1úһզ՚ì0úզ՚
*---------------------------------------------------------------------------*
NRF24L01_Init()						NRF24L01ԵʼۯìѼڬݬҩNRF24L01ˇرզ՚b˕עƤ׃ԵʼۯìԵʼۯֱޓַԃكگ˽ܴࠉ

*---------------------------------------------------------------------------*
NRF24L01_RT_Init(void) 				NRF24L01˕עƤ׃Եʼۯ
*---------------------------------------------------------------------------*
NRF24L01_SendBuf(uint8_t *Buf)		NRF24L01ע̍נٶؖޚ˽ߝ

ӎ˽úҪע̍ؖޚզ؅ք˽ة
*---------------------------------------------------------------------------*
uint8_t NRF24L01_Get_Value_Flag()	ܱȡא׏Ҫ־ìһթޓ˕ս˽ߝì׵ܘ0

׵ܘֵú0úޓ˕ս˽ߝû1úδޓ˕ս˽ߝ

**************************     By:ǕǕͨͨOrdinary    **********************************/


/***************    ݇փɽl֧ԖһЂŶáQAQ  *******************************************/


#define TX_ADR_WIDTH    5     //5ؖޚַ֘࠭׈
#define RX_ADR_WIDTH    5     //5ؖޚַ֘࠭׈
#define TX_PLOAD_WIDTH  32    //32ؖޚԐЧ˽ߝ࠭׈
#define RX_PLOAD_WIDTH  32    //32ؖޚԐЧ˽ߝ࠭׈

const uint8_t TX_ADDRESS[TX_ADR_WIDTH]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 
const uint8_t RX_ADDRESS[RX_ADR_WIDTH]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 


void W_SS(uint8_t BitValue)
{
	GPIO_WriteBit(CSN_Port, CSN_Pin, (BitAction)BitValue);//A4
} 

void W_CE(uint8_t BitValue)
{
	GPIO_WriteBit(CE_Port, CE_Pin, (BitAction)BitValue);//B0
} 

void W_SCK(uint8_t BitValue)
{
	GPIO_WriteBit(SCK_Port, SCK_Pin, (BitAction)BitValue);//A5
} 

void W_MOSI(uint8_t BitValue)
{
	GPIO_WriteBit(MOSI_Port, MOSI_Pin,(BitAction)BitValue);//A7
} 

uint8_t R_MISO(void)
{
	return GPIO_ReadInputDataBit(MISO_Port, MISO_Pin);//A6
}

uint8_t R_IRQ(void)
{
	return GPIO_ReadInputDataBit(IRQ_Port, IRQ_Pin);//B1
}

void NRF24L01_Pin_Init(void)	
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = CSN_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(CSN_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = SCK_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(SCK_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = MOSI_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(MOSI_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = CE_Pin;  
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(CE_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = MISO_Pin;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(MISO_Port, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = IRQ_Pin;  			
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(IRQ_Port, &GPIO_InitStructure);
	
}
		


uint8_t SPI_SwapByte(uint8_t Byte)  
{
	uint8_t i;
	for(i = 0; i < 8; i ++) 
	{
		if((uint8_t)(Byte & 0x80) == 0x80)
		{
			W_MOSI(1);
		} 		
		else 
		{
			W_MOSI(0);
		}			
		Byte = (Byte << 1);			
		W_SCK(1);					
		Byte |= R_MISO();	        
		W_SCK(0);					
	}
	return Byte;
}
uint8_t NRF24L01_Write_Reg(uint8_t Reg, uint8_t Value)
{
	uint8_t Status;

	W_SS(0);                  
  	Status = SPI_SwapByte(Reg);
	SPI_SwapByte(Value);	
	W_SS(1);                 

	return Status;
}

uint8_t NRF24L01_Read_Reg(uint8_t Reg)
{
 	uint8_t Value;

	W_SS(0);              
  	SPI_SwapByte(Reg);		
	Value = SPI_SwapByte(NOP);
	W_SS(1);             	

	return Value;
}




uint8_t NRF24L01_Read_Buf(uint8_t Reg, uint8_t *Buf, uint8_t Len)
{
	uint8_t Status, i;
	W_SS(0);                    	
	Status =SPI_SwapByte(Reg);		
 	for(i = 0; i < Len; i ++)
	{
		Buf[i] = SPI_SwapByte(NOP);	
	}
	W_SS(1);                 		
	return Status;        			
}

uint8_t NRF24L01_Write_Buf(uint8_t Reg, uint8_t *Buf, uint8_t Len)
{
	uint8_t Status, i;
	W_SS(0); 
	Status = SPI_SwapByte(Reg);		
	for(i = 0; i < Len; i ++)
	{
		SPI_SwapByte(*Buf ++); 		
	}
	W_SS(1); 
	return Status;          		
}							  					   

uint8_t NRF24L01_GetRxBuf(uint8_t *Buf)
{
	uint8_t State;
	State = NRF24L01_Read_Reg(STATUS);  			
	NRF24L01_Write_Reg(nRF_WRITE_REG + STATUS, State);
	if(State & RX_OK)								
	{                                                       
		W_CE(1);											
		NRF24L01_Read_Buf(RD_RX_PLOAD, Buf, RX_PLOAD_WIDTH);
		NRF24L01_Write_Reg(FLUSH_RX, NOP);					
		W_CE(1);				
		Delay_us(150);
		return 0; 
	}	   
	return 1;
}
uint8_t NRF24L01_SendTxBuf(uint8_t *Buf)
{
	uint8_t State;
   
	W_CE(0);												
  	NRF24L01_Write_Buf(WR_TX_PLOAD, Buf, TX_PLOAD_WIDTH);	
 	W_CE(1);												
	while(R_IRQ() == 1);									
	State = NRF24L01_Read_Reg(STATUS);  					
	NRF24L01_Write_Reg(nRF_WRITE_REG + STATUS, State); 		
	if(State&MAX_TX)				 						
	{
		NRF24L01_Write_Reg(FLUSH_TX, NOP);					
		return MAX_TX; 
	}
	if(State & TX_OK)	
	{
		return TX_OK;
	}
	return NOP;	
}

uint8_t NRF24L01_Check(void)
{
	uint8_t check_in_buf[5] = {0x11 ,0x22, 0x33, 0x44, 0x55};
	uint8_t check_out_buf[5] = {0x00};

	W_SCK(0);
	W_SS(1);   
	W_CE(0);	

	NRF24L01_Write_Buf(nRF_WRITE_REG + TX_ADDR, check_in_buf, 5);

	NRF24L01_Read_Buf(nRF_READ_REG + TX_ADDR, check_out_buf, 5);

	if((check_out_buf[0] == 0x11) && (check_out_buf[1] == 0x22) && (check_out_buf[2] == 0x33) && (check_out_buf[3] == 0x44) && (check_out_buf[4] == 0x55))
	{
		return 0;
	}
	else 
	{
		return 1;
	}
}			


void NRF24L01_RT_Init(void) 
{	
	W_CE(0);		  
  	NRF24L01_Write_Reg(nRF_WRITE_REG+RX_PW_P0, RX_PLOAD_WIDTH);
	NRF24L01_Write_Reg(FLUSH_RX, NOP);									
  	NRF24L01_Write_Buf(nRF_WRITE_REG + TX_ADDR, (uint8_t*)TX_ADDRESS, TX_ADR_WIDTH);
  	NRF24L01_Write_Buf(nRF_WRITE_REG + RX_ADDR_P0, (uint8_t*)RX_ADDRESS, RX_ADR_WIDTH);   
  	NRF24L01_Write_Reg(nRF_WRITE_REG + EN_AA, 0x01);     
  	NRF24L01_Write_Reg(nRF_WRITE_REG + EN_RXADDR, 0x01); 
  	NRF24L01_Write_Reg(nRF_WRITE_REG + SETUP_RETR, 0x1A);
  	NRF24L01_Write_Reg(nRF_WRITE_REG + RF_CH, 0);        
  	NRF24L01_Write_Reg(nRF_WRITE_REG + RF_SETUP, 0x0F);  
  	NRF24L01_Write_Reg(nRF_WRITE_REG + CONFIG, 0x0F);    
	W_CE(1);									  
}

void NRF24L01_Init()
{
	NRF24L01_Pin_Init();
	while(NRF24L01_Check());
	NRF24L01_RT_Init();
	
}

void NRF24L01_SendBuf(uint8_t *Buf)
{
	W_CE(0);									
	NRF24L01_Write_Reg(nRF_WRITE_REG + CONFIG, 0x0E);   
	W_CE(1);
	Delay_us(15);
	NRF24L01_SendTxBuf(Buf);						
	W_CE(0);
	NRF24L01_Write_Reg(nRF_WRITE_REG + CONFIG, 0x0F);
	W_CE(1);	
}

uint8_t NRF24L01_Get_Value_Flag()
{
	return R_IRQ();
}

NPF24L01.h

#ifndef __nRF24L01_API_H
#define __nRF24L01_API_H

#include "stm32f10x.h"                  // Device header

uint8_t SPI_SwapByte(uint8_t byte);
uint8_t NRF24L01_Write_Reg(uint8_t reg,uint8_t value);
uint8_t NRF24L01_Read_Reg(uint8_t reg);
uint8_t NRF24L01_Read_Buf(uint8_t reg,uint8_t *pBuf, uint8_t len);
uint8_t NRF24L01_Write_Buf(uint8_t reg, uint8_t *pBuf, uint8_t len);
uint8_t NRF24L01_GetRxBuf(uint8_t *rxbuf);
uint8_t NRF24L01_SendTxBuf(uint8_t *txbuf);
uint8_t NRF24L01_Check(void);
void NRF24L01_RT_Init(void);
void NRF24L01_Init(void);
void NRF24L01_SendBuf(uint8_t *Buf);
void NRF24L01_Pin_Init(void);
//uint8_t NRF24L01_Get_Value_Flag();

#endif

 NPF24L01_lr

#ifndef __nRF24L01_H
#define __nRF24L01_H

/**********  NRF24L01ӽޅ֨ӥ  ***********/
#define IRQ_Port	GPIOB
#define CE_Port		GPIOB
#define CSN_Port	GPIOB
#define SCK_Port	GPIOB
#define MOSI_Port	GPIOB
#define MISO_Port	GPIOB

#define IRQ_Pin		GPIO_Pin_11
#define CE_Pin		GPIO_Pin_12
#define CSN_Pin		GPIO_Pin_10
#define SCK_Pin		GPIO_Pin_13
#define MOSI_Pin	GPIO_Pin_15
#define MISO_Pin	GPIO_Pin_14

/**********  NRF24L01݄զǷәطļ®  ***********/
#define nRF_READ_REG        0x00 
#define nRF_WRITE_REG       0x20 
#define RD_RX_PLOAD     0x61  
#define WR_TX_PLOAD     0xA0  
#define FLUSH_TX        0xE1  
#define FLUSH_RX        0xE2  
#define REUSE_TX_PL     0xE3  
#define NOP             0xFF  
/**********  NRF24L01݄զǷַ֘   *************/
#define CONFIG          0x00                           
#define EN_AA           0x01  
#define EN_RXADDR       0x02  
#define SETUP_AW        0x03  
#define SETUP_RETR      0x04  
#define RF_CH           0x05  
#define RF_SETUP        0x06  
#define STATUS          0x07  
#define OBSERVE_TX      0x08  
#define CD              0x09  
#define RX_ADDR_P0      0x0A  
#define RX_ADDR_P1      0x0B  
#define RX_ADDR_P2      0x0C  
#define RX_ADDR_P3      0x0D  
#define RX_ADDR_P4      0x0E  
#define RX_ADDR_P5      0x0F  
#define TX_ADDR         0x10  
#define RX_PW_P0        0x11   
#define RX_PW_P1        0x12   
#define RX_PW_P2        0x13   
#define RX_PW_P3        0x14   
#define RX_PW_P4        0x15  
#define RX_PW_P5        0x16  
#define FIFO_STATUS     0x17  

/******   STATUS݄զǷbitλ֨ӥ      *******/
#define MAX_TX  	0x10  	  //կսخճע̍Վ˽א׏
#define TX_OK   	0x20  	  //TXע̍Ϊԉא׏
#define RX_OK   	0x40  	  //ޓ˕ս˽ߝא׏

#endif

key.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "Timer.h"
//#include "can.h"
#include "OLED.h"
//#include "SendMessage.h"
//#include "MyFlash.h"
//#include "Queue.h"

u8 KeyNum0,KeyNum1,KeyNum2;
u32 KeyRaiseFlag,KeyReduceFlag;
u8 MotorFirstFlag,IDSetFlag;
u32 IDNum;

void Key_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);	
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitTypeDefStruct;
	GPIO_InitTypeDefStruct.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitTypeDefStruct.GPIO_Pin=GPIO_Pin_7;
	GPIO_InitTypeDefStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitTypeDefStruct);
	GPIO_InitTypeDefStruct.GPIO_Pin=GPIO_Pin_6;
	GPIO_Init(GPIOA,&GPIO_InitTypeDefStruct);
	
	GPIO_SetBits(GPIOB,GPIO_Pin_7);
	GPIO_SetBits(GPIOB,GPIO_Pin_6);
}
//按键1事件
void Key1PressShort(void)
{
  OLED_ShowNum(4, 1, 1, 4);
}

void Key1PressLong(void)
{
	OLED_ShowNum(4, 6, 1, 4);
}

void Key1Released(void)
{
	
}

//按键2事件	
void Key2PressShort(void)
{      
	 
}       
void Key2PressLong(void)
{
	

}

void Key2Released(void)
{
	
}

void (*OnKeyPressShort[])(void)={Key1PressShort,Key2PressShort};
void (*OnKeyPressLong[])(void)={Key1PressLong,Key2PressLong};
void (*OnKeyReleased[])(void)={Key1Released,Key2Released};

int KeyPin[]={GPIO_Pin_6,GPIO_Pin_7};
GPIO_TypeDef* KeyGPIO[]={GPIOB,GPIOB};

u8 PressedKey[]={0,0};
u32 KeyLastPressTime[]={0,0};
u32 KeyLastReleasedTime[]={0,0};
u8 KeyPressEvent[]={0,0};


void HandleKey(void)
{
	for(u8 i=0;i<2;i++)
	{
		if(PressedKey[i]==1)
		{
			if(CurrentTimes-KeyLastPressTime[i]>50)//如果按下按键超过50ms
			{
				if((GPIO_ReadInputDataBit(KeyGPIO[i],KeyPin[i])==1))
				{
					if(KeyPressEvent[i]==0)
					{
						if(OnKeyReleased[i])
						{
							OnKeyReleased[i]();
						}
						//触发松手事件
					}
					PressedKey[i]=0;//按键触发数组归零重新开始下一轮扫描
					KeyLastReleasedTime[i]=CurrentTimes;
				}
				else
				{
					if(CurrentTimes-KeyLastPressTime[i]>200)//控制长按时间
					{
						if(KeyPressEvent[i]==0)
						{
							KeyPressEvent[i]=1;
							if(OnKeyPressLong[i])
							{
								OnKeyPressLong[i]();
							}
							//触发长按事件
						}
					}
				}
			}
		}
		else//按键第一次按下
		{
			if((GPIO_ReadInputDataBit(KeyGPIO[i],KeyPin[i])==0)&&(CurrentTimes-KeyLastReleasedTime[i]>200))
			{
				PressedKey[i]=1;//按键已经按下
				KeyLastPressTime[i]=CurrentTimes;//记录按下的时间
				KeyPressEvent[i]=0;//清零长按事件的标志
				if(OnKeyPressShort[i])
				{
					OnKeyPressShort[i]();
				}
				//触发按下的事件
			}
		}
	}
}

key.h

#ifndef _Key_H_
#define _Key_H_


#define KEY2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)	
#define KEY3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6)


extern u8 KeyNum0,KeyNum1,KeyNum2;
extern u32 KeyRaiseFlag,KeyReduceFlag;
extern u8 MotorFirstFlag,IDSetFlag;


void Key_Init(void);
void HandleKey(void);

#endif

电路板

06523c8daddf376ad28581dba18db07a.png

0438c54a21952e17fe8981e373574dfa.png

6252f23b5478e06d5ed6c9544da1cdf8.png

e21d9401fe2a9aaaa7b5e3834cd88138.jpeg

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值