this folling code was written using AT89S51 is it possible t
this folling code was written using AT89S51 is it possible to change it to work with Atmega328p?
MOV P0,#83H // Initializing push button switches and initializing LED in OFF state.
READSW: MOV A,P0 // Moving the port value to Accumulator.
RRC A // Checking the vale of Port 0 to know if switch 1 is ON or not
JC NXT // If switch 1 is OFF then jump to NXT to check if switch 2 is ON
CLR P0.7 // Turn ON LED because Switch 1 is ON
SJMP READSW // Read switch status again.
NXT: RRC A // Checking the value of Port 0 to know if switch 2 is ON or not
JC READSW // Jumping to READSW to check status of switch 1 again (provided switch 2 is OFF) SETB P0.7 // Turning OFF LED because Switch 2 is ON
SJMP READSW // Jumping to READSW to read status of switch 1 again.
END
Solution
ATMEGA 328p is a powerful 8 bit RISC micro-controller. Programs written for AT89S51 can be transported to AT328P. The last two bits of port P0 (P0.0 and P0.1) is used as input for push button switches. P0.7 is used as output to a single LED, which is active low (because output pin is connected to a pull-up resisitor, hence active low). The following code written in Atmega328p assebly does the same function as the given assembly code.
LD1 R16,0x83 // Load Immediate.
OUT DDRA,R16 // Set port A for I/O. Last two pins of A are switch button input, first pin is LED output OFF state
READSW: IN R16,PORTA // read the data from port A to R16, portA is simply the register alias for Port A.
ROR R16 // Rotate right with carry
LDI R15,0x80 // R15 = 8\'b1000 0000
ADD R15,R16 // Add 1 to the msb of r16. Carry is set if R16.7 = 1, it helps to branch
BRCS NEXT // Branch is Carry is set. If switch 1 is off jump to NEXT to see if switch 2 is on.
LDI R16,0x00
OUT PORTA, R16 // if switch 1 is ON, Turn on LED which PORTA.7
IJMP READSW // Read the status of the switch again, IJMP = Indirect Jump, much like the short jump SJMP
NEXT: ROR R16 // Rotate right, then add 1 at the msb and check the carry.
LDI R15,0x80 // R15 = 8\'b1000 0000
ADD R15,R16 // Add 1 at the MSB
BRCS READSW // If switch 2 is OFF check both the switches again
LDI R16,0x80 // Switch 2 is ON, turn of LED.
OUT PORTA, R16 // if switch 2 is ON, Turn off LED which is PORTA.7
IJMP READSW
DDRA, PORTA are all register aliases. The AVR interpretor knows very well what physical registers they mean. However, you must include the proper header file in our code before generating the hex code. The proper header files (like m8515def.inc) are freely availble on avr websites.

