my code :
#include <reg52.h> #include <stdio.h> #define uchar unsigned char void main() { uchar value; unsigned int address; uchar *data_buf; TMOD=0X20; // timer 1 mode 2 TH1=0xFD; // 9600 baud at 11.0592MHz TCON=0x40; // start baud rate clock SCON=0x52; // enable RX,TX address=0x01CF; value=0x66; *data_buf=value; printf("\nAddress %04xH Value : %02x",address,*data_buf); while (1); }
This purpose for my pointer that I want to use in my function
//=============================================== // read EEPROM 24lc256 // input : address 16-bit, // return : 0 if write error // 1 if write complete and get data 1 byte from *data_buf //=============================================== bit read_EEPROM(unsigned int addr,unsigned char *data_out) { bit err; uchar data_in; err=0; i2c_start (); if (!i2c_write_one_byte(0xA0)) { if (!i2c_write_one_byte(addr>>8)) { if (!i2c_write_one_byte(addr)) { i2c_stop (); i2c_start (); if (!i2c_write_one_byte(0xA1)) { data_in = i2c_read_one_byte(No_ack); // read and send no ack *data_out = data_in; } else err=1; } else err=1; } else err=1; } else err=1; i2c_stop(); return (err); } void main() { unsigned char data_buf[1]; unsigned char value; . . . if (!read_EEPROM(0x1234,&data_in)) { value=data_buf[0]; printf("Read %2bX OK",value); } else printf("Read error"); . . } please help me if it wrong. thanks
"it correct?" No, because you still have exactly the same error that Stefan pointed out in the first place!
SO what is the purpose of the pointer? Why not write straight to data_in?
Hello I wrote new program here,it correct? #include <reg52.h> #include <stdio.h> #define uchar unsigned char void main() { uchar value,data_in; unsigned int address; uchar *data_buf; TMOD=0X20; // timer 1 mode 2 TH1=0xFD; // 9600 baud at 11.0592MHz TCON=0x40; // start baud rate clock SCON=0x52; // enable RX,TX address=0x01CF; value=0x66; data_buf=&data_in *data_buf=value; printf("\nAddress %04xH Value : %02x",address,*data_buf); while (1); }
"I can sole this problem as Stefan Duncanson say" But your code still has a serious bug: As Jon said, you are using an uninitialised pointer!
Thanks Stefan Duncanson and Jon ward I can sole this problem as Stefan Duncanson say Thanks again
I have a question. In your example, where is data_buf pointing? Jon
Because %x expects an integer, not a char. Use %bx instead.
View all questions in Keil forum