gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:52:42 25 4
gpt4 key购买 nike

我这里有一个新问题。我仍在为 PIC(xc8 编译器)学习 C,作为一个初学者项目,我正在用流行的 ds18b20 和我身边的 pic16f628 做一个温度计。我的程序在允许运行时表现良好,但当我使用指针、结构、数组等在函数中返回多个值时,我注意到有些东西出了问题,现在 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;
}

最佳答案

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

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

char scratchpad[9];

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

关于C for 16f628,程序计数器失控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16308609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com