Tutorial 1
Basics

Program structure

Here's the typical structure of Java source program:

import mcujavasource.mcu.*;

public class MyProgram extends Microcontroller
{
  //constants
  private static final int MY_NUMBER = 25;
  
  //fields
  private final Pin myPin;
  private final Timer timer;
  
  private volatile int counter = 0;
  
  public void init()
  { //register initialization on startup
  }
  
  public void start()
  { //global enabling interrupts, if needed, main program
  }
  
  //methods
  private void myMethod()
  { //do something
  }
  
  private class MyListener implements TimerListener
  {
    public void timerOverflowed()
    { //do something, it will be interrupt handler
    }
  }
  
}

import mcujavasource.mcu.*; - all MCU classes are in mcujavasource.mcu package. Documentation for this package is present.

public class MyProgram extends Microcontroller - main class is public, must extend Microcontroller. Class name doesn't matter for translator.

private static final int MY_NUMBER = 25; - constants are static final in Java. There is no #define in Java, so this is used instead of #define.

private final Pin myPin; - Object fields exist only in Java source.

public void init() - allways put register initialization to this method. This method is called after MCU reset or power on. It's highly optimized when translating. Don't enable interrupts globally here. All statements in this method can be shuffled, and this must not affect the work of device.

public void start() - this method is call after init(). Enable interrupts globally here, perform primitive variable calculations, if needed. Don't put empty discontinious loop (for(;;);), it's added automatically, if needed.

private class MyListener implements TimerListener - listener class. Allways implement the method(s) specified by interface (now TimerListener). Look to documentation for method name.

public void timerOverflowed() - contents of this method will be in the interrupt handler

Simple program

Program with no interrupts. Two LEDs flash one after another. Every LED's anode is connected to +5V through 330 Ohm resistor, cathode - to MCU port.

import mcujavasource.mcu.*;

/** Tutorial 1: simple program without interrupts.
* Two LEDs flash one after another.
* LED's anode is connected to +5V through 330 Ohm resistor,
* cathode - to MCU port.
*/
public class Tutorial1 extends Microcontroller
{
  /** Pin of the first LED, PB1 */
  private final Pin led1 = getHardware().getPort("B").getPin(1);
  
  /** Pin of the second LED, PB0 */
  private final Pin led2 = getHardware().getPort("B").getPin(0);
  
  public void init()
  { // setting all pins to input
    getHardware().setAllPortsDirection(Pin.IN);
    // enabling pull-ups on each pin
    getHardware().setAllPortsPullUp(true);
    led1.setDirection(Pin.OUT);
    // led1 is initially turned on
    led1.setOutput(Pin.LOW);
    led2.setDirection(Pin.OUT);
    led2.setOutput(Pin.HIGH);
  }
  
  public void start()
  { Delay delay = getHardware().getDelay();
    while(true)
    { led2.setOutput(Pin.HIGH);
     led1.setOutput(Pin.LOW);
     // pause 1 second
     delay.sleep(1000);
     led1.setOutput(Pin.HIGH);
     led2.setOutput(Pin.LOW);
     delay.sleep(1000);
    }
  }
  
}

Download java source file

Source file name must be class name + ".java" (in this case Tutorial1.java)

Note, that the first comment must be directly before class declaration, not before any "import".

led1 and led2 fields are final, so programmer cannot assign them twice.

Hardware variables - variables, which represent hardware. In this program it's led1, ded2 and delay

Don't assign hardware variables twice. It may result in undefined register handling, and program will not work correct. Use separate variable for each pin, timer, etc.

Understanding the program

All hardware objects are got in the same way: by appropriate method of HardwareFactory class. Open Javadoc for HardwareFactory now and inspect the list of methods. As you see, every internal device of MCU are got from here. Also some entities (as Delay) can be got. To get HardwareFactory, call getHardware() (this method belongs to Microcontroller class). For example, to get port B, write getHardware().getPort("B"). Find the methods that are used in this example.

Go to Port Javadoc. Find the method getPin(int index). To get the Pin, Port instance must firstly be got using getHardware().getPort("portId"). If myPort is port instance, code myPin = myPort.getPin(5) works. Next go to Pin documentation and look for what you can do with pin.

Go to Delay class. Its sleep is the same as for computer.

Compiling using NetBeans

It's the easiest way.

Install NetBeans module if you haven't done it before

Start new project (choose File - New Project, project type is "MCU", project template - "MCU Java source application"). Copy the code above, replace "class Tutorial1" with "class Main".

Choose Build - Build main project (F11) to compile project

To change transform settings (e.g. target MCU), modify the file "transform.properties".

To change Makefile template, modify "Makefile_mcuarch" ("Makefile_avr" for avr).

Compiling by hand

Save Tutorial1.java file with UTF-8 encoding. Open commandline shell and go to directory where Tutorial1.java is located:

cd directoryPath

Compile a Java class

javac -classpath . Tutorial1.java

If there are no compiler errors, go to the next step. Else correct the mistakes and recompile until there will be no errors.

Generated file Tutorial1.class is not used now. Copy Tutorial1.java to directory with McuJavaSourceTransformer.jar and go to that directory

java -jar McuJavaSourceTransformer.jar -mcuarch avr -mcu atmega8 -d . -SsourceEncoding=UTF-8 -SresultEncoding=UTF-8 -SclockFrequency=1000000 Tutorial1.java

Check the encoding of input java file and output c file. If you use WinAVR under Windows, resultEncoding will probably be windows-1251 or windows-1252 or similar.

File main.c will be generated. Then create makefile and compile as usual:

make all

If compiler gives an error because of unknows chars at the beginning of file, change resultEncoding.