Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

code in c not working but in asm work properly...

Status
Not open for further replies.

uday mehta

Advanced Member level 4
Joined
Dec 30, 2011
Messages
104
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Activity points
1,979
in asm:
Code:
org 00h
loop:
clr p3.0
sjmp loop
end

in c:
Code:
#include<reg51.h>
sbit i = P3^0;
void main()
{
 while(1)
{
 i = 0;
}
}

i think both code are same. there is no error in any code but on hardware only asm code works properly, means p3.0 port remain high when i burn hex file of "c" code and get low when i burn hex file of "asm" code. why code of "c" not work properly?? can any body explain? i am using at89c2051 micro-controller.
 

Try this with P3.0 set as output port.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<reg51.h>
 
sbit i = P3^0;
 
void main()
{
     i = 0;
 
         while(1)
         {
              i = 0;
         }
}

 

i think there is no change, logically both are same.
 

Which compiler are you using..?
You can check the dis-assembly of your C code after compiling (in debug mode). Maybe there might be some problem with the compiler optimization.

Just check the dis-assembly and post here, what the compiler is giving..?
 

What compiler are you using, KEIL C51?

If you are using KEIL C51, you can examine the disassembly window to examine the resulting code.

BigDog

- - - Updated - - -

You must have read my mind errakeshpillai. :grin:


BigDog
 

it work properly in debug mode. and also Proteus simulator but not work on hardware. i use c2051, s52 but output is always high. In asm it gets low but when i press reset button sometime it get high. one more interesting thing whenever p3.0 pin get low p3.3 pin also get low. i change the controller several time and also bread board.
 

it work properly in debug mode

Can you post the dis-assembled code from the compiler...?
By this means we can know the assembly instructions that are used in the code by the compiler. There can be a problem in that, otherwise the code seems fine and should work.
Or else you might be missing out on some other hardware points..!
 

