We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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; }
#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; }
here are some suggestions from a previous thread: http://www.keil.com/forum/docs/thread4173.asp
"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... ;-)
Thanks guys, I will look into it. I have extensively trawled the data sheets to see if what I was doing fitted in with the timings, and I have designed the code to fit in with this. Another thing that happens is that after the ports have been set up as inputs and outputs, they all go high, when the code that doesn't work is burnt into the device ( I assume it just crashes). I have it working using .asm code, but not C. I need to get the C working unless any of you have an algorithm that can do Natural Logarithms in assembler!! :o) Its for a university project... So I have about a month and a half to get it fully working... Hopefully!!!!
"I have it working using .asm code, but not C. I need to get the C working" Why? There is no problem in having your LCD stuff in assembler, and your maths in 'C'. Just read the Manual section on Interfacing 'C' and Assembler. "I have about a month and a half to get it fully working" All the more reason not to waste time converting working assembler to 'C'!
Like I said I am new to this, I already knew a bit of C, so I went for coding all of it in C. But as it would not work I went to assembler, to see what I could do, and got a fair portion running. So if I can get the C going, I already have it all written, otherwise I would have to write it all in assembler, and I havent the first idea how to impliment natural Logs! Do you know anywhere with source code for such a maths function? Cheers.
You could always step into the code for the log() function and see what it does. Or look here: http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/#dirlist
"I would have to write it all in assembler" No, you wouldn't. There is absolutely no reason whatsoever why your project should not contain both 'C' modules and assembler modules. http://www.keil.com/support/man/docs/c51/c51_ap_ctoasm.htm As you say you're on a tight schedule, and you also say you have some working assembler, do not waste time just converting assembler to 'C' - it is pointless! "I havent the first idea how to impliment natural Logs!" So this is naturally the bit to do in 'C' - using the standard maths functions! http://www.keil.com/support/man/docs/c51/c51_lib_math.htm http://www.keil.com/support/man/docs/c51/c51_log.htm http://www.keil.com/support/man/docs/c51/c51_log10.htm
I have it working using .asm code, but not C. You can go to your develop environment, select the check box of "Assembly Code" in "Listing" page under the menu "Project -> Options for Target'XXX'", and rebuild your project. You get a detail .LST file now. Maybe you can find out the difference.
"You get a detail .LST file now. Maybe you can find out the difference." It is highly unlikely that a piece of hand-written assembler will bear any resemblance at all to the compiler's output from an independent bit of 'C' source code! As I keep saying: if the assembler is working, just use it! There is absolutely no point whatsoever in wasting effort converting it to 'C' just for the sake of it! Especially as he says he has other work to do, and little time to do it in!
Well, I shall be trying to interface C from assembly code using the previously mentioned threads as help. Cheers, for the help everyone. Hopefully It should all be working soon! Regards Tom