• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
:::


Browsing this Thread:   1 Anonymous Users






請問UART傳送資料問題(使用PIC16F873A,Hi-Tech c)
#1
新會員
新會員


查看用戶資訊
各位先進好

小弟初次使用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 之設定有什麼要注意的地方?

謝謝各位先進的幫忙
再次感謝

發表於: 2008/8/19 16:28

Edited by Ryang on 2008年08月20日 09:11:44
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------