Problem with programming the 8051

Discussion in 'Embedded Systems and Microcontrollers' started by maxfourier, Nov 23, 2011.

  1. maxfourier

    Thread Starter New Member

    Aug 7, 2011
    16
    0
    Software used:Keil.
    Microcontroller:Atmel AT89S52(8051)

    If i write a program in asm, create hex file and program it to the 8051, it works fine.

    But if a write my code in C (I've tried many codes, from books, websites, self, etc) and program the 8051, it doesnt work.

    Please help out as i need to write my code in C.
     
  2. stahta01

    Member

    Jun 9, 2011
    133
    21
    Find the C compiler option to output asm code; compare the two sets of asm code.

    Tim S.
     
  3. embedded.anto

    New Member

    Nov 23, 2011
    9
    0
    same procedure....... save the program in .c............. add and compile the program......... ref: muhammed ali mazudhi book for 8051
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Can you post both asm and C code so we can see what you are doing?
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Better define what "it doesnt work" means. Be specific as possible.

    Find portion of program responsible for this function. Repair this portion.

    Rinse, lather, repeat until operation is complete and rock solid.
     
  6. embedded.anto

    New Member

    Nov 23, 2011
    9
    0
    i attached the circuit diagram.... follow that......... move the value 1 to 9 from port 2 and view the output...........

    program

    org 0000h
    mov a,#00h
    top: mov p2,a
    acall delay
    inc a
    sjmp top

    write your own delay program
     
  7. embedded.anto

    New Member

    Nov 23, 2011
    9
    0
    c code is

    #include<reg51.h>
    void delay();
    void main()
    {
    int a=0;
    while(a<=9)
    {
    P2=a;
    a++;
    delay()
    }
    }
    void delay()
    {
    int i,j;
    for(i=0;i<=255;i++)
    for(j=0;j<=255;j++);
    }


    try this
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    replace

    Code ( (Unknown Language)):
    1. while(a<=9)
    with

    Code ( (Unknown Language)):
    1. while(1)

    Make sure P2 is configured as an output port.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Sorry to say your schematic has several flaws.

    First, the 7447 has open collector outputs, meaning the outputs are driven low to turn on a segment LED, and are left open to turn the segment off. They are intended to drive a common anode display. So you need to make sure you have such a display. If you are unsure post the part number and we can check for you.

    Next, the common terminal of the display should go to 5V, not ground as you show.

    Also there should be a limiting resistor between each display pin and each drive pin on the 7447 for a total of 7 resistors. 470 ohms is a typical value.

    Finally, you show no power on the AT89C51, and you also show no crystal. I am unfamiliar with this device so it is possible it has an internal oscillator but it is also something you should check.
     
  10. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    A couple of comments:
    (1) An embedded system must ALWAYS have a main loop in order to keep the program from running off to Never-Land. The "TOP:" / "SJMP TOP" lines in the ASM code does this. C will not. As suggested, put a "while(1){ / "}" around the code in the main() function.
    (2) It's probably a typo, but the call to "delay()" in the main loop needs a semicolon after it.

    OK, three comments! There's no limit check on the Accumulator in the ASM code.

    --Rich
     
Loading...