I suggest the folowing steps... (1) Start with the loop termination algorithm I outlined in the frst post of this thread. (2) Try to add step=0 handling, should be reasonably easy. (3) Try to add runtime step decrement handling, this will have a more difficult effort/size/complexity compromise. (4) Conduct a rigorous gamut of testing, especially considering the extremes of the range of each variable type to identify over/under flows.
I have checked the source code for the FOR loop and and this documentation was helpfully provided... 'Then to this: (27/8/2010) 'V = SV - ST 'If SV not const or EV not const, if V > EV then goto SysForLoopEnd(n) 'SysForLoop(n): 'V += ST '... 'if V <= EV then goto SysForLoop(n) 'SysForLoopEnd(n): This is JUNK. It only works as expected, that is the value of V (loop variable) is on or between the limits (SV - start value, EV - end value) for the special case where the difference between the limits...
Using GCB Great Cow BASIC (0.98.01 2017-10-27) on Linux Mint 18.3 There appear to be a considerable range of numeric values of FOR/NEXT loop parameters that produce unexpected operations but none get a mention in the documentation. A specific example is For i = 1 to 252 step 5 This initially produces the expected steps of i=1,6,11,16... etc, but then spans the entire range again with i= 0,5,10,15 etc. For i = 252 to 1 step 5 This loop spans the entire range 4 times. The unexpected overflow problems...
I have tested on a mega328p and the program seemed to work but the assembler code contains fragments like this... AVRPWMOFF31: lds SysCalcTempA,AVRPWMCHANNEL cpi SysCalcTempA,2 brne ENDIF19 in SysValueCopy,TCCR0A cbr SysValueCopy,1<<COM0B1 out TCCR0A,SysValueCopy in SysValueCopy,TCCR0A cbr SysValueCopy,1<<COM0B0 out TCCR0A,SysValueCopy The TCCR0x registers are associated with the unused timer 0, while the conditional tests probably prevent execution the unwanted default AVRPWMCHANNEL 2 is wasting...
The mega8 16x speed reference to slave mode is at the top of page 164 (Bit Rate Generator Unit) but if the same formula is used in master mode then the limitation is relevant. To use the 400khz TWI bus speed then the CPU must be faster than 6.4mhz
Using GCB Great Cow BASIC (0.98.01 2017-10-27) on Linux Mint 18.3 When attempting to compile programs with this configuration this unhelpful error is produced.. /opt/GCBASIC/include/lowlevel/hwi2c.h (508): Error: Cannot store -3 in the byte variable TWBR ;----- Configuration #chip mega8, 1 ' Setup Hardware I2C HI2CMode Master #define HI2C_DATA #define HI2C_BAUD_RATE 100 ' or 400khz 'Set up LCD #define LCD_IO 10 #define LCD_I2C_Address_1 0x7E The reason for the problem is alluded to in the Atmel data...
The demonstration program now compiles cleanly and operates as expected. Thanks for the prompt responses.
The GCB particulars were clearly stated in the first line of the post. The code is this https://github.com/Anobium/Great-Cow-BASIC-Demonstration-Sources/blob/master/PWM%20Solutions/AVR%20Command%20Usage%20Examples/Mega328p-005.gcb Changed cip version attached.