单片机源程序如下:
- /*
- RFINT不能用PA1 要改板 改在PC6
- */
- /* Includes ------------------------------------------------------------------*/
- #define MAIN_C
- #include "ALL.h"
- void Delay(u16 nCount)
- {
- /* Decrement nCount value */
- while (nCount != 0)
- {
- nCount--;
- }
- }
- //10ms定时中断初始 在PWM时可更改
- void TIME1_CONFIG(void)
- {
- /* Init TIMER 1 */
- CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
- TIM1->IER = 0x00;//??????????? // 禁止中断
- TIM1->EGR = 0x01;//??????????? // 允许产生更新事件
- TIM1->PSCRH = 0x0;//
- TIM1->PSCRL = 0x4;// 2@2MHZ 4@4MHZ
- TIM1->ARRH = 0x27; // 设定重装载时的寄存器的高8位 2710=10ms
- TIM1->ARRL = 0x10;
- //TIM2->CNTRH = 0xEA; // 设定计数器的初值
- //TIM2->CNTRL = 0x60; // 定时周期=1*60000=60000uS=60ms
- TIM1->CR1 = 0x01;// b0 = 1,允许计数器工作 ?// b1 = 0,允许更新 设置控制器,启动定时器
- TIM1->IER = 0x01;// 允许更新中断
- }
- //5ms定时中断初始 CPU主频2Mhz
- void TIME2_CONFIG(void)
- {
- /* Init TIMER 4 */
- CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
- TIM2->IER = 0x00;//??????????? // 禁止中断
- TIM2->EGR = 0x01;//??????????? // 允许产生更新事件
- #ifdef FREQ_2M
- TIM2->PSCR = TIM2_PRESCALER_2;// 2@2M 4@4M 取决于CPU频率
- #else
- TIM2->PSCR = TIM2_PRESCALER_4;// 2@2M 4@4M 取决于CPU频率
- #endif
- #if 0//def GUDINGMA//临时为固定码增加用
- TIM2->PSCR = TIM2->PSCR/2;// 让定时器速度再快一倍出来
- #endif
- TIM2->ARRH = 0xc3; // 设定重装载时的寄存器的高8位 c350=50ms
- TIM2->ARRL = 0x50; //本参数配合无线
- //TIM2->CNTRH = 0xEA; // 设定计数器的初值
- //TIM2->CNTRL = 0x60; // 定时周期=1*60000=60000uS=60ms
- TIM2->CR1 = 0x01;// b0 = 1,允许计数器工作 ?// b1 = 0,允许更新 设置控制器,启动定时器
- TIM2->IER = 0x01;// 允许更新中断
- }
- //5ms定时中断设置 CPU主频2Mhz
- void TIM4_Config(void)
- {
- /* Init TIMER 4 */
- CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, ENABLE);
- /* Init TIMER 4 prescaler: / (2^6) = 64/ */
- #ifdef FREQ_2M
- TIM4->PSCR = TIM4_PRESCALER_64;//// 4M=128 2M=64
- #else
- TIM4->PSCR = TIM4_PRESCALER_128;//// 4M=128 2M=64
- #endif
- /* HSI div by 1 --> Auto-Reload value: 16M/16 /64 = 1/64M, (1/64M) / 100 = 156.25*/
- TIM4->ARR = 156;
- /* Counter value: 2, to compensate the initialization of TIMER*/
- //TIM4->CNTR = 2;
- /* clear update flag */
- TIM4->SR1 &= ~TIM4_SR1_UIF;
- /* Enable Counter */
- TIM4->CR1 = TIM4_CR1_CEN;
- TIM4->IER=1;
- }
- #define CFG_GCR_SWD ((u8)0x01) /*!< Swim disable bit mask */
- void INITIAL_ALL_PIN(void)
- {
- //CFG->GCR |= CFG_GCR_SWD;/*disable SWIM interface*/
- /*
- GPIO_Init(GPIOA,GPIO_PIN_ALL,GPIO_MODE_OUT_OD_LOW_SLOW);//关SET LED灯
- GPIO_Init(GPIOB,GPIO_PIN_ALL,GPIO_MODE_OUT_OD_LOW_SLOW);//关SET LED灯
- GPIO_Init(GPIOC,GPIO_PIN_ALL,GPIO_MODE_OUT_OD_LOW_SLOW);//关SET LED灯
- GPIO_Init(GPIOD,GPIO_PIN_ALL,GPIO_MODE_OUT_OD_LOW_SLOW);//关SET LED灯
- */
- //GPIO_Init(GPIOA,GPIOA_OUT_L,GPIO_MODE_OUT_PP_HIGH_SLOW);//关SET LED灯
- GPIO_Init(GPIOB,GPIOB_OUT_H,GPIO_MODE_OUT_OD_HIZ_SLOW); //关ALARM LED灯
- GPIO_Init(GPIOC,GPIOC_OUT_L,GPIO_MODE_OUT_PP_LOW_SLOW);//PWM
- GPIO_Init(GPIOC,GPIOC_IN_PU,GPIO_MODE_IN_PU_NO_IT);//按键中断
- GPIO_Init(GPIOD,GPIOD_OUT_H,GPIO_MODE_OUT_PP_HIGH_FAST);//串口发送
- GPIO_Init(GPIOD,GPIOD_IN_PU,GPIO_MODE_IN_PU_NO_IT);//i2c
- EXTI->CR1 =(EXTI_SENSITIVITY_RISE_FALL<<4);
- GPIO_Init(GPIOC,GPIO_PIN_7,GPIO_MODE_IN_FL_IT);//无线接收
- }
- void UART_CONFIG(void )
- {
- UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
- UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
- UART1_ITConfig(UART1_IT_TC, DISABLE);
- UART1_Cmd(ENABLE);
- }
- void PUT_CHAR(uchar Data)
- {
- int i=10000;
- UART1->SR&=~UART1_FLAG_TC;//COM_USART[COM1]->SR&~USART_FLAG_TC;
- UART1->DR = Data;
- while((UART1->SR & UART1_FLAG_TC) == 0x00)
- {if(--i==0)break;
- }// 若发送寄存器不空,则等待
- }
- const char hex[] = "0123456789ABCDEF";
- //********************************************************
- PRINT_DEC_OR_HEX(uchar format,uchar d)
- {
- uchar i;
- switch(format)
- {case 'd':
- i=d/100;
- if(i!=0)PUT_CHAR(hex[i]);
- i=(d%100)/10;
- if(d>=100||i!=0)PUT_CHAR(hex[i]);
- PUT_CHAR(hex[d%10]);
- break;
- case 'D':
- PUT_CHAR(hex[d/10]);
- PUT_CHAR(hex[d%10]);
- break;
- case 'x':
- PUT_CHAR(hex[d>>4]);
- PUT_CHAR(hex[d&0x0f]);
- break;
- }
- }
- //********************************************************
- void PRINTF (uchar *pstring,uchar *arg)
- {
- uchar str;
- char count=0;
- uchar i;
- uint t;
- //PUT_CHAR ('\r');
- //PUT_CHAR ('\n');
- while(1) /* until full pstring string read */
- {
- str = *pstring++; /* until '%' or '\0' */
- if(str=='%')
- {str = *pstring++;
- if(str=='i')
- {t=(arg[count]<<8)+arg[count+1];
- if(t>=10000)
- {str=t/10000;PRINT_DEC_OR_HEX('d',str);}
- if(t>=1000)
- {str=t%10000/1000;PRINT_DEC_OR_HEX('d',str);}
- if(t>=100)
- {str=t%1000/100;PRINT_DEC_OR_HEX('d',str);}
- if(t>=10)
- {str=t%100/10;PRINT_DEC_OR_HEX('d',str);}
- {str=t%10; PRINT_DEC_OR_HEX('d',str);}
- count++;
- }
- else
- PRINT_DEC_OR_HEX(str,arg[count++]);
- }
- #if 0
- else if(str=='
- )//打印数组
- {
- str = *pstring++;
- count=0;
- while(1)
- {
- if(str=='x')
- {i=arg[count]>>4;
- if(i==0x0b)PUT_CHAR('G');
- else if(i==0x0A)PUT_CHAR('+');
- else if(i>9)break;
- else PUT_CHAR(hex[i]);
-
- i=arg[count]&0x0f;
- if(i==0x0b)PUT_CHAR('G');
- else if(i==0x0A)PUT_CHAR('+');
- else if(i>9)break;
- else PUT_CHAR(hex[i]);
- }
-
- else{
- PRINT_DEC_OR_HEX('x',arg[count]);
- }
- count++;
-
- if(count>=16)break;
- }
-
- }
- #endif
- else if(str==0)return;
- //else if(str==0XFF)return;
- else
- {
- PUT_CHAR (str);
-
- }
- }
- }
- void DELAY(uint T)
- {
- while(T--);
- }
- void RF_STUDY_START(void)
- {
- SYS.study_sec=30;
- LED_STA_CONVER(led_study);
- DEBUG_COM_STREAM("\r\n等待学习",NULL);
- }
- void RF_STUDY_EXIT(void)
- {
- SYS.study_sec=0;
- LED_STA_CONVER(led_standby);
- DEBUG_COM_STREAM("\r\n退出学习",NULL);
- }
- void PER_SECOND_EXE(void)
- {
- if(TIME.b_second==0)return;
- TIME.b_second=0;
- CHECK_RF_CONTINUE_TIME();
- if(0!=SYS.study_sec){if(--SYS.study_sec==0)LED_STA_CONVER(led_standby);}
- //DEBUG_COM_STREAM("\r\n我是秒测试",NULL);
- }
- void CHECK_KEY_STUDY(void)
- {
- uchar t_50ms=0;
- uint i;
- if(p_key1==1)return;
- WAIT_5ms(10);
- if(p_key1==1)return;
- #ifdef DEBUG_TX
- RF.pulse_us=400;
- i=RF.pulse_us*32;
- DEBUG_COM_STREAM("\r\n同步%i",(u8*)&i);
- TX1527(&SYS_SAVE.ini_flag);
- return;
- #endif
- while(1)
- {
- if(p_key1==1)break;
- WAIT_5ms(10);
- if(++t_50ms>70)////3.5 sec
- {
- RF_STUDY_EXIT();
- RESET_EEPROM();
- DEBUG_COM_STREAM("\r\n清空配件",NULL);
-
- LED_STA_CONVER(led_key);
- LED_ALL_FLASH(1,200);//全灯长亮一次
- while(p_key1==0);
- LED_STA_CONVER(led_standby);
- return;
- }
- }
- if(0!=SYS.study_sec)
- {RF_STUDY_EXIT();
- }
- else
- {
- RF_STUDY_START();
- }
- }
- void Flash_OFF_act_halt(void)
- {
- FLASH->CR1 |= 0x04;
- }
- //启用LPVR 如果HALT时不启用LPVR 耗电增加140uA
- /*Configure the MVR to be OFF during the halt phase of HALT mode*/
- void MVReg_OFF_act_halt(void)
- {
- CLK->ICKR |= 0x20;
- }
- //t必须小于50
- void WAIT_5ms(uchar t)
- {
- TIME.ms5_ex=0;
- while(TIME.ms5_ex<t);//wfi();
- }
- const uchar reg_key[]={1,9,7,8,2,0,0,8};
- void main(void)
- {
- #ifdef FREQ_2M
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
- #else
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV4);
- #endif
- //CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
- INITIAL_ALL_PIN();
- TIM4_Config();
- TIME2_CONFIG();
- UART_CONFIG();
- READ_SYS_SAVE();
- RF_ARRAY_INI();
- enableInterrupts();
- CHECK_CPU_REG();
- LED_STA_CONVER(led_standby);
- LED_ALL_FLASH(1,100);//500ms
- while (1)
- {
- wfi();
- PER_SECOND_EXE();
- RF_CHECK_QUEUE();
- RF_DECODE();
- CHECK_KEY_STUDY();
- if(UART.b_rxonoff)
- {
- UART.b_rxonoff=0;
- DEBUG_COM_STREAM("\r\n串口收到:",NULL);
- DEBUG_COM_STREAM(UART.rxbuff,NULL);
- if(memcmp(UART.rxbuff,reg_key,8)==0)
- {
- EXE_CPU_REG();
- CHECK_CPU_REG();
- }
- else if(memcmp(UART.rxbuff,"SYS?",4)==0)
- PRINT_SYS();
- }
- //TEST_TIM2();
- }
- }
复制代码
全部资料51hei附件下载:
脉宽自适应_通用解码.7z
(3.95 MB, 下载次数: 46)
|