1:所需元器件
2NRF24l01无线模块2个,STM32F03C8T6 2个,摇杆(电位器)1个,OLED1个,小车底板,电机,驱动杜邦线等
2:图片
发射
接受
3:代码
NRF24L01每个包有32个数据位宽:
#define TX_PLOAD_WIDTH 32 //32字节的用户数据宽度
#define RX_PLOAD_WIDTH 32 //32字节的用户数据宽度
NRF24L01 FIFO状态寄存器配置:
#define NRF_FIFO_STATUS 0x17 //FIFO状态寄存器;bit0,RX FIFO寄存器空标志;bit1,RX FIFO满标志;bit2,3,保留,bit4,TX FIFO空标志;bit5,TX FIFO满标志;bit6,1,循环发送上一数据包.0,不循环;
NRF24L01接受发送数据包配置:
//启动NRF24L01发送一次数据
//txbuf:待发送数据首地址
//返回值:发送完成状况
u8 NRF24L01_TxPacket(u8 *txbuf)
{
u8 sta;
SPI2_SetSpeed(SPI_BaudRatePrescaler_8);//spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)
NRF24L01_CE=0;
NRF24L01_Write_Buf(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);//写数据到TX BUF 32个字节
NRF24L01_CE=1;//启动发送
while(NRF24L01_IRQ!=0);//等待发送完成
sta=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //清除TX_DS或MAX_RT中断标志
if(sta&MAX_TX)//达到最大重发次数
{
NRF24L01_Write_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器
return MAX_TX;
}
if(sta&TX_OK)//发送完成
{
return TX_OK;
}
return 0xff;//其他原因发送失败
}
//启动NRF24L01接受一次数据
//txbuf:待发送数据首地址
//返回值:0,接收完成;其他,错误代码
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
u8 sta;
SPI2_SetSpeed(SPI_BaudRatePrescaler_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)
sta=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //清除TX_DS或MAX_RT中断标志
if(sta&RX_OK)//接收到数据
{
NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据
NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器
return 0;
}
return 1;//没收到任何数据
}
STM32 4路12位独立扫描模式下ADC配置:
#define ADC1_DR_Address ((u32)0x4001244C)
__IO u16 ADC_ConvertedValue;
/*配置采样通道端口 使能GPIO时钟 设置ADC采样PA0端口信号*/
void ADC1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //GPIO设置为模拟输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*配置ADC1的工作模式为MDA模式 */
void ADC1_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能MDA1时钟
/* DMA channel1 configuration */
DMA_DeInit(DMA1_Channel1); //指定DMA通道
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;ADC1地址---代表ADC1保存转换值的寄存器
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //设置DMA内存地址,ADC转换结果直接放入该地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外设为设置为数据传输的来源
DMA_InitStructure.DMA_BufferSize = 4; //传输总数据---2通道需要传输2个数据
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址固定
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址自增---总体表示始终从外设ADC1地址处取值---依次保存到连续的两个内存变量中---
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //内存传输数据单元---半字16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式---2个数据依次循环接收从外设ADC1传输过来的ADC值---
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE); //使能DMA通道
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1时钟
/* ADC1 configuration */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //使用独立模式,扫描模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //模数转换工作在扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换使用软件触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //使用数据右对齐
ADC_InitStructure.ADC_NbrOfChannel =4; // ADC转换通道个数
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel11 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5); //通道1采样周期55.5个时钟周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); //使能ADC的DMA
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); //使能ADC1
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1); //复位校准
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //开始转换
}
/*初始化ADC1 */
void ADC1_Init(void)
{
ADC1_GPIO_Config();
ADC1_Mode_Config();
}
发射主程序:
while(1)
{
OLED_Refresh_Gram();
OLED_ShowString(0,12,"TX_Mode",12);
//OLED_ShowString(0,24,"Sended DATA:",12);
//TX模式
OLED_ShowNum(0,24,ADC_ConvertedValue[0]/16/100,1,16); //油门
OLED_ShowNum(8,24,ADC_ConvertedValue[0]/16%100/10,1,16);
OLED_ShowNum(16,24,ADC_ConvertedValue[0]/16%100%10,1,16);
OLED_ShowNum(40,24,ADC_ConvertedValue[1]/16/100,1,16); //方向舵
OLED_ShowNum(48,24,ADC_ConvertedValue[1]/16%100/10,1,16);
OLED_ShowNum(56,24,ADC_ConvertedValue[1]/16%100%10,1,16);
OLED_ShowNum(80,24,ADC_ConvertedValue[2]/16/100,1,16); //方向舵
OLED_ShowNum(88,24,ADC_ConvertedValue[2]/16%100/10,1,16);
OLED_ShowNum(96,24,ADC_ConvertedValue[2]/16%100%10,1,16);
if(NRF24L01_TxPacket(tmp_buf)==TX_OK)
{
u16 sum0 = 0,sum1 = 0,sum2 = 0;
u8 avr0 = 0,avr1 = 0,avr2 = 0;
u8 t = 0;
for(t=0;t<10;t++)
{
tmp_buf[t]=ADC_ConvertedValue[0]/16;
sum0 = sum0 + tmp_buf[t];
}
avr0 = sum0/10;
for(t=10;t<20;t++)
{
tmp_buf[t]=ADC_ConvertedValue[1]/16;
sum1 = sum1 + tmp_buf[t];
}
avr1 = sum1/10;
for(t=20;t<30;t++)
{
tmp_buf[t]=ADC_ConvertedValue[2]/16;
sum2 = sum2 + tmp_buf[t];
}
avr2 = sum2/10;
tmp_buf[30]=0;
tmp_buf[31]=0;
tmp_buf[32]=0;
//NRF24L01_TxPacket(tmp_buf);
OLED_ShowNum(0,40,avr0/100,1,16);
OLED_ShowNum(8,40,avr0%100/10,1,16);
OLED_ShowNum(16,40,avr0%100%10,1,16);
OLED_ShowNum(40,40,avr1/100,1,16);
OLED_ShowNum(48,40,avr1%100/10,1,16);
OLED_ShowNum(56,40,avr1%100%10,1,16);
OLED_ShowNum(80,40,avr2/100,1,16);
OLED_ShowNum(88,40,avr2%100/10,1,16);
OLED_ShowNum(96,40,avr2%100%10,1,16);
}
}
接受:
if(NRF24L01_RxPacket(tmp_buf)==0)//一旦接收到信息,则显示出来.
{
u8 cont = 0;
u8 avr0 = 0,avr1 = 0,avr2 = 0;
u16 sum0 = 0,sum1 = 0,sum2 = 0;
for(cont=0;cont<10;cont++)
{
sum0 = sum0 + tmp_buf[cont];
}
avr0 = sum0/10;
for(cont=10;cont<20;cont++)
{
sum1 = sum1 + tmp_buf[cont];
}
avr1 = sum1/10;
for(cont=20;cont<30;cont++)
{
sum2 = sum2 + tmp_buf[cont];
}
avr2 = sum2/10;
tmp_buf[30] = 0;
tmp_buf[31] = 0;
tmp_buf[32] = 0;
OLED_ShowNum(0,24,avr0/100,1,16); //油门
OLED_ShowNum(8,24,avr0%100/10,1,16);
OLED_ShowNum(16,24,avr0%100%10,1,16);
OLED_ShowNum(40,24,avr1/100,1,16); //方向舵
OLED_ShowNum(48,24,avr1%100/10,1,16);
OLED_ShowNum(56,24,avr1%100%10,1,16);
OLED_ShowNum(80,24,avr2/100,1,16); //方向舵
OLED_ShowNum(88,24,avr2%100/10,1,16);
OLED_ShowNum(96,24,avr2%100%10,1,16);
OLED_Refresh_Gram();
if(avr1>130)
{
if(avr2<123) //左前
{
qianjin();
pwm1 = (126-avr2+avr0)/2*7;
pwm2 = (avr1+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
else if(avr2>129) //右前
{
qianjin();
pwm1 = (avr2+avr0)/2*7;
pwm2 = (avr1-128+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
else //前进
{
qianjin();
pwm1 = (avr1-127+avr0)/2*7;
pwm2 = (avr1-127+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
}
else if(avr1<124&&(avr1!=0)) //后退
{
if(avr2<123) //左退
{
houtui();
pwm1 = (avr2+avr0)/2*7;
pwm2 = (127-avr1+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
else if(avr2>129) //右退
{
houtui();
pwm1 = (avr2-126+avr0)/2*7;
pwm2 = (avr1+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
else
{
houtui();
pwm1 = (127-avr1+avr0)/2*7;
pwm2 = (127-avr1+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
}
else
{
tingzhi();
pwm1 = 0;
pwm2 = 0;
Set_PWM(pwm1,pwm2);
}
}
else
{
OLED_Refresh_Gram();
for(cont = 0;cont<33;cont++)
{
tmp_buf[cont] = 0;
}
tingzhi();
pwm1 = 0;
pwm2 = 0;
Set_PWM(pwm1,pwm2);
}
}
1:所需元器件
2NRF24l01无线模块2个,STM32F03C8T6 2个,摇杆(电位器)1个,OLED1个,小车底板,电机,驱动杜邦线等
2:图片
发射
接受
3:代码
NRF24L01每个包有32个数据位宽:
#define TX_PLOAD_WIDTH 32 //32字节的用户数据宽度
#define RX_PLOAD_WIDTH 32 //32字节的用户数据宽度
NRF24L01 FIFO状态寄存器配置:
#define NRF_FIFO_STATUS 0x17 //FIFO状态寄存器;bit0,RX FIFO寄存器空标志;bit1,RX FIFO满标志;bit2,3,保留,bit4,TX FIFO空标志;bit5,TX FIFO满标志;bit6,1,循环发送上一数据包.0,不循环;
NRF24L01接受发送数据包配置:
//启动NRF24L01发送一次数据
//txbuf:待发送数据首地址
//返回值:发送完成状况
u8 NRF24L01_TxPacket(u8 *txbuf)
{
u8 sta;
SPI2_SetSpeed(SPI_BaudRatePrescaler_8);//spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)
NRF24L01_CE=0;
NRF24L01_Write_Buf(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);//写数据到TX BUF 32个字节
NRF24L01_CE=1;//启动发送
while(NRF24L01_IRQ!=0);//等待发送完成
sta=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //清除TX_DS或MAX_RT中断标志
if(sta&MAX_TX)//达到最大重发次数
{
NRF24L01_Write_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器
return MAX_TX;
}
if(sta&TX_OK)//发送完成
{
return TX_OK;
}
return 0xff;//其他原因发送失败
}
//启动NRF24L01接受一次数据
//txbuf:待发送数据首地址
//返回值:0,接收完成;其他,错误代码
u8 NRF24L01_RxPacket(u8 *rxbuf)
{
u8 sta;
SPI2_SetSpeed(SPI_BaudRatePrescaler_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)
sta=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //清除TX_DS或MAX_RT中断标志
if(sta&RX_OK)//接收到数据
{
NRF24L01_Read_Buf(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//读取数据
NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器
return 0;
}
return 1;//没收到任何数据
}
STM32 4路12位独立扫描模式下ADC配置:
#define ADC1_DR_Address ((u32)0x4001244C)
__IO u16 ADC_ConvertedValue;
/*配置采样通道端口 使能GPIO时钟 设置ADC采样PA0端口信号*/
void ADC1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //GPIO设置为模拟输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*配置ADC1的工作模式为MDA模式 */
void ADC1_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能MDA1时钟
/* DMA channel1 configuration */
DMA_DeInit(DMA1_Channel1); //指定DMA通道
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;ADC1地址---代表ADC1保存转换值的寄存器
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //设置DMA内存地址,ADC转换结果直接放入该地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外设为设置为数据传输的来源
DMA_InitStructure.DMA_BufferSize = 4; //传输总数据---2通道需要传输2个数据
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址固定
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址自增---总体表示始终从外设ADC1地址处取值---依次保存到连续的两个内存变量中---
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //内存传输数据单元---半字16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式---2个数据依次循环接收从外设ADC1传输过来的ADC值---
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE); //使能DMA通道
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1时钟
/* ADC1 configuration */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //使用独立模式,扫描模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //模数转换工作在扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换使用软件触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //使用数据右对齐
ADC_InitStructure.ADC_NbrOfChannel =4; // ADC转换通道个数
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel11 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5); //通道1采样周期55.5个时钟周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); //使能ADC的DMA
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); //使能ADC1
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1); //复位校准
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //开始转换
}
/*初始化ADC1 */
void ADC1_Init(void)
{
ADC1_GPIO_Config();
ADC1_Mode_Config();
}
发射主程序:
while(1)
{
OLED_Refresh_Gram();
OLED_ShowString(0,12,"TX_Mode",12);
//OLED_ShowString(0,24,"Sended DATA:",12);
//TX模式
OLED_ShowNum(0,24,ADC_ConvertedValue[0]/16/100,1,16); //油门
OLED_ShowNum(8,24,ADC_ConvertedValue[0]/16%100/10,1,16);
OLED_ShowNum(16,24,ADC_ConvertedValue[0]/16%100%10,1,16);
OLED_ShowNum(40,24,ADC_ConvertedValue[1]/16/100,1,16); //方向舵
OLED_ShowNum(48,24,ADC_ConvertedValue[1]/16%100/10,1,16);
OLED_ShowNum(56,24,ADC_ConvertedValue[1]/16%100%10,1,16);
OLED_ShowNum(80,24,ADC_ConvertedValue[2]/16/100,1,16); //方向舵
OLED_ShowNum(88,24,ADC_ConvertedValue[2]/16%100/10,1,16);
OLED_ShowNum(96,24,ADC_ConvertedValue[2]/16%100%10,1,16);
if(NRF24L01_TxPacket(tmp_buf)==TX_OK)
{
u16 sum0 = 0,sum1 = 0,sum2 = 0;
u8 avr0 = 0,avr1 = 0,avr2 = 0;
u8 t = 0;
for(t=0;t<10;t++)
{
tmp_buf[t]=ADC_ConvertedValue[0]/16;
sum0 = sum0 + tmp_buf[t];
}
avr0 = sum0/10;
for(t=10;t<20;t++)
{
tmp_buf[t]=ADC_ConvertedValue[1]/16;
sum1 = sum1 + tmp_buf[t];
}
avr1 = sum1/10;
for(t=20;t<30;t++)
{
tmp_buf[t]=ADC_ConvertedValue[2]/16;
sum2 = sum2 + tmp_buf[t];
}
avr2 = sum2/10;
tmp_buf[30]=0;
tmp_buf[31]=0;
tmp_buf[32]=0;
//NRF24L01_TxPacket(tmp_buf);
OLED_ShowNum(0,40,avr0/100,1,16);
OLED_ShowNum(8,40,avr0%100/10,1,16);
OLED_ShowNum(16,40,avr0%100%10,1,16);
OLED_ShowNum(40,40,avr1/100,1,16);
OLED_ShowNum(48,40,avr1%100/10,1,16);
OLED_ShowNum(56,40,avr1%100%10,1,16);
OLED_ShowNum(80,40,avr2/100,1,16);
OLED_ShowNum(88,40,avr2%100/10,1,16);
OLED_ShowNum(96,40,avr2%100%10,1,16);
}
}
接受:
if(NRF24L01_RxPacket(tmp_buf)==0)//一旦接收到信息,则显示出来.
{
u8 cont = 0;
u8 avr0 = 0,avr1 = 0,avr2 = 0;
u16 sum0 = 0,sum1 = 0,sum2 = 0;
for(cont=0;cont<10;cont++)
{
sum0 = sum0 + tmp_buf[cont];
}
avr0 = sum0/10;
for(cont=10;cont<20;cont++)
{
sum1 = sum1 + tmp_buf[cont];
}
avr1 = sum1/10;
for(cont=20;cont<30;cont++)
{
sum2 = sum2 + tmp_buf[cont];
}
avr2 = sum2/10;
tmp_buf[30] = 0;
tmp_buf[31] = 0;
tmp_buf[32] = 0;
OLED_ShowNum(0,24,avr0/100,1,16); //油门
OLED_ShowNum(8,24,avr0%100/10,1,16);
OLED_ShowNum(16,24,avr0%100%10,1,16);
OLED_ShowNum(40,24,avr1/100,1,16); //方向舵
OLED_ShowNum(48,24,avr1%100/10,1,16);
OLED_ShowNum(56,24,avr1%100%10,1,16);
OLED_ShowNum(80,24,avr2/100,1,16); //方向舵
OLED_ShowNum(88,24,avr2%100/10,1,16);
OLED_ShowNum(96,24,avr2%100%10,1,16);
OLED_Refresh_Gram();
if(avr1>130)
{
if(avr2<123) //左前
{
qianjin();
pwm1 = (126-avr2+avr0)/2*7;
pwm2 = (avr1+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
else if(avr2>129) //右前
{
qianjin();
pwm1 = (avr2+avr0)/2*7;
pwm2 = (avr1-128+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
else //前进
{
qianjin();
pwm1 = (avr1-127+avr0)/2*7;
pwm2 = (avr1-127+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
}
else if(avr1<124&&(avr1!=0)) //后退
{
if(avr2<123) //左退
{
houtui();
pwm1 = (avr2+avr0)/2*7;
pwm2 = (127-avr1+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
else if(avr2>129) //右退
{
houtui();
pwm1 = (avr2-126+avr0)/2*7;
pwm2 = (avr1+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
else
{
houtui();
pwm1 = (127-avr1+avr0)/2*7;
pwm2 = (127-avr1+avr0)/2*7;
if(pwm1>899)
pwm1 = 899;
if(pwm2>899)
pwm2 = 899;
Set_PWM(pwm1,pwm2);
}
}
else
{
tingzhi();
pwm1 = 0;
pwm2 = 0;
Set_PWM(pwm1,pwm2);
}
}
else
{
OLED_Refresh_Gram();
for(cont = 0;cont<33;cont++)
{
tmp_buf[cont] = 0;
}
tingzhi();
pwm1 = 0;
pwm2 = 0;
Set_PWM(pwm1,pwm2);
}
}
举报