# Problem with programming the 8051

#### maxfourier

Joined Aug 7, 2011
16
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.

#### stahta01

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

Tim S.

#### embedded.anto

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

#### MrChips

Joined Oct 2, 2009
21,871
Can you post both asm and C code so we can see what you are doing?

#### ErnieM

Joined Apr 24, 2011
8,055
...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.
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.

#### embedded.anto

Joined Nov 23, 2011
9
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

#### Attachments

• 118.8 KB Views: 20

#### embedded.anto

Joined Nov 23, 2011
9
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

#### MrChips

Joined Oct 2, 2009
21,871
replace

Rich (BB code):
while(a<=9)
with

Rich (BB code):
while(1)

Make sure P2 is configured as an output port.

#### ErnieM

Joined Apr 24, 2011
8,055
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.

#### RiJoRI

Joined Aug 15, 2007
536