PIC 单片机485通信 接收机
扫描二维码
随时随地手机看文章
#include #include __CONFIG(WDTDIS & HS & PWRTDIS & BORDIS);//设置配置位 uchar redata; //---------------------------------------------------------- RCSTA = 0x80;
#include "stdio.h"
#define uchar unsigned char
#define uint unsigned int
// WDTDIS : disable watchdog timer
// LVPDIS : low voltage programming disabled
// HS : high speed crystal/resonator
// PWRTDIS : disable power up timer
// BORDIS : disable brown out reset
#define PORTAIT(adr,bit) ((unsigned)(&adr)*8+(bit)) //绝对位操作指令;
static bit DI @(PORTAIT(PORTB,0);
static bit CK @(PORTAIT(PORTB,1);
static bit LA @(PORTAIT(PORTB,2);
static bit OE @(PORTAIT(PORTB,3);
#define RE RC2 //接收允许控制位
#define DE RC3 //发送允许控制位
//---------------------------------------------------
void USART_Init(void);
void SEND_Byte(uchar dat);
void SEND_DM114_DA(uchar DAT);
void delay50us(uchar z);
//-------------------------------------------------
void delay50us(uchar z)
{
uchar i,j;
for(j=0;j
;
}
//--------------------------------
void SEND_Byte(uchar dat)
{
if(dat ==0)
// DI = 0;
RB0 = 0;
else
DI = 1;
// RB0 = 1;
}
void SEND_DM114_DA(uchar DAT)
{
uchar i;
OE = 1;
// RB3 = 1;
LA = 0;
// RB2 = 0;
for(i=0;i<8;i++)
{
SEND_Byte(DAT&0x80);
DAT << = 1;
CK = 0;
CK = 1;
// RB1 = 0;
// RB1 = 1;
}
LA = 1;
OE = 0;
// RB2 = 1;
// RB3 = 0;
}
//---------------------------------------------------------
void USART_Init(void)
{
SPBRG = 77; //高速 波特率9600 12MHz SPBRG = 0x4D
// SPBRG = 129; //高速 波特率9600 20MHz
TXSTA = 0x04;
// TXSTA : CSRC TX9 TXEN SYNC -- BRGH TRMT TX9D
// 0 0 0 0 1 0 0
// RCSTA: SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
// 1 0 0 0 0 0 0 0
RCIE = 1; //开接收中断
TXEN = 1;
CREN = 1; //接收使能
TRISC6 = 1; //设置为输入状态
TRISC7 = 1;
// TXIE = 1; //开发送中断
TRISC3 = 0; //设置为输出状态
TRISC2 = 0;
RE = 0; //SN75176开启接收
DE = 0; // SN75176关闭发送
PEIE = 1; //开外设中断
GIE = 1; //开总中断
TRISB = 0x00; //B口为输出
}
//-----------------------------------------------
void interrupt com(void)
{
if(RCIE&&RCIF)
{
redata=RCREG;
RE = 1; //SN75176关闭接收
DE = 1; // SN75176开启发送
TXREG=redata;
while(!TRMT);
RE = 0; //SN75176开启接收
DE = 0; //SN75176关闭发送
SEND_DM114_DA(redata);
}
}
//------------------------------------------------
void main(void)
{
uchar i,j;
USART_Init();
while(1)
{
// for(i=0;i<100;i++)
// delay50us(60);
// j++;
// if(j>250)
// j=0;
// SEND_DM114_DA(j);
}
}