PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

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

Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 21-03-2015, 10:54 PM   #4
duongvanthuy_qt
Nhập môn đệ tử
 
Tham gia ngày: Sep 2009
Bài gửi: 3
:
Sample code of PIC for AM2301 (Giao tiếp PIC với AM2301)

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);
}

}
duongvanthuy_qt vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 

Ðiều Chỉnh
Xếp Bài

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à 11:54 PM.


Đượ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