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

Problem while using external RAM

Hi,

I am stuck up a peculiar problem while using 8051 external RAM.

I am declaring a variable:
unsigned int xdata x _at_ 0xA004;

then I equate:
x=6;

and i display this value on the hyperterminal (RS232) window using the function:

serial_num(x+0x30);

every thing goes fine and the value 6 is diaplayed on the hyper terminal window.

PROBLEM:

when i equate the value x to another variable *p and try to display the value *p i am unable to get the value on the hyper terminal window.

unsigned int xdata *p _at_ 0xA008;

x=6;
p=&x;
serial_num(*p+0x30);

when I try to display this on the hyper terminal (RS232) window I get garbage values displayed on the screen.

Here is the code that was developed.

#pragma LARGE
#include<reg52.h>
#include<string.h>
#include<absacc.h>
#include<intrins.h>
#include<ctype.h>

sbit RXD_pin=P3^0;
sbit TXD_pin=P3^1;
sbit INT0_pin=P3^2;
sbit INT1_pin=P3^3;
sbit WRITE_pin=P3^6;
sbit READ_pin=P3^7;

unsigned int xdata read_byte _at_ 0xA001, write_byte _at_ 0xA003;
unsigned int xdata count _at_ 0xA005, *p _at_ 0xA007 , x _at_ 0xA009;
unsigned int idata i;
unsigned char xdata CS5_enable _at_ 0XA000;
//CS5_enable is the variable declared to chip select the external RAM. Static RAM used is HY6264. 8K RAM
unsigned char xdata CS2_enable _at_ 0x4000;

void read_ram(void);
void write_ram(void);
void serial_init(void);
void serial_num(unsigned char);
void delay(void);

void main(void)
{ serial_init();
while(1)
{ write_ram();
x=6;
p=&x;
write_ram();
write_byte=*p;//write value to external ram location
read_ram();
read_byte=write_byte;//read value from external ram location
serial_num(read_byte+0x30); //display on hyper terminal window screen
} }

void read_ram(void)
{ CS5_enable=1;
WRITE_pin=1;
READ_pin=1;
}

void write_ram(void)
{ CS5_enable=0;
WRITE_pin=0;
}

void serial_num(unsigned char s)
{ SBUF=s;
while(TI==0);
TI=0;
}

void serial_init(void)
{ TMOD=0x20;
SCON=0x50;
TH1=0xcc;
TR1=1;
}

  • New to programming,
    just seeing how the program works,

    "unsigned int xdata count _at_ 0xA005, *p _at_ 0xA007 , x _at_ 0xA009;" generated a warning L6: Xdata space memory overlap,
    From: A009h
    To: A009h

    make it A00A, and warninig disappeared,
    though output is the same in both cases.6.

  • serial_num(read_byte+0x30); //display on hyper terminal window screen
    


    You need to be clear about exactly what it is that your code does and doesn not do.

    Hyperterminal is a Windows application that runs on a PC.
    An 8051 cannot run hyperterminal; it knows nothing at all about hyperterminal - no 8051 program can display anything on a hyperterminal window.

    Presumably, what serial_num actually does is to send a character to the 8051's serial port (UART)? That is all it does - what may or may not be connected to the UART is completely beyond the knowledge & control of the program

    // Adding 0x30 coz, (0x30 = 48), 48 is ASCII value for '0x00'  or '0'
    


    No - 0x00 is a numerical value (zero); '0' is a character, for which the ASCII code is 0x30 = 48.

    ASCII code 0x00 is the NUL character.

    As I said before, your code would be clearer is you used the literal character constant in your code - then your comment wouldn't need to explain the derivation of the "magic number" 0x30:

    serial_num( read_byte + '0' ); // Send the byte to the UART as an ASCII character
                                   // Note that this assumes that 0 <= read_byte <= 9;
                                   // Other vales (particularly values > 79) will not work
    

  • beginners as told put a printf
    why on earth overload memory with that monster?
    This particular 'problem' sounds like something that can be sorted out in the simulator.

    Erik

  • why all that discussion on precedence?

    depending on precedence makes code virtually unreadable. Once you have figured out the precedence sequence, you have lost what it was you were looking for in the first place

    BE EXPLICIT, if it takes one more line or a few parantheses, so what. Only Cidiots would ever state "save on the parantheses and line count".

    Erik

  • BE EXPLICIT, if it takes one more line or a few parantheses, so what. Only Cidiots would ever state "save on the parantheses and line count".

    I've seen code that was unreadable because of too many parentheses (oh yeah, and every cast, I repeat, every cast, was done explicitly).

    And once you get enough parentheses into one expression, you might have eliminated any chance of getting the precedence wrong ... as long as each and every pair of the parentheses are correct. And have fun making any changes to that expression.

    Unless you're under MISRA rules or something similar, putting parentheses in a simple *p + 30 makes things messy and confusing. Pointer dereferencing is done before any type of dyadic algebraic or bitwise operator. If there's any doubt about that, refer to any C textbook

    And don't forget that a lot of things in C are operators. Even a simple assignment is somewhere in the precedence list. Would you write

    (a = (b + 5));
    

    ?