I have just started to study the AVR a week ago. The eight LEDs and registors are connected between the VCC and ATMEGA128 PORTB. I found that some CodeVision C programs does not work as I expected. After careful examination of the codes, I can simplify the problem to the following Assembler coding. (PORTD.0 is connected to the chattering switch) =2ECSEG _main: ; 8 PORTB=3D0xff; LDI R30,LOW(255) OUT 0x18,R30 ; 9 DDRB=3D0xFF; OUT 0x17,R30 ; 10 ; 11 PORTD=3D0x00; CLR R30 OUT 0x12,R30 ; 12 DDRD&=3D0xFE; CBI 0x11,0 ; 13 ;CALL __LSLB12 ;=3D=3D=3D=3D> If commented OK!!!!! But uncommented??<=3D= =3D=3D LDI R30,LOW(5) OUT 0x18,R30 _0x3: RJMP _0x3 __LSLB12: RET If I comment the CALL statement, the the LEDs light as I expected. But when I revive the CALL statement, the LEDs doesn't light at all! I loaded this assembler to the AVRStudio simulator. But the simluator shows PORTB works as I expected irrespective of the CALL statement. Where am I wrong?
Strange behavior of ATMEGA128 PORT
Started by ●March 10, 2006
Reply by ●March 10, 20062006-03-10
qhhong wrote:> I have just started to study the AVR a week ago. > The eight LEDs and registors are connected between the VCC and > ATMEGA128 PORTB. > > I found that some CodeVision C programs does not work as I expected. > After careful examination of the codes, I can simplify the problem to > the following Assembler coding. (PORTD.0 is connected to the > chattering switch) > > .CSEG > _main: > ; 8 PORTB=0xff; > LDI R30,LOW(255) > OUT 0x18,R30 > ; 9 DDRB=0xFF; > OUT 0x17,R30 > ; 10 > ; 11 PORTD=0x00; > CLR R30 > OUT 0x12,R30 > ; 12 DDRD&=0xFE; > CBI 0x11,0 > ; 13 > ;CALL __LSLB12 ;====> If commented OK!!!!! But uncommented??<=== > LDI R30,LOW(5) > OUT 0x18,R30 > _0x3: > RJMP _0x3 > > __LSLB12: > RET > > If I comment the CALL statement, the the LEDs light as I expected. > But when I revive the CALL statement, the LEDs doesn't light at all! > > I loaded this assembler to the AVRStudio simulator. But the simluator > shows PORTB works as I expected irrespective of the CALL statement. > > Where am I wrong? >Did anybody initialize your stack pointer? -- Tauno Voipio tauno voipio (at) iki fi
Reply by ●March 10, 20062006-03-10
Yes, there's initialization routine just before above code. ;STACK POINTER INITIALIZATION LDI R30,LOW(0x10FF) OUT SPL,R30 LDI R30,HIGH(0x10FF) OUT SPH,R30 ;DATA STACK POINTER INITIALIZATION LDI R28,LOW(0x500) LDI R29,HIGH(0x500) JMP _main .ESEG .ORG 0 .DB 0 ; FIRST EEPROM LOCATION NOT USED, SEE ATMEL ERRATA SHEETS .DSEG .ORG 0x500 -- QH Hong.