Hints for using ZBasic on an Arduino

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Hints for using ZBasic on an Arduino

Post by spamiam »

I just modified some Arduino Nano devices to work with ZBasic. It was surprisingly hard to figure out how to make the ZBasic IDE happy with it. I wanted to use a Nano so I could have a USB programing interface, and all the I/O pins of the CPU available.

You will need to program the Nano with the ZBoot bootloader. The Nano uses a 16MHz clock which is not completely compatible with the standard settings for ZBasic.

You will need to recompile the ZBoot bootloader to be compatible with a 16 Mhz compatible communication speed. I selected 125000 baud. In the file makeboot.bat you will need to edit some settings.

Use set

Code: Select all

set F_CPU=16000000

Code: Select all

set BAUD=125000
and

Code: Select all

set DEVICE=mega328p
You do not have to edit zboot.h, but if you turn OFF the double speed function of the Nano's hardware, you might have slightly more reliable communication. Add the double slash in front of the "#define...."

Code: Select all

// Uncomment this line to use the USART's 2x mode.
	//#define USE_USART_2X
If you use a baud rate that is completely compatible with a 16Mhz clock (i.e. 125000) you will not need double speed. You should use the double speed function when you use a baud rate that is not perfect for the 16MHz clock (but has less than 2% error). I recommend using one of the compatible speeds. Remember, this is just a speed used internally in the Nano. Externally the Nano is working via USB, and the PC is using USB even though PC programs think they are using a serial port.

You will need to check and possibly modify some fuses on the Nano's CPU.

Brownout is set to 2.7V on mine and that seems fine. You could also select 4.xV

Bootsz is 1024 words with Address $3C00 on a 328P CPU

Bootrst should be checked.

Then the last is to set the SUT_CKSEL to "Ext Crystal Osc 8.0 - MHz; Startup time PWRDOWN/RESET: 258clk/14clk + 4.1 ms". This was very important in my experience. The default setting was a much longer startup time which appears to be too long for the ZBasic IDE. It would fail to communicate with the bootloader. The faster startup time allows proper communication.


Then in your ZBasic Programs, you will need to let the compiler know what to do. Here are the options I used at the top of the first ZBasic file:

Code: Select all

Option Signon Off
Option TargetDevice ATmega328P
Option DeviceParameter package "PDIP-28"
Option DeviceParameter clockFrequency 16000000
Option DeviceParameter rtcFrequency 500
Option DeviceParameter rtcScale 1
Option DeviceParameter swUartDivisor 8
Option DeviceParameter swUartMinSpeed 300
Option DeviceParameter swUartMaxSpeed 19200
Option DeviceParameter swUartBaseSpeed 2400
Option DeviceParameter timerSpeed1Divisor 1
Option DeviceParameter timerSpeed2Divisor 8
Option DeviceParameter ZBasicBootloader True
Then you need to add some 16MHz compatible serial speeds.
Click "Options" on the menu, then select "Open User Options File"

Look for the line

Code: Select all

debug.speed=115200
. It may have a different value in your case. After it, add:

Code: Select all

debug.speed.list=300,600,1200,2400,4800,9600,14400,19200,28800,38400,50000,57600,76923,100000,115200,125000,230400,250000,460800,500000
Do the same for

Code: Select all

download.speed=115200

Other Arduino devices will need similar, but not necessarily identical changes.
twesthoff
Posts: 247
Joined: 17 March 2006, 6:45 AM
Location: Fredericksburg, VA

Hints for using ZBasic on an Arduino

Post by twesthoff »

This would make a nice application note!

On 10/8/2013 10:05 AM, ZBasic wrote:
I just modified some Arduino Nano devices to work with ZBasic. It was surprisingly hard to figure out how to make the ZBasic IDE happy with it. I wanted to use a Nano so I could have a USB programing interface, and all the I/O pins of the CPU available.

You will need to program the Nano with the ZBoot bootloader. The Nano uses a 16MHz clock which is not completely compatible with the standard settings for ZBasic.

