Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Weird code behaviour. PIC16F887 or PIC18F4550. Soft I2C, GLCD and two OneWire devices

Status
Not open for further replies.

p77

Newbie level 3
Joined
Nov 3, 2016
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
54
Hello everyone,

This is my first post to the forum, but I have been reading/using it for quite a while and I must thank you all for contributing and helping. Sorry for my english, its a foreign language to me.

Equipment:
Two temperature sensors 18B20 each interfaced via separate OneWire comm.
12864B GLCD
DS3231 RTC via software I2C
PIC 16F887
Issue:
The following code bellow programmed and the alterations i make and issues are following:

When all of the function calls in while loop in function main are uncommented the microcontroller doesnt even jump into program, eg. not working.
If I comment read/display date functions, everything what is left work perfectly fine, the menu, two temperature readouts and current time.
If I comment two temperature readouts and leave time and date readout uncommented, again what is left in the program do work.

This led me to following belief that I have done something horribly stupid and wrong with declarations and am experiencing some memory issue, so I switched to 18F4550 with 5 times the RAM and Program memory of 887. Again, the issue is the same.

So let me put this together one more time: All of my functions work fine but without luck to use them all :D.

Code:
#include "UART.h"

char GLCD_DataPort          at PORTD;

sbit GLCD_CS1               at RB0_bit;
sbit GLCD_CS2               at RB1_bit;
sbit GLCD_RS                at RB2_bit;
sbit GLCD_RW                at RB3_bit;
sbit GLCD_EN                at RB4_bit;
sbit GLCD_RST               at RB5_bit;
sbit Soft_I2C_Scl           at RA3_bit;
sbit Soft_I2C_Sda           at RA4_bit;

sbit GLCD_CS1_Direction     at TRISB0_bit;
sbit GLCD_CS2_Direction     at TRISB1_bit;
sbit GLCD_RS_Direction      at TRISB2_bit;
sbit GLCD_RW_Direction      at TRISB3_bit;
sbit GLCD_EN_Direction      at TRISB4_bit;
sbit GLCD_RST_Direction     at TRISB5_bit;
sbit Soft_I2C_Scl_Direction at TRISA3_bit;
sbit Soft_I2C_Sda_Direction at TRISA4_bit;
  

void Convert_Time(unsigned short tc) {
int ac=0, ic=0;
int bits[8]={0,0,0,0,0,0,0,0};
for (ic=128, ac=7; ic>=1; ic=ic/2)
{
bits[ac]=tc/ic;
if (bits[ac]) {tc=tc%ic;}
ac--;
}
tc=(((bits[6]*4)+(bits[5]*2)+(bits[4]*1))*10)+(bits[3]*8)+(bits[2]*4)+(bits[1]*2)+(bits[0]*1);
echo1[0]=(tc/10)+48;
echo1[1]=(tc%10)+48;
return;

}

void Convert_Month(unsigned short tc) {
int ac=0, ic=0;
int bits[8]={0,0,0,0,0,0,0,0};
for (ic=128, ac=7; ic>=1; ic=ic/2)
{
bits[ac]=tc/ic;
if (bits[ac]) {tc=tc%ic;}
ac--;
}
tc=(((bits[5]*2)+(bits[4]*1))*10)+(bits[3]*8)+(bits[2]*4)+(bits[1]*2)+(bits[0]*1);
echo2[0]=(tc/10)+48;
echo2[1]=(tc%10)+48;
return;

}

void Read_Time(){
  Soft_I2C_Init();
  Soft_I2C_Start();               // Issue start signal
  Soft_I2C_Write(0b11010000);              // Start from address 2
  Soft_I2C_Write(0x00);
  Soft_I2C_Stop();
  Soft_I2C_Start();
  Soft_I2C_Write(0b11010001);
  rtc_seconds = Soft_I2C_Read(1);
  rtc_minutes = Soft_I2C_Read(1);
  rtc_hours   = Soft_I2C_Read(0);
  Soft_I2C_Stop();
  return;

}

void Read_Date(){
  Soft_I2C_Init();
  Soft_I2C_Start();
  Soft_I2C_Write(0b11010000);
  Soft_I2C_Write(0x03);
  Soft_I2C_Stop();
  Soft_I2C_Start();
  Soft_I2C_Write(0b11010001);
  rtc_day     = Soft_I2C_Read(1);
  rtc_date    = Soft_I2C_Read(1);
  rtc_month   = Soft_I2C_Read(1);
  rtc_year    = Soft_I2C_Read(0);
  Soft_I2C_Stop();
  return;

}




void PrintMenu(){
     Glcd_Write_Text(Brand, 59, 0, 2);
     Glcd_Write_Text(PrintT, 0, 7, 2);
     Glcd_Write_Text(MenuT, 37, 7, 2);
     Glcd_Write_Text(BackT, 74, 7, 2);
     Glcd_Write_Text(SetT, 108, 7, 2);
     return;
     
}

