News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Microcontrollers

Started by felipe, October 26, 2017, 04:11:54 PM

Previous topic - Next topic

felipe

Here it is a sample program:

.cseg
.org 0

.equ PORTB =0x18 ; Port b's output register.
.equ DDRB  =0x17 ; Port b's data direction
;  register.

ldi r17,0xff ; Configure port b as output.
out DDRB,r17

ldi r16,0x00 ; Init the counter.

lp:
out PORTB,r16 ; Put the count in port b.
inc r16

rjmp lp ; Infinite loop.


I want to explain a little just some instructions (and directives). Just like a service for those who want to get closer with assembly programming with a microcontroller... :idea:
LDI = LOAD IMMEDIATE
RJMP = RELATIVE JUMP
.CSEG = USE THE PROGRAM (FLASH) MEMORY FOR THIS PIECE OF CODE
.ORG  0= START THE PROGRAM EXECUTION IN ADDRESS 0 FROM PROGRAM MEMORY

...Isn't super fun?  :bgrin:

hutch--

I have seen long ago people who wrote their own assembler for various non x86 hardware and it was usually because the instruction set is nowhere as complicated as the x86/64 sets. Where you have close matching between common mnemonics for a particular processor and the opcodes they represent, the task is not all that difficult as you have the logic,

mnemonic = opcode

and a method of keeping track of the arguments AND basically line numbers for each mnemonic.

aw27

In general it is easy to learn those RISC instruction sets. Not so easy for Arm because there are many variations and specificities, if you have a book make sure you do have the specific microprocessor available otherwise you are largely waisting your time.

Siekmanski

Hi Filipe,

A "hello world" example for the Atmega, it's a blinking led. ( the Atmega88 runs on his max. with a 20 MHz crystal )
Included the whole project for AVR Studio and pictures to show the pin connections and a simple 5V voltage regulator.

;Siekmanski's led project

.include <m88def.inc>   ; Atmega88 definitions

.equ F_CPU = 20000000   ; Crystal = 20 MHz "cpu clock speed for Wacht-macro"
.include "_wachten.inc" ; Wacht-macro --> _Wacht_us 1000 == 1 milisecond, 1000000 == 1 second


.cseg                   ; Code Segment
.org 0x0000             ; Code load adres 0x0000

    rjmp    Init        ; Make Relative jump to Init Lable


Init:
    ldi     r16,low(RAMEND) ; Init STACK
    out     SPL,r16
    ldi     r16,high(RAMEND)
    out     SPH,r16

    ldi     r16,0b11111111  ; r16 = set all 8 pins as output
    out     DDRC,r16        ; Save in PORTC Data direction Register


Start:                   
    sbi     PORTC,PC5       ; set pin 28 (PC5) high ( led on )

    _Wacht_us 1000000       ; wait 1 second

    cbi     PORTC,PC5       ; led off

    _Wacht_us 1000000       ; wait 1 second

    rjmp    Start


Creative coders use backward thinking techniques as a strategy.

felipe

That's great siekmanski, thank you very much.  :icon14:

aw27

For people interested in more recent versions of Atmel Studio this is a snapshot of version 7, which is composed from a Visual Studio Isolated Shell. Very cool.


Siekmanski

Build your own computer for less than $5.00 and play Music and Demo graphics on an Atmega88 microcontroller ($1.50)  8)

https://www.youtube.com/watch?v=sNCqrylNY-0

Linus Akesson shows you how to do it: http://www.linusakesson.net/scene/craft/
Creative coders use backward thinking techniques as a strategy.

felipe

 :greenclp: This guy is a genius. First i thought, sure he write all in c...but voila, all in assembly  :greensml: Amazing project.  :t

felipe

Ok, i was wrong. There are a few sources in c. But still is a great project.   :icon14:  :icon_mrgreen: