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.
Hi, i'm pretty new to programming. I'm currently using Keil uvision to program AT89S4051 Microcontroller in C language. I'm trying to read bit by bit data from the I/O port every 50us and store it in an external memory bit by bit. However, i would want to store a bit then shift to next bit and store. After 8 bits, store on a new byte and for 300 bytes. Anyone could suggest a method on how to do that? My program is posted down below (it's probably full of mistakes) Thanks in advance.
#include <reg51.h> sbit Tsignal = P1^2; //Input Signal sbit Learn = P1^3; unsigned int i; unsigned char xdata x[300]; unsigned char xdata y[300]; void timer0(void) { TMOD = 0x01; //Timer0 , Mode 1 TH0 = 0xFF; //Set timer to count from -46 = FFD2h TL0 = 0xD2; TR0 = 1; while(TF0==0); //Turn on Timer0 TF0 = 0; //Set Flag to 0; TR0 = 0; //Turn off Timer0 } void main(void) { Learn=0; if(Learn==1) //If learn button is pressed, data as stored as sampling data { for(i=0;i<299;i++) //Loop for 300 bytes { timer0(); //Call timer0 function x[i] = Tsignal; //Store Tsignal in x } } else //When Learn button is not pressed, data is stored as { for(i=0;i<299;i++) //Loop for 300 bytes { timer0(); //Call timer0 function y[i] = Tsignal; //Store Tsignal in y } } }
Thank you Sir, for your reply. Your Sentence "Next thing - there is nothing magic about this processsor.", are you trying to say that this processor is really straight forward to program?
So, for me to be able to store bit by bit then shift to next byte, i have to use bitshift till i stored all 8 bits. Then shift byte and continue the process?
Regarding the 300 samples, i was trying to declare 300 bytes instead of bits. Well, my programming is seriously weak, so bear with my mistakes. So if i were to declare 300 bytes, it would mean that i will be storing my every bit data into every byte of data? If so, i will change it from 300 to 2400 as it will become 300bytes.
www.8052.com/.../120112
You have to either store one sample / byte (which would consume lots of extra RAM) or you have to perform traditional bit shift operations to fit 8 samples into the first byte before moving to the second byte.
The processor do not have any magic bit-array instruction that can allow it to write to the nth bit of an array - the code is limited to either write to a hard-coded bit position (in the specific memory bit-addressable memory area which is smaller than 300 bits anyway) or must perform compute which specific bit in which specify byte and then update that byte using standard C code.
"...you have to perform traditional bit shift operations to fit 8 samples into the first byte before moving to the second byte." That is exactly what i wanted to do. I'm trying to store 2400 bits = 300 bytes. Which i want to use the bit shift method in order to save memory. However, i would want to ask if the first data be stored in the LSB or MSB of the byte?
Thanks in advance.
"However, i would want to ask if the first data be stored in the LSB or MSB of the byte?"
That is entirely up to you. Do you want to shift in from left or from right? As I said - there isn't anything magic with the processor. You make the decisions, and select the suitable C constructs.
I'm currently using Keil uvision to program AT89S4051 Microcontroller I'm trying to store 2400 bits = 300 bytes WHERE?, in thin air?
Erik
That depends on what you want to do with the byte once you have filled it!
If you want it to be usable as a 'C' variable, then you will have to use the same convention that the compiler uses - which is described in the compiler Manual:
http://www.keil.com/support/man/docs/c51/c51_ap_datastorage.htm
Ah i'm sorry, trying to program it on an external memory 16Kbit.
"...or you have to perform traditional bit shift operations to fit 8 samples into the first byte before moving to the second byte."
But how do i exactly do that in the program?
x[i] = x[i]>>1;
This way? Then how do i shift the byte? Is there any link to any website that will teach me how to bit shift by 1 and store and byte shift? Thank You.
That is a basic 'C' coding question - nothing specifically to do with the 8051 or Keil.
Newly-updated 'C' learning/reference resources here: blog.antronics.co.uk/.../
Note that a shift "up" is usually taken to mean a shift to the left:
x <<= 1;
how are you going to connect an external memory to an AT89S4051 ?
Oh, i just realised that AT89S4051 doesn't support external memory. Sorry, i'm really a newb. I will change it to another Microcontroller
Almost all microcontrollers supports external memory. Just note that not all external memory are parallell-accessed RAM.
There are I2C-connected EEPROM memories.
Or for maximum write speed while still reducing the amount of processor pins consumed, there are SPI-connected F-RAM memories.
An interesting thing with the SPI interface is that it is a serial interface that makes it quite easy to integrate the shifting of new bit data from your input pin directly to the memory chip. So you send out eight bits (a full byte) and the F-RAM memory moves to next byte and expects 8 new bits.
With EEPROM or F-RAM, the memory is non-volatile, so the stored data will be retained even you remove the power. But you need to add some extra logic to store some indexing data, so you can later figure out if there are valid measurements stored in the memory or not.
If you don't need non-volatile memory, then it's enough to select a processor with enough built-in RAM memory, allowing you to use a standard array for storing the sampled data.
So, if i were to use Fujitsu I2C 16Kbit FRAM (www.semiconductorstore.com/.../viewPrd.asp)it will still work perfectly fine? And Yes, i do need a non-volatile memory.
if i were to use Fujitsu I2C 16Kbit FRAM the AT89S4051 has hardware SPI, so using an I²C EEPROM when SPI EEPROMS are plentiful is a strange choice.
Oh, i just realised that AT89S4051 doesn't support external memory. Sorry, i'm really a newb. being a newbie is not an excuse for not reading the datasheet. au contraire, it makes it much more important.
STOP with what you are doing, and familiarize yourself with "the bible" (link in earlier post) and the datasheet. To get what you want done the method is NOT to bungle along and every time you find a bump in the road write another post. Many of your previous questions would not have been asked had you read the bible and the datasheet.
Okay, thanks alot Erik. I will follow your advice well and sorry for all the inconvenience/annoyance caused :)