„Sziasztok!

Én mikroc-ben szeretnék programozni egy pic16f877-est az alábbi kondíciókkal:

PIC16F877
4Mhz

5db DS18B20 hőmérő
5db digitális bemenet úszókapcsolók jeleinek érzékelésére
4db digitális relés kimenet

Az összeépített vezérlőnek hőmérsékletet, valamint úszókapcsolókat kell figyelnie, melyeket LCD-re is ki kell írnia. Az LCD-vel egyidejűleg a soros portra is be kell küldenie, mért hőmérsékleti adatokat, valamint a portról fogadnia kell bekapcsolási parancsot a relék meghúzásához.
A mikroc helpje alapján összeraktam a kódot,de most tanácstalan vagyok hogy miért nem küldi megfelelően az adatokat.(lásd csatolt kép)

A kód maga így néz ki,de sajnos nem működik helyesen:

// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections

bit oldstate1;
bit oldstate2;
bit oldstate3;
bit oldstate4;
bit oldstate5;
bit oldstate6;
bit oldstate7;
bit oldstate8;
bit oldstate9;
bit oldstate10;
bit oldstate11;
bit oldstate12;

char receive;

// 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; //user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively.

char *text1 = "000.0000";
unsigned temp1;

char *text2 = "000.0000";
unsigned temp2;

char *text3 = "000.0000";
unsigned temp3;

char *text4 = "000.0000";
unsigned temp4;

char *text5 = "000.0000";
unsigned temp5;

void Display_Temperature1(unsigned int temp2write1) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole1;
unsigned int temp_fraction1;

// Check if temperature is negative
if (temp2write1 & 0x8000) {
text1[0] = '-';
temp2write1 = ~temp2write1 + 1;
}

// Extract temp_whole
temp_whole1 = temp2write1 >> RES_SHIFT ;

// Convert temp_whole to characters
if (temp_whole1/100)
text1[0] = temp_whole1/100 + 48;
else
text1[0] = '0';

text1[1] = (temp_whole1/10)%10 + 48; // Extract tens digit
text1[2] = temp_whole1%10 + 48; // Extract ones digit

// Extract temp_fraction and convert it to unsigned int
temp_fraction1 = temp2write1 << (4-RES_SHIFT);
temp_fraction1 &= 0x000F;
temp_fraction1 *= 625;

// Convert temp_fraction to characters
text1[4] = temp_fraction1/1000 + 48; // Extract thousands digit
text1[5] = (temp_fraction1/100)%10 + 48; // Extract hundreds digit
text1[6] = (temp_fraction1/10)%10 + 48; // Extract tens digit
text1[7] = temp_fraction1%10 + 48; // Extract ones digit

UART1_Write_Text ("1.TEMPERATURE: ");
UART1_Write_Text (text1);
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)

Lcd_Out(1, 1, "1.TEMPERATURE: "); // Print temperature on LCD
Lcd_Chr(2,13,223); // Different LCD displays have different char code for degree
Lcd_Chr(2,14,'C'); // Print degree character, 'C' for Centigrades
Lcd_Out(2, 5, text1);
Delay_ms(2000);
}

void Display_Temperature2(unsigned int temp2write2) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole2;
unsigned int temp_fraction2;

// Check if temperature is negative
if (temp2write2 & 0x8000) {
text2[0] = '-';
temp2write2 = ~temp2write2 + 1;
}

// Extract temp_whole
temp_whole2 = temp2write2 >> RES_SHIFT ;

// Convert temp_whole to characters
if (temp_whole2/100)
text2[0] = temp_whole2/100 + 48;
else
text2[0] = '0';

text2[1] = (temp_whole2/10)%10 + 48; // Extract tens digit
text2[2] = temp_whole2%10 + 48; // Extract ones digit

// Extract temp_fraction and convert it to unsigned int
temp_fraction2 = temp2write2 << (4-RES_SHIFT);
temp_fraction2 &= 0x000F;
temp_fraction2 *= 625;

// Convert temp_fraction to characters
text2[4] = temp_fraction2/1000 + 48; // Extract thousands digit
text2[5] = (temp_fraction2/100)%10 + 48; // Extract hundreds digit
text2[6] = (temp_fraction2/10)%10 + 48; // Extract tens digit
text2[7] = temp_fraction2%10 + 48; // Extract ones digit

