This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Code working in uVision 2 but not in hardware...!

Hello, being new to this, i'm not totally sure I'm not doing something silly wrong. Below are two 'C' files, one which works in the target hardware and another which does not. I am using an Atmel AT89S8252. All code compiles fine and seems to run well on the simulator. The code just initialises an LCD, and writes some characters to it. The only difference between the two programs is that one puts the LCD initialisation in a function called from main()and the working one just does it all in main(). Why will this not work? Any help is welcomed. I use alot more function calls in my project, but am equally unable to get that work aswell. Code follows:

Working code:

#include <AT898252.H>
#include <stdio.h>

void Delay(void);
void Data_Write(void);
void Initialise(void);

sbit RS = P2^0;		//
sbit R_W = P2^1;	// Set LCD controls
sbit Enable = P2^2;	//

main()
{
TMOD=0x12;		// Timer 1 is set for mode 1 operation
P0=0x00;		// Make P1 output for LCD data
RS=0;			//
R_W=0;			// RS, R/W, E = Outputs
Enable=0;		//


Delay();		// 8 bit mode, 2 line, 5x8 character
P0=0x38;
Data_Write();

Delay();		// Display ON, Cursor OFF, Blink OFF
P0=0x0C;
Data_Write();

Delay();		// Clear display and return to home
P0=0x01;
Data_Write();

Delay();		// Increment at write and shift cursor to right
P0=0x06;
Data_Write();

Delay();		// Set position to line 1 pos 6
P0=0x86;
Data_Write();

RS=1;
Delay();		// Write H to LCD
P0='H';
Data_Write();

Delay();		// Write M to LCD
P0='M';
Data_Write();

Delay();		// Write E to LCD
P0='E';
Data_Write();

Delay();		// Write ' ' to LCD
P0=' ';
Data_Write();

Delay();		// Write ' ' to LCD
P0=' ' ;
Data_Write();

Delay();		// Write M to LCD
P0='M';
Data_Write();

Delay();		// Write k to LCD
P0='k';
Data_Write();

Delay();		// Write 5 to LCD
P0='5';
Data_Write();

while (1);		// Stay here
}

void Delay(void)
{
int n;			// Delay for 6.1ms with 24MHz clock
TH0=0x00;
TR0=1;
for (n=0;n<50;n++) {
	while (!TF0);
	TF0=0;
	}
TR0=0;
}

void Data_Write(void)
{
int x=4;		// Write Data to LCD
int y=20;
while (x) x--;
Enable=1;
while (y) y--;
Enable=0;
}

Non working code:
#include <AT898252.H>
#include <stdio.h>

void Delay(void);
void Data_Write(void);
void Initialise(void);

sbit RS = P2^0;		//
sbit R_W = P2^1;	// Set LCD controls
sbit Enable = P2^2;	//

main()
{
TMOD=0x12;						// Timer 1 is set for mode 1 operation
P0=0x00;						// Make P1 output for LCD data
RS=0;							//
R_W=0;							// RS, R/W, E = Outputs
Enable=0;						//

Initialise();

while (1);
}

void Initialise(void)
{
Delay();		// 8 bit mode, 2 line, 5x8 character
P0=0x38;
Data_Write();

Delay();		// Display ON, Cursor OFF, Blink OFF
P0=0x0C;
Data_Write();

Delay();		// Clear display and return to home
P0=0x01;
Data_Write();

Delay();		// Increment at write and shift cursor to right
P0=0x06;
Data_Write();

Delay();		// Set position to line 1 pos 6
P0=0x86;
Data_Write();

RS=1;
Delay();		// Write H to LCD
P0='H';
Data_Write();

Delay();		// Write M to LCD
P0='M';
Data_Write();

Delay();		// Write E to LCD
P0='E';
Data_Write();

Delay();		// Write ' ' to LCD
P0=' ';
Data_Write();

Delay();		// Write ' ' to LCD
P0=' ' ;
Data_Write();

Delay();		// Write M to LCD
P0='M';
Data_Write();

Delay();		// Write k to LCD
P0='k';
Data_Write();

Delay();		// Write 5 to LCD
P0='5';
Data_Write();
}

void Delay(void)
{
int n;  		// Delay for 6.1ms with 24MHz clock
TH0=0x00;
TR0=1;
for (n=0;n<50;n++) {
	while (!TF0);
	TF0=0;
	}
TR0=0;
}

void Data_Write(void)
{
int x=4;		// Write Data to LCD
int y=20;
while (x) x--;
Enable=1;
while (y) y--;
Enable=0;
}

Parents
  • "one which works in the target hardware and another which does not ... All code compiles fine and seems to run well on the simulator."

    As Erik says, timing must be a prime suspect here.

    You need to get out your oscilloscope (or logic analyser, if you have one) and look at what's actually happening on the interface.
    What is the hardware doing?

    Take timing measurements, and check them against the datasheet.

    Chances are your timing in the "working" version is right on the edge, and you're actually quite lucky that it works at all! With the other version, the timing is just that little bit different so that it all just goes over the edge & stops working.

    Well, I've heard it said that these situations can arise... ;-)

Reply
  • "one which works in the target hardware and another which does not ... All code compiles fine and seems to run well on the simulator."

    As Erik says, timing must be a prime suspect here.

    You need to get out your oscilloscope (or logic analyser, if you have one) and look at what's actually happening on the interface.
    What is the hardware doing?

    Take timing measurements, and check them against the datasheet.

    Chances are your timing in the "working" version is right on the edge, and you're actually quite lucky that it works at all! With the other version, the timing is just that little bit different so that it all just goes over the edge & stops working.

    Well, I've heard it said that these situations can arise... ;-)

Children