16f628的C,程序计数器失控

jexiocij  于 2023-08-03  发布在  其他
关注(0)|答案(4)|浏览(73)

我有个新问题我还在学习PIC(xc 8编译器)的C语言,作为一个初学者,我正在用流行的ds18b20和我身边的pic 16 f628做一个温度计。我的程序在允许运行的情况下确实表现良好,但当我在玩指针,结构,数组等时。在一个函数中返回多个值,我注意到有些东西出了问题,现在PC来回运行,不允许程序顺序运行,至少这是我在mplabx中使用模拟器时看到的。我很确定我忘记了一些关于程序和/或内存位置的东西,但我不知道是什么或为什么。有人能帮帮我吗我把主代码贴在这里,你还需要什么?

/*
 * 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;
}

字符串

0ve6wy6x

0ve6wy6x1#

for(char k=0;k<10;k++){
    scratchpad[k] = ow_read_byte();
}

字符串
...将从0-9(10个字符)运行,而...

char scratchpad[9];


...仅为9保留空间。这可能会覆盖堆栈(即返回地址)

s4n0splo

s4n0splo2#

这一点:

temps.eratura = ~temps.eratura;                 //2's complement
        temps.eratura += 1;                             
        temps.decimali = ~temps.decimali;               //2's complement
        temps.decimali += 1;

字符串
是一个很大的NOP,因为temps是union而不是struct。你想干什么?为什么不呢:
temps.eratura = -temps.eratura;
也许你的意思是union的第二个成员是int?在这种情况下,它仍然会失败,但在read_temp中使用它会更有意义一些
正如其他人所提到的,您正在访问一个9字符数组的10个字符。
基于评论的更多信息:
你肯定想用一个结构来表示你的临时值,因为你想在内存中有两个不同的值。而且,虽然我不确定您的编译器允许什么,

if(temps.eratura < 0){              
            temps.eratura = -temps.eratura;
            temps.decimali = -temps.decimali;
            lcd_send_dat('-'); 
    }


看起来有点直截了当--让编译器为您处理2s恭维。
下一页:

temp = (temps.eratura/100)& 0x0F;


的工作空间非常小,因为该值最多只能达到128。这基本上是将temps.eratura小于100时的temp设置为0,大于100时的temp设置为1。这里不需要&。啊,你在发送数字。好的,我会的

temp = temps.eratura;

if(temp >= 100)
{
    temp -= 100;  
    lcd_send_dat('1');
}

if(temps.eratura >= 10)
{
    lcd_send_dat('0' + (temp / 10));
}

lcd_send_dat('0' + (temp % 10));


那么对于你的小数:

const char decims[16] =
   {'0', '0', '1', '1', '2', '3', '3', '4', '5', '5', '6', '6', '7', '8', '8', '9'};


和/或

lcd_send_dat('.');  
lcd_send_dat(decims[temps.decimali]); 
lcd_send_dat(0xDF);


或者,我们可以完全摆脱decims转换器:

lcd_send_dat('0' + ((temps.decimali * 10) / 16));


基本上,所有这些更改都允许编译器为您做一些工作,并使代码更容易理解。

ff29svar

ff29svar3#

在微芯片论坛上,他们发现了代码中的一个漏洞。原来问题出在我身上,我没有考虑decims[];数组中的负索引,当它在负温度的情况下进行评估时

if(temps.eratura < 0){ 
 temps.eratura = -temps.eratura; 
 temps.decimali = -temps.decimali; 
 lcd_send_dat('-'); 
 }

字符串
然后用于

lcd_send_dat(decims[temps.decimali]); //decimals


仅包含较低半字节(0x 0 F)的字节的2的补码具有最高有效半字节集(0xF 1)。这就是我所有问题的根源!在补充字节后,在较低的半字节上添加掩码解决了这个问题:

if(temps.eratura < 0){ 
 temps.eratura = -temps.eratura; 
 temps.decimali = -temps.decimali & 0x0F; 
 lcd_send_dat('-'); 
 }


谢谢你们的回答,你们真的帮助了我很多,让我明白了事情是如何运作的!

fcwjkofz

fcwjkofz4#

这可能是C编译器优化代码,它可以给予非线性代码执行的外观。尝试关闭优化器的单步执行。

相关问题