|
前幾天做了一用433M的無(wú)線傳輸,用430單片機(jī)發(fā)送和就收數(shù)據(jù)。但是很有很多的不足之處。現(xiàn)在想繼續(xù)用兩對(duì)這樣的模塊來(lái)實(shí)現(xiàn)雙工通信。
發(fā)送程序
#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;//設(shè)置上拉
P2IES |= BIT1;//P2.1設(shè)置為下降沿觸發(fā)中斷方式
P2IE |= BIT1;//使能P2.1中斷
__enable_interrupt();
}
void Send_Start_Code()//引導(dǎo)碼
{
SET_DATA_PORT;
__delay_cycles(24000);//delay 5ms
CLR_DATA_PORT;
__delay_cycles(16000);
}
void Send_Data_Code(uchar bit)//數(shù)據(jù)碼
{
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)//發(fā)送一個(gè)字節(jié)的數(shù)據(jù)
{
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時(shí)循環(huán),但不能超過(guò)8ms
{
i++;
__delay_cycles(400);
}
return i;
}
uchar Get_High_Line()
{
uchar i=0;
while(DATA_INPUT&&i<80)//為1時(shí)循環(huán),但不能超過(guò)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);
}
}
|
評(píng)分
-
查看全部評(píng)分
|