PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > MiWi, Zigbee, Ethernet

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

MiWi, Zigbee, Ethernet ZigBee có khả năng kết nối tới 65000 node, MiWi dành riêng cho Microchip chỉ kết nối được 1000 node, nhưng điều gì đang diễn ra...?

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 18-09-2012, 04:45 PM   #1
truongqt
Đệ tử 2 túi
 
truongqt's Avatar
 
Tham gia ngày: Apr 2007
Nơi Cư Ngụ: Quảng Trị - Đà Nẵng
Bài gửi: 43
:
Module Wireless CYWM6935 và Vi điều khiển PIC 16F877A/886...

Mình đang làm truyền nhận dữ liệu giữa 2 bo mạch thu thập dữ liệu. Trên mỗi bo dùng PIC, truyền nhận dữ liệu bằng module CYWM6935. Theo datasheet của module CYWM6935 thì khoảng cách lên đến 100m, dùng sóng 2.4Ghz nên khả năng xuyên vật cản là rất tốt. Tuy nhiên mình làm nó chỉ được khoảng cách vài mét, cách 1 bức tường thì cũng ok, chứ không được vài chục mét. Anh em ai làm rồi hoặc có đam mê thì cùng chia sẻ nhé. Driver module CYWM6935 mình gửi kèm dưới đây, viết trong CCS-PIC nhé.

#include <stddef.h>
/* dinh nghia nay dung cho PIC 16F877, 886
//#define IRQ RB0
#define nSS PIN_B3//RB2
#define nPD PIN_B5//RB3
#define nRESET PIN_B2//RB1
#define SPI_OUT PIN_C5//doi tu C7 sang C5
#define SPI_IN PIN_B0
#define SPI_CLK PIN_B1
*/

// dung cho PIC16F688
#define nSS PIN_C1
#define nRESET PIN_C2
#define SPI_OUT PIN_C3
#define SPI_IN PIN_C0
#define SPI_CLK PIN_C4


//SPI_CLK PIN_C3
//SPI_IN PIN_C4
//SPI_OUT PIN_C5

#separate void reset_WM();
#separate void init_WM();
#separate void spi_output(dout);
#separate byte spi_input();
#separate void Write_WM(int reg,int data);
#separate int Read_WM(int reg);
#separate void Set_Tx_mode();
#separate void Set_Rx_mode();
#separate void transmit_WM(int data);
#separate int receive_WM();
#separate void set_channel(int channel);


#separate void reset_WM() // ham reset module wireless
{
output_low(nRESET);
delay_ms(10);
output_high(nRESET);
delay_ms(10);
}

#separate void init_WM()
{
output_high(nSS);// khong chon truyen SPI, chi khi nao truyen moi clear bit nay
reset_WM();
Write_WM(REG_ANALOG_CTL,0x01);// Reset tat ca thanh ghi ve gia tri mac dinh
delay_us(250);
Write_WM(REG_CLOCK_MANUAL,0x41);// Ghi gia tri 0x41 vao thanh ghi Reg_Clock_Manual de module hoat dong dung
delay_us(250);
Write_WM(REG_CLOCK_ENABLE,0x41);// Ghi gia tri 0x41 vao thanh ghi Reg_Clock_Enable de module hoat dong dung
delay_us(250);
Write_WM(REG_CONTROL,0x14);// Bybass the PLL Lock
delay_us(250);
Write_WM(REG_SERDES_CTL,0x08);//EOF frame lenght
delay_us(250);
Write_WM(REG_VCO_CAL, 0xC0);
delay_us(250);
Write_WM(REG_CONFIG,0x01); //high level active interrupt
delay_us(250);

Write_WM(REG_PA,0x07); //max PA
delay_us(250);

}

// SPI read - write routines
// this was written because I could not get the CCS implementation using 18F2550 hardware to
// work reliabily.
//
// Data is clocked at approx 1MHz with 48MHz clock, time to write/read a byte is approx 8 uSec.


// Output a byte to the SPI device
#separate void spi_output(dout) {
byte i;

for(i = 0; i < 8; i++) {
rotate_left(&dout, 1); // place the MSB into bit 0 position
if(dout & 1) output_high(SPI_OUT); // and set output high if necessary (output is already low)
output_high(SPI_CLK); // strobe data on the low to high transition
delay_cycles(2);
output_low(SPI_CLK); // reset the clock
output_low(SPI_OUT); // and set the data line low for the next bit
}
}


// Input a byte from the SPI device
#separate byte spi_input() {
byte i, din = 0;

for(i = 0; i < 8; i++) {
rotate_left(&din, 1); // prepare the input variable
output_high(SPI_CLK); // data should be available on the low to high transition
if(input(SPI_IN)) bit_set(din, 0); // if we have a high input set the bit (var is initilised to 0)
output_low(SPI_CLK); // and reset the clock
}
return din;
}
// Wireless Module (WM) registers read/write routines
//

// Write data to addr in the WM
#separate void Write_WM(byte addr, byte data) {
output_low(nSS);
spi_output(addr | 0x80);
spi_output(data);
output_high(nSS);
}

// Read from addr in the WM
#separate byte Read_WM(byte addr) {
byte d;
output_low(nSS);
spi_output(addr);
d = spi_input();
output_high(nSS);
return d;
}

#separate void Set_Tx_mode()
{
int data;
Write_WM(REG_TX_VALID,0xFF);//Chon so bit valid :8 bit
delay_us(250);
data=Read_WM(REG_TX_INT_STAT);//Doc de xoa thanh ghi trang thai
delay_us(250);
Write_WM(REG_CONTROL,0x54);//Chon che do truyen,PA_Enable
delay_us(250);
Write_WM(REG_PA,0x07);
delay_us(250);
Write_WM(REG_TX_INT_EN,0x01);// Cho phep ngat empty
delay_ms(10);
}

#separate void Set_Rx_mode()
{
int data;
data=Read_WM(REG_RX_INT_STAT);//Doc de xoa thanh ghi trang thai
delay_us(250);
Write_WM(REG_RX_INT_EN,0x03);// cho phep ngat nhan Full A
delay_us(250);
Write_WM(REG_CONTROL,0x90); //set RX mode = 0x80
delay_ms(10);
}

#separate void transmit_WM(int data)
{
Write_WM(REG_TX_DATA,data);
delay_ms(10);
}

#separate int receive_WM()
{
int data;
//delay_ms(1);
//while(Read_WM(REG_RX_VALID_A)!=0xff);
while(!(Read_WM(REG_RX_INT_STAT) &0x01));
data=Read_WM(REG_RX_DATA_A);
return data;
}

#separate void set_channel(int channel)
{
Write_WM(REG_CHANNEL,channel);
delay_ms(10);
}
__________________
Trường.
http://vn.myblog.yahoo.com/nguyenvantruongqt
truongqt vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 02:18 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam