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 an 8051, first time Microcontroller user...

Status
Not open for further replies.

Jezhead

New Member
I am having real trouble trying to program an 8051 microcontroller using C and the Keil uVision2 complier suite. I have experience of C++ programming and I know exactly what I want to do with the microcontroller, as outlined below. The problem I’m having is I don’t know where or even how to start! I’ve read most of the Primer manual and looked at numerous examples, however as most of the code I’ve found isn’t commented and I’m having trouble understanding what’s going on… Can anyone point me in the right direction or help me find my feet in this new area of programming?

Many thanks, Jezhead

The microcontroller is a P89C51/RC2 and I need it to read in four 8bit numbers from four line drivers (reading one line driver at a time through the same port) when the microcontroller receives an interrupt. Once the data has been read into the controller I want to output the four numbers through a different port. I plan to use port 0 to read in the data, port 2 to output the data and pins 1.1 to 1.4 to control the line drivers (to select which driver is being read from).
 
The trickyest thing about moving from programming on a PC and Programming on a micro is interacting with the low level hardware. Microcontrollers have memory mapped peripherals. This means that by reading and writing from various memory locations you can tell the hardware what to do.

The first thing you should do is find the .h file that defines all the Special Functions Registers (this is the name for the special memory locations that talk to the hardware). The manufacturer should provide this file. The file just contains a whole bunch of variable declarations. It will look something like this but a lot longer:

sfr P0 = 0x80; /* PORT 0 */
sfr SP = 0x81; /* STACK POINTER */
sfr DPL = 0x82; /* DATA POINTER - LOW BYTE */
sfr DPH = 0x83; /* DATA POINTER - HIGH BYTE */

The sfr type binds the name to the memory location. To access the hardware you just write to the variables. If you cant find the .h file you can get all the memory locations from the data seet for your controller.

So if you wanted to write to port0 the code looks like:
P0 = 0xff; //write all ones to port 0

This line of code writes 0xff to memory location 0x80 as defines by the sfr P0. The value in this memory location is sent to Port 0 by the hardware. You can read the port in the same way- just like reading a variable.

About interupts: You need to set the sfr values correctly to tell the micro which interrupts you want enabled. This will be in the data sheet (you write the interrupt sfr just like the port sfr). Then you need an interrupt service routine.

void interrupt_handler (void) interrupt 5
{
//your code here
}

This tells the compiler that your interrupt routine is at interrupt location 5. The datasheet will tell you what interrupt number coresponds to the interrupt you are using.

Hope this give you a place to start
Brent
 
yea.. i had this difficulty too when I just started to use Keil C51 in a week ago. I think Keil doesnt provide a good documentaion for beginner. I'd recommend u a book, "Embedded C". I don't have the book, but I download its free lecture notes, which I already forgot the website.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top