UART1_Write_Text ("2.TEMPERATURE:");
UART1_Write_Text (text2);
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)

Lcd_Out(1, 1, "2.TEMPERATURE: "); // Print temperature on LCD
Lcd_Chr(2,13,223); // Different LCD displays have different char code for degree
Lcd_Chr(2,14,'C'); // Print degree character, 'C' for Centigrades
Lcd_Out(2, 5, text2);
Delay_ms(2000);
}

void Display_Temperature3(unsigned int temp2write3) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole3;
unsigned int temp_fraction3;

// Check if temperature is negative
if (temp2write3 & 0x8000) {
text3[0] = '-';
temp2write3 = ~temp2write3 + 1;
}

// Extract temp_whole
temp_whole3 = temp2write3 >> RES_SHIFT ;

// Convert temp_whole to characters
if (temp_whole3/100)
text3[0] = temp_whole3/100 + 48;
else
text3[0] = '0';

text3[1] = (temp_whole3/10)%10 + 48; // Extract tens digit
text3[2] = temp_whole3%10 + 48; // Extract ones digit

// Extract temp_fraction and convert it to unsigned int
temp_fraction3 = temp2write3 << (4-RES_SHIFT);
temp_fraction3 &= 0x000F;
temp_fraction3 *= 625;

// Convert temp_fraction to characters
text3[4] = temp_fraction3/1000 + 48; // Extract thousands digit
text3[5] = (temp_fraction3/100)%10 + 48; // Extract hundreds digit
text3[6] = (temp_fraction3/10)%10 + 48; // Extract tens digit
text3[7] = temp_fraction3%10 + 48; // Extract ones digit

UART1_Write_Text ("3.TEMPERATURE: ");
UART1_Write_Text (text3);
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)

Lcd_Out(1, 1, "3.TEMPERATURE: "); // Print temperature on LCD
Lcd_Chr(2,13,223); // Different LCD displays have different char code for degree
Lcd_Chr(2,14,'C'); // Print degree character, 'C' for Centigrades
Lcd_Out(2, 5, text3);
Delay_ms(2000);
}

void Display_Temperature4(unsigned int temp2write4) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole4;
unsigned int temp_fraction4;

// Check if temperature is negative
if (temp2write4 & 0x8000) {
text4[0] = '-';
temp2write4 = ~temp2write4 + 1;
}

// Extract temp_whole
temp_whole4 = temp2write4 >> RES_SHIFT ;

// Convert temp_whole to characters
if (temp_whole4/100)
text4[0] = temp_whole4/100 + 48;
else
text4[0] = '0';

text4[1] = (temp_whole4/10)%10 + 48; // Extract tens digit
text4[2] = temp_whole4%10 + 48; // Extract ones digit

// Extract temp_fraction and convert it to unsigned int
temp_fraction4 = temp2write4 << (4-RES_SHIFT);
temp_fraction4 &= 0x000F;
temp_fraction4 *= 625;

// Convert temp_fraction to characters
text4[4] = temp_fraction4/1000 + 48; // Extract thousands digit
text4[5] = (temp_fraction4/100)%10 + 48; // Extract hundreds digit
text4[6] = (temp_fraction4/10)%10 + 48; // Extract tens digit
text4[7] = temp_fraction4%10 + 48; // Extract ones digit

UART1_Write_Text ("4.TEMPERATURE: ");
UART1_Write_Text (text4);
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)

Lcd_Out(1, 1, "4.TEMPERATURE: "); // Print temperature on LCD
Lcd_Chr(2,13,223); // Different LCD displays have different char code for degree
Lcd_Chr(2,14,'C'); // Print degree character, 'C' for Centigrades
Lcd_Out(2, 5, text4);
Delay_ms(2000);
}
void Display_Temperature5(unsigned int temp2write5) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole5;
unsigned int temp_fraction5;

// Check if temperature is negative
if (temp2write5 & 0x8000) {
text5[0] = '-';
temp2write5 = ~temp2write5 + 1;
}

// Extract temp_whole
temp_whole5 = temp2write5 >> RES_SHIFT ;

// Convert temp_whole to characters
if (temp_whole5/100)
text5[0] = temp_whole5/100 + 48;
else
text5[0] = '0';

