|
Tài trợ cho PIC Vietnam |
Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC |
|
Ðiều Chỉnh | Xếp Bài |
02-05-2013, 05:07 PM | #1 |
Nhập môn đệ tử
Tham gia ngày: Feb 2013
Bài gửi: 2
: |
[ÁN]_Giao tiếp SPI giữa 1 master và 2 slave dùng CCS
chào tất cả mọi người,em đang làm đồ án về SPI mọi người vào xem giúp em với, em sửa mãi mà vẫn chưa được? flie e gửi kèm
/************CHUONG TRINH CHO MASTER******************/ #include "16f887.h" #device adc=8 #FUSES NOWDT //No Watch Dog Timer #FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD) //#FUSES NOPUT //No Power Up Timer //#FUSES MCLR //Master Clear pin enabled #FUSES NOPROTECT //Code not protected from reading #FUSES NOCPD //No EE protection #FUSES NOBROWNOUT //No brownout reset //#FUSES IESO //Internal External Switch Over mode enabled //#FUSES FCMEN //Fail-safe clock monitor enabled #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES NODEBUG //No Debug mode for ICD #FUSES NOWRT //Program memory not write protected //#FUSES BORV40 //Brownout reset at 4.0V //#FUSES RESERVED //Used to set the reserved FUSE bits #use delay(clock=20000000) #include <lcd_lib_4bit.c> #use spi(DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3,BITS=8) #use fast_io(b) int8 nho,phim,value,data[10]; int8 a,b,c; void read_button(void) { if(input_state(pin_d0)==0) nho=1; if(nho==1&&input_state(pin_d0)==1) { nho=0; phim++; } } void doc_adc(void) { set_adc_channel(0); // chon kenh chuyen doi la kenh 0/AN0 delay_us(10); // thoi gian tre can thiet de adc 8bit co the chuyen doi duoc value=read_adc(); // doc gia tri adc sau khi chuyen doi xong a=value/100; b=(value%100)/10; c=(value%100)%10; } void doc_dulieu(void) { while(spi_data_is_in()==true) { data[0]=spi_read(); data[1]=spi_read(); data[2]=spi_read(); } } void gui_dulieu(void) { spi_write(0x00); delay_us(10); spi_write(a); delay_us(10); spi_write(b); delay_us(10); spi_write(c); delay_us(10); } void gui_dulieu1(void) { spi_write(0x01); delay_us(10); spi_write(a); delay_us(10); spi_write(b); delay_us(10); spi_write(c); delay_us(10); } void gui_dulieu2(void) { spi_write(0x02); delay_us(10); spi_write(a); delay_us(10); spi_write(b); delay_us(10); spi_write(c); delay_us(10); } void xoa_dulieu(void) { spi_write(0); spi_write(0); spi_write(0); spi_write(0); } void main() { set_tris_a(0x01); output_high(pin_b0); output_high(pin_b1); set_tris_d(0x01); setup_adc(ADC_CLOCK_INTERNAL);// cho phep su dung modul A/D setup_adc_ports(sAN0|VSS_VDD);// chon chan AN0 lam chan A/D dau vao, su dung dien ap mau cua chip setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_4|SPI_ SS_DISABLED); lcd_init(); phim=0; value=0; while(true) { read_button(); doc_adc(); lcd_putcmd(0x80); lcd_putchar("Phim : "); lcd_putchar(phim+48); lcd_putcmd(0xc0); lcd_putchar("Adc : "); lcd_putchar(a+48); lcd_putchar(b+48); lcd_putchar(c+48); switch(phim) { case 0: lcd_putcmd(0x89); lcd_putchar("Xoa Du lieu"); output_low(pin_b0); output_low(pin_b1); xoa_dulieu(); break; case 1: lcd_putcmd(0x89); lcd_putchar("Gui 2 Slave"); output_low(pin_b0); output_low(pin_b1); gui_dulieu(); break; case 2: lcd_putcmd(0x89); lcd_putchar("Gui Slave 1"); output_low(pin_b0); output_high(pin_b1); gui_dulieu1(); doc_dulieu(); lcd_putcmd(0xc9); lcd_putchar("Nhan slave1: "); lcd_putchar(data[0]+48); lcd_putchar(data[1]+48); lcd_putchar(data[2]+48); break; case 3: lcd_putcmd(0x89); lcd_putchar("Gui Slave 2"); output_high(pin_b0); output_low(pin_b1); gui_dulieu2(); doc_dulieu(); lcd_putcmd(0xc9); lcd_putchar("Nhan slave2: "); lcd_putchar(data[0]+48); lcd_putchar(data[1]+48); lcd_putchar(data[2]+48); break; case 4: output_high(pin_b0); output_high(pin_b1); phim=0; break; } } } /*********************CHUONG TRINH CHO SLAVE1*********************/ #include "16f887.h" #device adc=8 #FUSES NOWDT //No Watch Dog Timer #FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD) #FUSES NOPUT //No Power Up Timer //#FUSES MCLR //Master Clear pin enabled #FUSES NOPROTECT //Code not protected from reading #FUSES NOCPD //No EE protection #FUSES NOBROWNOUT //No brownout reset //#FUSES IESO //Internal External Switch Over mode enabled //#FUSES FCMEN //Fail-safe clock monitor enabled #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES NODEBUG //No Debug mode for ICD #FUSES NOWRT //Program memory not write protected //#FUSES BORV40 //Brownout reset at 4.0V //#FUSES RESERVED //Used to set the reserved FUSE bits #use delay(clock=20000000) #use spi(DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3,BITS=8) #include <lcd_lib_4bit.c> int8 data[10],a,b,c,value; void gui_dulieu(void); void doc_dulieu(void); void doc_adc(void); void doc_adc(void) { set_adc_channel(0); // chon kenh chuyen doi la kenh 0/AN0 delay_us(10); // thoi gian tre can thiet de adc 8bit co the chuyen doi duoc value=read_adc(); // doc gia tri adc sau khi chuyen doi xong a=value/100; b=(value%100)/10; c=(value%100)%10; } void gui_dulieu(void) { spi_write(a); delay_us(10); spi_write(b); delay_us(10); spi_write(c); delay_us(10); } void doc_dulieu(void) { while(spi_data_is_in()==true) { data[0]=spi_read(1); data[1]=spi_read(1); data[2]=spi_read(1); data[3]=spi_read(1); } if(data[0]==0x01) gui_dulieu(); } void main() { set_tris_a(0x01); setup_adc(ADC_CLOCK_INTERNAL);// cho phep su dung modul A/D setup_adc_ports(sAN0|VSS_VDD);// chon chan AN0 lam chan A/D dau vao, su dung dien ap mau cua chip setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_spi(SPI_SLAVE|SPI_H_TO_L|SPI_CLK_DIV_4); lcd_init(); data[0]=0; data[1]=0; data[2]=0; data[3]=0; while(true) { doc_adc(); doc_dulieu(); lcd_putcmd(0x80); lcd_putchar("Adc_gui:"); lcd_putchar(a+48); lcd_putchar(b+48); lcd_putchar(c+48); lcd_putcmd(0xc0); lcd_putchar("Adc_nhan:"); lcd_putchar(data[1]+48); lcd_putchar(data[2]+48); lcd_putchar(data[3]+48); } } /*********************CHUONG TRINH CHO SLAVE2*********************/ #include "16f887.h" #device adc=8 #FUSES NOWDT //No Watch Dog Timer #FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD) #FUSES NOPUT //No Power Up Timer //#FUSES MCLR //Master Clear pin enabled #FUSES NOPROTECT //Code not protected from reading #FUSES NOCPD //No EE protection #FUSES NOBROWNOUT //No brownout reset //#FUSES IESO //Internal External Switch Over mode enabled //#FUSES FCMEN //Fail-safe clock monitor enabled #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES NODEBUG //No Debug mode for ICD #FUSES NOWRT //Program memory not write protected //#FUSES BORV40 //Brownout reset at 4.0V //#FUSES RESERVED //Used to set the reserved FUSE bits #use delay(clock=20000000) #use spi(DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3,BITS=8) #include <lcd_lib_4bit.c> int8 data[10],a,b,c,value; void gui_dulieu(void); void doc_dulieu(void); void doc_adc(void); void doc_adc(void) { set_adc_channel(0); // chon kenh chuyen doi la kenh 0/AN0 delay_us(10); // thoi gian tre can thiet de adc 8bit co the chuyen doi duoc value=read_adc(); // doc gia tri adc sau khi chuyen doi xong a=value/100; b=(value%100)/10; c=(value%100)%10; } void gui_dulieu(void) { spi_write(a); delay_us(10); spi_write(b); delay_us(10); spi_write(c); delay_us(10); } void doc_dulieu(void) { while(spi_data_is_in()==true) { data[0]=spi_read(1); data[1]=spi_read(1); data[2]=spi_read(1); data[3]=spi_read(1); } if(data[0]==0x02) gui_dulieu(); } void main() { set_tris_a(0x01); setup_adc(ADC_CLOCK_INTERNAL);// cho phep su dung modul A/D setup_adc_ports(sAN0|VSS_VDD);// chon chan AN0 lam chan A/D dau vao, su dung dien ap mau cua chip setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_spi(SPI_SLAVE|SPI_H_TO_L|SPI_CLK_DIV_4); lcd_init(); data[0]=0; data[1]=0; data[2]=0; data[3]=0; while(true) { doc_adc(); doc_dulieu(); lcd_putcmd(0x80); lcd_putchar("Adc_gui :"); lcd_putchar(a+48); lcd_putchar(b+48); lcd_putchar(c+48); lcd_putcmd(0xc0); lcd_putchar("Adc_nhan :"); lcd_putchar(data[1]+48); lcd_putchar(data[2]+48); lcd_putchar(data[3]+48); } } |
|
|