nrf51822串口协议及傅里叶变换的理解原理

nrf51822串口协议
串口是以一个一个字符输入进来的,并且不包含/0



///**********************************************************************************
//* 描 述 : main 函数 串口控制灯--
//* 参 数 : 无
//* 返回值 : 无
//**********************************************************************************/ 

int main()
{
       int i=0;
	 LEDS_CONFIGURE(LEDS_MASK);//配置驱动LED指示灯的管脚
	 LEDS_OFF(LEDS_MASK);
	 uint32_t err_code;
	 //初始化串口结构体
	 const app_uart_comm_params_t comm_params =
	 {
		 RX_PIN_NUMBER, // UART_RX:P0.11
		 TX_PIN_NUMBER, // UART_TX:P0.09
		 RTS_PIN_NUMBER,
		 CTS_PIN_NUMBER,
		 APP_UART_FLOW_CONTROL_DISABLED, //关闭流控
		 false,
		 UART_BAUDRATE_BAUDRATE_Baud9600 //波特率设置为 115200bps
	 };
	 APP_UART_FIFO_INIT(&comm_params,
	 UART_RX_BUF_SIZE,
	 UART_TX_BUF_SIZE,
	 uart_error_handle,
	 APP_IRQ_PRIORITY_LOW,
	 err_code);
	 APP_ERROR_CHECK(err_code);    //检查错误代码
	 
	 uint8_t temp[UART_BUF_LEN] ={0};
	int Rxcnt=0;
	while(true)
	{
		uint8_t cr;

		while(app_uart_get(&cr)!=NRF_SUCCESS);
		if((cr !='#') && (Rxcnt<3))
		{
				temp[Rxcnt++]  =cr;
				printf("n:%d\n s0:%c s1:%c s2:%c\n",Rxcnt,temp[0],temp[1],temp[2]); 
//				 printf("n:%d\n s:%c\n",Rxcnt,temp[2]); 
				
				  
//				 if(Rxcnt>=2)
//				 {
//				    temp[Rxcnt]='\0';
//						printf("%s",temp);
//				 }
		
		}
		if(Rxcnt==2)
		{
		   temp[Rxcnt++]  ='\0';
			 printf("char:%s",temp) ;
			   Rxcnt=0;
				 
		}
//		Rxcnt=0;
//		if(Rxcnt==3)
//		{
//		    printf("p:%c\n",temp[Rxcnt]);
//				Rxcnt=0;
//				memset(temp,0x0,4);
//		}
		
//		else
//		{
//	
//			if(Rxcnt>=3)
//			{
//				Rxcnt=0;
//					printf("111222");
//			
//			}
			
			if(strcmp((char*)temp,"D1")==0)
			{
				printf("xxxx");
				//指示灯 D1 以 200ms 的间隔闪烁
				
				nrf_gpio_pin_clear(LED_1);  //常亮
				
	
				nrf_delay_ms(500);
				nrf_gpio_pin_set(LED_1);  //常亮
//				LEDS_OFF(LEDS_MASK);
//				LEDS_CONFIGURE(LEDS_MASK);//配置驱动LED指示灯的管脚
//				nrf_delay_ms(1000);
//				 LEDS_OFF(LEDS_MASK);
				memset(temp,0x0,3);
//				break;
					
			}
			 else if(strcmp((char*)temp,"D2")==0)
			{
				nrf_gpio_pin_clear(LED_2);  //常亮
				
	
				nrf_delay_ms(500);
				nrf_gpio_pin_set(LED_2);  //常亮
					memset(temp,0x0,3);
					
			}
			 else if(strcmp((char*)temp,"D3")==0)
			{
				nrf_gpio_pin_clear(LED_3);  //常亮
				
	
				nrf_delay_ms(500);
				nrf_gpio_pin_set(LED_3);  //常亮
					memset(temp,0x0,3);
					
			}
				else if(strcmp((char*)temp,"D4")==0)
			{
				nrf_gpio_pin_clear(LED_4);  //常亮
				
	
				nrf_delay_ms(500);
				nrf_gpio_pin_set(LED_4);  //常亮
					memset(temp,0x0,3);
					
			}
			else if(strcmp((char*)temp,"D5")==0)
			{
			
			for(i=0;i<3;i++)
			{
				if(i==1)
				{
					nrf_gpio_pin_toggle(LED1);
					nrf_delay_ms(100);
					nrf_gpio_pin_toggle(LED2);
					nrf_delay_ms(100);
					nrf_gpio_pin_toggle(LED3);
					nrf_delay_ms(100);
					nrf_gpio_pin_toggle(LED4);
					nrf_delay_ms(100);
				
				}
				else
				{
				   nrf_gpio_pin_toggle(LED4);
					nrf_delay_ms(100);
					nrf_gpio_pin_toggle(LED3);
					nrf_delay_ms(100);
					nrf_gpio_pin_toggle(LED2);
					nrf_delay_ms(100);
					nrf_gpio_pin_toggle(LED1);
					nrf_delay_ms(100);
				
				}
				
			
			}
			//所有 LED 同时闪烁 2 次
			for(i=0;i<3;i++)
			{
				nrf_gpio_pin_set(LED1);
				nrf_gpio_pin_set(LED2);
				nrf_gpio_pin_set(LED3);
				nrf_gpio_pin_set(LED4);
			  nrf_delay_ms(200);
			
				nrf_gpio_pin_clear(LED1);
				nrf_gpio_pin_clear(LED2);
				nrf_gpio_pin_clear(LED3);
				nrf_gpio_pin_clear(LED4);
				 nrf_delay_ms(200);
			
			}
			for(i=0;i<5;i++)
			{
				if(i%2==0)
				{ 
				nrf_gpio_pin_set(LED1);
			
				nrf_gpio_pin_set(LED3);
				nrf_gpio_pin_set(LED2);
			
				nrf_gpio_pin_set(LED4);
			  nrf_delay_ms(200);
			
				
				nrf_gpio_pin_clear(LED2);
			
				nrf_gpio_pin_clear(LED4);
				 nrf_delay_ms(200);
				
				}
				else
				{ 
					nrf_gpio_pin_set(LED2);
				
					nrf_gpio_pin_set(LED4);
					nrf_gpio_pin_set(LED1);
				
					nrf_gpio_pin_set(LED3);
				
					nrf_delay_ms(200);
				
					nrf_gpio_pin_clear(LED1);
				
					nrf_gpio_pin_clear(LED3);
			
				 nrf_delay_ms(200);
				
				}
							
			
			}
			nrf_gpio_pin_set(LED1);
			nrf_gpio_pin_set(LED2);
			nrf_gpio_pin_set(LED3);
			nrf_gpio_pin_set(LED4);   
			
			}
		
	//	}
		
		
	
	}








//	return 0;



	//配置用于控制 OLED 的管脚
	 nrf_gpio_cfg_output(LED_1);//配置 P0.21 为输出
	 nrf_gpio_pin_set(LED_1); //指示灯 D1 初始状态为熄灭
	 while (true)
	 {
	 //指示灯 D1 以 200ms 的间隔闪烁
		nrf_gpio_pin_clear(LED_1);  //常亮
	 }

///***************************************************************/
流水灯
///***************************************************************/
//		uint8_t i=0;
//		nrf_gpio_range_cfg_output(LEDSTATR,LEDEND);
//		nrf_gpio_pin_set(LED1);
//		nrf_gpio_pin_set(LED2);
//		nrf_gpio_pin_set(LED3);
//		nrf_gpio_pin_set(LED4);
//		while(true)
//		{   //所有 LED 轮流闪烁 2 次
//			for(i=0;i<3;i++)
//			{
//				if(i==1)
//				{
//					nrf_gpio_pin_toggle(LED1);
//					nrf_delay_ms(100);
//					nrf_gpio_pin_toggle(LED2);
//					nrf_delay_ms(100);
//					nrf_gpio_pin_toggle(LED3);
//					nrf_delay_ms(100);
//					nrf_gpio_pin_toggle(LED4);
//					nrf_delay_ms(100);
//				
//				}
//				else
//				{
//				   nrf_gpio_pin_toggle(LED4);
//					nrf_delay_ms(100);
//					nrf_gpio_pin_toggle(LED3);
//					nrf_delay_ms(100);
//					nrf_gpio_pin_toggle(LED2);
//					nrf_delay_ms(100);
//					nrf_gpio_pin_toggle(LED1);
//					nrf_delay_ms(100);
//				
//				}
//				
//			
//			}
//			//所有 LED 同时闪烁 2 次
//			for(i=0;i<3;i++)
//			{
//				nrf_gpio_pin_set(LED1);
//				nrf_gpio_pin_set(LED2);
//				nrf_gpio_pin_set(LED3);
//				nrf_gpio_pin_set(LED4);
//			  nrf_delay_ms(200);
//			
//				nrf_gpio_pin_clear(LED1);
//				nrf_gpio_pin_clear(LED2);
//				nrf_gpio_pin_clear(LED3);
//				nrf_gpio_pin_clear(LED4);
//				 nrf_delay_ms(200);
//			
//			}
//			for(i=0;i<5;i++)
//			{
//				if(i%2==0)
//				{ 
//				nrf_gpio_pin_set(LED1);
//			
//				nrf_gpio_pin_set(LED3);
//				nrf_gpio_pin_set(LED2);
//			
//				nrf_gpio_pin_set(LED4);
//			  nrf_delay_ms(200);
//			
//				
//				nrf_gpio_pin_clear(LED2);
//			
//				nrf_gpio_pin_clear(LED4);
//				 nrf_delay_ms(200);
//				
//				}
//				else
//				{ 
//					nrf_gpio_pin_set(LED2);
//				
//					nrf_gpio_pin_set(LED4);
//					nrf_gpio_pin_set(LED1);
//				
//					nrf_gpio_pin_set(LED3);
//				
//					nrf_delay_ms(200);
//				
//					nrf_gpio_pin_clear(LED1);
//				
//					nrf_gpio_pin_clear(LED3);
//			
//				 nrf_delay_ms(200);
//				
//				}
//							
//			
//			}
//			nrf_gpio_pin_set(LED1);
//			nrf_gpio_pin_set(LED2);
//			nrf_gpio_pin_set(LED3);
//			nrf_gpio_pin_set(LED4);    
//			}
		
}