You will need to recompile the ZBoot bootloader to be compatible with a 16 Mhz compatible communication speed. I selected 125000 baud. In the file makeboot.bat you will need to edit some settings.

Use set Code: F_CPU=16000000 Code: set BAUD=125000 and Code: set DEVICE=mega328p

You do not have to edit zboot.h, but if you turn OFF the double speed function of the Nano's hardware, you might have slightly more reliable communication. Add the double slash in front of the "#define...."
Code: // Uncomment this line to use the USART's 2x mode.
   //#define USE_USART_2X

If you use a baud rate that is completely compatible with a 16Mhz clock (i.e. 125000) you will not need double speed. You should use the double speed function when you use a baud rate that is not perfect for the 16MHz clock (but has less than 2% error). I recommend using one of the compatible speeds. Remember, this is just a speed used internally in the Nano. Externally the Nano is working via USB, and the PC is using USB even though PC programs think they are using a serial port.

You will need to check and possibly modify some fuses on the Nano's CPU.

Brownout is set to 2.7V on mine and that seems fine. You could also select 4.xV

Bootsz is 1024 words with Address $3C00 on a 328P CPU

Bootrst should be checked.

Then the last is to set the SUT_CKSEL to "Ext Crystal Osc 8.0 - MHz; Startup time PWRDOWN/RESET: 258clk/14clk + 4.1 ms". This was very important in my experience. The default setting was a much longer startup time which appears to be too long for the ZBasic IDE. It would fail to communicate with the bootloader. The faster startup time allows proper communication.


Then in your ZBasic Programs, you will need to let the colpiler know what to do. Here are the options I used at the top of the first ZBasic file:
Code: Option Signon Off
Option TargetDevice ATmega328P
Option DeviceParameter package "PDIP-28"
Option DeviceParameter clockFrequency 16000000
Option DeviceParameter rtcFrequency 500
Option DeviceParameter rtcScale 1
Option DeviceParameter swUartDivisor 8
Option DeviceParameter swUartMinSpeed 300
Option DeviceParameter swUartMaxSpeed 19200
Option DeviceParameter swUartBaseSpeed 2400
Option DeviceParameter timerSpeed1Divisor 1
Option DeviceParameter timerSpeed2Divisor 8
Option DeviceParameter ZBasicBootloader True

Then you need to add some 16MHz compatible serial speeds.
Click "Options" on the menu, then select "Open User Options File"

Look for the line Code: Debug.speed=115200 . It may have a different value in your case. Replace it with the following:
Code: debug.speed.list=300,600,1200,2400,4800,9600,14400,19200,28800,38400,50000,57600,76923,100000,115200,125000,230400,250000,460800,500000

Do the same for Code: download.speed=115200


Other Arduino devices will need similar, but not necessarily identical changes.



rich
Posts: 81
Joined: 19 November 2015, 12:23 PM

Post by rich »

If the clock oscillator was swapped out with a 14.7456 MHz part, would all these changes need to be made.

Richard
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

rich wrote:If the clock oscillator was swapped out with a 14.7456 MHz part, would all these changes need to be made.
I don't think so. It is important to note that the original post was referring to using Arduino hardware to make a ZBasic device. This an entirely different proposition than trying to build a ZBasic application to run on unmodified Arduino hardware - using the Arduino as an Arduino but writing the application in ZBasic.
- Don Kinzer
rich
Posts: 81
Joined: 19 November 2015, 12:23 PM

Post by rich »

oh, what i meant was if an Srdino UNO board had it's clock changed to 14. MHz and the ZBasic bootloader load and the fuses set correctly, would the board be a ZBasic board?

Richard
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

rich wrote:[...]would the board be a ZBasic board?
No, because the ZX-328N bootloader is not the same as the bootloader you would get if you build for 14.7MHz, etc. There are some minor differences between a ZX bootloader and a generic device bootloader. The IDE interacts with the two bootloaders using a different (but similar) protocol.
- Don Kinzer
Post Reply