我在這裏有一個新問題。 我仍然在學習用於PIC(xc8編譯器)的C語言,並且作爲一個初學者項目,我正在做一個帶有廣受歡迎的ds18b20和pic16f628的溫度計。 我的程序在允許運行時表現得很好,但是當我玩指針,結構體,數組等等時,我在一個函數中返回了多個值,我注意到有些東西變得不合時宜,現在PC來回移動,不允許程序順序運行,至少這就是我看到如果我在mplabx中使用模擬器。我很確定我忘記了有關程序和/或內存位置的信息,但我無法弄清楚什麼或爲什麼。有人能幫我嗎?我在這裏粘貼主代碼,你還需要什麼?C爲16f628,程序計數器出現故障
/*
* File: termometro.c
* Author: zakkos
* Created on April 18, 2013, 2:20 PM
*
*/
/*ESSENTIAL DEFINITIONS*/
#define _XTAL_FREQ 4000000
/*INCLUSIONS*/
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <lcd.h>
#include <1-wire.h>
/*CONFIG PRAGMA*/
#pragma config BOREN = OFF, CPD = OFF, FOSC = INTOSCIO, MCLRE = OFF, WDTE = OFF, CP = OFF, LVP = OFF, PWRTE = ON
//typedef unsigned char uint8_t;
void read_temp(void);
union {
char eratura;
char decimali;
}temps;
int main(void) {
INTCON = 0x00;
PIE1 = 0x00;
CMCON = 0x07; //disabilito i comparatori - disable comparators
TRISA = 0x00;
PORTA = 0x00;
TRISB = 0x00;
PORTB = 0x00;
const char decims[16] = {0, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9};
char temp;
lcd_init();
lcd_send_cmd(LCD_CLR);
lcd_send_cmd(LCD_HOME);
writeString("Hello,");
lcd_send_cmd(LCD_LN2);
writeString("World!");
__delay_ms(1000);
while(1)
{
read_temp();
lcd_send_cmd(LCD_CLR);
lcd_send_cmd(LCD_HOME);
writeString("Temp:");
lcd_send_cmd(LCD_LN2);
if((temps.eratura & 0x80)){ //if sign bit is set
temps.eratura = ~temps.eratura; //2's complement
temps.eratura += 1;
temps.decimali = ~temps.decimali; //2's complement
temps.decimali += 1;
lcd_send_dat(0x2D); //minus
}
temp = (temps.eratura/100)& 0x0F; //centinaia 157/100=1 (hundreds)
if(temp){
lcd_send_dat(0x30 | temp);
temp = ((temps.eratura/10)%10) & 0x0F; //decine 157/10=15%10=5 (tens if hundreds is set, meaning it will display also a 0)
lcd_send_dat(0x30 | temp);
} else {
temp = ((temps.eratura/10)%10) & 0x0F; //decine 157/10=15%10=5 (tens if hundreds is no set, meaning it will not display if 0)
if(temp){lcd_send_dat(0x30 | temp);
}
}
lcd_send_dat(0x30 | (temps.eratura%10)& 0x0F); //unita 157%10=7 (ones)
lcd_send_dat(0x2E); //dot
lcd_send_dat(0x30 | decims[temps.decimali] & 0x0F); //decimals
lcd_send_dat(0xDF); //degrees
}
}
void read_temp(void){
char scratchpad[9];
while(ow_reset());
ow_write_byte(0xCC);
ow_write_byte(0x44);
while(ow_read_bit()==0);
__delay_ms(1);
while(ow_reset());
ow_write_byte(0xCC);
ow_write_byte(0xBE);
for(char k=0;k<10;k++){
scratchpad[k] = ow_read_byte();
}
temps.decimali = scratchpad[0] & 0x0F;
temps.eratura = (scratchpad[1] << 4)|(scratchpad[0] >> 4);
return;
}
不確定是否stackoverflow是您發佈的最佳位置。您可能有更多的機會在這裏:http://electronics.stackexchange.com/ – joce 2013-04-30 20:45:11
謝謝,我會試試看! – zakkos 2013-04-30 20:50:47
也嘗試http://www.microchip.com/forums/,雖然這裏有一些有希望的答案。 – 2013-04-30 20:55:00