text5[1] = (temp_whole5/10)%10 + 48; // Extract tens digit
text5[2] = temp_whole5%10 + 48; // Extract ones digit

// Extract temp_fraction and convert it to unsigned int
temp_fraction5 = temp2write5 << (4-RES_SHIFT);
temp_fraction5 &= 0x000F;
temp_fraction5 *= 625;

// Convert temp_fraction to characters
text5[4] = temp_fraction5/1000 + 48; // Extract thousands digit
text5[5] = (temp_fraction5/100)%10 + 48; // Extract hundreds digit
text5[6] = (temp_fraction5/10)%10 + 48; // Extract tens digit
text5[7] = temp_fraction5%10 + 48; // Extract ones digit

UART1_Write_Text ("5.TEMPERATURE: ");
UART1_Write_Text (text5);
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)

Lcd_Out(1, 1, "5.TEMPERATURE: "); // Print temperature on LCD
Lcd_Chr(2,13,223); // Different LCD displays have different char code for degree
Lcd_Chr(2,14,'C'); // Print degree character, 'C' for Centigrades
Lcd_Out(2, 5, text5);
Delay_ms(2000);
}
void main() {

UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
UART1_Write_Text("START!"); // Send start message
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)

TRISB.B0 = 0 ; // set PORTB 0 as digital OUTPUT
TRISB.B1 = 0 ; // set PORTB 1 as digital OUTPUT
TRISB.B2 = 0 ; // set PORTB 2 as digital OUTPUT
TRISB.B3 = 0 ; // set PORTB 3 as digital OUTPUT
TRISB.B7 = 0 ; // set PORTB 7 as digital OUTPUT

PORTB.B0 = 0 ; // set PORTB 0 to logical zero
PORTB.B1 = 0 ; // set PORTB 1 to logical zero
PORTB.B2 = 0 ; // set PORTB 2 to logical zero
PORTB.B3 = 0 ; // set PORTB 3 to logical zero
PORTB.B7 = 0 ; // set PORTB 7 to logical zero

TRISB.B4 = 1 ; // set PORTB 4 as digital INPUT
TRISB.B5 = 1 ; // set PORTB 5 as digital INPUT
TRISB.B6 = 1 ; // set PORTB 6 as digital INPUT
TRISB.B7 = 1 ; // set PORTB 7 as digital INPUT
TRISD.B2 = 1 ; // set PORTD 2 as digital INPUT

TRISC.B0 = 1 ; // set PORTC 0 as digital INPUT
TRISC.B1 = 1 ; // set PORTC 1 as digital INPUT
TRISC.B2 = 1 ; // set PORTC 2 as digital INPUT
TRISC.B3 = 1 ; // set PORTC 3 as digital INPUT
TRISC.B4 = 1 ; // set PORTC 4 as digital INPUT
TRISC.B5 = 1 ; // set PORTC 5 as digital INPUT

oldstate1 = 1;
oldstate2 = 0;
oldstate3 = 1;
oldstate4 = 0;
oldstate5 = 1;
oldstate6 = 0;
oldstate7 = 1;
oldstate8 = 0;
oldstate9 = 0;
oldstate10 = 0;
oldstate11 = 0;
oldstate12 = 0;

ADCON1 = 0x06; // Configure AN pins as digital
delay_ms(500);

Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off

