It is currently 22 Apr 2018 13:29

All times are UTC + 1 hour




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: 17 May 2017 18:59 
Offline

Joined: 29 May 2011 01:41
Posts: 15
The following code is not working. During debug the variables, sTime1 and sTime2, are correct however when displayed on the GLCD they are incorrect. The values should be for sTime1 = 1, and for sTime2 = 51 which is correct in debug but not on the display. The display indicates 53 for both.
  sTime1           : string[ 3 ];  // Used for converting time elements to string
  sTime2           : string[ 3 ];  // Used for converting time elements to string


  ByteToStr( Alarm1_Hour, sTime1 );
  ByteToStr( ALarm1_Minute, sTime2 );
  Glcd_Write_Text( 'ALARM1:' + sTime1 + ':' + sTime2, 20, 2, 0 );


I also sometimes receive an error message "Debug Error: pc out of scope....". Perhaps the two issues are related.

I am connected to an EasyPIC6 with a 16F887. The IDE is mikroPascal V.7.1.0 with mikroProg Suite for PIC v.2.51. Any help is greatly appreciated.


Top
 Profile  
 
PostPosted: 18 May 2017 17:08 
Offline

Joined: 18 Feb 2006 13:17
Posts: 4892
Better avoid string concatenation with the '+' operator (except for simple ones when assigning a string variable, like string1:=string2+' s'). Complex expressions may cause problems for the compiler.

Note also that string parameter may be passed only by address, which means that in statements like
Glcd_Write_Text( 'ALARM1:' + sTime1 + ':' + sTime2, 20, 2, 0 );
compiler has to form an internal string variable to pass it to a subroutine. This may lead to wasteful use of RAM as compiler has to ensure there's enough space to fit the substrings. Not to mention that programmer doesn't have access to such internal string and thus cannot reuse it.

Try following approach:
var txt: string[16];
     sTime: string[ 3 ];

  txt:='ALARM1:';
  ByteToStr(Alarm1_Hour, sTime);
  strcat(txt, sTime);
  ByteToStr(ALarm1_Minute, sTime);
  txt:=txt+':';
  strcat(txt, sTime);
  Glcd_Write_Text(txt, 20, 2, 0 );

_________________
Replacement libraries for mP PRO and PIC18 processors, mP PRO tips & trics


Top
 Profile  
 
PostPosted: 27 May 2017 19:14 
Offline

Joined: 29 May 2011 01:41
Posts: 15
Great, thanks for the help and explanation.


Top
 Profile  
 
PostPosted: 02 Jul 2017 02:56 
Offline

Joined: 29 May 2011 01:41
Posts: 15
Update: I am back on this project and am having the same results after modifying the code as above. Here are snippets of each of the procedures used to display the information on the GLCD. All values are testing. The values display for each hour and minute are the same as Alarm3_minute.

  // Write Alarm1 hour
  EEPROM_Write( 0x00, 0x01 );

  // Write Alarm1 minute
  EEPROM_Write( 0x02, 0x33 );


procedure ReadAlarm1;
begin
  // Get hour data
  Alarm1_hour := EEPROM_Read( 0x00 );

  // Get minute data
  Alarm1_minute := EEPROM_Read( 0x02 );
end;


Here is the section of code that displays the information.
  //End of Shift 1 Time
  txt:='ALARM1: ';
  ByteToStr( Alarm1_Hour, sTime );
  strcat( txt, sTime );
  ByteToStr( ALarm1_Minute, sTime );
  txt:=txt+':';
  strcat( txt, sTime );
  Glcd_Write_Text( txt, 10, 2, 0 );

  //End of Shift 2 Time
  txt:='ALARM2: ';
  ByteToStr( Alarm2_Hour, sTime );
  strcat( txt, sTime );
  ByteToStr( ALarm2_Minute, sTime );
  txt:=txt+':';
  strcat( txt, sTime );
  Glcd_Write_Text( txt, 10, 3, 0 );
 
  //End of Shift 3 Time
  txt:='ALARM3: ';
  ByteToStr( Alarm3_Hour, sTime );
  strcat( txt, sTime );
  ByteToStr( ALarm3_Minute, sTime );
  txt:=txt+':';
  strcat( txt, sTime );
  Glcd_Write_Text( txt, 10, 4, 0 );


After making the changes the debug information is correct for txt variable but again, the information on the display is incorrect. What am I doing wrong? If attaching the project for review would be best please let me know.


Top
 Profile  
 
PostPosted: 02 Jul 2017 12:33 
Offline

Joined: 18 Feb 2006 13:17
Posts: 4892
If the string prepared for GLCD is correct then the part of code you've presented is valid and the problem has to reside elsewhere. If you can attach the whole project then please do so.

_________________
Replacement libraries for mP PRO and PIC18 processors, mP PRO tips & trics


Top
 Profile  
 
PostPosted: 03 Jul 2017 14:46 
Offline

Joined: 29 May 2011 01:41
Posts: 15
Thank you very much for taking time to review this.

