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.

Programming the Mini-Max 8051 microcontroller

Status
Not open for further replies.

blimon

New Member
hello there, I am recently working on a school project. I plan to connect an ultrasonic range sensor (SRF10 https://www.robot-electronics.co.uk/htm/srf10tech.htm.) with a microcontroller (mini-max 8051 https://www.bipom.com/minimax51-set.shtm). I am planning to connect these two and use them to monitor distance. For example, when an object is at a certain distance, then the sensor will notify the microcontroller, when the object is out of range, then the microcontroller will notified again. I need help to program this. I am planning to use C, using the Micro IDE compiler. I have read the manuals from both the microcontroller and sensor but im still lost. Any help or suggestions will be appreciated.
Thanks
Ben L.
 
Your 8051 doesn't have an I2C bus built in. This is the bus used by your ultrasound module. You either have to find (or write) 8051 software to emulate the I2C bus or you could try to emulate the I2C bus with your SPI bus. Either way you will want to read the I2C bus specification.

You will also want to read the datasheet for your 8051. It should be on Atmel's website.
 
Thanks for the advice. Yeah i searched the 8051 manual and there is a way to simulate the I2C bus. The problem is that I dont know very well how to do this. Can anyone suggest any site that might have some code or does anyone know how to generate that particular code?
Thanks
 
Because I2C is clocked it won't be too hard to do in software.

Heres the idea:

Make_I2C_Data_Pin_Output();
for (x;x<8;x++)
{
I2C_Data_Pin = Data_Bit_x;
I2C_Clk_Pin = 1;
Delay(/*insert delay value to make clock rate correct*/);
I2C_Clk_Pin = 0;
Delay(/*insert delay value to make clock rate correct*/);
}

same idea for the receiver.
 
This is the program that works with PIC processors. I am not using a PIC processor, im using the mini-max 8051 microcontroller with the DIO Expander from https://www.bipom.com/minimax51-set.shtm. If anyone has any suggestions on how to modify this program please let me know. Thanks for all your help.
Ben L.



#include <8051io.h>
#include <8051bit.h> /* Bit set/clear macros */
#include <8051reg.h>

#define SCL_IN RB4 //???needs to be adjusted
#define SDA_IN RB1 // ???
#define SCL TRISB4 //I2C Bus
#define SDA TRISB1
//#define SS P1.5
//#define PIC_ADDR 0xB0

void i2c_start(void);
void i2c_dly(void);
void i2c_stop(void);
unsigned char i2c_rx(char ack);
unsigned char i2c_tx(unsigned char d);
extern bit i2cbusy;

//setbit (SDA);
//SCL = 1;
//SCL_IN = SDA_IN = 0;

void main()
{
unsigned char rangehigh, rangelow;
while(1)
{
i2c_start(); // send start sequence
i2c_tx(0xE0); // SRF10 I2C start up address with R/W bit
clear
i2c_tx(0x00); // SRF10 command register address
i2c_tx(0x50); // command to start ranging in inches
i2c_stop(); // send stop sequence

i2c_start(); // send start sequence
i2c_tx(0xE0); // SRF10 I2C address with R/W bit clear
i2c_start(); // send a restart sequence
i2c_tx(0xE1); // SRF10 I2C address with R/W bit set
rangehigh = i2c_rx(1); // get the high byte of the range and send
acknowledge.
rangelow = i2c_rx(0); // get low byte of the range - note we don't
acknowledge the last byte.
i2c_stop(); // send stop sequence
}
}

void i2c_start(void)
{
setbit (SDA); // i2c start bit sequence
i2c_dly(); //
setbit (SCL);
i2c_dly();
clrbit (SDA);
i2c_dly();
clrbit (SCL);
i2c_dly();
}

void i2c_stop(void)
{
clrbit (SDA); // i2c stop bit sequence
i2c_dly();
setbit (SCL);
i2c_dly();
setbit (SDA);
i2c_dly();
}

void i2c_dly(void)
{
}

unsigned char i2c_rx(char ack) //ack = acknowledgement bit
{
char x,d;
d=0;
setbit (SDA);
for(x=0; x<8; x++)
{
d <<= 1;
do {
setbit (SCL);
}
while(clrbit(SCL_IN)); // wait for any SCL clock stretching
i2c_dly();
if(SDA_IN) d |= 1;)
clrbit (SCL);
}
if(ack) clrbit (SDA);
else setbit (SDA);
setbit (SCL);
i2c_dly(); // send (N)ACK bit
clrbit (SCL);
setbit (SDA);
return d;
}

unsigned char i2c_tx(unsigned char d) //function to receive data
{
char x;
static bit b;
for(x=8; x; x--) {
if(d&0x80) setbit (SDA);
else clrbit (SDA);
sebit (SCL);
d <<= 1;
clrbit (SCL);
}
setbit (SDA);
setbit (SCL);
i2c_dly();
b = SDA_IN; // possible ACK bit
clrbit (SCL);
return b;
}
 
It looks like all you need to do is rewrite the i2c_dly, clrbit and setbit functions. The delay function should be fairly easy with a timer or you can increment a volatile variable in a loop (regular variables incremented in a loop are removed by compiler optimisations). The setbit function will use compiler specific syntax to set or clear the bit - you'll have to look at the documentation on your compiler about how to do this.

There is a weird use of clrbit here:
while(clrbit(SCL_IN)); // wait for any SCL clock stretching

You might want to rewrite theis line to make this less strange. Right now it seems that clrbit has a return value which by it's name it really shouldn't.
 
Those clear and setbits have already been modified to work for my program. I think I can set and clear the bits fine except for the weird part you mentioned. The original code is

while(SCL_IN==0);

however the compiler would flag errors and say "type clash, syntax error, unknown RB4" I changed it to while(clrbit(SCL)) but it wouldnt compile either.

More specifically, this program has 3 major flaws that showed up when I compile. For one, it doesnt recognize RB4 and RB1. Those are port pins that needed to be read. I've changed it to all kinds of port pin possible on the microcontroller being used, yet it doesnt recognize.

Second, the initialization of "SCL = 1 and SCL_IN = SDA_IN = 0" near the top of the program wouldnt compile and always show the error "undefined RB1, non-assignable, constant expression required".

And third is the weird while part mentioned above.

Thanks bmcculla, you've been great help. https://www.bipom.com/devsys/8051dev.zip This is the link for the MIcroIDE program i am using if you should need.
 
You need to assign SCL and the other signals to port pins. The 8051 has bit addressable ports like the PIC. The Keil C syntax to do this is:

sfr P2 = 0xXX; //replace XX by the address of Port2
sbit SCL = P2^3; // Assign the symbol SCL to be the 4th bit in Port2.
//now you can use SCL like a variable:

SCL = 1; //Set SCL
(SCL == 1) // returns true if SCL is equal to 1
 
Hi there, im also doing a Final Year Project which is alike. My project objective is to capture a human height into 8051 and display the height of the human on the LCD screen in centimetres. I am given a 8051 microcontroller, an ultrasonic range finder(SRF08) and was told to use Assembly Language to implement an I2C bus for interfacing with the range finder. As I have only little knowledge on Assembly Language, therefore need help from the experts to build a program using Assembly Language to implement the I2C.

Thanx... Pls help me with it...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top