Chương trình chạy mô phỏng trên ISIS - Proteus:
mô phỏng SPI trên Proteus
Code Master:
#include "16f877a.h"
#include "def_877a.h"
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use rs232(baud=38400,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use fast_io(B)
#use fast_io(D)
#use fast_io(A)
#define REG_Write 0x80
#INT_SSP
void spi()
{
PORTD=spi_read(PORTB);
delay_ms(10);
}
void main()
{
port_b_pullups(TRUE);
setup_spi(spi_master|spi_l_to_h|spi_clk_div_16);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
SET_TRIS_B(0xff);
SET_TRIS_D(0x00);
SET_TRIS_A(0x00);
while(1)
{
delay_ms(100);
output_low(PIN_A5);//Chân C2 dùng Select chip.
delay_ms(10);//Tao tre de Slave chuan bi.
spi_write(PORTB);
output_high(PIN_A5);
}
}
Code Slave:
#include "16f877a.h"
#include "def_877a.h"
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use rs232(baud=38400,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use fast_io(B)
#use fast_io(D)
#INT_SSP
void spi()
{
PORTD=spi_read(PORTB);//Vua nhan vua truyen.
delay_ms(10);
}
void main()
{
port_b_pullups(TRUE);
setup_spi(spi_slave|spi_l_to_h|spi_clk_div_16);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
TRISB=0xff;
TRISD=0x00;
while(1)
{
}
}
Phạm Văn Ngọc Anh- 01644326695
Đăng nhận xét