program GLCDTest;
var
// GLCD module Connections - Begin
 GLCD_DataPort : byte at PORTD;

 GLCD_CS1 : sbit at RB0_bit;
 GLCD_CS2 : sbit at RB1_bit;
 GLCD_RS  : sbit at RB2_bit;
 GLCD_RW  : sbit at RB3_bit;
 GLCD_EN  : sbit at RB4_bit;
 GLCD_RST : sbit at RB5_bit;

 GLCD_CS1_Direction : sbit at TRISB0_bit;
 GLCD_CS2_Direction : sbit at TRISB1_bit;
 GLCD_RS_Direction  : sbit at TRISB2_bit;
 GLCD_RW_Direction  : sbit at TRISB3_bit;
 GLCD_EN_Direction  : sbit at TRISB4_bit;
 GLCD_RST_Direction : sbit at TRISB5_bit;
// GLCD Module Connections - End

  alarm1_minute    : byte;   //Used for first shift check
  alarm1_hour      : byte;
  alarm2_minute    : byte;   //Used for second shift check
  alarm2_hour      : byte;
  alarm3_minute    : byte;   //Used for third shift check
  alarm3_hour      : byte;
  sTime1           : string[ 4 ];  // Used for converting time elements to string
  sTime2           : string[ 4 ];  // Used for converting time elements to string


procedure SetTestAlarmTimes;   //For Testing only
begin
  // Write Alarm1 hour
  EEPROM_Write( 0x00, 0x01 );

  // Write Alarm1 minute
  EEPROM_Write( 0x02, 0x33 );

  // Write Alarm2 hour
  EEPROM_Write( 0x04, 0xC );

  // Write Alarm2 minute
  EEPROM_Write( 0x06, 0x34 );

  // Write Alarm3 hour
  EEPROM_Write( 0x08, 0xD );

  // Write Alarm3 minute
  EEPROM_Write( 0x0A, 0x2C );
end;

procedure ReadAlarm1;
begin
  // Get hour data
  Alarm1_hour := EEPROM_Read( 0x00 );

  // Get minute data
  Alarm1_minute := EEPROM_Read( 0x02 );
end;

procedure ReadAlarm2;
begin
  // Get hour data
  Alarm2_hour := EEPROM_Read( 0x04 );

  // Get minute data
  Alarm2_minute := EEPROM_Read( 0x06 );
end;

procedure ReadAlarm3;
begin
  // Get hour data
  Alarm3_hour := EEPROM_Read( 0x08 );

  // Get minute data
  Alarm3_minute := EEPROM_Read( 0x0A );
end;


procedure DisplayMainScreen;     //Display main screen
                                 //Main screen displays current time,
                                 //Alarm time, and displays if stacklight is on.
var txt1: string[ 16 ];
    txt2: string[ 16 ];
    txt3: string[ 16 ];
    sTime: string[ 3 ];

begin
  //Current Time
  Glcd_Write_Text( '55:44:99', 10, 0, 0 );             //Display current time

  //Date
  Glcd_Write_Text( 'MMM DDD XX/XX/20XX', 10, 1, 0 );   //Display current date and day

  //End of Shift 1 Time
  txt1 := 'ALARM1: ';
  ByteToStr( Alarm1_Hour, sTime );
  strcat( txt1, sTime );
  ByteToStr( ALarm1_Minute, sTime );
  txt1 := txt1+':';
  strcat( txt1, sTime );
  Glcd_Write_Text( txt1, 10, 2, 0 );

  //End of Shift 2 Time
  txt2 := 'ALARM2: ';
  ByteToStr( Alarm2_Hour, sTime );
  strcat( txt2, sTime );
  ByteToStr( ALarm2_Minute, sTime );
  txt2 := txt2 + ':';
  strcat( txt2, sTime );
  Glcd_Write_Text( txt2, 10, 3, 0 );

  //End of Shift 3 Time
  txt3 := 'ALARM3: ';
  ByteToStr( Alarm3_Hour, sTime );
  strcat( txt3, sTime );
  ByteToStr( ALarm3_Minute, sTime );
  txt3 := txt3 + ':';
  strcat( txt3, sTime );
  Glcd_Write_Text( txt3, 10, 4, 0 );
end;

begin

  ANSEL  := 0;                                      // Configure AN pins as digital I/O
  ANSELH := 0;

  Glcd_Init();                                      // Initialize GLCD

  Glcd_Fill( 0xFF );                                // Clear GLCD

  Glcd_Set_Font( @FontSystem5x7_v2, 5, 7, 32 );     // Set font

  // For testing only
  SetTestAlarmTimes;

  // Get alarm times
  ReadAlarm1;
  ReadAlarm2;
  ReadAlarm3;

  //Setup Display
  DisplayMainScreen;

  while TRUE do
  begin

  end;
end.


Top
 Profile  
 
PostPosted: 04 Jul 2017 00:15 
Offline

Joined: 18 Feb 2006 13:17
Posts: 4892
The problem lays most probably in making EEPROM reads immediately after EEPROM write. Just place a delay of 20ms between SetTestAlarmTimes and the first EEPROM read procedure and all should work.

_________________
Replacement libraries for mP PRO and PIC18 processors, mP PRO tips & trics


Top
 Profile  
 
PostPosted: 04 Jul 2017 00:54 
Offline

Joined: 29 May 2011 01:41
Posts: 15
Add Delay_ms( 20 ) worked perfectly. Still testing but so far so good.

Thank you again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: