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.

[Help] Hitech C compiler build successful but the code doesn't work right

Status
Not open for further replies.

cheese

Newbie level 4
Joined
Feb 3, 2012
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Vietnam
Activity points
1,330
Hello, I wrote this code for PIC16F887 uC and use Hitech C to compile. There's no error or warning. But the code doesn't work in the right way when i use Style 2. Style 1 is OK. I don't know the reason. Anybody helps me to explain this problem plz!

Code:
unsigned char gcan[5] = {0b01110010,0b11011000,0b00011011,0b10010010,0b01101100};

void countdown()
{
	const unsigned char position = 0;
	unsigned char i, j;
	
	for (i = 0; i <= 7; i++)
	{
		for (j = 0; j <= 4; j++)
		{
			//Style 1
			//gcan[position + j] = ((gcan[position + j] << 1));
			//gcan[position + j] |= 0x01;
			
			//Style 2
			gcan[position + j] = ((gcan[position + j]) << 1) | 0x01;
		}
	}
}

I attach full code and project for your convenience.
View attachment TestHitechC_err.zip
View attachment main.c.txt
 

What do you mean by Stlye 1 and Style 2..
Your Coding Style is Right...

What u want to do with your code..
Use MPLAB SIM as Debugger and Check whether your code is running Properly on Debugger.
 
Yes, code is right. 2 styles are the same meaning in C language.
But when I use style 2 like this
Code:
gcan[position + j] = ((gcan[position + j]) << 1) | 0x01;
it works like this
Code:
gcan[position + j] = ((gcan[position + j]) << 1);
OR operator doesn't work!! :?: The Hitech C compiler ignore OR operator. I don't know why.
Style 1 is OK: shift left, then OR with 0x01.
U can debug the project to see this problem.
 

Sorry!!!

Its not working..
Don't know why..

Your Code is Okay..

If you find the solution to this problem then pls Tell me..

---------- Post added at 23:05 ---------- Previous post was at 22:41 ----------

Hey.. One strange thing happens

Code:
gcan[position + j] = ((gcan[position + j]) << 1) | 0xF0;

//if you use this ORring works.. but not working with 0x01 infact anding also works..

If anyone having the solution for this pls reply
 
Everything looks right. I don't have Hitech C. I compiled this in mikroC and debugged. It was fine. There was no problem. So, I have no idea what your problem is.

You can try setting optimization level to zero in the compiler. I don't know if this will help, but you can try.

How did you deduce that you get erroneous result?
 

Code:
gcan[position + j] = ((gcan[position + j]) << 1);
gcan[position + j] |= 0x01;
This is working fine


Code:
gcan[position + j] = ((gcan[position + j]) << 1) | 0x01;
But in this Oring is not performed..
MPLAB IDE has inbuilt MPLAB SIM Debugger where we can see the values of all variables...


I don't know how to set Optimization Level to Zero..

cheese and me both are facing same problem..

And I am using Hi-Tech C Lite version which is not much optimized as compared to the other pain versions of Hi_Tech C..
 
I don't see a problem. I downloaded Hi-Tech C (free version), copy-pasted the code, built it and ran the simulation and I get proper results. I've even changed the code to this:
Code:
unsigned char gcan[5] = {0b01110010,0b11011000,0b00011011,0b10010010,0b01101100};
unsigned char jcan[5] = {0b01110010,0b11011000,0b00011011,0b10010010,0b01101100};

void main()
{
	const unsigned char position = 0;
	unsigned char i, j;
	
	for (i = 0; i <= 7; i++)
	{
		for (j = 0; j <= 4; j++)
		{
			//Style 1
			jcan[position + j] = ((jcan[position + j] << 1));
			jcan[position + j] |= 0x01;
			
			//Style 2
			gcan[position + j] = ((gcan[position + j]) << 1) | 0x01;
//Position A
		}
	}
}

and both gcan and jcan have the same values when the program is at position A, which proves that it's working fine.

So, where is the problem?
 
  • Like
Reactions: cheese

    cheese

    Points: 2
    Helpful Answer Positive Rating
This is my problem!
HitechCerr.GIF

I've tried setting optimization level to 1 (minimum level) in the compiler, but it still not working (tested with Lite and Pro ver).
@arunsharma0731: yes, this OR operator works well with any number but 0x01. :x
 

I just glanced at your code and noticed the following configuration bit masks are from an older version of the Hi-Tech Compiler:

Code:
//**********************************************************************************
//Configuration Bits
__CONFIG(HS & WDTDIS & PWRTEN & MCLREN & UNPROTECT & SWBOREN & IESODIS & FCMDIS & LVPDIS & DEBUGDIS); //1st config. Word
__CONFIG(BORV21); //2st config. Word*/
//**********************************************************************************

Also, some of these Configuration Bit Masks, such as MCLREN -- the enabe/disable MCLR feature, are actually not supported by the PIC16F877.

The following are the recent Configuration Register Bit Mask Definitions from the device specific header file, pic16f877.h, of a more recent version of the compiler.

Code:
//
// Configuration mask definitions
//


