Behöver banneimig hjälp med en sak till.
Kod: Markera allt
// LCD module connections
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
// End LCD module connections
// Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
// 18S20: 9 (default setting; can be 9,10,11,or 12)
// 18B20: 12
const unsigned short TEMP_RESOLUTION = 9;
unsigned char *text = " 00";
unsigned temp,ute,stigare,inne,ovr;
int tt,v =0;
int te;
int jim=0;
char txt[6];
char Display_Temperature(unsigned int temp2write) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;
unsigned int temp_fraction;
// Check if temperature is negative
if (temp2write & 0x8000) {
text[0] = '-';
temp2write = ~temp2write + 1;
}
// Extract temp_whole
temp_whole = temp2write >> RES_SHIFT ;
// Convert temp_whole to characters
if (temp_whole/100)
text[0] = temp_whole/100 + 48;
else
text[0] = ' ';
text[1] = (temp_whole/10)%10 + 48; // Extract tens digit
text[2] = temp_whole%10 + 48; // Extract ones digit
// Extract temp_fraction and convert it to unsigned int
temp_fraction = temp2write << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;
// Convert temp_fraction to characters
// text[4] = "\0"; // Extract thousands digit
//text[5] = (temp_fraction/100)%10 + 48; // Extract hundreds digit
//text[6] = (temp_fraction/10)%10 + 48; // Extract tens digit
//text[7] = temp_fraction%10 + 48; // Extract ones digit
return *text;
}
unsigned read_temp_ute () {
//--- Perform temperature reading
Ow_Reset(&PORTC, 0); // Onewire reset signal
Ow_Write(&PORTC, 0, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTC, 0, 0x44); // Issue command CONVERT_T
Delay_us(120);
Ow_Reset(&PORTC, 0);
Ow_Write(&PORTC, 0, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTC, 0, 0xBE); // Issue command READ_SCRATCHPAD
temp = Ow_Read(&PORTC, 0);
temp = (Ow_Read(&PORTC, 0) << 8) + temp;
return temp;
}
unsigned read_temp_stigare () {
//--- Perform temperature reading
Ow_Reset(&PORTB, 5); // Onewire reset signal
Ow_Write(&PORTB, 5, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTB, 5, 0x44); // Issue command CONVERT_T
Delay_us(120);
Ow_Reset(&PORTB, 5);
Ow_Write(&PORTB, 5, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTB, 5, 0xBE); // Issue command READ_SCRATCHPAD
temp = Ow_Read(&PORTB, 5);
temp = (Ow_Read(&PORTB, 5) << 8) + temp;
return temp;
}
unsigned read_temp_inne () {
//--- Perform temperature reading
Ow_Reset(&PORTA, 2); // Onewire reset signal
Ow_Write(&PORTA, 2, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 2, 0x44); // Issue command CONVERT_T
Delay_us(120);
Ow_Reset(&PORTA, 2);
Ow_Write(&PORTA, 2, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 2, 0xBE); // Issue command READ_SCRATCHPAD
temp = Ow_Read(&PORTA, 2);
temp = (Ow_Read(&PORTA, 2) << 8) + temp;
return temp;
}
unsigned read_temp_ovr() {
//--- Perform temperature reading
Ow_Reset(&PORTC, 1); // Onewire reset signal
Ow_Write(&PORTC, 1, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTC, 1, 0x44); // Issue command CONVERT_T
Delay_us(120);
Ow_Reset(&PORTC, 1);
Ow_Write(&PORTC, 1, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTC, 1, 0xBE); // Issue command READ_SCRATCHPAD
temp = Ow_Read(&PORTC, 1);
temp = (Ow_Read(&PORTC, 1) << 8) + temp;
return temp;
}
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
//TRISB = 0;
TRISB4_bit = 0; // set shunt_2 as output
TRISB7_bit = 0; //set shunt_1 as output
TRISB6_bit = 1; //set button 1 as input
TRISA3_bit = 1; //set button 2 as input
PORTB &= ~(1 << 4);
PORTB &= ~(1 << 7);
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,1,"Loading....:");
//--- Main loop
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(4,1,"Val:");
do {
// Lcd_Cmd(_LCD_CLEAR);
ute=read_temp_ute();
stigare=read_temp_stigare();
inne=read_temp_inne();
ovr=read_temp_ovr();
Lcd_Out(2, 1, "ute");
*text=Display_Temperature(ute);
Lcd_Out(2, 6, ltrim(text));
*text=Display_Temperature(stigare);
Lcd_Out(2, 9, "Stig");
Lcd_Out(2, 14, ltrim(text));
Lcd_Out(3, 1, "inne");
*text=Display_Temperature(inne);
Lcd_Out(3, 6, ltrim(text));
Lcd_Out(3, 9, "ovr");
*text=Display_Temperature(ovr);
Lcd_Out(3, 14, ltrim(text));
Lcd_Out(4,12,"v:");
inttostr(v,txt);
Lcd_Out(4,6,ltrim(txt)); //Output the V-value (ref value)
te = 20 - ((int)ute >> 1) + 10 + v;
//te = 25;
inttostr(te,txt);
Lcd_Out(4,15,ltrim(txt));
// ## Detta är if satserna som slutar att fungera, dvs de säger minska oavsett ##
if ( (int)stigare < (te << 1) ) {
Lcd_Out(1,1,"Oka ");
PORTB |= 1 << 7;
delay_ms(50);
PORTB &= ~(1 << 7);
} else if ( (int)stigare > (te << 1) ){
Lcd_Out(1,1,"Minska");
PORTB |= 1 << 4;
delay_ms(50);
PORTB &= ~(1 << 4);
} else {
// Do nothing since we got correct temperatur
Lcd_Out(1,1,"Lika ");
}
// ## Detta är for satsen jag snackar om ##
//for ( jim=0;jim < 2;jim++)
//{
if (Button(&PORTB, 6, 1, 0)) { // Detect logical one
v--;
delay_ms(300);
}
if (Button(&PORTA, 3, 1, 0)) { // Detect logical one
v++;
delay_ms(300);
}
// inttostr(v,txt);
// Lcd_Out(4,6,ltrim(txt));
delay_ms(50);
//}
Delay_ms(300);
} while (1);
}
Fenomenet är som sådant att använder jag mig av for loopen som har variablen jim i sig så funar inte if satserna före. Dvs temperaturen vill hela tiden minska och inte öka. Tar jag bort for loopen som den är nu dvs så fungerar if satserna korrekt. Hur kan den påverka if-satserna?