Hi everyone, should hopefully be a quick one!!
Below is some code for a thermostat, pay attention to the lower half. Essentially I am looking to have a thermostat with 3 set temperatures to choose from (22, 30 and 40), with 22C being the default option until one is selected.
However, when the circuit is active, it boots displaying the SETTEMP (user chosen) to 40C, even though none of the switches have been pressed. Pressing the other two switches has no effect. I am at a loss at to how to adapt the code to get it working effectively.. Anyone have any ideas? In particular the "if" statements...
Code:
#include <pic.h>
#include <string.h>
#include <stdio.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS);
int i, j, k, n, z, p; // Use Global Variables for Debug
int TEMP = 0;
int SETTEMP;
char TopMessage[17];
char BottomMessage[17];
#define E RC4 // Define the LCD Control Pins
#define RS RC5
const int thirtythreems = 20000;
const int Fourtyms = 2500;
const int Twentyms = 1150; // Declare a Constant for 20 ms Delay
const int Fivems = 300;
const int TwoHundredus = 10;
LCDWrite(int LCDData, int RSValue)
{
PORTC = (LCDData >> 4) & 0x0F; // Get High 4 Bits for Output
RS = RSValue;
E = 1; E = 0; // Toggle the High 4 Bits Out
PORTC = LCDData & 0x0F; // Get Low 4 Bits for Output
RS = RSValue;
E = 1; E = 0; // Toggle the Low 4 Bits Out
if ((0 == (LCDData & 0xFC)) && (0 == RSValue))
n = Fivems; // Set Delay Interval
else
n = TwoHundredus;
for (k = 0; k < n; k++); // Delay for Character
} // End LCDWrite
LCDReadout()
{
for (i = 0; TopMessage[i] != 0; i++)
LCDWrite(TopMessage[i], 1);
LCDWrite(0b11000000, 0); // move curosr to second line
for (i = 0; BottomMessage[i] != 0; i++)
LCDWrite(BottomMessage[i], 1);
}
Delay()
{
for (i =0; i < z; i++); // wait for 0.32sec
for (i =0; i < z; i++); // wait for 0.32sec for (i =0; i < z; i++);
for (i =0; i < z; i++); // wait for 0.32sec
for (i =0; i < z; i++); // wait for 0.32sec
for (i =0; i < z; i++);
}
int main(void)
{
PORTC = 0; // Start with Everything Low
PORTA = 0;
CMCON0 = 7; // Turn off Comparators
ANSEL = 0b0000001; // Set RA0 as analogue input
TRISA= 0b00001111; // Port A as inputs- RA4 AND RA3 is output
TRISC = 0; // All of PORTC are Outputs
ADCON0= 0b00000001; //* Result left justified format, voltage reference= VDD,
//AN2= analogue input, initiates ADC and waiting to start conversion.
ADCON1= 0b00010000; //For 4 Mhz, 8Tosc= 2 uS, which > 1.6 uS, the minimum requirement.
// Initialise LCD
j = Twentyms;
for (i = 0; i < j; i++); // Wait for LCD to Power Up
PORTC = 3; // Start Initialization Process
E = 1; E = 0; // Send Reset Command
j = Fivems;
for (i = 0; i < j; i++);
E = 1; E = 0; // Repeat Reset Command
j = TwoHundredus;
for (i = 0; i < j; i++);
E = 1; E = 0; // Repeat Reset Command Third Time
j = TwoHundredus;
for (i = 0; i < j; i++);
PORTC = 2; // Initialize LCD 4 Bit Mode
E = 1; E = 0;
j = TwoHundredus;
for (i = 0; i < j; i++);
LCDWrite(0b00101000, 0); // LCD is 4 Bit I/F, 2 Line
LCDWrite(0b00000001, 0); // Clear LCD
LCDWrite(0b00000110, 0); // Move Cursor After Each Character
LCDWrite(0b00001110, 0); // Turn On LCD and Enable Cursor
z = thirtythreems;
p = 0;
SETTEMP=22;
while (1==1)
{
if (RA1==1);
{ SETTEMP=22;
for (i =0; i < z; i++);
}
if (RA2==1);
{ SETTEMP=30;
for (i =0; i < z; i++);
}
if (RA3==1);
{ SETTEMP=40;
for (i =0; i < z; i++);
}
// Turn off LED
GODONE = 1;
TEMP = ADRESH;
LCDWrite (0b00000001, 0);
sprintf (TopMessage, " Temp = %dC", TEMP); // Display measured temp
sprintf (BottomMessage, " Tgt. Temp = %dC", SETTEMP); // Display ideal temp
LCDReadout();
for (i =0; i < z; i++); // wait for 0.32sec
if (TEMP > SETTEMP)
{
RA4 = 0;
}
else
{
RA4 = 1;
}
}
}