How to Use KPCAVR - teraKUHN integer Pascal Compiler for Arduino's with AVR CPUs

To use KPCAVR, start by downloading the Windows console app to your Windows computer.

Download Debug AVR (DebugAVRInstall.msi) for 32-bit Windows (works on 64-bit Windows)
includes KPCAVR Pascal compiler

Unlike other teraKUHN integer compilers, KPCAVR does not come with an assembler, and must be used in conjuction with the Arduino IDE development tools. If you don't already have Arduino IDE installed, download it and install it.

The teraKUHN integer Pascal Compiler for Arduino (KPCAVR) has been tested with an ATmega328 which is the CPU in an Arduino Uno.


KPCAVR - teraKUHN integer Pascal Compiler for Arduino

KPCAVR (teraKUHN integer Pascal Compiler for Arduino) is an AVR CPU architecture Pascal Compilers that might be helpful to the hobbyist or student who wishes to develop code for an Arduino AVR architect CPU. This compiler is only intended to support integer, not real, math operations at runtime for embedded systems. Floating point operations are allowed at compile time; however, since the processor this compiler target only support integer math in hardware, only integer math operations are supported in the runtime code generated. Floating point math operations supported at compile time for constant initialization include: abs(), sin(), cos(), tan(), acos(), asin(), atan(), floor(), round(), ceil(), log(), sqrt(), and exp().
Thus, you can make statements like:
const nDistance=sqrt((nX*nX)+(nY*nY));
The current implementation only supports a very limited subset of the Pascal language. The grammar for the compiler is based on the grammar presented in the back of the book "Compilers" by Aho, Sethi, and Ullman (the dragon book). However, this grammar has not yet all been implemented. The compiler only supports single file compilation. The compiler does not support multiline comments, but does support single line comments starting with {. The compiler does not support strings or records. The compiler should generate correct code for integer calculations and assignment as well as IF statements and WHILE, REPEAT, and FOR loops. Pascal is a case insensitive language. Thus, the latest versions of KPCAVR treats all keywords as case insensitive. However, KPCAVR treats symbols as case sensitive. Thus, 'Variable1' and 'variable1' are considered different symbols since the first character has a different case.

The KPCAVR is a 32-bit Windows application that can run on 32-bit or 64-bit Windows. They are not digitally signed so you will get a warning when you install them that the author is unknown. If you get a dialog inidicating 'Windows protected your PC', select 'More info', and then 'Run anyway'. Updates happen a few times a year so if something isn't working, make sure you have the latest version installed. Also, if you identify any problems please report them to the teraKUHN project.


Compile and assemble code

First you need to write a program. Here's an example of a Pascal program that sets bits in I/O port 081h as if it's wired to an H-bridge circuit for turning on and off motors, and calling a wait routine in between each motor state.

  program hbridge(input, output);
 
  const nTimePerDistance = 1; { ToDo: implement }
  const nTimePerAngle = 1; { ToDo: implement }
 
  procedure SimpleDelay(nTime : integer);
  begin
    while (nTime > 0) do
    begin
      nTime := nTime - 1
    end
  end;
 
  { stopped }
  procedure Stop;
  begin
    outp(0x81, 0x00);
    SimpleDelay(1000)
  end;
 
  procedure setup;
  begin
    Stop
  end;
 
  { forward }
  procedure Forward(nDistance : integer);
  begin
    outp(0x81, 0x0F);
    SimpleDelay(nDistance * nTimePerDistance)
  end;
 
  { reverse }
  procedure Reverse(nDistance : integer);
  begin
    outp(0x81, 0x05);
    SimpleDelay(nDistance * nTimePerDistance)
  end;
 
  { forward on one side and reverse on the other }
  { results in turning }
  procedure TurnRight;
  begin
    outp(0x81, 0x07);
    SimpleDelay(5000)
  end;
 
  procedure loop;
  begin
    Forward(5000);
    Stop;
    Reverse(5000);
    Stop;
    TurnRight;
    Stop
  end;
 
  begin
    setup;
    while (true) do
    begin
      loop
    end
  end.

Otherwise pick almost any of the numerous pieces of example code on the internet.

The command to run KPCAVR will look something like this:

  KPCAVR.exe {source_file_name}.pas {output_file_name}.S

KPCAVR will produce a *.S file in the same directory as the *.pas source file. Make a note of the path to the directory where this *.S file is created. Note that you must give the assembly file a capital 'S' file extension in order for the Arduino IDE assembler (avr-as) to recognize it.

You can then use the assembler and linker that comes with the Arduino IDE. The commands to run the Arduino IDE assembler will look something like this:

  {tool_path}\avr-as" -c -mmcu=atmega328p {output_file_name}.S -o {output_file_name}.S.o
  {tool_path}\avr-gcc" -Os -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o {output_file_name}.S.elf {output_file_name}.S.o -lm
  {tool_path}\avr-objcopy" -O ihex -R .eeprom {output_file_name}.S.elf {output_file_name}.S.hex

The Arduino IDE tools will produce a *.hex file in the same directory as the *.S assembly source file. Make a note of the path to the directory where the *.hex file is created. That's the directory where you will load the *.hex file from inside of DebugAVR or in your EPROM programmer or upload to flash memory. To upload a compiled *.hex file to an Arduino board without using the Arduino IDE, you can use the avrdude.exe tool. Note the tool path to avrdude.exe is not necessarily the same as the path to the avr-as.exe assembler. The command to run avrdude will look something like this:
{tool_path}\avrdude -C{config_path}\avrdude.conf -v -patmega328p -carduino -PCOM21 -b115200 -D -Uflash:w:{output_file_name}.S.hex:i


Disclaimer, Comments on Shareware, and Copyright

KPC AVR is distributed 'as is', so the user bears all responsibility for testing that the functionality is acceptable. Use KPC AVR at your own risk. If this software has undesired effects, your sole recourse is to discontinue use of it. That said, we hope you enjoy using this software and are able to get some practical work accomplished with it. Feel free to write to us if you have questions, suggestions, feeback, or bug reports regarding it. Contact us at the teraKUHN project.

KPC AVR is a shareware programs. Please help more people learn about it by pointing friends, classmates, and co-workers at the download location for them to try it. Please do not redistribute it on your own. If you use KPC AVR on a regular basis, you are required to register it. With a registration you will receive an email notification when the next version of the software is available. To register through PayPal send $5.50 US to teraKUHN@yahoo.com with the program name 'KPC AVR' in the note to seller. Alternatively send a check for $5.00 US payable to R. J. Kuhn and a note with your email address and the program name 'KPC AVR' to:
R. J. Kuhn
5412 158th Pl NE
Redmond, WA 98052

Software Copyright (C) 2001 - 2002, 2021 - 2024 R. J. Kuhn - All Rights Reserved





teraKUHN solutions
Hardware Setup Control System Hardware Abstraction Development Tools
Smartphone Controller
(with PWM or advanced latch circuit)
USB-Controller teraKUHN IO System (KIOS) Debug Z80 KCC80 KPC80
Smartphone Controller
(with advanced latch circuit)
AVR-Controller C & C++ coding abstraction macros Debug AVR Arduino C++ KPCAVR
Arduino Controller
.
N/A C & C++ coding abstraction macros Debug AVR Arduino C++ KPCAVR

. custom hardware
.
N/A teraKUHN IO System (KIOS) Debug Z80 KCC80 KPC80

. custom hardware
.
N/A none available Debug 6811 KCC11 KPC11



Here are some architecture characteristics that can be demonstrated by these two control solutions. These differences can be used for teaching processor and microcontroller concepts.

control solutions
Z80 custom with KIOS AVR Arduino
CISC RISC
von Neumann architecture Harvard architectures
Run-time software abstraction layer Compile time coding abstraction macros
custom electonics board pre built Arduino board



Text Copyright (C) 2024 R. J. Kuhn. Please note that you are not allowed to reproduce or rehost this page without written permission.

Flag Counter