void RTC_Init(){

          Soft_I2C_Start();
          Soft_I2C_Write(0xD0);
          Soft_I2C_Write(0xF);
          Soft_I2C_Write(0x00);
          Soft_I2C_Stop();
          Soft_I2C_Start();
          Soft_I2C_Write(0xD0);
          Soft_I2C_Write(0xE);
          Soft_I2C_Write(0x00);
          Soft_I2C_Stop();
          return;
}


void Display_Time(){
   Convert_Time(rtc_hours);
   rtc_time_string[0]=echo1[0];
   rtc_time_string[1]=echo1[1];
   Convert_Time(rtc_minutes);
   rtc_time_string[3]=echo1[0];
   rtc_time_string[4]=echo1[1];
   Convert_Time(rtc_seconds);
   rtc_time_string[6]=echo1[0];
   rtc_time_string[7]=echo1[1];

   Glcd_Write_Text(rtc_time_string, 0, 4, 2);
   return;
}

void Display_Date(){

   Convert_Time(rtc_date);
   rtc_date_string[0]=echo1[0];
   rtc_date_string[1]=echo1[1];
   Convert_Month(rtc_month);
   rtc_date_string[3]=echo2[0];
   rtc_date_string[4]=echo2[1];
   Convert_Time(rtc_year);
   rtc_date_string[8]=echo1[0];
   rtc_date_string[9]=echo1[1];

   Glcd_Write_Text(rtc_date_string, 0, 3, 2);
   return;
}



void main() {

     INTCON = 0;
     INTCON2 = 0xF5;
     INTCON3 = 0xC0;
     RCON.IPEN = 0;
     PIE1 = 0;
     PIE2 = 0;
     PIR1 = 0;
     PIR2 = 0;
     ADCON1 |= 0x0F;
     CMCON  |= 7;
  
  Soft_I2C_Init();
  UART1_Init(19200);
  Delay_ms(200);
  RTC_Init();
  Glcd_Init();
  Glcd_Fill(0x00);
  Glcd_Set_Font(System3x5, 3, 5, 32);

    while (1) {
    
    //Read_Time();
    Read_Date();
    Read_Temp2();
    //Read_Temp1();
    
    Glcd_Fill(0x00);
    
    //Display_Time();
    Display_Date();
    PrintMenu();
    Display_Temperature2();
    //Display_Temperature1();
    Delay_ms(300);

    
  }
}

Secondary code:

Code:
#define t1t "TEMP 1:"
#define t2t "TEMP 2:"
extern unsigned temp1, temp2;
const unsigned short TEMP_RESOLUTION = 12;
extern char text1[];
extern char text2[];
extern char Temp1T[];
extern char Temp2T[];
extern char temp_whole;

const unsigned short RES_SHIFT2=4;

void Display_Temperature1() {
 unsigned int temp_fraction;
 char temp_whole2;
 unsigned int temp_fraction2;
  if (temp1 & 0x8000)
     {
     text1[0] = '-';
     temp1 = ~temp1 + 1;
     }

  temp_whole2 = temp1 >> RES_SHIFT2 ;

  if (temp_whole2/100)
     text1[0] = temp_whole2/100  + 48;
  else
     text1[0] = ' ';

  text1[1] = (temp_whole2/10)%10 + 48;
  text1[2] =  temp_whole2%10     + 48;

  temp_fraction2  = temp1 << (4-RES_SHIFT2);
  temp_fraction2 &= 0x000F;
  temp_fraction2 *= 625;

  text1[4] =  temp_fraction2/1000    + 48;
  text1[5] = (temp_fraction2/100)%10 + 48;
  text1[6] =' ';
  text1[7] =  'C';
  Glcd_Write_Text(t1t, 0, 5, 2);
  Glcd_Write_Text(text1, 60, 5, 2);

  return;

}

void Display_Temperature2() {

unsigned int temp_fraction;
char temp_whole2;
unsigned int temp_fraction2;

  if (temp2 & 0x8000)
     {
     text2[0] = '-';
     temp2 = ~temp2 + 1;
     }

  temp_whole2 = temp2 >> RES_SHIFT2 ;

  if (temp_whole2/100)
     text2[0] = temp_whole2/100  + 48;
  else
     text2[0] = ' ';

  text2[1] = (temp_whole2/10)%10 + 48;
  text2[2] =  temp_whole2%10     + 48;

  temp_fraction2  = temp2 << (4-RES_SHIFT2);
  temp_fraction2 &= 0x000F;
  temp_fraction2 *= 625;

  text2[4] =  temp_fraction2/1000    + 48;               // Extract thousands digit
  text2[5] = (temp_fraction2/100)%10 + 48;               // Extract hundreds digit
  text2[6] =' ';//(temp_fraction/10)%10  + 48;           // Extract tens digit
  text2[7] =  'C'; //temp_fraction%10      + 48;         // Extract ones digit
  Glcd_Write_Text(t2t, 0, 6, 2);
  Glcd_Write_Text(text2, 60, 6, 2);

  return;
}