// Config Register: CONFIG
#define CONFIG               0x2007
// Oscillator Selection bits
// RC oscillator
#define FOSC_EXTRC           0xFFFF
// HS oscillator
#define FOSC_HS              0xFFFE
// XT oscillator
#define FOSC_XT              0xFFFD
// LP oscillator
#define FOSC_LP              0xFFFC
// Watchdog Timer Enable bit
// WDT enabled
#define WDTE_ON              0xFFFF
// WDT disabled
#define WDTE_OFF             0xFFFB
// Power-up Timer Enable bit
// PWRT disabled
#define PWRTE_OFF            0xFFFF
// PWRT enabled
#define PWRTE_ON             0xFFF7
// FLASH Program Memory Code Protection bits
// Code protection off
#define CP_OFF               0xFFFF
// 1F00h to 1FFFh code protected
#define CP_UPPER_256         0xEFEF
// 1000h to 1FFFh code protected
#define CP_HALF              0xDFDF
// 0000h to 1FFFh code protected
#define CP_All               0xCFCF
// Brown-out Reset Enable bit
// BOR enabled
#define BOREN_ON             0xFFFF
// BOR disabled
#define BOREN_OFF            0xFFBF
// Low Voltage In-Circuit Serial Programming Enable bit
// RB3/PGM pin has PGM function; low-voltage programming enabled
#define LVP_ON               0xFFFF
// RB3 is digital I/O, HV on MCLR must be used for programming
#define LVP_OFF              0xFF7F
// Data EE Memory Code Protection
// Code Protection off
#define CPD_OFF              0xFFFF
// Data EEPROM memory code-protected
#define CPD_ON               0xFEFF
// FLASH Program Memory Write Enable
// Unprotected program memory may be written to by EECON control
#define WRT_ON               0xFFFF
// Unprotected program memory may not be written to by EECON control
#define WRT_OFF              0xFDFF
// In-Circuit Debugger Mode bit
// In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
#define DEBUG_OFF            0xFFFF
// In-Circuit Debugger enabled, RB6 and RB7 are dedicated to the debugger
#define DEBUG_ON             0xF7FF

While I have not had the opportunity to test your code in a more recent version of the compiler, I find it rather odd that Tahmid indicated the issue did not exist when the code was compiled in the most recent version of the compiler. While I do not believe the use of unsupported Configuration Register Bit Masks, would be the source of this issue, the use of an older version of the HiTech Compiler might. Neither the C89 nor C99 C Standards specify or define a "rotation" operator, however the more update versions HiTech Compiler being do a reasonably good job of translating the combinations of the both "shift" and bitwise 'OR" operators into an appropriate "rotate" instruction during compilation, in an attempt to optimize the resulting code for the embedded device. An older version of the HiTech Compiler may have a known "bug" which incorrectly translates your statement and has since been corrected in newer versions, this might explain why the issue is only present when the two operators are using in a single statement rather than two sequential statements.

I would suggest updating your compiler to see if this in fact remedies the situation as Tahmid mentioned.

When I have the time, I will test compile your code in several versions of the HiTech Compiler and review the resulting code for the cause of the issue.

What version of the HiTech Compiler are you currently using?


BigDog
 
  • Like
Reactions: cheese

    cheese

    Points: 2
    Helpful Answer Positive Rating
I'm using MPLAB 8.56 and Hitech C 9.71a for PIC16F887.
I've tried with the newest version of HTC, 9.83 Lite. OR operator in Style 2 code still not working with the number 0x01.
 

Hello Everyone..

I am using Hi-Tech C Compiler v9.81...
And it is not working, when we OR gcan with 0x01.
But it works with other like 0x0F etc etc
Even ANDing with 0x01 works.. Don't know whats the problem with ORing.

BigDOgGuru i am not using any config macros in my code..
But still it doesn't work we both are having the same problem..
 
This issue also happens with + (ADD) operator.
Code:
gcan[position + j] = ((gcan[position + j]) << 1) | 0x01; 
gcan[position + j] = ((gcan[position + j]) << 1) + 0x01;
 

I still don't get the error. I even downloaded your attached project file and built it and still don't get the error. One thing I noticed is that, you are using the watch window to check the value of the variables. May be there's some error here. Instead use the file registers to check the values. That's what I do.

Goto "View" > Click "File Registers" > Scroll down to where you see gcan (and jcan if you've included that). Here's what I got:


4_1328352994.png


As you can see, each of the corresponding elements of arrays gcan and jcan holds the same value. In short, there's nothing wrong.

Hope this helps.
Tahmid.
 
Last edited:
  • Like
Reactions: cheese

    cheese

    Points: 2
    Helpful Answer Positive Rating
@Tahmid. Thank you for your showing me how to use File Registers. But it still doesn't get right result. I'm using Window 7 Professional (MSDN AA) and Hitech C Lite 9.83.
HitechCerr2.GIF
 

Attachments

  • HitechCerr2.GIF
    HitechCerr2.GIF
    101 KB · Views: 61

Attach the project file you're working with right now. I'll see if I get the same results. Besides that, maybe, you're having some problem with Hi-Tech C running on Windows 7 Professional version. I don't know, just a guess.
 

I still get no error. Maybe some problem with Windows 7? Or something in your PC? Uninstall and reinstall Hi-Tech compiler and try.
 

I am using Windows 7 Ultimate edition...
I will try it..
and then inform you..
We both are getting same thing :-(
 
That may be the problem. I'm running it on Windows XP SP2. Here, I get proper results all the time.
Later, I'll test this on my laptop where I have Windows 7. Then, I'll let you know.
 
  • Like
Reactions: cheese

    cheese

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top