Thứ Hai, 28 tháng 3, 2016

BÀI 28 : ĐO NHIỆT ĐỘ LM335 HIỂN THỊ LED 7 ĐOẠN PIC16F887 XC8

Xem lý thuyết về LM335 bên PIC16F877A nha !
- Đây là ảnh mô phỏng protues.
đo nhiệt độ lm335 hiển thị led 7 đoạn pic16f887

- Đây là code chương trình.
/*
 * File:   335_7seg.c
 * Author: Lam 3 Ngon
 * My Blog http://xcvn.blogspot.com
 * Created on March 27, 2016, 7:31 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#define _XTAL_FREQ 4000000 // if 8Mhz is HS
// CONFIG1
#pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator: 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)
const unsigned char maled7[] = {0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
void ADCinit (void);
unsigned int ReadADC (void);
unsigned int ADCvalue = 0;
unsigned int Temp;
unsigned int ones = 0,tens = 0;
void main(void)
{
TRISC = 0X00;
TRISD = 0X00;
ADCinit();
while(1)
{
__delay_us(100);
ADCvalue = ReadADC();
Temp = ((5000.0f / 1023 * ADCvalue) / 10)- 273;

ones=Temp%10;
tens=Temp/10;

PORTC = maled7[tens];
RD0=1;
__delay_ms(5);
RD0=0;

PORTC = maled7[ones];
RD1=1;
__delay_ms(5);
RD1=0;

PORTC = 0X9C;
RD2=1;
__delay_ms(5);
RD2=0;

PORTC = 0XC6;
RD3=1;
__delay_ms(5);
RD3=0;

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


EmoticonEmoticon