C for 16f628, PC goes haywire

Status
Not open for further replies.

zakkos

Newbie level 6
Joined
Jan 17, 2013
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,439
hello all, I have a new problem here.
I'm still learning C for PIC (xc8 compiler) and as a beginner project i'm doing a thermometer with the popular ds18b20 and a pic16f628 i had lying around.
My program does behave good when it is allowed to run but while i was playing with pointers, structures, arrays etc. to return more than one value in a function i noticed something went haywire and now the PC goes back and forth not allowing the program to run sequentially. I'm quite sure I have forgot something about program and/or memory locations but i can't figure out what or why. Can someone help me? What's the best way to share the whole main code plus includes files here for you to examine it? for now i just paste the main code...

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * 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;
}

 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…