Code:
C:
.
.
.
.
0x07FC    00       NOP      
C:0x07FD    00       NOP      
C:0x07FE    00       NOP      
C:0x07FF    00       NOP      
C:0x0800    787F     MOV      R0,#0x7F
C:0x0802    E4       CLR      A
C:0x0803    F6       MOV      @R0,A
C:0x0804    D8FD     DJNZ     R0,C:0803
C:0x0806    758107   MOV      SP(0x81),#0x07
C:0x0809    02080C   LJMP     main(C:080C)
     4: void main() 
     5: { 
     6:  while(1) 
     7: { 
     8:  i = 0; 
C:0x080C    C2B0     CLR      i(0xB0.0)
     9: } 
C:0x080E    80FC     SJMP     main(C:080C)
C:0x0810    00       NOP      
C:0x0811    00       NOP      
C:0x0812    00       NOP      
C:0x0813    00       NOP      
C:0x0814    00       NOP      
C:0x0815    00       NOP 
.
.
.

it works on Proteus properly. is there any problem in burner. there is no hardware problem because when i clear whole port then hardware work properly. but when i clear only single bit it not works.
 
Last edited:

this is the whole content...
 

Attachments

  • C2051.rar
    21.8 KB · Views: 132

today i got something new...
when i connect an another programmer and lode the hex file, it give some warning "some data out of range has been ignored". and this warning comes only when i lode hex file of "c" code. hex of asm lode successfully. where is the problem. will i change compiler?
 

i think if u r compiler is working properly and hardware is also working then might be problem in flash magic u are flash magic setting should clear all flash memory before burning firmware.
 

Yeah may be... did you selected the exact device name in compiler and flash magic and correct programming area in flash magic...
 

I believe a possible issue maybe due to the code being incorrectly located during the linking process.

Can you post a screen capture of the "Options for Target" window?



The following is a disassembly of your code imported into a uVision v4 Project with the AT89C2051 selected as target and default startup code utilized:


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
126: ?C_STARTUP:     LJMP    STARTUP1 
   127:  
   128:                 RSEG    ?C_C51STARTUP 
   129:  
   130: STARTUP1: 
   131:  
   132: IF IDATALEN <> 0 
C:0x0000    020003   LJMP     STARTUP1(C:0003)
   133:                 MOV     R0,#IDATALEN - 1 
C:0x0003    787F     MOV      R0,#0x7F
   134:                 CLR     A 
C:0x0005    E4       CLR      A
   135: IDATALOOP:      MOV     @R0,A 
C:0x0006    F6       MOV      @R0,A
   136:                 DJNZ    R0,IDATALOOP 
C:0x0007    D8FD     DJNZ     R0,IDATALOOP(C:0006)
   185:                 MOV     SP,#?STACK-1 
   186:  
   187: ; This code is required if you use L51_BANK.A51 with Banking Mode 4 
   188: ;<h> Code Banking 
   189: ; <q> Select Bank 0 for L51_BANK.A51 Mode 4 
   190: #if 0    
   191: ;     <i> Initialize bank mechanism to code bank 0 when using L51_BANK.A51 with Banking Mode 4. 
   192: EXTRN CODE (?B_SWITCH0) 
   193:                 CALL    ?B_SWITCH0      ; init bank mechanism to code bank 0 
   194: #endif 
   195: ;</h> 
C:0x0009    758107   MOV      SP(0x81),#0x07
   196:                 LJMP    ?C_START 
C:0x000C    02000F   LJMP     main(C:000F)
     4: void main() 
     5: { 
     6:  while(1) 
     7: { 
     8:  i = 0; 
C:0x000F    C2B0     CLR      i(0xB0.0)
     9: } 
C:0x0011    80FC     SJMP     main(C:000F)
C:0x0013    00       NOP      
C:0x0014    00       NOP      
C:0x0015    00       NOP      
C:0x0016    00       NOP      
C:0x0017    00       NOP      
C:0x0018    00       NOP      
C:0x0019    00       NOP      
C:0x001A    00       NOP      
C:0x001B    00       NOP



You will notice the difference between the location of your code and the above version, C:0x0809 versus C:0x000C for entry into main().

BigDog
 

    V

    Points: 2
    Helpful Answer Positive Rating
now its work, but...
i think there is problem in compiler or in my system.
because when i use same hardware and same program but another system then it works.
target.JPG
Code:
      126: ?C_STARTUP:     LJMP    STARTUP1 
   127:  
   128:                 RSEG    ?C_C51STARTUP 
   129:  
   130: STARTUP1: 
   131:  
   132: IF IDATALEN <> 0 
C:0x0000    020800   LJMP     STARTUP1(C:0800)
C:0x0003    00       NOP      
C:0x0004    00       NOP      
..
.
.

.
.
C:0x07F9    00       NOP      
C:0x07FA    00       NOP      
C:0x07FB    00       NOP      
C:0x07FC    00       NOP      
C:0x07FD    00       NOP      
C:0x07FE    00       NOP      
C:0x07FF    00       NOP      
   133:                 MOV     R0,#IDATALEN - 1 
C:0x0800    787F     MOV      R0,#0x7F
   134:                 CLR     A 
C:0x0802    E4       CLR      A
   135: IDATALOOP:      MOV     @R0,A 
C:0x0803    F6       MOV      @R0,A
   136:                 DJNZ    R0,IDATALOOP 
C:0x0804    D8FD     DJNZ     R0,IDATALOOP(C:0803)
   185:                 MOV     SP,#?STACK-1 
   186:  
   187: ; This code is required if you use L51_BANK.A51 with Banking Mode 4 
   188: ;<h> Code Banking 
   189: ; <q> Select Bank 0 for L51_BANK.A51 Mode 4 
   190: #if 0    
   191: ;     <i> Initialize bank mechanism to code bank 0 when using L51_BANK.A51 with Banking Mode 4. 
   192: EXTRN CODE (?B_SWITCH0) 
   193:                 CALL    ?B_SWITCH0      ; init bank mechanism to code bank 0 
   194: #endif 
   195: ;</h> 
C:0x0806    758107   MOV      SP(0x81),#0x07
   196:                 LJMP    ?C_START 
C:0x0809    02080C   LJMP     main(C:080C)
     3: void main() 
     4: { 
     5:  i=0; 
C:0x080C    C2B0     CLR      i(0xB0.0)
     6: }
C:0x080E    22       RET      
C:0x080F    00       NOP      
C:0x0810    00       NOP      
C:0x0811    00       NOP      
C:0x0812    00       NOP      
C:0x0813    00       NOP      
C:0x0814    00       NOP  
.
.
.

in this so many nop in between starting and main program...
from address 0x0000 it jump to 0x07FF.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top