1. 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.
    Dismiss Notice

Programming an 8051, first time Microcontroller user...

Discussion in '8051/8951' started by Jezhead, Mar 31, 2004.

  1. Jezhead

    Jezhead New Member

    Joined:
    Dec 18, 2003
    Messages:
    4
    Likes:
    0
    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).
     
  2. bmcculla

    bmcculla New Member

    Joined:
    Sep 5, 2003
    Messages:
    898
    Likes:
    0
    Location:
    California, USA
    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
     
  3. StupidDum

    StupidDum New Member

    Joined:
    Nov 3, 2003
    Messages:
    242
    Likes:
    0
    Location:
    Malaysia
    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.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. TKS

    TKS New Member

    Joined:
    Sep 1, 2003
    Messages:
    560
    Likes:
    0
    Location:
    -----

    :D

    i think it your name... :p.
     
  6. Jezhead

    Jezhead New Member

    Joined:
    Dec 18, 2003
    Messages:
    4
    Likes:
    0
    Thanks

    Thanks that helped me clear up a few things got the code to work now.
     

Share This Page