caddish12
19-01-2014, 01:50 AM
http://i1142.photobucket.com/albums/n606/caddish12/IMG00309-20140119-0031_zps593bbc90.jpg (http://s1142.photobucket.com/user/caddish12/media/IMG00309-20140119-0031_zps593bbc90.jpg.html)
Chào các bạn!
Mình xin chia sẻ mạch dành cho những bạn mới làm quen với PIC16F887, Board ứng dụng nho nhỏ tích hợp sẵn ds1307+SHT11+UART+LCD16x2, mạch này thích hợp để ủi ^^
Gồm schematic+layout (orcad)+firmware
Main Code:
#include <htc.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd.h"
#include "rtc.h"
#include "i2c.h"
#include "uart.h"
#include "SHT.h"
__CONFIG(HS & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
void putch(char c)
{
uart_putc(c);
}
char buf[50];
unsigned char s,m,h,day,date,month,year;
void ftoa(float value, char *string)
{
if (value < 0) {
*string++ = '-';
value = -value;
}
sprintf(string, (far char *)"%d.%02u",
(int) value,
(int) ((value - (int) value) * 100. + 0.5)); //important
}
unsigned char bcd2dec(unsigned char a)
{
unsigned char r,t;
t = a & 0x0F;
r = t;
a = 0xF0 & a;
t = a >> 4;
t = 0x0F & t;
r = t*10 + r;
return r;
}
unsigned char dec2bcd(unsigned char a)
{
unsigned char t1, t2;
t1 = a%10;
t1 = t1 & 0x0F;
t2 = a/10;
t2 = t2 << 4;
t2 = 0xF0 & t2;
t1 = t1 | t2;
return t1;
}
void rtc_readdatetime(void)
{
s = rtc_read(0);
m = rtc_read(1);
h = rtc_read(2);
day = rtc_read(3);
date = rtc_read(4);
month = rtc_read(5);
year = rtc_read(6);
switch(bcd2dec(day))
{
case 0:
sprintf(buf,"Mon");
break;
case 1:
sprintf(buf,"Tue");
break;
case 2:
sprintf(buf,"Wed");
break;
case 3:
sprintf(buf,"Thu");
break;
case 4:
sprintf(buf,"Fri");
break;
case 5:
sprintf(buf,"Sat");
break;
case 6:
sprintf(buf,"Sun");
break;
}
lcd_gotoxy(0,0);
sprintf(buf,"%s %02u-%02u-%02u",buf,bcd2dec(date),bcd2dec(month),bcd2dec(year));
lcd_puts(buf);
printf("\n\r%s %02u-%02u-%02u",buf,bcd2dec(date),bcd2dec(month),bcd2dec(year));
printf("\n\rTime: %02u:%02u:%02u",bcd2dec(h),bcd2dec(m),bcd2dec(s));
}
void main()
{
unsigned int a;
char i;
float tem,humi;
ANSEL = 0;
ANSELH = 0;
TRISC0 = 0;
uart_init();
lcd_init();
sht_init(SHT_14_12_BIT);
i2c_init();
// rtc_write(0,dec2bcd(0));
// rtc_write(1,dec2bcd(53));
// rtc_write(2,dec2bcd(13));
// rtc_write(3,dec2bcd(4));
// rtc_write(4,dec2bcd(17));
// rtc_write(5,dec2bcd(1));
// rtc_write(6,dec2bcd(13));
printf("\n\r\n\rEmbedded RealTime System Project\n\r");
rtc_readdatetime();
__delay_ms(90);
a=0;
while(1)
{
a++;
RC0 ^= 1;
s = rtc_read(0);
m = rtc_read(1);
h = rtc_read(2);
lcd_gotoxy(1,0);
sprintf(buf,"Time: %02u:%02u:%02u",bcd2dec(h),bcd2dec(m),bcd2dec(s));
lcd_puts(buf);
if (a==200)
{
sht_readTemHumi(&tem,&humi);
//ftoa(tem,buf); //enable if you use Hi-Tech PRO only
printf("\n\rT: %s",buf);
//ftoa(humi,buf);
//printf(" H: %s\n\r",buf);
a = 0;
}
}
}
Nếu bạn dùng bản Hi-Tech PRO thì hãy uncomment 3 dòng sau nhé{
ftoa(tem,buf);
ftoa(humi,buf);
printf(" H: %s\n\r",buf);
lý do là vì bản lite không có optimize code nên sẽ không đủ bộ nhớ cho PIC16F887
Chào các bạn!
Mình xin chia sẻ mạch dành cho những bạn mới làm quen với PIC16F887, Board ứng dụng nho nhỏ tích hợp sẵn ds1307+SHT11+UART+LCD16x2, mạch này thích hợp để ủi ^^
Gồm schematic+layout (orcad)+firmware
Main Code:
#include <htc.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd.h"
#include "rtc.h"
#include "i2c.h"
#include "uart.h"
#include "SHT.h"
__CONFIG(HS & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
void putch(char c)
{
uart_putc(c);
}
char buf[50];
unsigned char s,m,h,day,date,month,year;
void ftoa(float value, char *string)
{
if (value < 0) {
*string++ = '-';
value = -value;
}
sprintf(string, (far char *)"%d.%02u",
(int) value,
(int) ((value - (int) value) * 100. + 0.5)); //important
}
unsigned char bcd2dec(unsigned char a)
{
unsigned char r,t;
t = a & 0x0F;
r = t;
a = 0xF0 & a;
t = a >> 4;
t = 0x0F & t;
r = t*10 + r;
return r;
}
unsigned char dec2bcd(unsigned char a)
{
unsigned char t1, t2;
t1 = a%10;
t1 = t1 & 0x0F;
t2 = a/10;
t2 = t2 << 4;
t2 = 0xF0 & t2;
t1 = t1 | t2;
return t1;
}
void rtc_readdatetime(void)
{
s = rtc_read(0);
m = rtc_read(1);
h = rtc_read(2);
day = rtc_read(3);
date = rtc_read(4);
month = rtc_read(5);
year = rtc_read(6);
switch(bcd2dec(day))
{
case 0:
sprintf(buf,"Mon");
break;
case 1:
sprintf(buf,"Tue");
break;
case 2:
sprintf(buf,"Wed");
break;
case 3:
sprintf(buf,"Thu");
break;
case 4:
sprintf(buf,"Fri");
break;
case 5:
sprintf(buf,"Sat");
break;
case 6:
sprintf(buf,"Sun");
break;
}
lcd_gotoxy(0,0);
sprintf(buf,"%s %02u-%02u-%02u",buf,bcd2dec(date),bcd2dec(month),bcd2dec(year));
lcd_puts(buf);
printf("\n\r%s %02u-%02u-%02u",buf,bcd2dec(date),bcd2dec(month),bcd2dec(year));
printf("\n\rTime: %02u:%02u:%02u",bcd2dec(h),bcd2dec(m),bcd2dec(s));
}
void main()
{
unsigned int a;
char i;
float tem,humi;
ANSEL = 0;
ANSELH = 0;
TRISC0 = 0;
uart_init();
lcd_init();
sht_init(SHT_14_12_BIT);
i2c_init();
// rtc_write(0,dec2bcd(0));
// rtc_write(1,dec2bcd(53));
// rtc_write(2,dec2bcd(13));
// rtc_write(3,dec2bcd(4));
// rtc_write(4,dec2bcd(17));
// rtc_write(5,dec2bcd(1));
// rtc_write(6,dec2bcd(13));
printf("\n\r\n\rEmbedded RealTime System Project\n\r");
rtc_readdatetime();
__delay_ms(90);
a=0;
while(1)
{
a++;
RC0 ^= 1;
s = rtc_read(0);
m = rtc_read(1);
h = rtc_read(2);
lcd_gotoxy(1,0);
sprintf(buf,"Time: %02u:%02u:%02u",bcd2dec(h),bcd2dec(m),bcd2dec(s));
lcd_puts(buf);
if (a==200)
{
sht_readTemHumi(&tem,&humi);
//ftoa(tem,buf); //enable if you use Hi-Tech PRO only
printf("\n\rT: %s",buf);
//ftoa(humi,buf);
//printf(" H: %s\n\r",buf);
a = 0;
}
}
}
Nếu bạn dùng bản Hi-Tech PRO thì hãy uncomment 3 dòng sau nhé{
ftoa(tem,buf);
ftoa(humi,buf);
printf(" H: %s\n\r",buf);
lý do là vì bản lite không có optimize code nên sẽ không đủ bộ nhớ cho PIC16F887