您的位置:首页 > 其它

吊扇项目总结(二)— 433无线模块

2016-02-09 09:10 148 查看
</pre>简述433MHz无线模块:<p></p><p>433MHz无线模块软件编解码很方便简单,但在调试过程中发现手头上的433模块在接受器上一直存在一个2KHz的方波 。如果在高脉冲上接受到数据,此段数据将会受到影响;如果在低脉冲上接受到数据,此数据为有效。此结论是个人调试发现的,后来在硬件上各种滤波处理,但该方波依然存在。如果有朋友遇到相同情况并解决的可以私信一下本人。</p><p></p><p>本次项目使用STM8为主控芯片,并使用ST公司官方库。</p><p>关于编解码问题:</p><p>(一)发送端编码:433MHz模块是单引脚发送/接收数据的,通过控制高低电平的长度来控制一个位(bit)是1还是0。</p><p>以下提供发送端代码:部分变量自行定义,同时也可以进一步集成函数代码,使得其移植性更好。</p><p></p><pre name="code" class="plain">/*******************************************************************************
* 名称: SendData433
* 功能: 发送数据
* 形参: 无
* 返回: 无
* 调用:外部调用
* 说明:一次发送四组相同的数据
******************************************************************************/
void SendData433( uint8_t address , uint8_t number )
{
ChangeAddress(address , AddressArray);
ChangeData(number , DataArray);

SendSynchronous();/************第一次发送***************/
for(uint8_t i=0; i < 8; i ++)
{
if( AddressArray[i] == 1 )
SendHighlevel();
else
SendLowlevel();
}
for(uint8_t i=0; i < 4; i ++)
{
if( DataArray[i] == 1 )
SendHighlevel();
else
SendLowlevel();
}

SendSynchronous();/************第二次发送***************/
for(uint8_t i=0; i < 8; i ++)
{
if( AddressArray[i] == 1 )
SendHighlevel();
else
SendLowlevel();
}
for(uint8_t i=0; i < 4; i ++)
{
if( DataArray[i] == 1 )
SendHighlevel();
else
SendLowlevel();
}

SendSynchronous();/************第三次发送***************/
for(uint8_t i=0; i < 8; i ++)
{
if( AddressArray[i] == 1 )
SendHighlevel();
else
SendLowlevel();
}
for(uint8_t i=0; i < 4; i ++)
{
if( DataArray[i] == 1 )
SendHighlevel();
else
SendLowlevel();
}

SendSynchronous();/************第四次发送***************/
for(uint8_t i=0; i < 8; i ++)
{
if( AddressArray[i] == 1 )
SendHighlevel();
else
SendLowlevel();
}
for(uint8_t i=0; i < 4; i ++)
{
if( DataArray[i] == 1 )
SendHighlevel();
else
SendLowlevel();
}
}
<pre name="code" class="plain">/*******************************************************************************
* 名称: ChangeAddress
* 功能: 地址数据转换
* 形参: 无
* 返回: 无
* 调用:外部调用
* 说明:数据转换之后,高位在前,有八位数据
******************************************************************************/
static void ChangeAddress(uint8_t address , uint8_t array[])
{
for(uint8_t i=0; i < 8; i ++)
AddressArray[i] = 0x00;

uint8_t temp = 0x80;

for(uint8_t i=0; i < 8; i ++)
{
if( (address & temp) == temp )
{
array[i] = 1;
}
else
{
array[i] = 0;
}
temp = temp >> 1;
}
}
/*******************************************************************************
* 名称: ChangeData
* 功能: 数据转换
* 形参: 无
* 返回: 无
* 调用:外部调用
* 说明:数据转换之后,高位在前,只有四位数据
******************************************************************************/
static void ChangeData(uint8_t number , uint8_t array[])
{
for(uint8_t i=0; i < 4; i ++)
DataArray[i] = 0x00;

uint8_t temp = 0x08;

for(uint8_t i=0; i < 4; i ++)
{
if( (number & temp) == temp )
{
array[i] = 1;
}
else
{
array[i] = 0;
}
temp = temp >> 1;
}
}


(二)接收端解码:作为接收端,在已经确认正确编码的情况下,根据编码bit的脉冲长度来进行解码。

INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
BitStatus bit_status;
bit_status = GPIO_ReadInputPin(GPIOA, GPIO_PIN_3);

if( bit_status != RESET )
{
connumber = 0x0001;
time_one = connumber;

while( GPIO_ReadInputPin(GPIOA, GPIO_PIN_3) != RESET )
time_two = connumber;//记录高脉冲长度

gettingnum = DataComparison(time_two,time_one);//匹配该位的所表示的数字量(1/0)

if( gettingnum == Failure )
ReviceFlagClear();//数据出错即可重置参数

if( Flag_Synchronization_Code == AgreeToStart || gettingnum == Synchronization_Code )//已经读取或正在读取:同步码
{
if( Flag_Synchronization_Code == AgreeToStart )//在已经成功读取同步码后,允许读取下列数据,否则将数据丢弃
{
if( gettingnum != Synchronization_Code )//本次读取不为同步码,则该位为数据位
{
GetTransArrary[TransArraryNum] = gettingnum;
TransArraryNum ++;
}

if( TransArraryNum == 12 )//本次读取次数为命令的的最大数据位
{
TransArraryNum = 0x00;
Flag_Receive = ReceiveComplete;
Flag_Synchronization_Code = AgreeToStop;
}
}

if( gettingnum == Synchronization_Code )//本次读取位同步码
Flag_Synchronization_Code = AgreeToStart;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: