STM8单片机无线315&433脉宽自适应_通用解码程序
2021-11-25 来源:eefocus
单片机源程序如下:
/*
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 '