Ðăng Nhập

View Full Version : [SPI]Kết nối 1 master và nhiều slave như thế nào?


Vo Bien Cuong
02-01-2009, 10:47 AM
Các bác cho em hỏi để kết nối các con pic6f877a với nhau sử dụng SPI, VD:1 con là Master còn 2 con là Slave thì ta làm thế nào. Có phải là chân SCK của Master nối với SCK của 2 con Slave, chân SDO của Master nối với chân SDI của 2 chú Slave, 2 chân SDO của 2 chú Slave nối với SDI của Master, 1 chân của Master nối với chân SS (RA5) của Slave1,1 chân của Master nối với chân SS (RA5) của Slave2 để chọn chíp không? Có bác nào đã làm rồi thì có thể post cho em xin đoạn code được không? Em xin cảm ơn.

mua_sao_bang
23-01-2010, 11:47 PM
không ai trả lời bác Vo Bien Cuong ah?
Em cũng đang bí về phần đó.

dothanhhuyen
14-03-2015, 12:21 AM
Em thấy bài này lâu rùi, tiền bối có thể cho em xin code mẫu k. Em mới bắt đầu nên bí quá.

duongvanthuy_qt
21-03-2015, 10:54 PM
Tình hình là mình mới mua cái module cảm biến nhiệt độ và độ ẩm AM2301, mà lười viết giao tiếp để đọc dữ liệu từ cảm biến về vi điều khiển PIC quá nên lên mạng tìm code mẫu. Mà không tìm ra code mẫu cho PIC, mình ngồi viết luôn và share cho mọi người để lỡ sau này cần thì tham khảo cho nhanh đở mất công đi tìm.

Code viết cho PIC 16f886, và module cảm biến nhiệt độ và độ ẩm AM2301. PIC16f886 đọc dữ liệu từ module cảm biến sau đó gửi dữ liệu đó lên máy tính qua cổng RS232.


#include <main.h>
#define SDA PIN_A0 // define the Pin to receive data from sensor module

unsigned char Recieved_Data [5] = {0,0,0,0,0}; // array contains data from sensor module
double Humidity, Temperature; // value of Humidity and Temperature
unsigned char array [8] = {128, 64, 32, 16, 8, 4, 2, 1};// array to calculate received data


void read_data_from_AM2301()
{
Recieved_Data [0] = 0;// initialize original values when start to receive data
Recieved_Data [1] = 0;
Recieved_Data [2] = 0;
Recieved_Data [3] = 0;
Recieved_Data [4] = 0;
Read:
output_low(SDA);// create start condition
delay_us(1000);
output_high(SDA);
int count = 0;
while (input(SDA))// don't have response signal from AM2301
{
;
count++;
if(count > 200)
goto Read; // when wait time for response signal greater 200us back to read again
}
while (!input(SDA));
while (input(SDA));

unsigned char i, j;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 8; j++)
{
while (!input(SDA));// receive 40 bits from sensor module
delay_us(40);
if(!input(SDA))
{
Recieved_Data [i] = Recieved_Data [i];
}
else
{
Recieved_Data [i] = Recieved_Data [i] | (array [j]);
while (input(SDA));
}
}
}
}
void convertion_of_Humidity_temperature()// convert received data to real values of humidity and temperature
{
Humidity = (Recieved_Data [0] & 0x0f) * 256 + ((Recieved_Data [1] & 0xf0)>>4)*16 + (Recieved_Data [1] & 0x0f) ;
Temperature = (Recieved_Data [2] & 0x0f) * 256 + ((Recieved_Data [3] & 0xf0)>>4)*16 + (Recieved_Data [3] & 0x0f) ;
}



void main()
{

setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
delay_ms(3000);// wait for stabilization of sensor module
char mystring[20];// declare a cursor variable to convert double values to strings for transfer data to personal computer ;
while (true)
{
Loop:
read_data_from_AM2301();
if((Recieved_Data [0]+Recieved_Data [1]+Recieved_Data [2]+Recieved_Data [3]) != Recieved_Data [4])
GOTO Loop;

convertion_of_Humidity_temperature();

sprintf(mystring,"DA: %.1f ",Humidity/10);
printf(mystring);

sprintf(mystring,"ND: %.1f --- ",Temperature/10);
printf(mystring);

delay_ms(700);
}

}