Programa de adquisición y control de temperatura y humedad basado en microcontrolador (lenguaje C)
¡Te ofrecemos un programa de adquisición de temperatura para DS18B20!
//Creador de aplicaciones ICC-AVR: 2009-10-25 10:43:39
// Objetivo: M16
// Cristal: 16.000Mhz
#include lt;iom16v.hgt;
#include lt;macros.hgt;
#define uint unsigned int
# definir uchar unsigned char
#include "xianshi.c"
#include "delay.h"
#define CLR_DIR_1WIRE DDRDamp;=~BIT(4) //Simplemente modifica los parámetros aquí
#define SET_DIR_1WIRE DDRD|=BIT(4) //¡No se necesita nada en él!
#define CLR_OP_1WIRE PORTDamp;=~BIT(4)
#define SET_OP_1WIRE PORTD|=BIT(4)
#define CHECK_IP_1WIRE (PINDamp;0x10) // Detectar
unsigned char wmh, wml;
void init_1820()
{
SET_DIR_1WIRE //Establecer PD4 como salida;
SET_OP_1WIRE;
CLR_OP_1WIRE;
delay_nus(480); //Por encima de 480us
SET_OP_1WIRE; CLR_DIR_1WIRE;
delay_nus(20); //15~60us
while(CHECK_IP_1WIRE);
SET_DIR_1WIRE; /p>
delay_nus(140); //60~240us
}
void write_1820(carácter x sin firmar)
{
encanto sin firmar
for(m=0;mlt;8;m)
{
CLR_OP_1WIRE
if(xamp;(1lt;lt;m)) //Al escribir datos, ¡escriba primero los bits bajos!
SET_OP_1WIRE;
else
{CLR_OP_1WIRE;}
delay_nus(40); //15~60us
SET_OP_1CABLE
p>
}
SET_OP_1WIRE;
}
carácter sin firmar read_1820()
{
temperatura de carácter sin firmar, k, n;
temp=0;
for(n=0;nlt;8;n)
{
CLR_OP_1WIRE;
SET_OP_1WIRE;
CLR_DIR_1WIRE;
k=(CHECK_IP_1WIRE); //Leer datos, comenzando desde el bit bajo
if(k)
temp|=(1lt;lt;n);
else
tempamp;=~(1lt;lt;n);
retardo_nus(50); //60~120us
SET_DIR_1WIRE
}
retorno (temperatura); p>
}
unsigned int gettemp() //Leer valor de temperatura
{
unsigned char temh, teml, wm0, wm1, wm2, wm3 ;
init_1820(); //Restablecer 18b20
write_1820(0xcc); //Emitir comando de conversión
write_1820(0x44);
// delay_nms(800); //Funciona bien sin demora, ¡no sé qué está pasando!
init_1820();
write_1820(0xcc); //Emitir comando de lectura
write_1820(0xbe); (); //Leer datos
temh=read_1820();
wm0=temlgt;gt;4; //Siempre que los 8 bits altos, los cuatro bits bajos y los 8 bits bajos Los cuatro dígitos superiores, el rango de temperatura es 0 ~ 99.
wm1=temhlt;lt;4;
wm2=wm1 wm0; //Convierte hexadecimal a decimal
return wm2;
}
void main()
{
uint tem, ad[4], i
port_init() ; p>
while(1)
{
tem = gettemp();
for(i=0; ilt; 4; i )
{
anuncio[3-i]=tem10;
tem=tem/10;
}
for(i=0;ilt;4;i)
{
show1(ad[i], i);
retardo(5);
}
}
}
Función de visualización:
#include lt;iom16v.hgt;
#include lt;macros.hgt;
#define uint unsigned int
#define uchar unsigned char
#pragma datos: código
const uint tab1[]={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E, 0x00}; //*** Tabla de códigos de tubos digitales Yang
const uint tab2[]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d , 0x07, 0x7f,
0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0X00} //*** Tabla de códigos de tubos digitales Yin
void port_init(void )
{
DDRA = 0xFF
DDRB = 0xFF
DDRC = 0xFF; = 0x00;
}
retraso nulo (uint ms)
{
uint i,j;
for(i=0;ilt;ms;i)
{
for(j=0;jlt;1141;j);
}
}
void show1(uchar j, uchar k)//Función de visualización
{
PORTB = ~BIT(k);
PORTA = tab2[j];
retraso(1);
}
void show(uint ada)
{
uint i, anuncio[4];
for(i=0; ilt; 4; i )
{
anuncio[3-i]=ada10;
ada = ada/10
}
mientras(1)
{
for(i=0;ilt;4;i)
{
show1((ad[i]),i);
delay(100);
}
}
}
He depurado todos los programas.
Sí, por favor pregúntame de nuevo. ¿Puedo dar puntos? Jeje