一首先你需要具备的知识
1.了解STM32F103C8T6的库的编程
2.了解舵机的工作性质。
3.了解nrf24L01的通信原理
4.了解驱动模块的工作状态
二 这是遥控器部分的代码
以下是遥控器部分代码
#include "stm32f10x.h"
#include "bsp_led.h"
#include "bsp_24l01.h"
#include "bsp_usart.h"
#include "bsp_adc.h"
u8 status;
u8 i;
extern __IO uint16_t ADC_ConvertedValue[8];
unsigned char RX_BUF5[100];
//void Delay_ms(unsigned int ms)
//{
// unsigned int i,j;
//
// for(i=0;i // for(j=0;j<7;j++);
//}
int main(void)
{
int value;
unsigned char temp=0;
float ADC_ConvertedValueLocal = 0.0;
USART_Config();
ADCx_Init();
SPI_NRF_Init();
status = NRF_Check();
if(status == SUCCESS)
printf("\r\n NRFÓëMCUÁ¬½Ó³É¹¦\r\n");
else
printf("\r\n ÕýÔÚ¼ì²âNRFÓëMCUÊÇ·ñÕý³£Á¬½Ó¡£¡£¡£\r\n");
while(1)
{
NRF_TX_Mode();
RX_BUF5[0]=0xa1;
RX_BUF5[1]=0xa2;
RX_BUF5[2]=(char) (ADC_ConvertedValue[0]>>8);
RX_BUF5[3]=(char) ADC_ConvertedValue[0];
printf("\r\n The current AD0 value = %d V \r\n",ADC_ConvertedValue[0]);
RX_BUF5[4]=(char) ADC_ConvertedValue[1];
RX_BUF5[5]=(char) (ADC_ConvertedValue[1]>>8);
printf("\r\n The current AD1 value = %d V \r\n",ADC_ConvertedValue[1]);
RX_BUF5[6]=(char) ADC_ConvertedValue[2];
RX_BUF5[7]=(char) (ADC_ConvertedValue[2]>>8);
printf("\r\n The current AD2 value = %d V \r\n",ADC_ConvertedValue[2]);
RX_BUF5[8]=(char) (ADC_ConvertedValue[3]>>8);
RX_BUF5[9]=(char) ADC_ConvertedValue[3];
printf("\r\n The current AD3 value = %d V \r\n",ADC_ConvertedValue[3]);
RX_BUF5[10]=(char) ADC_ConvertedValue[4];
RX_BUF5[11]=(char) (ADC_ConvertedValue[4]>>8);
printf("\r\n The current AD4 value = %d V \r\n",ADC_ConvertedValue[4]);
RX_BUF5[12]=(char) ADC_ConvertedValue[5];
RX_BUF5[13]=(char) (ADC_ConvertedValue[5]>>8);
printf("\r\n The current AD5 value = %d V \r\n",ADC_ConvertedValue[5]);
RX_BUF5[14]=(char) (ADC_ConvertedValue[6]>>8);
RX_BUF5[15]=(char) ADC_ConvertedValue[6];
printf("\r\n The current AD6 value = %d V \r\n",ADC_ConvertedValue[6]);
RX_BUF5[16]=(char) ADC_ConvertedValue[7];
RX_BUF5[17]=(char) (ADC_ConvertedValue[7]>>8);
printf("\r\n The current AD7 value = %d V \r\n",ADC_ConvertedValue[7]);
RX_BUF5[18]=0xa3;
NRF_Tx_Dat(RX_BUF5);
}
}
以下是执行端部分代码
#include "stm32f10x.h"
#include "bsp_TiMbase.h"
#include "bsp_24l01.h"
#include "bsp_usart.h"
#include "pid.h"
u8 status; //ÓÃÓÚÅжϽÓÊÕ/·¢ËÍ״̬
u8 rxbuf[1]; //½ÓÊÕ»º³å
float i=160;
float j=160;
char buf2[10] = {"0"};
unsigned char RX_BUF5[100];
unsigned char RX_Complete=0;
unsigned char RX_DataCount = 0;
union
{
float ADC_ConvertedValueLocal1;
u8 txbuf[4];
}AD_Rec_data;
int YM,ZY,SX,ONE,TWO,THREE,FOUR,FINE,SIX,SEVEN,EIGHT;
void control(void);
//ÑÓʱº¯Êý
void Delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i for(j=0;j<7;j++);
}
int main(void)
{
USART_Config();
SPI_NRF_Init();
TIM1_Config();
TIM3_Config();
status = NRF_Check();
if(status == SUCCESS)
printf("\r\n NRFÓëMCUÁ¬½Ó³É¹¦\r\n");
else
printf("\r\n ÕýÔÚ¼ì²âNRFÓëMCUÊÇ·ñÕý³£Á¬½Ó¡£¡£¡£\r\n");
while(1)
{
control();
}}
void control(void)
{
NRF_RX_Mode();
NRF_Rx_Dat(RX_BUF5);
if(RX_BUF5[0]==0xa1&&RX_BUF5[1]==0xa2&&RX_BUF5[18]==0xa3)
{
ZY=(RX_BUF5[2]<<8)|RX_BUF5[3];//×ó ÏÂ ÓÒ
printf("\r\n The current AD1 value = %d V \r\n",ZY);
YM=(RX_BUF5[5]<<8)|RX_BUF5[4];//×ó ÏÂ ÉÏ
printf("\r\n The current AD2 value = %d V \r\n",YM);
SX=(RX_BUF5[7]<<8)|RX_BUF5[6];// ÓÒ ÏÂ ×ó
printf("\r\n The current AD3 value = %d V \r\n",SX);
ONE=(RX_BUF5[8]<<8)|RX_BUF5[9];// ÓÒ ÏÂ ÉÏ
printf("\r\n The current AD4 value = %d V \r\n",ONE);
TWO=(RX_BUF5[11]<<8)|RX_BUF5[10];//ÓÒÉÏ ×ó
printf("\r\n The current AD5 value = %d V \r\n",TWO);
THREE=(RX_BUF5[13]<<8)|RX_BUF5[12];//ÓÒÉÏ ÉÏ
printf("\r\n The current AD6 value = %d V \r\n",THREE);
FOUR=(RX_BUF5[14]<<8)|RX_BUF5[15];//×óÉÏ ×ó
printf("\r\n The current AD7 value = %d V \r\n",FOUR);
FINE=(RX_BUF5[17]<<8)|RX_BUF5[16];//×óÉÏ ÉÏ
printf("\r\n The current AD8 value = %d V \r\n",FINE);
if(YM>=2500)
{
forword();//ºó
SX=SX*2100/4096;
TIM_SetCompare4(TIM1, YM);
TIM_SetCompare1(TIM1, YM);//¸ù¾Ý¼ÆËãÖµÐÞ¸ÄPWMÕ¼¿Õ±È
}
else
if(YM<=1500)
{
forword4();//Ç°
YM=2100-YM*2100/4096;
TIM_SetCompare4(TIM1, YM);//¸ù¾Ý¼ÆËãÖµÐÞ¸ÄPWMÕ¼¿Õ±È
TIM_SetCompare1(TIM1, YM);
}
else
if(ZY>=2500)
{
forword2();//you
ZY=ZY*2100/4096;
TIM_SetCompare1(TIM1, ZY);//¸ù¾Ý¼ÆËãÖµÐÞ¸ÄPWMÕ¼¿Õ±È
TIM_SetCompare4(TIM1, ZY);
}
else
if(ZY<=1500)
{
forword1();//zuo
ZY=2100-ZY*2100/4096;
TIM_SetCompare4(TIM1,ZY);
TIM_SetCompare1(TIM1,ZY);
}
else
if(1500 {
forword3();
TIM_SetCompare4(TIM1, ZY);//¸ù¾Ý¼ÆËãÖµÐÞ¸ÄPWMÕ¼¿Õ±È
TIM_SetCompare3(TIM1, ZY);
}
if(ONE<=1500)//µÚÒ»¸ö¶æ»ú
{
if(i<=262)
{
i=i+10;
Delay_ms(10);
TIM_SetCompare1(TIM8,i);
}
}
else
if(ONE>=3500) //22222222
{
if(100 {
i=i-10;
Delay_ms(10);
TIM_SetCompare1(TIM8,i);
}
}
else
if(1500 {
if(TWO<=1500)//µÚ¶þ¸ö¶æ»ú
{ //31111
if(j<=262)
{
j=j+10;
Delay_ms(10);
TIM_SetCompare2(TIM8,j);
printf("\r\n The current AD5 value = %d V \r\n",TWO);
}
}
else
if(TWO>=3500) //322222
{
if(100 {
j=j-10;
Delay_ms(10);
TIM_SetCompare2(TIM8,j);
printf("\r\n The current AD5 value = %d V \r\n",TWO);
}
}
else
if(1500 {
if(THREE<=1500)//µÚÈý¸ö¶æ»ú //411111
{
if(j<=262)
{
j=j+10;
Delay_ms(10);
TIM_SetCompare3(TIM8,j);
}
}
else
if(THREE>=3500) ///42222
{
if(100 {
j=j-10;
Delay_ms(10);
TIM_SetCompare3(TIM8,j);
}}
else
if(1500 {
if(FOUR<=1500)//µÚËĸö¶æ»ú
{
if(j<=262)
{
j=j+10;
Delay_ms(10);
TIM_SetCompare4(TIM8,j);
}
}
else
if(FOUR>=3500)
{
if(100 {
j=j-10;
Delay_ms(10);
TIM_SetCompare4(TIM8,j);
}}
else
if(1500 {
if(FINE<=1500)//µÚÎå¸ö¶æ»ú
{
if(j<=262)
{
j=j+10;
Delay_ms(10);
TIM_SetCompare2(TIM1,j);
}
}
else
if(FINE>=3500)
{
if(100 {
j=j-10;
Delay_ms(10);
TIM_SetCompare2(TIM1,j);
}}
else
if(1500 {
}
}
}
}
}
}
}
想要代码或者原理图下面留言。邮箱发给大家。