//--- Main loop
do {
//--- Perform temperature reading
Ow_Reset(&PORTB, 4); // Onewire reset signal
Ow_Write(&PORTB, 4, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTB, 4, 0x44); // Issue command CONVERT_T
Delay_us(120);

Ow_Reset(&PORTB, 4);
Ow_Write(&PORTB, 4, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTB, 4, 0xBE); // Issue command READ_SCRATCHPAD

temp1 = Ow_Read(&PORTB, 4);
temp1 = (Ow_Read(&PORTB, 4) << 8) + temp1;

//--- Format and display result on Lcd
Display_Temperature1(temp1);
Delay_ms(2000);

//--- 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

temp2 = Ow_Read(&PORTB, 5);
temp2 = (Ow_Read(&PORTB, 5) << 8) + temp2;

//--- Format and display result on Lcd
Display_Temperature2(temp2);
Delay_ms(2000);

//--- Perform temperature reading
Ow_Reset(&PORTB, 6); // Onewire reset signal
Ow_Write(&PORTB, 6, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTB, 6, 0x44); // Issue command CONVERT_T
Delay_us(120);

Ow_Reset(&PORTB, 6);
Ow_Write(&PORTB, 6, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTB, 6, 0xBE); // Issue command READ_SCRATCHPAD

temp3 = Ow_Read(&PORTB, 6);
temp3 = (Ow_Read(&PORTB, 6) << 8) + temp3;

//--- Format and display result on Lcd
Display_Temperature3(temp3);
Delay_ms(2000);

//--- Perform temperature reading
Ow_Reset(&PORTB, 7); // Onewire reset signal
Ow_Write(&PORTB, 7, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTB, 7, 0x44); // Issue command CONVERT_T
Delay_us(120);

Ow_Reset(&PORTB, 7);
Ow_Write(&PORTB, 7, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTB, 7, 0xBE); // Issue command READ_SCRATCHPAD

temp4 = Ow_Read(&PORTB, 7);
temp4 = (Ow_Read(&PORTB, 7) << 8) + temp4;

//--- Format and display result on Lcd
Display_Temperature4(temp4);
Delay_ms(2000);

//--- Perform temperature reading
Ow_Reset(&PORTD, 2); // Onewire reset signal
Ow_Write(&PORTD, 2, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTD, 2, 0x44); // Issue command CONVERT_T
Delay_us(120);

Ow_Reset(&PORTD, 2);
Ow_Write(&PORTD, 2, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTD, 2, 0xBE); // Issue command READ_SCRATCHPAD

temp5 = Ow_Read(&PORTD, 2);
temp5 = (Ow_Read(&PORTD, 2) << 8) + temp5;

//--- Format and display result on Lcd
Display_Temperature5(temp5);
Delay_ms(2000);

if (UART1_Data_Ready() == 1){
receive = UART1_Read();
switch(receive) {
case 0x41: oldstate1=0; PORTB.B0 = 0x01 ; break;
case 0x42: oldstate1=1; PORTB.B0 = 0x00 ; break;
case 0x43: oldstate3=0; PORTB.B1 = 0x01 ; break;
case 0x44: oldstate3=1; PORTB.B1 = 0x00 ; break;
case 0x45: oldstate5=0; PORTB.B2 = 0x01 ; break;
case 0x46: oldstate5=1; PORTB.B2 = 0x00 ; break;
case 0x47: oldstate7=0; PORTB.B3 = 0x01 ; break;
case 0x48: oldstate7=1; PORTB.B3 = 0x00 ; break;
default:;
}
}

if (oldstate1 && Button(&PORTC, 0, 1, 1)) {

PORTB.B0 = 0x00;
oldstate2 = 1;

}

if (oldstate2 && Button(&PORTC, 0, 1, 0)){

PORTB.B0 = 0x01 ;
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Out(1, 1, "LEVEL_1!");
oldstate2 = 0;
}

if (oldstate3 && Button(&PORTC, 1, 1, 1)) {

PORTB.B1 = 0x00 ;
oldstate4 = 1;

}

if (oldstate4 && Button(&PORTC, 1, 1, 0)){

PORTB.B1 = 0x01 ;
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Out(1, 1, "LEVEL_2!");
oldstate4 = 0;
}

if (oldstate5 && Button(&PORTC, 2, 1, 1)) {

PORTB.B2 = 0x00 ;
oldstate6 = 1;

}

if (oldstate6 && Button(&PORTC, 2, 1, 0)){

PORTB.B2 = 0x01 ;
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Out(1, 1, "LEVEL_3!");
oldstate6 = 0;
}
if (oldstate7 && Button(&PORTC, 3, 1, 1)) {

PORTB.B3 = 0x00 ;
oldstate8 = 1;

}

if (oldstate8 && Button(&PORTC, 3, 1, 0)){

PORTB.B3 = 0x01 ;
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Out(1, 1, "LEVEL_4!");
oldstate8 = 0;
}

if (oldstate9 && Button(&PORTC, 4, 1, 1)) {

PORTB.B7 = 0x00 ;
oldstate10 = 1;

}

if (oldstate10 && Button(&PORTC, 4, 1, 0)){

PORTB.B7 = 0x01 ;
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Out(1, 1, "LEVEL_5!");
oldstate10 = 0;
}

} while (1);
}”