Xem lý thuyết về LM35 bên PIC16F877A nha !
- Đây là ảnh mô phỏng protues.
- Đây là code chương trình.
/*
* File: LM35.c
* Author: Dell
* my blog http://xcvn.blogspot.com
* Created on November 21, 2015, 10:10 AM
*/
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
#include <xc.h>
#include "LCD.h"
// CONFIG1
#pragma config FOSC = XT // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
void ADCinit (void);
unsigned int ReadADC (void);
unsigned int ADCvalue = 0,tram = 0, chuc = 0, donvi = 0;
unsigned int Temp = 0,K;
char s[30];
void main(void)
{
TRISD = 0X00;
PORTD = 0;
ADCinit();
Lcd_Init();
Lcd_Set_Cursor(1,4);
Lcd_Write_String("DEMO LM35");
Lcd_Set_Cursor(2,3);
Lcd_Write_String("BY LAM 3 NGON");
__delay_ms(1000);
Lcd_Clear();
while(1)
{
ADCvalue = ReadADC();
Temp = (5000.0f/1023*ADCvalue)/10;
// cong thuc doi sang do F = Temp*1.8 + 32;
sprintf(s,"Temp =%3u",Temp);
Lcd_Set_Cursor(1,1);
Lcd_Write_String(s);
Lcd_Set_Cursor(1,10);
Lcd_Write_Char(223);
Lcd_Set_Cursor(1,11);
Lcd_Write_String("C");
}
}
void ADCinit (void)// adc
{
// chon tan so clock cho bo adc
ADCON0bits.ADCS1 = 0, ADCON0bits.ADCS0 = 1;
// chon kenh ADC
ADCON0bits.CHS0 = 0, ADCON0bits.CHS1 = 0, ADCON0bits.CHS2 = 0, ADCON0bits.CHS3 = 0;
// chon cach luu data
ADCON1bits.ADFM = 1;
// cap nguon cho khoi adc
ADCON0bits.ADON = 1;
}
unsigned int ReadADC (void)// doc len 7 doan
{
unsigned int TempValue = 0;
ADCON0bits.GO_nDONE = 1;
while(ADCON0bits.GO_nDONE);
TempValue = ADRESH*256 + ADRESL;
return (TempValue);
}
- Link download project Click here* File: LM35.c
* Author: Dell
* my blog http://xcvn.blogspot.com
* Created on November 21, 2015, 10:10 AM
*/
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
#include <xc.h>
#include "LCD.h"
// CONFIG1
#pragma config FOSC = XT // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
void ADCinit (void);
unsigned int ReadADC (void);
unsigned int ADCvalue = 0,tram = 0, chuc = 0, donvi = 0;
unsigned int Temp = 0,K;
char s[30];
void main(void)
{
TRISD = 0X00;
PORTD = 0;
ADCinit();
Lcd_Init();
Lcd_Set_Cursor(1,4);
Lcd_Write_String("DEMO LM35");
Lcd_Set_Cursor(2,3);
Lcd_Write_String("BY LAM 3 NGON");
__delay_ms(1000);
Lcd_Clear();
while(1)
{
ADCvalue = ReadADC();
Temp = (5000.0f/1023*ADCvalue)/10;
// cong thuc doi sang do F = Temp*1.8 + 32;
sprintf(s,"Temp =%3u",Temp);
Lcd_Set_Cursor(1,1);
Lcd_Write_String(s);
Lcd_Set_Cursor(1,10);
Lcd_Write_Char(223);
Lcd_Set_Cursor(1,11);
Lcd_Write_String("C");
}
}
void ADCinit (void)// adc
{
// chon tan so clock cho bo adc
ADCON0bits.ADCS1 = 0, ADCON0bits.ADCS0 = 1;
// chon kenh ADC
ADCON0bits.CHS0 = 0, ADCON0bits.CHS1 = 0, ADCON0bits.CHS2 = 0, ADCON0bits.CHS3 = 0;
// chon cach luu data
ADCON1bits.ADFM = 1;
// cap nguon cho khoi adc
ADCON0bits.ADON = 1;
}
unsigned int ReadADC (void)// doc len 7 doan
{
unsigned int TempValue = 0;
ADCON0bits.GO_nDONE = 1;
while(ADCON0bits.GO_nDONE);
TempValue = ADRESH*256 + ADRESL;
return (TempValue);
}
anh ơi,h em sử dụng con cảm biến độ ẩm HS1101 THÌ CÔNG THỨC trên tính có đúng không ak,em chuyển tín hiệu tương tự ra số và từ số vào chân ngõ vào analog sao ak?em ko rõ lắm.
ReplyDeleteKhông e, mỗi con cảm biến có mỗi công thức tính khác nhau nha !
Deletewow tuyệt vời, cảm ơn bạn nhiều luôn
ReplyDeletehạt điều sấy mè trắng