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 } } }
"...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.
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