Browsing this Thread:
1 Anonymous Users
Topic options
View mode
舊的在前
兩顆PIC16F877以SPI互相溝通
新會員
Joined
: 2005/12/15 13:34最後登入時間
: 2005/12/15 13:35
Group:
註冊會員
Level : 1
HP : 0 / 0
MP : 0 / 0
EXP : 0
各位高手: 小弟目前想做的是由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
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.