各位先進好
小弟初次使用PIC16F873A,Hi-Tech c進行專案製作
程式如下:
#include <pic.h>
#include <pic168xa.h>
void main(void)
{
#define BAUD 9600
#define FOSC 4000000
unsigned char btBuf[9];
unsigned int i=0;
unsigned int nXOR;
int nSN;
int DIVIDER;
// bit definition for port B:input from MCU
#define PORTB0 RB0
#define PORTB1 RB1
#define PORTB2 RB2
#define PORTB3 RB3
#define PORTB4 RB4
#define PORTB5 RB5
#define PORTB6 RB6
#define PORTB7 RB7
//port B:input from MCU
TRISB0=1;
TRISB1=1;
TRISB2=1;
TRISB3=1;
TRISB4=1;
TRISB5=1;
TRISB6=1;
TRISB7=1;
//set up UART
TRISC6=0; //set RC6
TRISC7=1; //set RC7
SPEN=1;
TXEN=1; //transmitted enable
CREN=1; //received enable
BRGH=1;
SYNC=0;
TX9=0;
DIVIDER=FOSC/(16*BAUD)-1;
SPBRG=DIVIDER;
// active input
if(RB7==1)
{
nSN++;
btBuf[0]=0x80+(0x0F&nSN);
btBuf[1]=0x7F;
btBuf[2]=0x00;
btBuf[3]=0x32;
btBuf[4]=0x03;
btBuf[5]=0xAD;
btBuf[6]=0x00;
btBuf[7]=0x01;
nXOR=0;
for(i=0,nXOR=0;i<8;i++)
{
nXOR^=btBuf[i];
}
btBuf[8]=nXOR;
for(i=0;i<=8;i++)
{
TXEN=1;
TXREG=btBuf[i]; //transmitted data
while(1)
{
if(TXIF==1) break;
}
}
}
// disable input
else if(RB7==0)
{
nSN++;
btBuf[0]=0x80+(0x0F&nSN);
btBuf[1]=0x7F;
btBuf[2]=0x00;
btBuf[3]=0x32;
btBuf[4]=0x03;
btBuf[5]=0xAD;
btBuf[6]=0x00;
btBuf[7]=0x00;
nXOR=0;
for(i=0,nXOR=0;i<8;i++)
{
nXOR^=btBuf[i];
}
btBuf[8]=nXOR;
for(i=0;i<=8;i++)
{
TXEN=1;
TXREG=btBuf[i]; //transmitted data
while(1)
{
if(TXIF==1) break;
}
}
}
}
經程式驗證
與預期相同
但利用示波器讀取TX信號時
卻未讀出任何信號
(利用power supply外加電源、RB7接地)
想請教各位先進
可能出錯的地方在哪兒?
程式還缺少什麼部份?
configuration bit 之設定有什麼要注意的地方?
謝謝各位先進的幫忙
再次感謝
Edited by Ryang on 2008年08月20日 09:11:44