Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Storing char in 16f877 EEPROM memory

Status
Not open for further replies.

yueying_53

New Member
Hi,

I got this code from somewhere that writes 2 sets of hp numbers into the PIC (16f877) memory, then retrieves it and transmit them through the UART and finally is displayed on a Hyperterminal screen.

This code works, but I thought that since only 8 numbers were being stored, I could reduce the starting memory address of SVC_NUM to 18 (=10+8), thinking that each character is stored using 1 byte. However, I am not sure if it is due to the fact that 8 memory spaces are not enough, or it has something to do with my transmit function, the numbers shown on the screen do not correspond to what was stored before. In fact, I tried all sorts of numbers, and the least number that I needed to have for SVC_NUM is 26 (=10+16).

Hence I wondered if somehow each character needed 2 bytes instead. To test my theory, I tried reducing while(i<16) in my transmit_Num function to while (i<8), thinking that it would result in only 4 characters being displayed. But all 8 characters displayed correctly. It is only when i changed it to while(i<7), that one character was missing from the screen. So I'm not sure what's going on, and would be grateful to anyone would can help shed some light on this.

//memory locations to store numbers
#define ADMIN_HP_NUM 10
#define SVC_NUM 30

bank2 unsigned const char DefaultSVC[] = "96197777 ";
bank2 unsigned const char Dest_Num[] = "81201378 ";

void initMem()
{ int i=0;
for (i = 0; i < 16; i++)
{ write2EE(ADMIN_HP_NUM + i, Dest_Num);
write2EE(SVC_NUM + i, DefaultSVC);
}
}

void transmit(char data)
{
while(!TRMT);
TXREG = data;
}

void transmitNum(char add) //transmit hp number from memory
{
char temp;
int i=0;
while (i < 16) {
temp = readEE(add + i);
if (temp == NONE) break;
transmit(temp);
i++;
}

transmit('\"');
transmit(CR);
}

Steps in Main function:
initMEM();
transmitNum(SVC_NUM);
 
It is allocating 16 locations to store the string even though it only needs 8. Note that in the transmitNum routine it has the line "if (temp == NONE) break;" which will stop sending when the zero terminator of the string is reached.

Mike.
 
Thanks for replying. I understand what the temp==None part does, but in the #define part, why do I need at least 16 memory spaces for only 8 characters?
 
You don't need 16 but you do need 9. That is 8 for the characters and one for the terminator. I do notice though that you have a space at the end of the strings and so you may need 10. Is None equal to zero or 32?

Mike.
 
I do need at least 16 spaces. Because when I changed #define SVC_NUM to 20 instead of the current 30, my first few SVC numbers end up becoming garbage when being displayed. And hence my confusion described before. Why do I need to set aside 16 memory spaces when all I need later is just to read are 8 memory spaces?

None is defined as 0. slightly out of point here, but when we do something like this: #define None 0, is 0 an integer, binary, or char? I'm a bit confused because the string terminator is a null character '\0', but when I put temp==NULL, the error message says that the 2 operands not of the same type.

Another question: My program is polling for any pressed key from the keyboard. It then stores whatever it polls into the EEPROM memory. If nothing is pressed, what is being stored in the memory? In this case, will there be a string terminator? Because I used the transmitNum function to display what was stored in the memory space onto hyperterminal and the first char displayed is always garbage, followed by 16 spaces. I was expecting it to display nothing since there is the line: if (temp == NONE) break.
 
I do need at least 16 spaces. Because when I changed #define SVC_NUM to 20 instead of the current 30, my first few SVC numbers end up becoming garbage when being displayed. And hence my confusion described before. Why do I need to set aside 16 memory spaces when all I need later is just to read are 8 memory spaces?

Did you also change the for loop to only copy 10 (or 9) characters?

None is defined as 0. slightly out of point here, but when we do something like this: #define None 0, is 0 an integer, binary, or char? I'm a bit confused because the string terminator is a null character '\0', but when I put temp==NULL, the error message says that the 2 operands not of the same type.

#define simply replaces all occurrences of the first string with the second and so if None was defines as 0 (zero not O) then it's the same as writing temp==0.

Another question: My program is polling for any pressed key from the keyboard. It then stores whatever it polls into the EEPROM memory. If nothing is pressed, what is being stored in the memory? In this case, will there be a string terminator? Because I used the transmitNum function to display what was stored in the memory space onto hyperterminal and the first char displayed is always garbage, followed by 16 spaces. I was expecting it to display nothing since there is the line: if (temp == NONE) break.

You will have to post some more of your code as you didn't post this part.

Mike.
 
Hi, attached is my code. I modified the receive_char function such that when no key is pressed, garbage values won't be displayed. But i'm not able to receive any information through the UART.

Previously, I searched through many sample codes and coded it like what everyone does (commented out right below the current one). However, that returned garbage values when no key was pressed, and does not receive anything through the UART too.

What am I doing wrong?

Thank you in advance!
 

Attachments

  • sms1.c
    4.4 KB · Views: 274
The only thing I can spot that is wrong is the TX pin should be set to input for the UART to work.

From the data sheet,
Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be
set in order to configure pins RC6/TX/CK and RC7/RX/DT
as the Universal Synchronous Asynchronous Receiver
Transmitter.

Mike.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top