Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools Display Modes
Old 20th October 2008, 10:37 AM   #1 (permalink)
Default Storing char in 16f877 EEPROM memory

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[i]);
write2EE(SVC_NUM + i, DefaultSVC[i]);
}
}

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);
yueying_53 is offline  
Old 20th October 2008, 02:01 PM   #2 (permalink)
Default

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.
Pommie is offline  
Old 20th October 2008, 03:27 PM   #3 (permalink)
Default

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?
yueying_53 is offline  
Old 20th October 2008, 03:32 PM   #4 (permalink)
Default

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.
Pommie is offline  
Old 20th October 2008, 05:22 PM   #5 (permalink)
Default

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.
yueying_53 is offline  
Old 21st October 2008, 05:01 AM   #6 (permalink)
Default

Quote:
Originally Posted by yueying_53 View Post
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?

Quote:
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.

Quote:
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.
Pommie is offline  
Old 23rd October 2008, 07:29 AM   #7 (permalink)
Default

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!
Attached Files
File Type: c sms1.c (4.4 KB, 18 views)
yueying_53 is offline  
Old 23rd October 2008, 08:29 AM   #8 (permalink)
Default

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,
Quote:
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.
Pommie is offline  
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Title Starter Forum Replies Latest
Problem to write and read from EEPROM - 16F877 isaac82 Micro Controllers 7 27th May 2008 07:20 AM
16F877 and 24LC256 I2C eeprom spectrum Micro Controllers 0 26th November 2007 09:02 AM
convert int to char dspic30f kenobe Micro Controllers 5 10th October 2007 02:21 PM
c++ convert argv[1] to a char array danielsmusic Chit-Chat 5 18th March 2006 01:09 PM
EEPROM Memory GatorGuy Micro Controllers 25 17th June 2005 10:02 AM



All times are GMT. The time now is 08:54 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker