We are trying to program a PIC12F675. Our project the control of the comfort of a greenhouse.
Inputs: - temperature sensor (voltage proportional to temperature, analog input)
- humidity sensor (two states 1 or 0)
Outputs: opening windows, irrigation, resistance, ventilator
If someone could give it a look and tell us what's wrong we'll be really pleased! We think the error could be that there is some missing register or something like that. Thanks!
Source code:
#include "DEFINITIU.h"
//***************************************************************************
//Main() - Rutina principal
//***************************************************************************
int c;
int TEMPERATURA;
void main(void)
{
Init();
while(1)
{
c=0;
sensor_T();
if ( TEMPERATURA < 128 )
{
encendre_resistencia; //swich on resistance
}
apagar_resistencia;
if ( TEMPERATURA > 128 )
{
while (c < 3)
{
obrir_ventilador; //swich on ventilator
c=c+1;
}
obrir_finestres; //open windows
}
apagar_ventilador; // swich off ventilator
tancar_finestres; //close windows
}
if (SHUM==FALSE) // there's no humidity
{
REG==ON; //swich on irrigation
}
else
{
GPIO2==OFF; //swich off irrigation
}
}
// Initialization Routine //
void Init()
{
TRISIO = 0b11000010;
GPIO = 0b00000101;
ANSEL = 0b00010001;
ADCON0 = 0b00000001;
return;
}
// Temperature sensor function //
void sensor_T(void)
{
GODONE = 1; // Swich on A/D converter
while(GODONE==1) // waits until A/D coneverter is done (GODONE=0)
{
//It does nothing
}
TEMPERATURA = ADRESH;
}
//////////////////////////////////////////////
// OUTPUTS FUNCTIONS //
/////////////////////////////////////////////
// Obrir el ventilador //
void obrir_ventilador(void)
{
GPIO4==1;
}
// Tancar el ventilador //
void apagar_ventilador (void)
{
GPIO4==0;
}
// Encendre la resistència //
void encendre_resistencia(void)
{
GPIO5==1;
}
// Apagar la resistència //
void apagar_resistencia(void)
{
GPIO5==0;
}
// Obrir les finestres //
void obrir_finestres(void)
{
GPIO3==1;
}
// Tancar les finestres //
void tancar_finestres (void)
{
GPIO3==0;
}
Header code:
#include <pic.h>
__CONFIG(UNPROTECT & BOREN & MCLRDIS & PWRTEN & WDTDIS & INTIO);
//Defines
#define ON 1
#define OFF 0
#define TRUE 1
#define FALSE 0
#define SHUM GPIO0
#define REG GPIO2
//Function Prototypes
void Init(void);
void encendre_resistencia(void);
void obrir_ventilador(void);
void obrir_finestres(void);
void apagar_resistencia(void);
void apagar_ventilador (void);
void tancar_finestres (void);
void sensor_T(void);
Inputs: - temperature sensor (voltage proportional to temperature, analog input)
- humidity sensor (two states 1 or 0)
Outputs: opening windows, irrigation, resistance, ventilator
If someone could give it a look and tell us what's wrong we'll be really pleased! We think the error could be that there is some missing register or something like that. Thanks!
Source code:
#include "DEFINITIU.h"
//***************************************************************************
//Main() - Rutina principal
//***************************************************************************
int c;
int TEMPERATURA;
void main(void)
{
Init();
while(1)
{
c=0;
sensor_T();
if ( TEMPERATURA < 128 )
{
encendre_resistencia; //swich on resistance
}
apagar_resistencia;
if ( TEMPERATURA > 128 )
{
while (c < 3)
{
obrir_ventilador; //swich on ventilator
c=c+1;
}
obrir_finestres; //open windows
}
apagar_ventilador; // swich off ventilator
tancar_finestres; //close windows
}
if (SHUM==FALSE) // there's no humidity
{
REG==ON; //swich on irrigation
}
else
{
GPIO2==OFF; //swich off irrigation
}
}
// Initialization Routine //
void Init()
{
TRISIO = 0b11000010;
GPIO = 0b00000101;
ANSEL = 0b00010001;
ADCON0 = 0b00000001;
return;
}
// Temperature sensor function //
void sensor_T(void)
{
GODONE = 1; // Swich on A/D converter
while(GODONE==1) // waits until A/D coneverter is done (GODONE=0)
{
//It does nothing
}
TEMPERATURA = ADRESH;
}
//////////////////////////////////////////////
// OUTPUTS FUNCTIONS //
/////////////////////////////////////////////
// Obrir el ventilador //
void obrir_ventilador(void)
{
GPIO4==1;
}
// Tancar el ventilador //
void apagar_ventilador (void)
{
GPIO4==0;
}
// Encendre la resistència //
void encendre_resistencia(void)
{
GPIO5==1;
}
// Apagar la resistència //
void apagar_resistencia(void)
{
GPIO5==0;
}
// Obrir les finestres //
void obrir_finestres(void)
{
GPIO3==1;
}
// Tancar les finestres //
void tancar_finestres (void)
{
GPIO3==0;
}
Header code:
#include <pic.h>
__CONFIG(UNPROTECT & BOREN & MCLRDIS & PWRTEN & WDTDIS & INTIO);
//Defines
#define ON 1
#define OFF 0
#define TRUE 1
#define FALSE 0
#define SHUM GPIO0
#define REG GPIO2
//Function Prototypes
void Init(void);
void encendre_resistencia(void);
void obrir_ventilador(void);
void obrir_finestres(void);
void apagar_resistencia(void);
void apagar_ventilador (void);
void tancar_finestres (void);
void sensor_T(void);