#include #include __CONFIG(WDTDIS & HS & PWRTDIS & BORDIS);//設(shè)置配置位 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 //發(fā)送允許控制位
//---------------------------------------------------
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; //設(shè)置為輸入狀態(tài)
TRISC7 = 1;
// TXIE = 1; //開發(fā)送中斷
TRISC3 = 0; //設(shè)置為輸出狀態(tài)
TRISC2 = 0;
RE = 0; //SN75176開啟接收
DE = 0; // SN75176關(guān)閉發(fā)送
PEIE = 1; //開外設(shè)中斷
GIE = 1; //開總中斷
TRISB = 0x00; //B口為輸出
}
//-----------------------------------------------
void interrupt com(void)
{
if(RCIE&&RCIF)
{
redata=RCREG;
RE = 1; //SN75176關(guān)閉接收
DE = 1; // SN75176開啟發(fā)送
TXREG=redata;
while(!TRMT);
RE = 0; //SN75176開啟接收
DE = 0; //SN75176關(guān)閉發(fā)送
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);
}
}