Nu vill jag kunna trycka ner knappar för att ställa in en önskad temperatur. Första steget är att få knapparna att fungera... Knapparna är kopplade till RA1 och RA2 som på denna bild:

fast med Vin och Ground bytt så att jag för 5V när jag trycker ner knappen. Resistorerna är på 10K. Knapparna fungerar om jag kopplar in en LED men inte när jag försöker läsa dem med PIC:en. Det kan vara något fel med hur jag sätter bitarna som styr ADC:n... Jag har sökt runt massor på nätet men inte hittat något som löst mitt problem.
Har ni några idéer?
Kod: Markera allt
#include <p18f4550.h>
#include <delays.h>
#pragma config FOSC = HS // High-speed oscillator
#pragma config WDT = OFF // Watchdog timer off
typedef unsigned char uint8_t;
unsigned int adc_in; // Stores the ADC value
unsigned int pe_temp; // Peliter element temperature
int digit; // Digit to display on LED-segment
int digit_res; // The rest of the ADC value when digit has been subtracted
unsigned int i; // used in a for loop
uint8_t n; // the element to lookup in adc_to_t[64]
static const uint8_t seg_disp[10] = {
0b11101110,
0b01100000,
0b11001101,
0b11101001,
0b01100011,
0b10101011,
0b10101111,
0b11100000,
0b11101111,
0b11100011}; // 10 digits: 0-9
static const uint8_t seg_nbr[4] = {
0b00000000, // Display off
0b00000001, // 3rd digit
0b00000010, // 2nd digit
0b00000100}; // 1st digit or minus
static const int adc_to_t[64] = { // ADC to temperature for 6 MSBs
-7047, -7047, -6307, -5841, -5492, -5208, -4966, -4754,
-4564, -4390, -4230, -4080, -3939, -3805, -3677, -3554,
-3436, -3321, -3209, -3101, -2994, -2890, -2787, -2685,
-2585, -2485, -2386, -2288, -2190, -2091, -1993, -1894,
-1807, -6306, -1593, -1491, -1387, -1282, -1174, -1065,
-952, -837, -719, -597, -471, -340, -204, -61,
88, 246, 413, 591, 782, 989, 1215, 1464,
1742, 2059, 2426, 2865, 3410, 4130, 5187, 7133};
void main(){
TRISD = 0; // Set port D to output
TRISE = 0; // Set port E to output
LATD = seg_disp[0]; // Set the 7-segment display to 0 initially
LATE = seg_nbr[0]; // Display off
PORTA = 0b00000110; // Clear port A to prevent if from affecting TRISA
ADCON1 = 0b00001110; // VSS, VDD ref. AN0 analogue
TRISA = 0b00000111; // Set port A bit 0-2 to inputs
ADCON0 = 0b00000001; // channel 0 as input
ADCON2 = 0b10001000; // ADCON2 setup: Right justified, Tacq=2Tad, Tad=2*Tosc (or Fosc/s)
pe_temp = 633; // Initially set the desired temperature to -10 C
while(1){
if(PORTAbits.RA1 == 1) Delay10KTCYx(20); // Delay 10 ms if key is pressed
if(PORTAbits.RA1 == 1) adc_in = pe_temp; // If key is still pressed display the default peltier temperature
else{ // read thermistor
ADCON0bits.GO_DONE = 1; // Start A/D Conversion
while(ADCON0bits.GO_DONE != 0); // Just loop until A/D conversion is done
adc_in = ADRES; // Take all the 10 bits of the adc
}
for(i = 0; i < 35535; i++){ // Flash LEDs for a while
n = adc_in/16; // Lookup table index
digit_res = adc_to_t[n]/100; // temperature from table
digit_res += ((adc_in - (int)n*16)*(adc_to_t[n+1] - adc_to_t[n]))/1600; // Temperature correction by interpolation
if(digit_res > 0){ // If temperature above 0 C
digit = digit_res/100;
digit_res = digit_res - digit*100;
LATD = seg_disp[digit];
}
else{ // If temperature below 0 C
digit = - digit_res/100;
digit_res = - digit_res - digit*100;
LATD = 0b00000001; // Set a minus sign
}
LATE = seg_nbr[3]; // Flash digit or minus sign
Delay10KTCYx(1);
digit = (unsigned int)digit_res/10;
digit_res = digit_res - digit*10;
LATD = seg_disp[digit];
LATE = seg_nbr[2];
Delay10KTCYx(1);
LATD = seg_disp[digit_res];
LATE = seg_nbr[1];
Delay10KTCYx(1);
}
}
}