Problem with programming the 8051

Thread Starter

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.

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

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.
 
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
 

Attachments

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
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
 
Top