• slider image 514
  • slider image 516
  • slider image 517
  • slider image 518
  • slider image 519
:::


Browsing this Thread:   1 Anonymous Users






兩顆PIC16F877以SPI互相溝通
#1
新會員
新會員


查看用戶資訊

各位高手:
小弟目前想做的是由RS232丟訊號進入master,然後經由SPI,由slave的232丟出到另一台電腦.但是現在另外一台電腦所收到的資料都一同一個奇怪符號,貼上master and slave 的程式,希望幫我看一下:

//This is master chip to control the slave program

#pragma chip PIC16F877

bit ReceiveReady;
char RC_data;

#include "int16CXX.h"
#pragma origin 4
interrupt server(void)
{
int_save_registers
if(RCIF==1)
{
SSPBUF=RCREG;
ReceiveReady=1;
RCIF=0;
}

int_restore_registers
}

void initUSART()
{
GIE=1;
PEIE=1; //use interrupt

SPEN=1; //Enable Usart
CREN=1; //Enable CR
TXEN=1; //Enable TX

BRGH=1;
SPBRG=25; //Baud Rate =9.6k

SYNC=0; //asynchronous

TXIE=0; //disable transmit interrupt TXIF
RCIE=1; //enable receive interrupt RCIF

TX9=0; // TX 8-bit
RX9=0;
}

void enableSPI (void)
{
ADCON1=bin(1000.0100); // all digital
// when using SSP in SPI module
//TRISA.5; Master not care SS bar
SSPM3=0; SSPM2=0; SSPM1=0; SSPM0=0; //master moder clock=Fosc/4
TRISC.3=0; //for master mode (SCK)
//TRISC.4 is automatically controlled by SPI module
TRISC.5=0; //SDO

SMP=0;
CKP=1; //idle for high
CKE=0; //falling edge and no using SS bar
SSPEN=1; //enable the serial PORT

SSPIE=0;
SSPIF=0;
}

void disableSPI (void)
{
SSPEN=0; // disable the serial PORT
}

void main (void)
{
TRISC=bin(1000.0000);
PORTC=0x00;

TRISD=0x00;
PORTD=0x00;

initUSART();
enableSPI();

while(1)
{

if( ReceiveReady==1)
{
ReceiveReady=0;
}
BF=0;
SSPIF=0;
}

} //main
-----------------------------------------------------------------------


//This is a slave chip for receiving data from master chip
//then sent data to 232 to PC terminal

#pragma chip PIC16F877

bit ReceiveReady;
char RC_data;

#include "int16CXX.h"
#pragma origin 4
interrupt server(void)
{
int_save_registers

;

int_restore_registers
}

void initUSART()
{
GIE=1;
PEIE=1; //use interrupt

SPEN=1; //Enable Usart
CREN=1; //Enable CR
TXEN=1; //Enable TX

BRGH=1;
SPBRG=25; //Baud Rate =9.6k

SYNC=0; //asynchronous

TXIE=0; //disable transmit interrupt TXIF
RCIE=0; //disable receive interrupt RCIF

TX9=0; // TX 8-bit
RX9=0;
}

void enableSPI (void)
{
ADCON1=bin(1000.0100); // all digital(ss bar)
// when using SSP in SPI module

SSPM3=0; SSPM2=1; SSPM1=0; SSPM0=1; //slave mode, clock form master

TRISC.3=1; //for slave mode (SCK)
//TRISC.4 is automatically controlled by SPI module
TRISC.5=0; //SDO

SMP=0; //slave mode must be clear
CKP=1; //idle for high
CKE=0; //falling edge
SSPEN=1; //enable the serial PORT

SSPIF=0;
SSPIE=1;
}

void disableSPI (void)
{
SSPEN=0; //disable the serial PORT
}

void main (void)
{

TRISC=bin(1000.0000);
PORTC=0x00;

TRISD=0x00;
PORTD=0x00;

initUSART();
enableSPI();

while(1)
{

if(BF==1)
{
TXREG=SSPBUF;
while(!TRMT)
;
BF=0;
SSPIF=0;
}

}

} //main

發表於: 2005/12/15 13:46
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... ]

教育訓練中心

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