对于离散傅里叶变换(DFT)的理解:
在这里插入图片描述
1.离散-》周期
2.卷积-》相乘=加窗
3.做傅里叶,在对应的频率下会产生映射

DFT的原料首先是一个离散的序列,自然界的模拟信号我们对于他的频谱并不了解,所以必须根据奈奎斯特fs>2f0,进行采样,这里每秒会有N个采样点,对于输入的信号做DFT,就是对于离散信号的时域做完傅里叶变换后频域的采样,输入有N个点,频域就也存在N个点,并且满足对称性,实际中我们选取的数据,可以认为是加窗处理后的,并且如果采样点和频率满足某种条件可以准确的获取相位和幅度,加窗函数x[n]计算出来的DTFT是连续频谱,也就是说,其包含了所有的频率分量。(2) DFT是在DTFT上采样的结果,如果输入信号的频率正好等于采样点,则计算出来的频率是精确的。
所以说,因为窗函数的存在,其旁瓣导致有泄露;所以可以用泄露小的窗函数来减小其影响。
参看:(大神级别文章)
https://mp.weixin.qq.com/s?__biz=MzU0MTAzMjkyMg==&mid=2247484794&idx=1&sn=ca0c51ea315e921952a2b34a7640375c&chksm=fb316dcfcc46e4d9f3a80d6379cd9e4ea7e55f8dbff2fad020a357614d5b5706b437333b9731&scene=21#wechat_redirect

https://blog.csdn.net/weixin_35576881/article/details/86366523

https://blog.csdn.net/wholemoon/article/details/102902971

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值