void Read_Temp1(){

    Ow_Reset(&PORTE, 2);
    Ow_Write(&PORTE, 2, 0xCC);
    Ow_Write(&PORTE, 2, 0x44);
    Ow_Reset(&PORTE, 2);
    Ow_Write(&PORTE, 2, 0xCC);
    Ow_Write(&PORTE, 2, 0xBE);
    temp1 =  Ow_Read(&PORTE, 2);
    temp1 = (Ow_Read(&PORTE, 2) << 8) + temp1;
    return;
}

void Read_Temp2(){

    Ow_Reset(&PORTE, 1);
    Ow_Write(&PORTE, 1, 0xCC);
    Ow_Write(&PORTE, 1, 0x44);
    Ow_Reset(&PORTE, 1);
    Ow_Write(&PORTE, 1, 0xCC);
    Ow_Write(&PORTE, 1, 0xBE);
    temp2 =  Ow_Read(&PORTE, 1);
    temp2 = (Ow_Read(&PORTE, 1) << 8) + temp2;
    return;
}

Header file:

Code:
#define Brand "COLD MASTER 2016"
#define PrintT "PRINT"
#define MenuT "MENU"
#define BackT "BACK"
#define SetT "SET"

unsigned temp1, temp2;

unsigned short echo1[]= "00";
unsigned short echo2[]= "00";

unsigned int temp_fraction;

char rtc_time_string[]="00:00:00";
char rtc_date_string[]="xx/xx/20xx";

char text1[]= "000.0000";
char text2[]= "000.0000";

char rtc_seconds;
char rtc_minutes;
char rtc_hours;
char rtc_day;
char rtc_date;
char rtc_month;
char rtc_year;
char temp_whole;


void Convert_Time(unsigned short tc);
void Convert_Month(unsigned short tc);

void Read_Time();
void Display_Time();

void Read_Date();
void Display_Date();
void Print_Menu();
void RTC_Init();

void Read_Temp1();
void Display_Temperature1();

void Read_Temp2();
void Display_Temperature2();

And that would be it. I have lost 70 hours debugging this and I am really tired so i think it is ok to finally ask for help.

Thank you all,
p77.
 

Are you running this under a debugger?
How do you know that you are not entering the main program?
Is the header file included in both of the other code files? If so then you should be getting link errors about multiple declarations of variables such as 'temp1', 'echo1' and all of the 'rtc_xxx' ones. If not then place show us the complete code (including how you #include other files etc.). The config settings would be useful as well.
I suggest that you strip the code back to something very simple (in other words just the main loop) and make sure that works. Then add in one small bit of functionality and check that it is still working. Keep doing that until you encounter an error and the issue will (most likely) be related to what you have just added.
Susan
 

Are you running this under a debugger?
How do you know that you are not entering the main program?
Is the header file included in both of the other code files? If so then you should be getting link errors about multiple declarations of variables such as 'temp1', 'echo1' and all of the 'rtc_xxx' ones. If not then place show us the complete code (including how you #include other files etc.). The config settings would be useful as well.
I suggest that you strip the code back to something very simple (in other words just the main loop) and make sure that works. Then add in one small bit of functionality and check that it is still working. Keep doing that until you encounter an error and the issue will (most likely) be related to what you have just added.
Susan

Hi,

Complete thing is linked in MikroC PRO for PIC so the first code quote is the main code.
In the main code is included <UART.h> which is the header file for the project and this header file is in the third code quote.
Finally the second code quote is a part of main (UART.C File) containing temperature related functions all defined in UART.h

If you look close, it is all there. Nothing to hide. As for the config

config.jpgconfig2.jpg

And I do not simulate anything, I use EasyPIC 5 Development system and test realtime. I please you to take time and read the post again to see what is my issue.
I am sorry If I made all of this confusing

Thank you very much for your help
 

My comment was not about simulation but about use of the debugger.
Also it is still not good practice to define variables in a header file - you should only declare them. (If you don't knwo the difference between those terms then look them up - ity is important.)
If you don't want to take the approach I suggested before, then uncomment the troublesome lines of code and step through the execution with the debugger. That will tell you if the code is actually starting (one of your complaints) and also you can see where the execution stops.
Susan
 

My comment was not about simulation but about use of the debugger.
Also it is still not good practice to define variables in a header file - you should only declare them. (If you don't knwo the difference between those terms then look them up - ity is important.)
If you don't want to take the approach I suggested before, then uncomment the troublesome lines of code and step through the execution with the debugger. That will tell you if the code is actually starting (one of your complaints) and also you can see where the execution stops.
Susan

Thank you very much for your help.
I forgot to mention that when I use ICD, if i "set" the code to be able to work everything is fine and I can look into the variables and addresses, BUT on the other hand, when everything is uncommencted I can program the device but when I use debugger to step Into the program it actually cause the software, both compiler and debugger to display the very good known windows "Program as stopped responding" and I have to restart.

It all seems like the memory / direct RAM addressing issue but I dont have the idea how to debug it anymore.
 

Go back to the last part of my first post in this thread for the 'tried and true' technique to handle this situation.
Susan
 

You have to select ICD Debug type for build type in Project Options dialog box and then compile the project and burn it and debug.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top