找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2738|回复: 0
收起左侧

基于430f5529的无线按键传输

[复制链接]
ID:214217 发表于 2017-7-22 09:32 | 显示全部楼层 |阅读模式
前几天做了一用433M的无线传输,用430单片机发送和就收数据。但是很有很多的不足之处。现在想继续用两对这样的模块来实现双工通信。
发送程序


#include "msp430.h"
#include "HAL_UCS.h"
#include "system.h"

#define uchar unsigned char
#define uint unsigned int

#define SET_DATA_PORT P6OUT |= BIT5;
#define CLR_DATA_PORT P6OUT &= ~BIT5;

uchar table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar i=0,j=0,count=0;
void Port2_Init()
{
  P2DIR &= ~BIT1;
  P2OUT |= BIT1;
  P2REN |= BIT1;//设置上拉
  P2IES |= BIT1;//P2.1设置为下降沿触发中断方式
  P2IE |= BIT1;//使能P2.1中断  
  __enable_interrupt();
}

void Send_Start_Code()//引导码
{
  SET_DATA_PORT;
  __delay_cycles(24000);//delay 5ms
  CLR_DATA_PORT;
  __delay_cycles(16000);
}

void Send_Data_Code(uchar bit)//数据码
{
  if(bit)
  {
    SET_DATA_PORT;
    __delay_cycles(12000);//3000us high  2000us low 1
    CLR_DATA_PORT;
    __delay_cycles(8000);
  }
  else
  {
    SET_DATA_PORT;
    __delay_cycles(8000);
    CLR_DATA_PORT;
    __delay_cycles(12000);//2000us high 3000us low 0
  }
}

void Send_One_Byte(uchar data)//发送一个字节的数据
{
  uchar bi;
  for(i=0;i<8;i++)
  {
    bi=data&0x80;
    data=data<<1;
    if(bi)
    {
      Send_Data_Code(1);
    }
    else
    {
      Send_Data_Code(0);
    }
  }
}

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  System_Clock_Init();
  //Port2_Init();

  P4DIR |= BIT7;
  P4OUT |= BIT7;

  P6DIR |= BIT5;
  CLR_DATA_PORT;
  while(1)
  {
    Send_Start_Code();
    Send_One_Byte(0x3f);
    Send_One_Byte(0x06);
    Send_One_Byte(0x5b);
    Send_One_Byte(0x4f);
    Send_One_Byte(0x66);
    Send_One_Byte(0x6d);
    Send_One_Byte(0x7d);
    Send_One_Byte(0x07);
    Send_One_Byte(0x7f);
    Send_One_Byte(0x67);
    __delay_cycles(200000);
  }
}

接收程序

#include "msp430.h"
#include "HAL_UCS.h"
#include "system.h"

#define uchar unsigned char
#define uint unsigned int

#define DATA_INPUT P2IN&BIT0

uchar table[10];
uchar th,tl,i,j;

uchar Get_Start_Line()
{
  uchar i=0;
  while(DATA_INPUT&&i<50)//为1时循环,但不能超过8ms
  {
    i++;
    __delay_cycles(400);
  }
  return i;
}

uchar Get_High_Line()
{
  uchar i=0;
  while(DATA_INPUT&&i<80)//为1时循环,但不能超过8ms
  {
    i++;
    __delay_cycles(400);
  }
  return i;
}

uchar Get_Low_Line()
{
  uchar i=0;
  while(!DATA_INPUT&&i<60)
  {
    i++;
    __delay_cycles(400);
  }
  return i;
}

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  System_Clock_Init();
  P4DIR |= BIT7;
  P4OUT &= ~BIT7;
  P1DIR |= BIT0;
  P1OUT &= ~BIT0;
  P3DIR = 0xff;
  P3OUT = 0x3f;
  while(1)
  {
    restart:while(!DATA_INPUT) P1OUT &= ~BIT0;

    th=Get_High_Line();
    if(th<55||th>65) goto restart;
    tl=Get_Low_Line();
    if(tl<35||tl>45) goto restart;//<3.5ms||>4.5ms

    P1OUT |= BIT0;
    for(j=0;j<10;j++)
    {
      for(i=0;i<8;i++)
      {
        th=Get_High_Line();
        //if(th<15||th>35) goto restart;
        tl=Get_Low_Line();
        //if(tl<15||tl>35) goto restart;
        table[j]<<=1;
        if(th>22){table[j] |= 0x01;}
      }
    }

    P3OUT = table[0];
    __delay_cycles(2000000);
    P3OUT = table[1];
    __delay_cycles(2000000);
    P3OUT = table[2];
    __delay_cycles(2000000);
    P3OUT = table[3];
    __delay_cycles(2000000);
    P3OUT = table[4];
    __delay_cycles(2000000);
    P3OUT = table[5];
    __delay_cycles(2000000);
    P3OUT = table[6];
    __delay_cycles(2000000);
    P3OUT = table[7];
    __delay_cycles(2000000);
    P3OUT = table[8];
    __delay_cycles(2000000);
    P3OUT = table[9];
    __delay_cycles(2000000);
  }
}






评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表