Gcbasic 2019
Gcbasic 2019
For information on installing Great Cow BASIC and several other programs that may be helpful, please
see Getting Started with Great Cow BASIC.
If you are new to programming, you should try the Great Cow BASIC demonstration programs these
explains everything in a step-by-step manner, and assumes no prior knowledge.
If you have programmed in another language, then the demonstration files and this command
reference may be the best place to turn.
If there is anything else that you need help on, please visit the Great Cow BASIC forum.
- Complete the installation using the default values - select all the programmers but not the portable
mode.
- When a Great Cow BASIC source file is opened, check out the “GCB tools” menu ( IDE Tools / GCB tools
) - through this menu you can access the oneclick commands. Or try the right mouse button - this will
access the same options.
- The IDE Tools… commands (function keys F5 - F8) starts a Great Cow BASIC utility which calls the
(1)
batchfiles for compiling sourcecode and programming ("flashing") the target microcontroller. You
have to select the appropriate programmer in “Edit Programmer Preferences” ( IDE Tools / GCB tools /
Edit Programmer Preferences or by pressing Ctrl+Alt+E ). Find your programmer in the list and drag it
to the top beneath the heading “Programmers to use (in order)”. Great Cow BASIC will now attempt to
flash the microcontroller with that programmer first when you click on "Make HEX and FLASH" ( F5 )
or "FLASH previous made hexfile" ( F8 ).
- In the unlikely event that your programmer is not listed you can add it by pressing “Add…” in “Edit
Programmer Preferences”. You would have to know the working directory and command line options
etc. for the programmer. See the help tips at the bottom by clicking on the fields.
- For project-specific flashing you can edit the current programmers in “Edit Programmer Preferences”
to suit your needs by clicking on “Edit…”. Use the “Use If:” parameter to choose programmer
1
preferences. See the help tips. The chip model is autodetected by the IDE for use in “Use IF:” or in
command line options etc.
- Some programmers use a .hex file to "flash" the microcontroller. By selecting "Make HEX" ( F5 ), Great
Cow BASIC will compile the program and make a .hex file in the same directory as the Great Cow BASIC
file. This method can also be used to check for errors in the Great Cow BASIC program before flashing.
(1)
You need a suitable programmer to do this, and instructions should be included with the
programmer on how to download and connect the hardware to the microcontroller.
Programmer Preferences
The “Programmer Preferences” is a software tool to control and set-up the different programmers.
Review the GIF for instructions.
[graphic]
2
Changes
Formal Release of <>
• Revised GLCD ST7735 section to remove colors, added TFT colors to the GLCD overview section
Release 0.98.03
• Updated Randomize section
• Added HPWM_CCPTimerN
3
• Added LCD_IO_3
Release 0.98.02
• Updated chip section
• Updated compiler insights to ASM comments and an update to the command line parameters
• Added HSerPrintStringCRLF
Release 0.98.01
• Updated ILI9341 section for OLED fonts
• Updated for 10-bit PWM with resolution and CCP/PWM with DisableCCPFixedModePWM
constant
4
• Added HX8347 GLCD section
Release 0.98.00
• Updated #define USART_TX_BLOCKING examples
• Updated Conditions page with the known constraints of using functions as test conditions
• Updated ADC section to support all three read options for ReadAD, ReadAD10 and ReadAD12
• Updated PWM section to reflect support for PWM3 and PWM4 channels.
• Updated the GLCD section to include SSD1306 low memory configuration and table of the GLCD
capabilities.
• Updated the GLCD section to include SSD1306 and SH1106 low memory configuration and add
examples
5
• Added Ellipse and FilledEllipse
• Added OLED information to SSD1306 section only. OLED fonts are only support on the SSD1306
GLCD at this release.
Release 0.97.00
• #Startup section revised to document latest changes to priority of startup subroutines
Release 0.96.00
• Revised String usage and String examples to show use of quote marks within a string
6
Release 0.95.010
• Added improvements to SerSend and SerPrint
• Revised 7 Segment Section to provide clarity, improve code and remove errors.
Release 0.95.009
• Added SH1106 GLCD driver
Release 0.95.008
• Updated version number only.
Release 0.95.007a
• Added #option explicit.
Release 0.95.007
• Added new capabilities to support 16F1885x range of Microcontrollers.
• Removal of SetWith, FnEQUBit and FnNOTBit should now be replaced by built-in compiler
operations. Bits are now correctly handled by built-in Not operator.
• New command #option volatile. Sensitive pins can be marked as volatile, and native assignment
operators will set without glitching, replacing SetWith.
7
Release 0.95.006
• Completed rewrite of the Help File.
Release 0.95.005a
• Revised RS232 Hardware section to improve readability
Release 0.95.005
• Added LeftPad
Release 0.95.004
• Updated HSerial commands to support AVR 1, 2, 3 and 4.
Release 0.95
• Added weak pullup command set
• Added information on how to set address on mjkdz I2C LCD 1602 Modules
8
• Added new command set for second hardware I2C port. The HI2C2 command set
• Added new sub-section Compiler Options, moved options from Compiler Directives
9
• Added Support for USART2. HSerReceive1 and HSerReceive2
• Added HSerRecieveFrom
• Added HSerGetNum.HSertGetString
• Added Single channel measurement mode and Differential Channel Measurement mode to ADC
overview.
Release 0.94b
• Added HEFM support
Release v0.91
• Added USART_TX_BLOCKING
• Added LCD_SPEED
Release v0.9ho
• Updated parameter passing to Sub routines
• LAT has been deprecated. The compiler will redirect all I/O pin writes from PORTx to LATx
registers on PIC 16F1/18F.
Release v0.9hm
• Correct errors in PWM section and improved examples.
Release v0.9hn
• Changes to Arrays. Number of elements is now limited to 10,000 for 12F and 16F devices, or, the
available RAM.
10
3. constants and calculations within the single line data table entries are permitted
• New Pad command. The Pad method is used to create string to a specific length that is extended
with a specific character
v0.9hm
• Updated I2C - software and hardware. Demonstration code now uses Chipino demonstration
board. Changed to Serial I2C section with these new examples.
• Revised Functionality LCDHex now supports printing of leading zeros when the HEX number is
less than 0x10. call LCDHex as follows to ensure leading zeros are present.
LCDHex byte_value, LeadingZeroActive ; parameter called LeadingZeroActive
v0.9hl
• HSERPRINTCLRF - Added parameter to repeat the number of CRLF sent.
• Hardware I2C command set added. This is revised functionality to provide support the MSSP
module.
@0.9hk
• Help file updated to correct Power entry, it was in the incorrect section. Moved to Maths section
and other minors typos.
• Correct Timer0 information. Revised to show constants and the timer code was corrected.
@v0.9hj
• This information relates to the Hot Release 11 May 2014. Where functionality is not supported
by earlier versions of GCB please upgrade. Some functions will not work in the earlier releases
of Great Cow Basic.
11
Circle. Draws a circle on the GLCD screen.
FilledCircle. Fills a circle on the GLCD screen
Log function(s)
Power function.
@ v0.9hk
• Documented method for GCGB documentation.
Added MATHS.H
Added SQRT function.
@ v0.9hi
• Support for ST7735 documented. Functionality added to GLCD.h
• Revised GLCD section to include the one new and one undocumented device.
12
• Documented support for ST7735 GLCD.
• Trigonometry Circles
@v0.9hg
• Corrected GLCD Common Anode display pages
@v0.9hf
• Revised 7 Segment section to support Common Cathode. Split 7 Segment entry to show the two
options available.
13
@v0.9he
• New commands. Required post March 2014 LCD.h:
LCDHOME, LCDSPACE, LCDCreateGraph, LCDCursor, LCDCmd
• Added Concatenation
• Updated DisplayValue to show the support for HEX values. Required post March 2014
7Segment.h
• Updated GLCD example code to ensure the example compiled without external files.
• Updated the LCD Overview to include the LATx support for higher clock speed. Required post
March 2014 LCD.h
@v0.9hd
• Revised Rotate to clarify type supported byte types.
• Revised TABLE to show the limitation with respect to using WORDS when placing TABLES in
EEPROM
• I2CRestart
Jan 14
• New item(s):
Len, Asc, Chr, Trim, Ltrim, Rtrim, Swap4, Swap, Abs, Average, Trim, Ltrim, Rtrim, Wordtobin, Bytetobin,
GLCD, DectoBCD, BCDtoDec
Using variables
More on constants and variables
Acknowledgements
• Changes to:
14
Str, Hex, Poke, Else, Readtable, Exit (was exitsub)
Command line parameters Frequently asked questions
• Fixed typos.
@v0.9hg
• Corrected GLCD Common Anode display pages
@v0.9hf
• Revised 7 Segment section to support Common Cathode. Split 7 Segment entry to show the two
options available.
@v0.9he
• New commands. Required post March 2014 LCD.h:
LCDHOME, LCDSPACE, LCDCreateGraph, LCDCursor, LCDCmd
• Added Concatenation
• Updated DisplayValue to show the support for HEX values. Required post March 2014
7Segment.h
• Updated GLCD example code to ensure the example compiled without external files.
• Updated the LCD Overview to include the LATx support for higher clock speed. Required post
March 2014 LCD.h
@v0.9hd
• Revised Rotate to clarify type supported byte types.
• Revised TABLE to show the limitation with respect to using WORDS when placing TABLES in
15
EEPROM
• I2CRestart
Jan 14
• New item(s):
Len, sc, Chr, Trim, Ltrim, Rtrim, Swap4, Swap, Abs, Average, Trim, Ltrim, Rtrim, Wordtobin, Bytetobin,
GLCD, DectoBCD, BCDtoDec
Using variables
More on constants and variables
Acknowledgements
• Changes to:
Str, Hex, Poke, Else, Readtable, Exit (was exitsub)
Command line parameters Frequently asked questions
• Fixed typos.
16
GCBASIC [/O:output.asm] [/A:assembler] [/P:programmer] [/K:\{C|A}] [/V] [/L] [/NP]
filename
GCBASIC /version
/O:filename Sets the name of the assembly file generated to Same name as the input
filename. file, but with a .asm
extension.
(1)
/A:assembler Batch file used to call assembler . If /A:GCASM is The program will not be
given, Great Cow BASIC will use its internal assembled
assembler.
(1)
/P:programmer Batch file used to call programmer . This The program will not be
parameter is ignored if the program is not downloaded.
assembled.
/K:[C|A] Keep original code in assembly output. /K:C will No original code left in
save comments, /K:A will preserve all input code. output.
/NP Do not pause on errors. Use with IDEs. Pause when an error
occurs, and wait for the
user to press a key.
17
Switch Description Default
(1)
For the /A: and /P: switches, there are special options available. If %FILENAME% is present, it will be
replaced by the name of the .asm file. %FN_NOEXT% will be replaced by the name of the .asm file but
without an extension, and %CHIPMODEL% will be replaced with the name of the chip. The name of the chip
will be the same as that on the chip data file.
A batch file to load the ASM from Great Cow BASIC into MPASM. Command line should be like this:
A batch file to compile in Great Cow BASIC then load the ASM from Great Cow BASIC into GPASM.
Command line should be like this:
To instruct MAKEHEX.BAT to use GPASM. You have GPUTILS installed. The batch file should be edited
as follows:
18
Great Cow BASIC will compile the file, then assemble the program, and run this command:
`icprog -Lcompiled.hex`
'Assembler settings
Assembler = C:\Program Files\Microchip\MPASM Suite\mpasmwin
AssemblerParams = /c- /o- /q+ /l+ /x- /w1 "%FileName%"
'Programmer settings
Programmer = C:\Program Files\WinPic\Winpic.exe
ProgrammerParams = /device=PIC%ChipModel% /p "%FileName%"
This example will use MPASM to assemble the program. It will run the program specified in the
assembler = line, and give it these parameters:
Then, it will run the programmer, and give it these parameters when it calls it:
`/device=PIC16F88 /p "compiled.hex"`
%ChipModel% will get replaced with the chip you are using, so this the chip Great Cow BASIC will pass to
WinPIC.
Errors.txt
The compiler only produces the file errors.txt if there is an error. The creation of the errors.txt file
makes it easier for IDEs to detect if the program compiled successfully - if the file was not produced
then the IDE would be unalbe to present the error message to the user.
The file error.txt is always produced in the same folder as the compiler. Typically:
C:\GCB@Syn\GreatCowBasic\Errors.txt
19
Frequently Asked Questions
Why doesn’t anything come up when I run GCBASIC.exe?
Great Cow BASIC is a command line compiler. To compile a file, you can drag and drop it onto the
GCBASIC.exe icon. There are also several Integrated Development Environments, or IDEs, available for
Great Cow BASIC. These will give you an area where you can edit your program and a button to send
the program to the chip. Several are listed on the Great Cow BASIC website.
What Microchip PIC and Atmel AVR microcontrollers does Great Cow BASIC support?
Hopefully, all 8 bit Microchip PIC and Atmel AVR microcontrollers (those in the PIC10, PIC12, PIC16 and
PIC18 families). If you find one that Great Cow BASIC does not work with properly, please post about it
in the Compiler Problems section of the Great Cow BASIC forum.
No! For example, Set, SET, set, SeT, etc are all treated exactly the same way by Great Cow BASIC.
Setting a variable with other variable will expected. Great Cow BASIC support bitwise assignments. As
follows:
portc.0 = !porta.1
You can also use a shift function. As in other languages, by using the Shift Function FnLSL. AN
example is:
To set a bit of a port prevent glitches during the output update, use this method.
You can use this method to set a bit of a port. Encapsulate it in the SetWith method, this also eliminates
any glitches during the update, use this method.
20
SetWith(MyPORT, MyPORT OR FnLSL( 1, BitNum))
Because it hasn’t been thought of, or no-one has been able to implement it! If there are any features
that you would like to see in Great Cow BASIC, please post them in the "Open Discussion" section of the
Great Cow BASIC forum. Or, if you can, have a go at adding the feature yourself!
When using include files, in this instance the <ds3231.h> include, if you are not using all the functions
of the include file, does GCB know not to include the non used functions within the include file when
compiling, or does everything get included anyway. For instance, if I am not using the hardware I2C,
does all the code related to hardware I2C still get compiled in the code?
Great Cow BASIC only compiles functions and subroutines if they are called. Great Cow BASIC starts by
compiling the main routine, then anything called from there. Each time it finds a new subroutine that
is called, it compiles it and anything that it calls. If a subroutine is not needed, it does not get compiled.
Atmel AVR memory values are specified in WORDS in Great Cow BASIC. The Great Cow BASIC compiler
uses words, not bytes, for consistency between Microchip PIC and Atmel AVR microcontrollers. This
keeps parts of the compiler simpler.
See http://digital.ni.com/public.nsf/allkb/B59D2B24D624B823862575FC0056F3D0
21
Troubleshooting
Problem Common Causes More Assistance
Cannot compile a Great Cow BASIC has not been Great Cow BASIC Forums
program installed correctly - reinstall it.
There is a bug in Great Cow BASIC Post on the Great Cow BASIC Forums.
Ensure you state the version of your
compiler and attach your code as a ZIP.
22
Compiler Insights
This section will provide some insights into what the compiler does
How does the compiler cope with read only registers in the Chip Family 12 range?
Within this chip range the Option register is a write only register. Reading the register is not permitted.
Great Cow BASIC needs to update this when the user wants to change the configuration - the Sleep
process is an example of a user change.
The compiler handles this by the creation of the Option_reg byte variable. This byte is created by the
compiler to manage the required write process.
The Option_reg variable is a cache that compiler will create if any bits of option_reg have been set
manually.
If the user changes any of the bits in a program, then the compiler will find any uses of the option
instruction and insert a "movwf OPTION_REG" immediately before the option instruction to cache the
value in the buffer.
If Option_reg bits aren’t set individually anywhere, then option_reg doesn’t get created, and nothing
special is done with the option instruction.
Essentially the compiler maintains a special variable and manages the whole process without the user
being aware.
How does the compiler cope with the TRIS register in the 10f products?
The compiler ensures that a TRIS cache matches the actual TRIS register. The TRIS cache is a byte
variable called TRISIO. The TRISIO cache is required as TRIS is a write-only register.
All ports default to input ( where all TRIS bits to 1) on reset. Therefore, this is assummed to be the
value 255.
TRISIO is updated when required by the user code and then used in the writing to the correct register.
The example user code and the assiocated assembly shows TRISIO cache in use. This method complies
with datasheet.
User Code
23
'set as input
dir gpio.0 in
gpio0State = gpio.0
'set as output this will require TRIS GPIO to be set using the TRISIO cache.
dir gpio.0 out
gpio.0 = 1
ASM
;dir gpio.0 in
bsf TRISIO,0
movf TRISIO,W
tris GPIO
;gpio0State = gpio.0
clrf GPIO0STATE
btfsc GPIO,0
incf GPIO0STATE,F
;dir gpio.0 out
bcf TRISIO,0
movf TRISIO,W
tris GPIO
;gpio.0 = 1
bsf GPIO,0
Anywhere that an individual TRIS bit is set/cleared by change the port direction, the bit in the cache is
changed and then that gets written to the TRIS register.
It may be useful to force comments into the ASM file. The verbose mode of creating the ASM will
include ALL the source program as comments but it may be useful to have specific comments in the
ASM to aid the understanding of code or to support debugging.
Example.
The source file contains the following, where the comment text is OSCCON type is 100
24
asm showdebug OSCCON type is 100
OSCCON1 = 0x60
The generated assembly will be as following - this assumes verbose mode is not selected.
INITSYS
;osccon type is 100
movlw 96
banksel OSCCON1
25
Libraries Overview
About Libraries
Great Cow BASIC (as with most other microcontroller programming languages) supports libraries.
You can create you own device specific library, you are not limited to those shown below. If you create
a new device specific library - please submit for inclusion in the next release via the Great Cow BASIC
forum.
Maintenance of these libraries is completed by the Great Cow BASIC development team. If you wish to
adapt these libraries you should create a local copy, edit and save within your development file
structure. The development team may update these libraries as part of a release and we do not want
you to lose your local changes.
#include <3PI.H> 'this will include the 3PI capabilities within your program
To use a local copy of a library, simple inlcude the following in your user code
#include "C:\mydev\library\3pi.h" 'this will include a local copy of the the 3PI
capabilities within your program
3PI Polulu 3pi robot A library that interfaces the switch and the motors.
47XXX_EERAM.H I2C EERAM memory A device specific library for the Microchip EERAM
device classs
ADS7843 Touch Shield A library that interfaces with the ADS7843 touch screen.
BME280 Temp, Humidity and A library that interfaces with the BME280 and the
Pressure sensor BMP280 sensor.
DHT Temperature and A library that supports the DHT22 and the DHT11
Humidity Temperature and Humidity sensors.
26
Library Class Usage
DS1307 Clock A library that supports the timer clock and NVRAM
functions.
DS1672 Clock A library that supports the timer clock and NVRAM
functions.
DS3231 Clock A library that supports the timer clock and NVRAM
functions.
27
Library Class Usage
GLCD_T6963_64 Graphical T6963 LCD A device specific library for an Graphical LCD.
with 240 x 64 pixels
GLCD_T6963_128 Graphical T6963 LCD A device specific library for an Graphical LCD.
with 240 x 64 pixels
HEFLASH HEF Memory Driver A library that supports the HEF memory functions.
HWI2C_ISR_HANDL I2C Slave Driver A library that supports the use of a Microchip
ER microcontroller as an I2C slave.
HWI2C_MESSAGEIN I2C Slave A support library that supports the use of a Microchip
TERFACE microcontroller as an I2C slave.
HWI2C_ISR_HANDL I2C Slave Driver A library that supports the use of a Microchip
ERKMODE microcontroller as an I2C slave.
HWI2C_MESSAGEIN I2C Slave A support library that supports the use of a Microchip
TERFACEKMODE microcontroller as an I2C slave.
LCD2SERIALREDIRE LCD to Serial Handler A library that supports the use of a serial and PC
CT terminal as a psuedo LCD.
LEGO-PF Lego Mindstorms A library that supports the Lego Mindstorms robot
shield
LEGO Lego Mindstorms A library that supports the Lego Mindstorms robot
shield
MATHS Maths routines A library that supports maths functions such as logs,
power and atan.
MAX7219_ledmatrix LED 8*8 Matrix driver A library that supports the MAX7219 8*8 LED matrixes
_driver
MCP23008 i2C to serial A library that supports the I2C to serial functions.
MCP23017 i2C to serial A library that supports the I2C to serial functions.
28
Library Class Usage
MCP4XXXDIGITALP Digital Pot A library that supports the MCPxxxx range of digital
OT potentiometers.
MCP7940N Clock A library that supports the timer clock and NVRAM
functions.
NUNCHUCK Game controller A library that supports the NunChuck game controller.
PCA9685 PWM A device specific library for the 16channel PWM driver.
See the demonstrations for example on usage. Support
up to four devices via the I2C bus.
PCF85X3 Clock A library that supports the timer clock and alarms.
SMT_Timers Signal Measurment A library for Signal Measurment Timer for specific
Timer Microchip microcontrollers.
SONGLAY Music A library for play music. Supports QBASIC and RTTTL
format.
SRF02 Distance Sensor A library that supports the SRF02 ultrasonic sensor.
SRF04 Distance Sensor A library that supports the SRF04 ultrasonic sensor.
UNO_MEGA328P Shield A library that interfaces the shield with Arduino like
port addresses.
USB USB Supoort A library that interfaces the USB for 16f and 18f
microcontrollers.
29
Great Cow BASIC supports the following core libraries. These libraries are automatically included in
your user program therefore you do not need to use '#include' to access the libraries capabilities.
HWI2C I2C A library that supports the MSSP and TWI hardware modules of I2C
HWI2C2 I2C A library that supports the MSSP and TWI hardware modules of I2C on
channel two
HWSPI SPI A library that supports the MSSP and TWI hardware modules of SPI
LCD LCD A library that supports LCD functionality, library supports many
different communications methods.
REMOT Infrared A library that supports the functions of a NEC remote control.
E
STDBASI Utility Functions The library that contains many of the utility methods.
C
USART Serial The library that contains the hardware serial methods that use the MSSP
or AVR equivilent hardware module.
30
Library Class Usage
XPT2046 Touch Shield A library that interfaces with the APT2026 and the ADS7843 touch
sensors.
31
Acknowledgements
Developers and Contributors:
Evan Venn - Utilities, revised I2C routines, this help file and generally everything else!
Translation Contributors:
Other Contributors:
Chuck Hellebuyck - His documentation for the GLCD and other pieces, see http://www.elproducts.com.
Thomas Henry for the Select Case and the Sine Table examples.
Theo Loermans for the revised LCD sections and the library.
Chris Roper for the bitwise methods including the library including FnEquBit, FnNotBit, FnlslBit,
FnlsrBit, SetWith and 47xxx.
32
Microcontroller Fundamentals
Inputs/Outputs
About Inputs and Outputs
Most general purpose pins on a microcontroller can function in one of two modes: input mode, or
output mode.
When acting as an input, the general purpose input/output pin will be placed in a high impedance
state. The microcontroller will then sense the general purpose input/output pin, and the program can
read the state of the general purpose input/output pin and make decisions based on it.
When in output mode, the microcontroller will connect the general purpose input/output pin to either
Vcc (the positive supply), or Vss (ground, or the negative supply). The program can then set the state of
the general purpose input/output pin to either high or low.
Great Cow BASIC will attempt to determine the direction of each general purpose input/output pin, and
set it appropriately, when possible. Great Cow BASIC will try to set the direction of the general purpose
input/output pin. However, if the general purpose input/output pin is read from and written to in your
program, then the general purpose input/output pin must be configured to input or output mode by the
program, using the appropriate Dir commands.
'The port address is microcontroller specific. Portx.x is a general case for PICs
and AVRs
dir portb.0 in
dir portb.1 out
'The port address is microcontroller specific. gpiox.x is a general case for some
PICs
dir gpio.0 in
dir gpio.1 Out
33
For the specific ports and general purpose input/output pins available for a specific microcontroller
please refer to the datasheet.
PORTx maps to the microcontrollers Read: PORTx the port data register for a read uservar=PORTA
digital pins 0 to 7. Where x can be operation. uservar=PORTA
a,b,c,d,e,f or g .1
PORTx maps to microcontrollers Write: PORTx the port data register for a PORTA=255
digital pins 0 to 7. Where x can be write operation, and, where LATx is not PORTA.1=1
a,b,c,d,e,f or g required as Great Cow BASIC will implement
LATx when needed. See Option NoLatch for
more information on LAT registers and how
to disable this automatic function.
To read a general purpose input/output pin, you need to ensure the direction is correct DIR Portx IN is
set (default is IN) or a specific set of port bits. Where uservar = PORTx.n can be used.
Examples:
uservar = PORTb.0
uservar = PORTb
To write to a general purpose input/output pin, you need to ensure the direction is correct DIR Portx
OUT for port or a specific set of port bits. Where PORTx.n = uservar can be used.
Examples:
PORTb.0 = uservar
PORTb = uservar
Using a Mega328p as a general the following provides insights for the AVR devices. For the specific
ports and general purpose input/output pins available for a specific microcontroller please refer to the
datasheet.
PORTD maps to Mega328p (and, the AVR PORTD - The Port D Data Register - write PIND - The Port D
microcontrollers) digital pins 0 to 7 operation (a read operation to a port Input Pins Register
will provide the pull-up status) - read only
34
Port Write operation Read operation
PORTB maps to Mega328p (and, the AVR PORTB - The Port B Data Register - write PINB - The Port B
microcontrollers) digital pins 8 to 13. operation (a read operation to a port Input Pins Register
The two high bits (6 & 7) map to the will provide the pull-up status) - read only
crystal pins and are not usable
PORTC maps to Mega328p (and, the AVR PORTC - The Port C Data Register - write PINC - The Port C
microcontrollers) analog pins 0 to 5. operation (a read operation to a port Input Pins Register
Pins 6 & 7 are only accessible on the will provide the pull-up status) - read only
Mega328p (and, the AVR
microcontrollers) Mini
To read a general purpose input/output pin, you need to ensure the direction is correct DIR Portx IN is
set (default is IN) or a specific set of port bits. Where uservar = PINx.n can be used and therefore to
read data port use uservar = PINx.
Examples:
uservar = PINb.0
uservar = PINb
To write to a general purpose input/output pin you need to ensure the direction is correct DIR Portx
OUT for port or a specific set of port bits. Where PORTx.n = uservar can be used and therefore to write to
a data port use PORTx = uservar.
Examples:
PORTb.0 = uservar
PORTb = uservar
You can set a port as shown above with a variable, or, you can set with a constant or any combination
using the bitwise and logical operators.
PORTb = uservar OR 0b11110000 'will OR bits 4:7 to ensure bits 4:7 are set
35
The following is also valid - read a port.bit and then set port.bit with a variable or port value. As shown
below.
The user code above may cause issues with glitches when the read and write operations occurs. Let us
look at the generated assembler.
This option provides the following assembler resolving the glitch issue.
36
See also Dir, #Option Volatile
Configuration
About Configuration
(Note: This section does not apply to Atmel AVR microcontrollers. Atmel AVR microcontrollers do have a
similar configuration settings, but they are controlled through "Configuration Fuses". Great Cow BASIC
cannot set these - you MUST use the programmer software.)
Every Microchip PIC has a CONFIG word. This is an area of memory on the chip that stores settings
which govern the operation of the chip.
The following asects of the chip are governed by the CONFIG word:
• Oscillator selection - will the chip run from an internal oscillator, or is an external one attached?
• Automatic resets - should the chip reset if the power drops too low? If it detects it is running the
same piece of code over and over?
• Code protection - what areas of memory must be kept hidden once written to?
• Pin usage - which pins are available for programming, resetting the chip, or emitting PWM signals?
The exact configuration settings vary amongst chips. To find out a list of valid settings, please consult
the datasheet for the microcontrollers that you wish to use.
This can all be rather confusing - hence, Great Cow BASIC will automatically set some config settings,
unless told otherwise:
• Low Voltage Programming (LVP) is turned off. This enables the PGM pin (usually B3 or B4) to be
used as a normal I/O pin.
• Watchdog Timer (WDT) is turned off. The WDT resets the chip if it runs the same piece of code
over and over - this can cause trouble with some of the longer delay routines in Great Cow BASIC.
• Master Clear (MCLR) is disabled where possible. On many newer chips this allows the MCLR pin
(often PORTA.5) to be used as a standard input port. It also removes the need for a pull-up resistor
on the MCLR pin.
◦ If the microcontroller has an internal oscillator, and the internal oscillator is capable of
generating the speed specified in the #chip line, then the internal oscillator will be used.
◦ If the clock speed is 4 MHz or less, then the external XT oscillator mode is selected.
37
Note that these settings can easily be individually overridden whenever needed. For example, if the
Watchdog Timer is needed, adding the line
#config WDT = ON
This will enable the watchdog timer, without affecting any other configuration settings.
Using Configuration
Once the necessary CONFIG options have been determined, adding them to the program is easy. On a
new line type "#config" and then list the desired options separated by commas, such as in this line:
Great Cow BASIC also supports this format on 10/12/16 series chips:
However, for upwards compatibility with 18F chips, you should use the = style config settings.
It is possible to have several #config lines in a program - for instance, one in the main program, and
one in each of several #include files. However, care must then be taken to ensure that the settings in
one file do not conflict with those in another.
38
Syntax
Arrays
About Arrays
An array is a special type of variable - one which can store several values at once. It is essentially a list
of byte numbers in which each one can be addressed individually through the use of an "index". The
index is a value in brackets immediately after the name of the array.
Array/Index Meaning
Defining an array
The value for the number elements in an array must be a number or constant. The compiler allocates
RAM for arrays at compile time, and therefore you cannot use a variable because during compilation
the value of a variable cannot be determined.
It is possible to set several elements of an array with a single line of code. This short example shows
how:
Dim TestVar(10)
TestVar = 1, 2, 3, 4, 5, 6, 7, 8, 9
When using the method above element 0 of the array TestVar will be set to the number of items in the
39
list, which in this case is 9. Each element of the array will then be loaded with the corresponding value
in the list - so in the example, TestVar(1) will be set to 1, TestVar(2) to 2, and so on. Element 0 will only
be set to number of items in the array when using this method up to 48 data elements.
Array Length Element 0 should not be used to obtain the length of the array. Element 0 will only be a
consistent with respect to the length of the array when the array is set as shown above.
The correct method is to use a constant to set the array size and use the constant within your code to
obtain the array length.
Using Arrays
To use an array, its name is specified, then the index. Arrays can be used everywhere that a normal
variable can be used.
1. The 12F/16F series of chips the array limit is the physical RAM less a few bytes for array handling.
2. For the Atmel AVR or an 18F there is not limit other than free RAM.
3. However, Great Cow BASIC limits the array size of any array to 10,000 elements.
Array RAM usage is determined by the architecture of the chip type. Getting most out of the available
memory is determined by the allocation of the array within the available banks of memory.
An example is an array of 6 or 7 bytes when there is only 24 bytes of RAM and the 24 bytes is split
across multiple memory banks. Assume in this example that 18 bytes have allocated to other variables
and there is 29 bytes total available. An array of 6 bytes will fit into the free space in one bank, but the
array of 7 will not.
Great Cow BASIC currently cannot split an array over banks, so if there are 6 bytes free in one bank
and 5 in another, you cannot have an array of 7 bytes. This would be very hard to do efficiently on
12F/16F as there would be a series of special function registers in the middle of the array when using a
12F or 16F. This constraint is not the case on 16F1/18F as linear addressing makes it easy to span
banks because the SFRs are not making the problem (as with 12F/16F).
If there are many items in the array, it may be better to use a Lookup Table to store the items, and then
copy some of the data items into a smaller array as needed.
40
For more help, see Declaring arrays with DIM
Comments
About Comments
Adding comments to your Great Cow BASIC program is done with an apostrophe before the comment
line. You can also comment out sections of code if you want just by placing an apostrophe, a semi-colon
or use the statement REM at the beginning of each line. The SynGCB IDE has a feature to do this
automatically.
Warning: Great Cow Graphical BASIC uses semi-colons to mark comments that it has inserted
automatically. It does not read these comments when opening a file, so any comments in a Great Cow
BASIC program starting with a semi-colon will be deleted if the program is opened using Great Cow
Graphical BASIC.
Example:
REM You can create a header using an apostrophe before each line
REM This is a great way to describe your program
REM You can also use it to describe the hardware connections.
' You can place comments above the command or on the same line
Dir PORTB Out ' Initialise PORTB to all Outputs
Conditions
About Conditions
In Great Cow BASIC (and most other programming languages) a condition is a statement that can be
either true or false. Conditions are used when the program must make a decision. A condition is
generally given as a value or variable, a relative operator (such as = or >), and another value or
variable. Several conditions can be combined to form one condition through the use of logical
operators such as AND and OR.
41
Great Cow BASIC supports these relative operators:
Symbol Meaning
= Equal
In addition, these logical operators can be used to combine several conditions into one:
NOT is slightly different to the other logical operators, in that it only needs one other condition. Other
arithmetic operators may be combined in conditions, to change values before they are compared, for
example.
Great Cow BASIC has two built in conditions - TRUE, which is always true, and FALSE, which is always
false. These can be used to create Conditional tests and infinite loops.
The condition bit_variable = TRUE is treated as TRUE if the bit is on. Any non-zero value will be treated
as equal to a high bit. The condition bit_variable = other_type_of_variable generates a warning. If the
byte_variable is set to TRUE and then compared to the bit, it will always be FALSE because the high bit
will be treated as a 1. But the new warning will be generated, "Comparison will fail if %nonbit% is any
value other than 0 or 1"
It is also possible to test individual bits in conditions. To do this, specify the bit to test, then 1 or 0 (or
ON and OFF respectively). Presently there is no way to combine bit tests with other conditions - NOT,
AND, OR and XOR will not work.
Example conditions:
Condition Comments
42
Condition Comments
Mode = 1 AND Time > 10 True if Mode is 1 and Time is more than
10
Light >= 10 AND (NOT Time > 7) True if Light is 10 or more, and Time is
7 or less
As Great Cow BASIC is very flexible with the use of variables type this can cause issues when testing
constants and/or functions.
A few simple rules. Always put the function or constant first, or, always call the function with the
addition of the braces.
The example code below shows the correct method and an example that does compile but will not
work as expected.
'Example A - works
'Call the function by adding the braces
'
Do
Loop While HSerReceive() <> 62
'Example B - works
'Please the constant first - this is the general rule - put the constant first.
'
Do
Loop While 62 <> HSerReceive
43
Constants
About Constants
A constant tells the compiler to find a given word, and replace it with another word or number. Define
directives create constants.
Constants are useful for situations where a routine needs to be easily altered. For example, a define
could be used to specify the amount of time to run an alarm for once triggered.
It is also possible to use defines to specify ports - thus defines can be used to aid in the creation of code
that can easily be adapted to run on a different microcontroller with different ports.
Great Cow BASIC makes considerable use of defines internally. For instance, the LCD code uses defines
to set the ports that it must use to communicate with the LCD.
About Defines
To create a define is a matter of using the #define directive. Here are some examples of defines:
#define Line 34
#define Light PORTB.0
#define LightOn Set PORTB.0 on
Line is a simple constant - Great Cow BASIC will find Line in the program, and replace it with the
number 34. This could be used in a line following program, to make it easier to calibrate the program
for different lighting conditions.
Light is a port - it represents a particular pin on the microcontroller. This would be of use if the
program had many lines of code that controlled the light, and there was a possibility that the port the
light was attached to would need to change in the future.
LightOn is a define used to make the program more readable. Rather than typing Set PORTB.0 on over
and over, it would then be made possible to type LightOn, and have the compiler do the hard work.
44
#define ON 1
#define OFF 0
#define TRUE 255
#define FALSE 0
Forever is a special constant. For Great Cow Graphical BASIC users think of this as 'false'. For those not
using Great Cow Graphical BASIC think of this as a non numeric value that has no value. You can use
Forever in a DO-LOOP but not in a REPEAT-END REPEAT loop, as the in the later case the REPEAT will
have no value and you will create an error condition.
The #define command creates constants, and, a script can creates constants.
then, the #define in the include files. Constants defined in the include files will be ignored if they
conflict or are different to another constant in the main program,
then, the scripts are processed. Scripts that create constants always override any constant value
previously defined.
Scripts are highest priority, then constants in the main program, then constants in include files from
the main program, then constants in the standard libraries.
See #define
Functions
About Functions
Functions are a special type of subroutine that can return a value. This means that when the name of
the function is used in the place of a variable, Great Cow BASIC will call the function, get a value from
45
it, and then put the value into the line of code in the place of the variable.
Functions may also have parameters - these are treated in exactly the same way as parameters for
subroutines. The only exception is that brackets are required around any parameters when calling a
function.
Using Functions
This program uses a function called AverageAD to take two analog readings, and then make a decision
based on the average:
'Select chip
#chip 16F88, 20
'Define ports
#define LED PORTB.0
#define Sensor AN0
'Main code
Do
Set PORTB.0 Off
If AverageAD > 128 Then Set PORTB.0 On
wait 10 ms
Loop
Function AverageAD
'Get 2 readings, divide by 2, store in AverageAD
'Note the cast, the result of ReadAD needs to be converted to
'a word before adding, or the result may overflow.
AverageAD = ([word]ReadAD(Sensor) + ReadAD(Sensor)) / 2
end function
Labels
About Labels
Labels are used as markers throughout the program. Labels are used to mark a position in the program
to ‘jump to’ from another position using a goto, gosub or other command.
Labels can be any word (that is not already a reserved keyword) and may contain digits and the
46
underscore character. Labels must start with a letter or underscore (not digit), and are followed
directly by a colon (:) at the marker position. The colon is not required within the actual commands.
The compiler is not case sensitive. Lower and/or upper case may be used at any time.
Example:
'This program will flash the light until the button is pressed
'off. Notice the label named SWITCH_OFF.
#chip 16F628A, 4
Do
PulseOut LIGHT, 500 ms
If BUTTON = 1 Then Goto SWITCH_OFF
Wait 500 ms
If BUTTON = 1 Then Goto SWITCH_OFF
Loop
SWITCH_OFF:
Set LIGHT Off
'Chip will enter low power mode when program ends
Lookup Tables
About Lookup Tables
A lookup table is a list of values that are stored in the program memory of the chip, which can be
accessed using the ReadTable command.
The advantage of lookup tables is that they are memory efficient, compared to an equivalent set of IF
statements.
2. byte, word, longs and integer values are valid (no strings or decimals!)
47
4. constants and calculations within the single line data table entries are permitted
Defining Tables
#define calculation_constant 2
Data tables can now be loaded directly from a file. The source file will be read as a hexidecimal raw
file.
48
First, the table must be created. The code to create a lookup table is simple - a line that has Table and
then the name of the table, a list of numbers (up to 10,000 elements), and then End Table.
For tables with more than 255 elements it is mandated to used a WORD variable to read the size of the
table. See below.
Once the table is created, the ReadTable command is used to read data from it. The ReadTable command
requires the name of the table it is to read, the location of the item to retrieve, and a variable to store
the retrieved number in.
Lookup tables can store byte, word, longs and integer values. Great Cow BASIC will automatically
detect the type of the table depending on the values in it. Great Cow BASIC can be explicitly instructed
to cast the table to a variable type, as follows:
Item 0 of a lookup table stores the size of the table. If the ReadTable command attempts to read beyond
the end of the table, the value 0 will be returned.
For tables with more than 255 elements it is mandatory to use a WORD variable to read the size of the
table. See below.
table TestDataSource
'a table with more than 255 elements
end table
For tables that are defined using an external file the table data will be read into the TestDataSource
table from the external file.
49
The following program will import the external data file.
#chip 16f877a
for nn = 1 to 10
ReadTable TestDataSource, nn, inc
HSerPrint inc
next
You can use the Table statement to store the data table in EEPROM. If the compiler is told to store a data
table in "Data" memory, it will store it in the EEPROM.
NOTE The limitation of of using EPPROM tables is that you can only store BYTEs. You cannot store
WORD values in the EEPROM tables.
50
#chip 16F628
TableLoc = 2
ReadTable TestDataSource, TableLoc, SomeVar
Miscellaneous
About Miscellaneous things….
It is possible to combine multiple instructions on a single line, by separating them with a colon. For
example, this code:
Set PORTB.0 On
Set PORTB.1 On
Wait 1 sec
Set PORTB.0 Off
Set PORTB.0 Off
51
Set PORTB.0 On: Set PORTB.1 On
Wait 1 sec
Set PORTB.0 Off: Set PORTB.0 Off
In most cases, it will make no difference if commands share a line or not. However, special care should
be taken with If commands, as this code:
Also, the commands used to start and end subroutines, data tables and functions must be alone on a
line. For example, this is WRONG:
ReadTable
About ReadTable
The ReadTable command is used to read information from lookup tables. TableName is the name of the
table that is to be read, Item is the line of the table to read, and Output is the variable to write the
retrieved value in to.
Syntax:
Command Availability:
52
Available on all microcontrollers.
Explanation:
Item is 1 for the first line of the table, 2 for the second, and so on. If the Table is more than 256 elements
then Item must be WORD variable. Care must be taken to ensure that the program is not instructed to
read beyond the end of the table as Zero will be returned.
The type of Output should match the type of data stored in the table. For example, if the table contains
Word values then Output should be a Word variable. If the type does not match, Great Cow BASIC will
attempt to convert the value.
Example:
'Chip Settings
#chip 16F88, 20
'Hardware Settings
#define LED PORTB.0
Dir LED Out
'Main Routine
ReadTable TimesTwelve, 4, Temp
Set LED Off
If Temp = 48 Then Set LED On
53
Scripts
About Scripts
A script is a small section of code that Great Cow BASIC runs when it starts to compile a program. Uses
include performing calculations that are required to adjust the program for different chip frequencies.
Scripts are not compiled or downloaded to the microcontroller - Great Cow BASIC reads them, executes
them, then removes them from the program and then the results calculated can be used as constants in
the user program.
Inside a script, constants are treated like variables. Scripts can read the values of constants, and set
them to contain new values.
Using Scripts
Scripts start with #script and end with #endscript. Inside, they can consist of the following commands:
If
Assignment (=)
Error
Warning
Int()
If is similar to the If command in normal Great Cow BASIC code, except that it does not have an Else
clause. It is used to compare the values of the script constants.
The = sign is identical to that in Great Cow BASIC programs. The constant that is to be set goes on the
left side of the = and the new value goes to the right of the =.
Error is used to display an error message. Anything after the Error command is displayed at the end of
compilation, and is saved in the error log for the program.
Warning is used to display a warning message. Anything after the Warning command is displayed at the
end of compilation but warning does not halt compilation.
Int() will calculate the integer value of a calculation. Using Int() is critical to set the constant to the
integer component of the calculation.
Notes:
Use Warning to display constant values when creating and debugging scripts.
Scripts have a limited syntax and limited error checking when compiling. The compiler may halt if
you get something wrong.
Scripts that are incorrectly formatted may also halt the compiler or return unrelated error.
54
Scripts used for calculations should use the Int( expression ) where you may have a floating point
numbers returned.
Scripts do use floating point for all calculations and a failure to use Int() may set the script constant
and the resulting constant to 0.
Scripts may require that complex math expressions may require definition in multiple steps/line to
simplify the calculation.
The returned value could be incorrect if simplification is not implemented.
Scripts can only access existing constants both user and system defined.
User defines variables are not accessible within the scope of a script.
Scripts has precendence over #define. A #define constant statements are read first, then scripts run.
So, a script will always overwrite a constant that was set with #define.
Use Warning to display constants values when creating and debugging scripts.
Example Script
This script is used in the pwm.h file. It takes the values of the user defined constants PWM_Freq,
PWM_Duty and system constant ChipMHz and calculates the results using the equations. These
calculation are based on information from a Microchip PIC datasheet to calculate the correct values to
setup Pulse Width Modulation (PWM).
#script
PR2Temp = INT((1/PWM_Freq)/(4*(1/(ChipMHz*1000))))
T2PR = 1
If PR2Temp > 255 Then
PR2Temp = INT((1 / PWM_Freq) / (16 * (1 / (ChipMHz * 1000))))
T2PR = 4
If PR2Temp > 255 Then
PR2Temp = INT(( 1 / PWM_Freq) / (64 * (1 / (ChipMHz * 1000))))
T2PR = 16
If PR2Temp > 255 Then
Error Invalid PWM Frequency value
End If
End If
End If
During the execution of the script the calculations and assignment uses the constants in the script.
55
After this script has completed the constants PR2Temp, DutyCycleH and DutyCycleL are set using the
constants and/or the calculations.
The constants assigned in this script, PR2Temp, DutyCycleH and DutyCycleL, are made available as
constants in the user program.
Subroutines
About Subroutines
A subroutine is a small program inside of the main program. Subroutines are typically used when a
task needs to be repeated several times in different parts of the main program.
When the microcontroller comes to a subroutine it saves its location in the current program before
jumping to the start of, or calling, the subroutine. Once it reaches the end of the subroutine it returns
to the main program, and continues to run the code where it left off previously.
Normally, it is possible for subroutines to call other subroutines. There are limits to the number of
times that a subroutine can call another sub, which vary from chip to chip:
18F*, 18C* 16 31
These limits are due to the amount of memory on the microcontroller which saves its location before it
jumps to a new subroutine. Some Great Cow BASIC commands are subroutines, so you should always
allow for 2 or 3 subroutine calls more than your program has.
On 16F chips, the program memory is divided into pages. Each page holds 2048 instructions. If the
program jumps from code on one page to code on another, the compiler has to select the new
page. Having to do this makes the program bigger, so try to avoid this. To keep jumps between pages
down, Great Cow BASIC imposes a rule that each subroutine must be entirely within one page, so that
only jumps to other subroutines require the page selection. As an example, say you have two pages of
memory, each 2048 instructions (words) long.
If you have a main sub that is 1500 words, and four other subroutines each 600 words long, your total
program size would be 3900 words and you might expect it to fit into the 4096 words available. The
problem though is that once the main routine takes 1500 words from page 1, nothing else will fit after
it. Three of the 600 word subroutines would fit onto page 2, but that leaves one 600 word subroutine
56
that will not fit into the 500 left on page 1 or the 200 left on page 2. If you want to reduce the chance
of this happening, the best option is to keep your subroutines smaller - move anything out of the main
routine and into another one - this will resolve memory page constraints.
Atmel AVR microcontrollers have no fixed limit on how many subroutines can be called at a time, but
if too many are called then some variables on the chip may be corrupted. To check if there are too
many subroutines, work out the most that will be called at once, then multiply that number by 2 and
create an array of that size. If an out of memory error message comes up, there are too many calls.
Another feature of subroutines is that they are able to accept parameters. These are values that are
passed from the main program to the subroutine when it is called, and then passed back when the
subroutine ends.
Using Subroutines
To call a subroutine is very simple - all that is needed is the name of the sub, and then a list of
parameters. This code will call a subroutine named "Buzz" that has no parameters:
Buzz
If the sub has parameters, then they should be listed after the name of the subroutine. This would be
the command to call a subroutine named "MoveArm" that has three parameters:
Or, you may choose to put brackets around the parameters, like so:
All that this does is change the appearance of the code - it doesn’t make any difference to what the code
does. Decide which one meets your own personal preference, and then stick with it.
Creating subroutines
To create a subroutine is almost as simple as using one. There must be a line at the start which has sub,
and then the name of the subroutine. Also, there needs to be a line at the end of the subroutine which
reads end sub. To create a subroutine called Buzz, this is the required code:
57
sub Buzz
end sub
If the subroutine has parameters, then they need to be listed after the name. For example, to define the
MoveArm sub used above, use this code:
end sub
In the above sub, ArmX, ArmY and ArmZ are all variables. If the call from above is used, the variables will
have these values at the start of the subroutine:
ArmX = NewX
ArmY = NewY
ArmZ = 10
When the subroutine has finished running, Great Cow BASIC will copy the values back where possible.
NewX will be assigned to ArmX, and NewY will be assigned to ArmY. Great Cow BASIC will not attempt to set
the number 10 to ArmZ.
It is possible to instruct Great Cow BASIC not to copy the value back after the subroutine is called. If a
subroutine is defined like this:
end sub
Then Great Cow BASIC will copy the values to the subroutine, but will not copy them back.
Great Cow BASIC can also be prevented from copying the values back, by adding Out before the
parameter name. This is used in the EEPROM reading routines - there is no point copying a data value
into the read subroutine, so Out has been used to avoid wasting time and memory. The EPRead routine
is defined as Sub EPRead(In Address, Out Data).
58
Many older sections of code use #NR at the end of the line where the parameters are specified. The #NR
means "No Return", and when used has the same effect as adding In before every parameter. Use of #NR
is not recommended, as it does not give the same level of control.
It is possible to use any type of variable a as parameter for a subroutine. Just add As and then the data
type to the end of the parameter name. For example, to make all of the parameters for the MoveArm
subroutine word variables, use this code:
Optional parameters
Sometimes, the same value may be used over and over again for a parameter, except in a particular
case. If this occurs, a default value may be specified for the parameter, and then a value for that
parameter only needs to be given in a call if it is different to the default.
For example, suppose a subroutine to create an error beep is required. Normally it emits ! 440 Hz tone,
but sometimes a different tone is required. To create the sub, this code would be use:
Note the Optional before the parameter, and the = 440 after it. This tells Great Cow BASIC that if no
parameter is supplied, then set the OutTone parameter to 440.
ErrorBeep
ErrorBeep 1000
When using several parameters, it is possible to make any number of them optional. If the optional
parameter/s are at the end of the call, then no value needs to be specified. If they are at the start or in
the middle, then you must insert commas to allow Great Cow BASIC to tell where the optional
59
parameters are.
Overloading
It is possible to have 2 subroutines with the same name, but different parameters. This is known as
overloading, and Great Cow BASIC will automatically select the most appropriate subroutine for each
call.
An example of this is the Print routine in the LCD routines. There are actually several Print
subroutines; for example, one has a byte parameter, one a word parameter, and one a string
parameter. If this command is used:
Print 100
Then the Print (byte) subroutine will be called. However, if this command is used:
Print 30112
Then the Print (word) subroutine will be called. If there is no exact match for a particular call, Great
Cow BASIC will use the option that requires the least conversion of variable types. For example, if this
command is used:
Print PORTB.0
The byte print will be used. This is because byte is the closest type to the single bit parameter.
Variables
About Variables
A variable is an area of memory on the microcontroller that can be used to store a number or a series
of letters. This is useful for many purposes, such as taking a sensor reading and acting on it, or
counting the number of times the microcontroller has performed a particular task.
Each variable must be given a name, such as "MyVariable" or "PieCounter". Choosing a name for a
variable is easy - just don’t include spaces or any symbols (other than _), and make sure that the name
is at least 2 characters (letters and/or numbers) long.
Variable Types
There are several different types of variable, and each type can store a different sort of information.
These are the variable types that Great Cow BASIC can currently use:
60
Variable Information that this variable can Example uses for this type of variable
type store
Byte A whole number between 0 and 255 General purpose storage of data, such as
counters
Word A whole number between 0 and 65535 Storage of extra large numbers
Integer A whole number between -32768 and Anything where a negative number will
32767 occur
32
Long A whole number between 0 and 2 (4.29 Storing very, very big numbers
billion)
String A series of letters, numbers and symbols. Messages that are to be shown on a screen
Using Variables
Byte variables do not need any special commands to set them up - just put the name of the variable in
to the command where the variable is needed.
Other types of variable can be used in a very similar way, except that they must be "dimensioned" first.
This involves using the DIM command, to tell Great Cow BASIC that it is dealing with something other
than a byte variable.
A key feature of variables is that it is possible the have the microcontroller check a variable, and only
run a section of code if it is a given value. This can be done with the IF command.
String Variables
String variables default to the following rules and the RAM constraints of a specific chip.
• For chips that have less RAM then the required RAM to support the user define strings the strings
61
(and therefore the RAM) will be NOT be allocated. Please reduce string size.
You cannot store a string 20 characters long in a chip with 16 bytes of RAM.
To change the default string size handled internally by the Great Cow BASIC compiler you add
increase/decrease the default string size
Defining a length for the string is the best way to limit memory usage. It is good practice if you need a
string of a certain size to set the length of a strings, since the default length for a string variable
changes depending on the amount of memory in the microcontroller (see above).
Use the following method to show the string with the insertion of two quotation marks in a row as an
embedded quotation mark. These two examples apply to all output methods like HerPrint, Print etc.
Variable Aliases
Some variables are aliases, which are used to refer to memory locations used by other variables. These
are useful for joining predefined byte variable together to form word variables.
Alias are not like pointers in many languages - they must always refer to the same variable or variables
and cannot be changed.
Casting
Casting changes the type of a variable or value. Placing the type that the value should be converted to
62
in square brackets will tell the compiler to convert it. For example, this will cause two byte variables to
be treated as word variables by the addition code:
Why do this? If there are no casts, then Great Cow BASIC will add the two values using the byte
addition code, and then convert the result to a word to store in MyWord. Suppose that ByteVar is 150, and
AnotherByteVar is 231. When added, this will come to 381 - which will overflow, leaving 125 in the
result. However, when the cast is added, Great Cow BASIC will treat ByteVar as if it were a word, and so
will use the word addition code. This will cause the correct result to be calculated.
Checking variables and doing different things based on their value, see If, Do, For, Conditions
For more help, see: Declaring variables with DIM, Setting Variables
Converters
About Converters
Converters allow Great Cow BASIC to read files that have been created by other programs. A converter
can convert these files into Great Cow BASIC libraries or any Great Cow BASIC instruction or a Great
Cow BASIC dataset.
A typical use case is when you have a data source file from another computer system and you want to
consume the data within your Great Cow BASIC program. The data source file could be database,
graphic, reference data or music file. The converter will read these source files and convert them into a
format that can be processed by Great Cow BASIC. The conversion process is completed by external
application which can be written by the developer or you can use one of the converters provided with
63
the Great Cow BASIC release.
The Great Cow BASIC release includes the converter for BMP files and standard Text files.
With an appropriate Converter installed, and an associated #include to these non-Great Cow BASIC
files, Great Cow BASIC will detect that the file extension and hand the processing to the external
converting program. When the external converting program had complete, Great Cow BASIC will then
continue with the converted source file as a Great Cow BASIC source file.
An example of a converter is to read an existing picture file, convert the picture file to a GCB table and
then refer to the picture file table to display the picture file on a GLCD.
Conversion is achieved by including a command within the source program to transform external
data. The command used is the instruction #include followed by the data source. An example:
#include <..\converters\ManLooking.BMP>
The inclusion of the #include line within a Great Cow BASIC program will enable the commencement
of the following process:
1. Great Cow BASIC will examine the ..\converters folder structure for a configuration file that will
handle the file extension specified in the include statement.
2. Great Cow BASIC will examine the configuration file(s) *.INI for command line instructions.
3. Great Cow BASIC will at stage examine the folder structure for the source file and the target
transformed file. If the source file is older than the transformed file the next step will not be
executed, goto step 6.
4. Great Cow BASIC will execute the command as specified within the configuration file to transform
the source file to the target file.
The Conversion program must create the output file extension as specified in the configuration file.
If the include statement as an extension of .TXT and the configuration files states the input file
extension as .TXT and the output as .GCB the converted file must have the extension of .GCB.
#include <..\converters\ManLooking.BMP>
Init file is input file as BMP and output as GCB, then the file expected is
..\converters\ManLooking.GCB
5. Great Cow BASIC will attempt to include the transformed target file (with the file extension as
specified in the configuration file) within the Great Cow BASIC program.
6. Great Cow BASIC will resume normal processing of the Great Cow BASIC program including the
64
transformed target file, therefore, with normal compiling and errors handling.
The configuration file MUST have the extension of .INI. No leading spaces are permitted in the
configuration file. Specification of the configuration file. The file has four items: desc, in, out and
exe. Where:
You can have multiple configuration files within the ..\converters folder structure.
Great Cow BASIC will examine all configuration file to match the extension as specified in the
#include command.
Example 1 :
BMP (Black and White) conversion configuration file is called BMP2GCBasic.ini. The source
extension is .bmp, the transformed file extension is .GCB, and the executable is called
BMP2GCBASIC.exe.
An example :
#include <..\converters\ManLooking.BMP>
65
Example 2 :
Data file conversion configuration file is called TXT2GCB.ini. The source extension is .TXT, the
transformed file extension is .GCB, and the command line called AWKRUN.BAT .
An example :
#include <..\converters\InfraRedPatterns.TXT>
The example would require a supporting batch file and a script process to complete the
transformation.
2. Conversion Executable
The conversion executable MUST create the converted file with the correct file extension as
specified in the configuration file.
The conversion executable will be passed one parameter - the source file name. Using example #1
the conversion executable would be passed ..\converters\ManLooking.BMP
The conversion executable MUST create a Great Cow BASIC compatible source file. Any valid
commands/instruction are permitted.
3. Installation
The INI file, the source file and the conversion executable MUST be located in the ..\converters
folder. The converters folder is relative to the GCBASIC.EXE compiler folder.
66
This program converts the InfraRedPatterns.TXT into InfraRedPatterns.GCB that will have a Great
Cow BASIC table called DataSource. This example is located in the converter folder of the Great
Cow BASIC installation.
#chip16f877a, 16
#include <..\converters\InfraRedPatterns.TXT>
Do Forever
For TableReadPosition = 1 to TableLen step 2
ReadTable DataSource, TableReadPosition, TransmissionPattern
ReadTable DataSource, TableReadPosition+1 , PulseDelay
portb = TransmissionPattern
wait PulseDelay ms
next
Loop
67
This program converts a chip specific configuration file into manifest.GCB that will have a Great
Cow BASIC functions called DataIn and DataOut. This example is located in the converter folder of
the Great Cow BASIC installation.
#chip 16f18326
#include <..\converters\manifest.mcc>
DataOut ( TX, RA0 ) 'this method is created during the convert process. They do
not exist withiut the converter.
DataIn ( Rx, RC6 ) 'this method is created during the convert process. They do
not exist withiut the converter.
This example would use the optional parameters of params and deletetarget in the converter
configuration file as follows:
GCBBuildStr
GCBBuildTimeStr
The user code is simple. Using the #include statement specify any filename with an extension must be
cnt. As follows:
#include "GCBVersionNumber.cnt"
Complete code would like this - this not optimised - this shows the use of the exposed strings.
68
#include "GCBVersionNumber.cnt"
This outputs the following - where #20 is the current build and the date/time is correct for build time.
This works as the support INI file instructs the compiler to call a utility that automatically creates a
build number tracker file and the supportting string functions. The utility creates a tracker file and the
methods files in the same folder as your source program - so, each tracker is specific to each project.
The converter requires the following files - these are included within your Installation.
69
Command References
70
Analog/Digital conversion
This is the Analog/Digital conversion section of the Help file. Please refer the sub-sections for details
using the contents/folder view.
The analog to digital converter (ADC or A/D) module support is implemented by Great Cow BASIC to
provide 8-bit, 10-bit and 12-bit Single channel measurement mode and Differential Channel
Measurement mode.
Great Cow BASIC configures the analog to digital converter clock source, the programmed acquisition
time and justification of the response byte, word or integer (as defined in the Great Cow BASIC
method).
The Single channel measurement mode is the default method for reading the ADC port. The positive
input is attached to suitable device (a light sensor or adjustable resistor) and the command ReadADC,
ReadADC10, ReadADC12 with return a byte, word or word value respectively.
The A/D module on most microcontrollers only supports single-ended mode. Single channel mode uses
a single A/D port and the returned Value represents the difference between the voltage on the analog
pin and a fixed negative reference which is usually ground or Vss.
Example
Print ReadAD10(AN3)
71
Differential channel measurement mode
Some of the in the Microchip PIC family of devices also support differential analog to digital
conversion. With differential conversion, the differential voltage between two channels is measured
and converted to a digital value. The returned value can be either positive or negative (therefore an
integer value).
When configured to differential channel measurement mode, the positive channel is connected to the
defined positive analog pin (ANx), and the negative channel is connected to the defined negative
analog pin. These two pins are internally connected (within the microcontroller) to a unity gain
differential amplifier and once the amplifier has completed the comparison the result is returned as an
integer.
The positive channel Input is selected using the CHSx bits and the negative channel input is selected
using the CHSNx bits. These bits are managed by Great Cow BASIC. The programmer only needs to
supply the correct analog pin designators in the ReadADx commands.
The 12-bit returned result is available on the ADRESH and ADRESL registers which is returned by the
Great Cow BASIC methods as an integer variable.
Some Microchip PIC microcontrollers have differential A/D modules and support differential Mode as
well as 12-Bit A/D. With DIfferential mode the returned value can be either a positive or negative
number that represents the voltage differential between the two A/D ports.
The syntax for differential A/D is ReadAD( PositiveANPort , NegativeANPort ). Note: if "negative port"
is omitted readAd() will perform a single-ended read on the positive AN port.
Example
72
Print ReadAD12( AN3, An4 )
Great Cow BASIC supports a wide range of A/D modules and the supporting library addresses up to 34
channels. To reduce the size of the code produced you can define which channels are specifically
supported. See Optimising ADC code for more details.
The Microchip PIC microcontrollers that support Differential and 12-bit A/D are listed below. Dated
December 2015.
16f1782 16lf1782
16f1783 16lf1783
16f1784 16lf1784
16f1786 16lf1786
16f1787 16lf1787
16f1788 16lf1788
16f1789 16lf1789
18f65k22
18f66k22
18f67k22
18f85k22
18f86k22
18f86k22
18f25k80 18lf25k80
18f26k80 18lf26k80
18f45k80 18lf45k80
18f46k80 18lf46k80
18f65k80 18lf65k80
18f66k80 18lf66k80
18f65k90
18f66k90
18f67k90
18f85k90
18f86k90
18f87k22
18f87k90
73
ADFormat (Deprecated - Do not use)
Syntax:
Command Availability:
Explanation:
Left justified means 8 bits in the high byte, 2 in the low. Right justified means 2 in the high byte, and
the remaining 8 in the low byte. It’s only supported on Microchip PIC microcontrollers.
ADOff
This command is obsolete. There should be no need to call it. Great Cow BASIC will automatically
disable the A/D converter and set all pins to digital mode when starting the program, and after every
use of the ReadAD function.
ReadAD
Syntax:
For a Differential Channel read use the following. Where ANpX is the positive port, and ANnY is the
negative port.
To obtain a byte value from an AD Channel use the following to force an 8 bit AD Channel to respond
with a byte value [0 to 255].
Command Availability:
74
When using ReadAD ( ANx ) the returned value is an 8 bit number [0- 255]. The byte variable assigned
by the function can be a byte, word, integer or long.
When using ReadAD ( ANpX , ANnY ) the returned value is an integer, as negative values can be
returned.
When using ReadAD ( ANpX , TRUE ) the returned value is an integer, but you should treat as a byte.
ReadAD is a function that can be used to read the built-in analog to digital converter that many
microcontroller chips include. port should be specified as AN0, AN1, AN2, etc., up to the number of
analog inputs available on the chip that is in use. Those familiar with Atmel AVR microcontrollers can
also refer to the ports as ADC0, ADC1, etc. Refer to the datasheet for the microcontroller chip to find
the number of ports available. (Note: it’s perfectly acceptable to use ANx on AVR, or ADCx on the
microcontroller)
Other functions that are similar are ReadAD10 and ReadAD12. See the relevant Help page for the
specific usage of each function.
The constant AD_Delay controls is the acquisition delay. The default value is 20 us. This can be
changed by adding the following constant.
ADSpeed controls the source of the clock for the ADC module. It varies from one chip to another.
InternalClock is a Microchip PIC microcontroller only option that will drive the ADC from an internal
RC oscillator. The default value is 128.
Using ADSPEED
'default value
#define ADSpeed MediumSpeed
'pre-defined constants
#define HighSpeed 255
#define MediumSpeed 128
#define LowSpeed 0
AD_VREF_DELAY controls the charging time for VRef capacitor on Atmel AVR microcontrollers
only. This therefore controls the charge from internal VRef. ReadAD will not be accurate for internal
reference without this.
AD_Acquisition_Time_Select_bits also controls the Acquisition Time Select bits. Acquisition time is
the duration that the AD charge holding capacitor remains connected to AD channel from the instant
the read is commenced is set until conversions begins.
75
The default value of AD_Acquisition_Time_Select_bits is 0b100 or decimal 4, where all three ACQT bits
will be set. To change use the following.
Example 1
This example reads the ADC port and writes the output to the EEPROM.
#chip 16F819, 8
#config osc = int
Example 2
This example reads the ADC port and writes the output to the EEPROM. The output value will be in the
range of [0-255].
76
#chip 16F1789, 8
Example 3
This example used the diffential capabilities of ADC port and writes the output to the EEPROM. The
output value will be in the range of [-255 to 255].
AN0 and AN2 are used for the diffential ADC reading.
#chip 16F1789, 8
ReadAD10
Syntax:
77
For a normal ( also called a Single Channel ) read use.
For a Differential Channel read use the following. Where ANpX is the positive port, and ANnY is the
negative port.
To obtain a 10-bit value from an AD Channel use the following to force a 10 bit AD Channel to respond
with the correct value, in terms of the range [0 to 1023]
Command Availability:
ReadAD10 is a function that can be used to read the built-in analog to digital converter that many
microcontroller chips include. The port should be specified as AN0, AN1, AN2, etc., up to the number
of analog inputs available on the chip that is in use. Those familiar with Atmel AVR microcontrollers
can also refer to the ports as ADC0, ADC1, etc. Refer to the datasheet for the microcontroller chip to
find the number of ports available. (Note: it’s perfectly acceptable to use ANx on AVR, or ADCx on the
microcontroller.)
When using ReadAD10 ( ANX ) the returned value is the full range of the ADC module. Therefore, the
method will return an 8 bit value [0-255], or an 10 bit value [0-1023] or an 12 bit value [0-4095]. This is
dependent on the microcontrollers capabilities. For a 10 bit value [0-1023] always to be returned use
user_variable = ReadAD10( ANX , TRUE ). The user variable can be a byte, word, integer or long but
typically a word is recommnended.
When using ReadAD10 ( ANpX , ANnY ), for differential ADC reading, the returned value is an integer
as negative values will be returned.
When using ReadAD10 ( ANpX , TRUE ), to force a 10 bit ADC reading, the returned value is an integer.
Other functions that are similar are ReadAD and ReadAD12. See the relevant Help page for the
specific usage of each function.
AD_Delay controls is the acquisition delay. The default value is 20 us. This can be changed to a longer
acquisition delay by adding the following constant.
78
ADSpeed( controls the source of the clock for the ADC module. It varies from one chip to another.
InternalClock is a microcontroller only option that will drive the ADC from an internal RC
oscillator. The default value is 128.
'default value
#define ADSpeed MediumSpeed
'pre-defined constants
#define HighSpeed 255
#define MediumSpeed 128
#define LowSpeed 0
AD_Acquisition_Time_Select_bits also controls the Acquisition Time Select bits. Acquisition time is
the duration that the AD charge holding capacitor remains connected to AD channel from the instant
the read is commenced is set until conversions begins.
The default value of AD_Acquisition_Time_Select_bits is 0b100 or decimal 4, where all three ACQT bits
will be set. To change use the following.
#chip 16F819, 8
79
When selecting the reference source for ADC on ATmega328 Great Cow BASIC will overwrite anything
that you put into te ADMUX register - but this option allow you change the ADC reference source on
Atmel AVR microcontrollers. You can set the AD_REF_SOURCE constant to whatever you want to use.
It defaults to the VCC pin, as example you can set the Atmel AVR to use the 1.1V reference with this:
#define AD_REF_SOURCE AD_REF_256 where 256 refers to the 2.56V reference on some older AVRs,
but the same code will select the 1.1V reference on an ATmega328p
The example above sets the AD_REF_SOURCE to a variable, and then changes the value of the variable
to select the source. With this approach, we also need to allow time to charge the reference capacitor
to the correct voltage.
#chip 16F1789, 8
Example 4
This example used the diffential capabilities of ADC port and writes the output to the EEPROM. The
output value will be in the range of [-1023 to 1023].
AN0 and AN2 are used for the diffential ADC reading.
80
#chip 16F1789, 8
'USART settings
#define USART_BAUD_RATE 9600 'Initializes USART port with 9600 baud
#define USART_TX_BLOCKING ' wait for tx register to be empty
wait 100 ms
Next
ReadAD12
Syntax:
For a Differential Channel read use the following. Where ANpX is the positive port, and ANnY is the
negative port.
To obtain a 12-bit value from an AD Channel use the following to force a 12 bit AD Channel to respond
with the correct value, in terms of the range of [0 to 4095]
Command Availability:
81
When using ReadAD12 ( ANX ) the returned value is an 12 bit number [0-4095]. The user variable can
be a word, integer or long.
When using ReadAD12 ( ANpX , ANnY ) the returned value is an integer as negative values can be
returned.
ReadAD12 is a function that can be used to read the built-in analog to digital converter that many
microcontroller chips include. Port should be specified as AN0, AN1, AN2, etc., up to the number of
analog inputs available on the chip that is in use. Those familiar with Atmel AVR microcontrollers can
also refer to the ports as ADC0, ADC1, etc. Refer to the datasheet for the microcontroller chip to find
the number of ports available. (Note: it’s perfectly acceptable to use ANx on AVR, or ADCx on the
microcontroller.)
Other functions that are similar are ReadAD and ReadAD10. See the relevant Help page for the
specific usage of each function.
AD_Delay controls is the acquisition delay. The default value is 20 us. This can be changed to a longer
acquisition delay by adding the following constant.
ADSpeed( controls the source of the clock for the ADC module. It varies from one microcontroller to
another. InternalClock is a Microchip PIC microcontroller only option that will drive the ADC from an
internal RC oscillator. The default value is 128.
'default value
#define ADSpeed MediumSpeed
'pre-defined constants
#define HighSpeed 255
#define MediumSpeed 128
#define LowSpeed 0
AD_Acquisition_Time_Select_bits also controls the Acquisition Time Select bits. Acquisition time is
the duration that the AD charge holding capacitor remains connected to AD channel from the instant
the read is commenced is set until conversions begins.
The default value of AD_Acquisition_Time_Select_bits is 0b100 or decimal 4, where all three ACQT bits
will be set. To change use the following.
82
'change the default value
#define AD_Acquisition_Time_Select_bits 0b001 'this will only set ACQT bit 0, ACQT
bits 1 and 2 will be cleared.
#chip 16F1788, 8
#config osc = int
#chip 16F1788, 8
Example 3
83
This example used the diffential capabilities of ADC port and writes the output to the EEPROM. The
output value will be in the range of [-4095 to 4095].
AN0 and AN2 are used for the diffential ADC reading.
#chip 16F1789, 8
'USART settings
#define USART_BAUD_RATE 9600 'Initializes USART port with 9600 baud
#define USART_TX_BLOCKING ' wait for tx register to be empty
wait 100 ms
Next
The analog to digital converter (ADC or A/D) module support is implemented by Great Cow BASIC to
provide 8-bit, 10-bit and 12-bit Single channel measurement mode and Differential Channel
Measurement with support up to 34 channels. For compatibility all channels are supported.
1. To mimise the code, use the contstants to disable support for a specfic channels
2. To adapt the ADC configuration by inserting specfic commands to set registers or register bits.
The example below would disable support for ADC port 0 (AD0).
84
#define USE_AD0 FALSE
The following tables show the #defines that can be used to reduce the code size - these are the defines
for the standard microcontrollers. For 16f1688x and similar microcontrollers please see the second
table.
85
Channe Optimisation Default
l Value Value
86
Channe Optimisation Default
l Value Value
This is a example - disables every channel except the specified channel by defining every channel
except USE_AD0 as FALSE.
; ----- Configuration
#chip 16F1939
'USART settings
#define USART_BAUD_RATE 9600
#define USART_TX_BLOCKING
87
#define USE_AD7 FALSE
#define USE_AD8 FALSE
#define USE_AD9 FALSE
#define USE_AD10 FALSE
#define USE_AD11 FALSE
#define USE_AD12 FALSE
#define USE_AD13 FALSE
#define USE_AD14 FALSE
#define USE_AD15 FALSE
#define USE_AD16 FALSE
#define USE_AD17 FALSE
#define USE_AD18 FALSE
#define USE_AD19 FALSE
#define USE_AD20 FALSE
#define USE_AD21 FALSE
#define USE_AD22 FALSE
#define USE_AD23 FALSE
#define USE_AD24 FALSE
#define USE_AD25 FALSE
#define USE_AD26 FALSE
#define USE_AD27 FALSE
#define USE_AD28 FALSE
#define USE_AD29 FALSE
#define USE_AD30 FALSE
#define USE_AD31 FALSE
#define USE_AD32 FALSE
#define USE_AD33 FALSE
#define USE_AD34 FALSE
For 16f18855 family of microcontrollers this is a example. This will save 149 bytes of program
memory.
'Chip Settings.
#chip 16f18855,32
#Config FEXTOSC_OFF, RSTOSC_HFINT32
#Config WRT_OFF, CPD_ON, MCLRE_ON
'' -------------------LATA-----------------
88
'' Bit#: -7---6---5---4---3---2---1---0---
'' LED: ---------------|D5 |D4 |D3 |D1 |-
''-----------------------------------------
''
#define SWITCH_DOWN 0
#define SWITCH_UP 1
do
'Read the value from the EEPROM from register Zero in the EEPROM
EPRead ( 0, OutValue )
'Leave the Top Bytes alone and set the lower four bits
PortA = ( PortA & 0XF0 ) OR ( OutValue / 16 )
Sleep
loop
sub InterruptHandler
89
if ( SWITCH = DOWN ) then
'Read the ADC
adc_value = readad ( AN4 )
'Write the value to register Zero in the EEPROM
EPWrite ( 0, adc_value )
end if
IOCAN5 = 1 'ReEnable the InterruptHandler
routine
end if
end sub
90
2. Adapt the ADC configuration
The following example will set the specific register bits. The instruction will be added to the compiled
code.
The constant ADReadPreReadCommand can be used to adapt the ADC methods. The constant can
enable registers or register bit(s) that are required to managed for a specfic solution.
In the example above the following ASM will be added to your code. This WILL be added just before
the ADC is enabled and the setting of the acquisition delay.
;ADReadPreReadCommand
banksel ADCON
bcf ADCON,2
banksel ANSELA
bsf ANSELA,0
91
Bitwise
This is the Bitwise section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Great Cow BASIC (as with most other microcontroller programming languages) supports bitwise
operations.
Bitwise operations are performed on one or more bit patterns at the level of their individual bits.
Method Meaning
For more help, see: Set, SetWith, FnLSL, FnLSR and Rotate
FnLSL
Syntax:
Command Availability:
Explanation:
FnLSL (Logical Shift Left) will perform a Bitwise left shift. FnLSL will return BitsIn shifted NumBits to the
left, it is equivalent to the 'C' operation:
92
Each left shift is the equivalent of multiplying BitsIn by 2. BitsIn and NumBits may be may be a
variable and of type: Bit, Byte, Word, Long, Constant or another Function. Zeros are shifted in from the
right, Bits that are shifted out are lost.
It is useful for mathematical and logical operations, as well as creating serial data streams or
manipulating I/O ports.
Example:
' This program will shift the LEDs on the Microchip PIC Low Pin
' Count Demo Board from Right to Left, that is DS1(RC0) to
' DS4(RC3) and repeat
Do
LEDMask = FnLSL(LEDMask, 1) & 0x0F ' Mask the lower 4 bits
if LEDPORT.3 then LEDMask.0 = 1 ' Restart the sequence
LEDPORT = LEDMask ' Display the Pattern
wait 500 ms
Loop
End
FnLSR
Syntax:
Command Availability:
Explanation:
FnLSR (Logical Shift Right) will perform a Bitwise right shift. FnLSR will return BitsIn shifted NumBits to
the right, it is equivalent to the 'C' operation:
93
BitsOut = BitsIn >> NumBits
BitsIn and NumBits may be may be a variable and of type: Bit, Byte, Word, Long, Constant or another
Function.
Zeros are shifted in from the left, Bits that are shifted out are lost.
It is useful for mathematical and logical operations, as well as creating serial data streams or
manipulating I/O ports.
Example:
' This program will shift the LEDs on the Microchip PIC Low Pin Count Demo Board
' from Right to Left, that is DS4(RC3) to DS1(RC0) and repeat.
Do
LEDPORT = LEDMask ' Display the Pattern
wait 500 ms
LEDMask = FnLSR(LEDMask, 1) & 0x0F ' Mask the lower 4 bits
if LEDPORT.0 then LEDMask.3 = 1 ' Restart the sequence
Loop
End
SetWith
Syntax:
SetWith(TargetBit, Source)
Command Availability:
94
Explanation:
SetWith is an extended version of SET, it allows a Bit Field to be set or cleared by evaluating the content
of Source. SetWith should always be used when TargetBit is an I/O Bit and Source is a Function, in order
to avoid the possibility of I/O jitter.
Source may be a variable and of type: Bit, Byte, Word or Long, a Constant, an expression or a Function.
It will SET TargetBit to 1 if Source evaluates to anything other than zero. TargetBit will always be a 1
or a 0 regardless of the variable type of TargetBit.
Example:
' This program will reflect the state of SW1(RA3) on LED DS1(RC0) of the Microchip
' Low Pin Count Demo Board. Notice that because SW1 is normally High the state has to
' be inverted to turn on the LED (DS1) when SW1 is pressed.
Do
' set the Bit DS1 to equal the Bit SW1
SetWith( DS1, !SW1 )
Loop
END
95
Memory Devices
This is the Memory section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Eeprom
This is the Eeprom section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
EPRead
Syntax:
Command Availability:
Available on all Microchip PIC and Atmel AVR microcontrollers with EEPROM data memory.
Explanation:
EPRead is used to read information from the EEPROM data storage that many microcontroller chips are
equipped with. location represents the location to read data from, and varies from one chip to
another. store is the variable in which to store the data after it has been read from EEPROM.
Example:
96
'Program to turn a light on and off
'Will remember the last status
#chip tiny2313, 1
#define Button PORTB.0
#define Light PORTB.1
Dir Button In
Dir Light Out
If LightStatus = 0 Then
Set Light Off
Else
Set Light On
End If
Do
'Wait for the button to be pressed
Wait While Button = On
Wait While Button = Off
'Toggle value, record
LightStatus = !LightStatus
EPWrite 0, LightStatus
'Update light
If LightStatus = 0 Then
Set Light Off
Else
Set Light On
End If
Loop
EPWrite
Syntax:
Command Availability:
97
Available on all Microchip PIC and Atmel AVR microcontrollers with EEPROM data memory.
Explanation:
EPWrite is used to write information to the EEPROM data storage, so that it can be accessed later by a
programmer on the PC, or by the EPRead command. location represents the location to read data from,
and varies from one chip to another. data is the data that is to be written to the EEPROM, and can be a
value or a variable.
Example:
#chip 16F819, 8
#config osc = int
Next
ProgramErase
Syntax:
ProgramErase (location)
Command Availability:
Available on all Microchip PIC microcontrollers with self write capability. Not available on Atmel AVR
at present.
Explanation:
ProgramErase erases information from the program memory on chips that support this feature. The
largest value possible for location depends on the amount of program memory on the Microchip PIC
microcontroller, which is given on the datasheet.
98
This command must be called before writing to a block of memory. It is slow in comparison to other
Great Cow BASIC commands. Note that it erases memory in 32-byte blocks - see the relevant Microchip
PIC microcontroller datasheet for more information.
This is an advanced command which should only be used by advanced developers. Care must be taken
with this command, as it can easily erase the program that is running on the microcontroller.
ProgramRead
Syntax:
Command Availability:
Available on all Microchip PIC microcontrollers with self write capability. Not available on Atmel AVR
at present.
Explanation:
ProgramRead reads information from the program memory on chips that support this feature. location
and store are both word variables, meaning that they can store values over 255.
The largest value possible for location depends on the amount of program memory on the Microchip
PIC microcontroller, which is given on the datasheet. store is 14 bits wide, and thus can store values up
to 16383.
Example:
ProgramWrite
Syntax:
Command Availability:
Available on all Microchip PIC microcontrollers with self write capability. Not available on Atmel AVR
at present.
99
Explanation:
ProgramWrite writes information to the program memory on chips that support this feature. location
and value are both word variables.
The largest value possible for location depends on the amount of program memory on the
microcontroller , which is given on the datasheet. value is 14 bits wide, and thus can store values up to
16383.
This is an advanced command which should only be used by advanced developers. ProgramErase must
be used to clear a block of memory BEFORE ProgramWrite is called.
Example:
100
HEFM
This is the HEFM section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
HEFM Overview
Introduction:
Some enhanced mid-range Microchip PIC devices support High-Endurance Flash (HEF) memory. These
devices lack the data EEPROM found on other devices. Instead, they implement an equivalent
amount of special flash memory, called HEF memory, that can provide an endurance comparable to
that of a traditional data EEPROM. HEF memory can be erased and written 100,000 times. HEF
memory appears in the regular program memory space and can be used for any purpose, like regular
flash program memory.
As with all flash memory, data must be erased before it can be written and writing this memory will
stall the device. Methods to read, write and erase the HEF memory are included in Great Cow BASIC
and they are described in this introduction. Also see Microchip application note AN1673, Using the
PIC16F1XXX High-Endurance Flash (HEF) Block.
The hefsaf.h library supports HEF operations for Great Cow BASIC.
Note: By default, Great Cow BASIC will use HEF memory for regular executable code unless it is told
otherwise. If you wish to store data here, you should reserve the HEF memory by using the compiler
option, as shown below to reserve 128 words of HEF memory:
HEF memory is a block of memory locations found at the top of the flash program memory. Each
memory location can be used to hold a 8-bit byte value. To further explain, the PIC 16F Enhanced
Midrange Sevices memory architecture is 14-bits wide. Therefore, for a single 14-bit memory
location it is only practical to store an 8-bit byte value, and two 14-bit memory locations to hold one 16-
bit word value. This is because the memory architecture only allows the use of the the lower 8-bits
of each 14-bit flash memory location for HEF usage
The main difference between HEF memory and EEPROM is that EEPROM allows byte-by-byte erase
whereas the HEF memory does not. With HEF memory, data must be erased before a write and the
erase can only be performed in blocks of memory. The blocks, also called rows, are a fixed size
associated with the specific device.
Great Cow BASIC handles the erase operation automatically. When a write operation is used by a user
the Great Cow BASIC library reads to a cache, updates the cache, erase the block and finally write the
caches. The complexity of using HEF memory is reduced with the automatically handling of these
101
operations.
The hefsaf.h library provides a set of methods to support the use of HEF memory.
102
HEFEraseBlock a HEFEraseBlock ( 0 )
subroutine
with the A value of 0,1,2,3 etc.
parameters:
block_num
ber
The library also defines a set constants that are specific to the device. These may be useful in the user
program. These constants are used by the library. A user may use these public constants.
103
CHIPERASEROWSIZEWORDS Word Device specific constant for the number
of HEFM in an erase row
Warning
Whenever you update the hex file of your Microchip PIC micro-controller with your programmer you
MAY erase the data that are stored in HEF memory. If you want to avoid that you will have to flash
your Microchip PIC micro-controller with software that allows memory exclusion when flashing. This
is the case with Microchip PIC MPLAB IPE (Go to Advanced Mode/Enter password/Select Memory/Tick
“Preserve Flash on Program”/ Enter Start and End address of your HEFM). Or, simply use the
PICkitPlus suite of software to preserve HEF memory during programming.
HEFRead
Syntax:
'as a subroutine
HEFRead ( location, data )
'as a function
data = HEFRead ( location )
Command Availability:
Explanation:
HEFRead is used to read information, byte values, from HEFM, so that it can be accessed for use in a
user program.
location represents the location or relative address to read. The location will range from location 0 to
HEF_BYTES - 1, or for all practical purposes 0-127 since all PIC Microcontrollers with HEF support 128
bytes of HEF Memory. HEF_BYTES is a Great Cow BASIC constant that represents the number of bytes
of HEF Memory.
104
data is the data that is to be read from the HEFM data storage area. This can be a byte value or a byte
variable.
This method reads data from HEFM given the specific relative location. This method is similar to the
EPRead method for EEPROM.
Example 1:
'The following example reads the HEFM data value into the byte variable “byte_value”
using a subroutine.
Example 2:
105
'... code preamble to select part '... code preamble to select part
'... code to setup PPS
'... code to setup serial
'The following example reads the HEFM data value into the byte variable “byte_value”
using a function.
HEFReadWord
Syntax:
'as a subroutine
HEFReadWord ( location, data_word_variable )
'as a function
data_word_variable = HEFReadWord ( location )
Command Availability:
Explanation:
HEFReadWord is used to read information, word values, from HEFM so that it can be accessed for use
in a user program.
location represents the location or relative address to read. The location will range from location 0 to
HEF_BYTES - 1, or for all practical purposes 0-127 since all PIC Microcontrollers with HEF support 128
106
bytes of HEF Memory. HEF_BYTES is a Great Cow BASIC constant that represents the number of bytes
of HEF Memory.
data is the data that is to be read from the HEFM data storage. This must be a word variable.
This method reads data from HEFM given the specific relative location.
Example 1:
'The following example reads the HEFM value into the word variable
“data_word_variable” by initially writing some word values.
Value = 4660
Example 2:
107
'... code preamble to select part
'... code to setup serial
'The following example uses a function to read the HEFM value into the word variable
“data_word_variable”.
Value = 17185
HEFWrite
Syntax:
Command Availability:
Explanation:
HEFWrite is used to write information, byte values, to HEFM so that it can be accessed later for use in a
user program.
location represents the location or relative address to write. The location will range from location 0 to
HEF_BYTES - 1, or for all practical purposes 0-127 since all PIC Microcontrollers with HEF support 128
bytes of HEF Memory. HEF_BYTES is a Great Cow BASIC constant that represents the number of bytes
108
of HEF Memory.
data is the data that is to be written to the HEFM location. This can be a byte value or a byte variable.
This method writes information to the HEFM given the specific location. This method is similar to the
EPWrite method for EEPROM.
Example 1:
'The following example writes a byte value of 126 into HEFM location 34
Example 2:
'This example will populate all 128 bytes of HEF memory with a value that is same as
the HEFM location
End
Sub HEFM_DUMP
109
Dim Blocknum as Byte
NVM_Address = HEF_START_ADDR
BlockNum = 0
IF NVM_Address % 8 = 0 then
HSerPrintCRLF
hserprint hex(NVM_Address_H)
hserprint hex(NVM_ADDRESS)
hserprint " "
end if
hserprint hex(DataWord_H)
hserprint hex(DataWord)
hserprint " "
NVM_Address++
End Repeat
HserPrintCRLF
End sub
110
Block0 0 1 2 3 4 5 6 7
3F80 3F00 3F01 3F02 3F03 3F04 3F05 3F06 3F07
3F88 3F08 3F09 3F0A 3F0B 3F0C 3F0D 3F0E 3F0F
3F90 3F10 3F11 3F12 3F13 3F14 3F15 3F16 3F17
3F98 3F18 3F19 3F1A 3F1B 3F1C 3F1D 3F1E 3F1F
Block1 0 1 2 3 4 5 6 7
3FA0 3F20 3F21 3F22 3F23 3F24 3F25 3F26 3F27
3FA8 3F28 3F29 3F2A 3F2B 3F2C 3F2D 3F2E 3F2F
3FB0 3F30 3F31 3F32 3F33 3F34 3F35 3F36 3F37
3FB8 3F38 3F39 3F3A 3F3B 3F3C 3F3D 3F3E 3F3F
Block2 0 1 2 3 4 5 6 7
3FC0 3F40 3F41 3F42 3F43 3F44 3F45 3F46 3F47
3FC8 3F48 3F49 3F4A 3F4B 3F4C 3F4D 3F4E 3F4F
3FD0 3F50 3F51 3F52 3F53 3F54 3F55 3F56 3F57
3FD8 3F58 3F59 3F5A 3F5B 3F5C 3F5D 3F5E 3F5F
Block3 0 1 2 3 4 5 6 7
3FE0 3F60 3F61 3F62 3F63 3F64 3F65 3F66 3F67
3FE8 3F68 3F69 3F6A 3F6B 3F6C 3F6D 3F6E 3F6F
3FF0 3F70 3F71 3F72 3F73 3F74 3F75 3F76 3F77
3FF8 3F78 3F79 3F7A 3F7B 3F7C 3F7D 3F7E 3F7F
HEFWriteWord
Syntax:
Command Availability:
Explanation:
HEFWriteWord is used to write information, word values, to HEFM, so that it can be accessed in a user
program via the HEFReadWord command.
location presents the location or relative address to write write. A data Word requires 2 HEF Locations,
111
therefore the location will range from 0 to 126 in steps of 2.
data is the data that is to be written to the HEFM ;ocation. This can be a word value or a word
variable.
This method writes information to the HEFM given the specific location in the HEFM data storage . This
method is similar to the methods for EEPROM but this method supports Word values.
Example 1:
HEFWrite( 0, 0x1234)
Example 2:
'This example will write two word values to two specific locations.
HEFWriteWord (16, 0xAA01)
HEFWriteWord (18, 0xBB02)
If example 2 were displayed on a serial terminal. The result would show, where -- is the existing
value.
Block0
3F00 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
3F10 01 AA 02 BB -- -- -- -- -- -- -- -- -- -- -- --
3F20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
3F30 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
112
HEFWriteBlock, HEFEraseBlock
HEFReadBlock
Syntax:
Command Availability:
Explanation:
HEFReadBlock is used to read information from the HEFM data storage into the buffer. Once the
buffer is populated it can be accessed for use within a user program.
block_number represents the block to be written to. The block_number parameter is used to calculate
the physical memory location(s) that are updated.
buffer() represents an array or string. The buffer will be used as the data target for the block read
operation. The buffer is handled as a buffer of bytes values. In most cases the buffer should be the
same size as a row/block of HEFM. For most PIC Microcontrollers this will be 32 bytes. For PIC
microcontrollers with 2KW or less of Flash Program Memory this will be 16 Bytes. Once data is read
into the buffer from HEFM, the user program must handle the data as Byte, Word or String values, as
appropriate.
num_bytes is an optional parameter, and can be used to specify number of bytes to read from HEFM,
starting at the first location in the selected HEFM block. This parameter is not normally required as
the default is set to the Great Cow BASIC constant HEF_ROWSIZE_BYTES.
Example 1:
113
'... code preamble to select part
'... code to setup serial
Dim My_Buffer(HEF_ROWSIZE_BYTES)
Dim index as byte
HEFWriteBlock
Syntax:
Command Availability:
Explanation:
HEFWriteBlock is used to write information from a user buffer to HEFM. Once the block is written it
can be accessed for use within a user program.
block_number represents the block to be written to. The block_number parameter is used to calculate
114
the physical memory location(s) that are updated.
buffer() represents an array or string. The buffer will be used as the data source that is written to the
HEFM block. The buffer is handled as a buffer of bytes values. In most cases the buffer should be
the same size as a row/block of HEFM. For most PIC Microcontrollers this will be 32 bytes. For PIC
microcontrollers with 2KW or less of Flash Program Memory this will be 16 Bytes. Best practice is to
size the buffer using the HEF_ROWSIZE_BYTES constant. If the size of the buffer exceeds the device
specific HEF_ROWSIZE_BYTES, the excess data will not be handled and the buffer will be truncated at
the HEF_ROWSIZE_BYTES limit.
num_bytes is an optional parameter, and can be used to specify number of bytes to write to HEFM,
starting at the first location in the selected HEFM block. This parameter is not normally required as
the default is set to the Great Cow BASIC constant HEF_ROWSIZE_BYTES.
Example 1:
Dim My_Buffer(HEF_ROWSIZE_BYTES)
My_Buffer =
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
'HEFwriteBlock operation!!
HEFwriteBlock(2, My_Buffer)
If example 1 were displayed on a serial terminal using HEFM_Dump. The result would show. Note the
value display at the start of block 2 @ 0x3F80.
115
Block0 1 0 3 2 5 4 7 6 9 8 B A D C F E
7F00 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
7F10 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
7F20 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
7F30 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
Block1 1 0 3 2 5 4 7 6 9 8 B A D C F E
7F40 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
7F50 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
7F60 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
7F70 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
Block2 1 0 3 2 5 4 7 6 9 8 B A D C F E
7F80 0201 0403 0605 0807 0A09 0C0B 0E0D 100F
7F90 1211 1413 1615 1817 1A19 1C1B 1E1D 201F
7FA0 2120 2322 2524 2726 2928 2B2A 2D2C 2F2E
7FB0 3130 3332 3534 3736 3938 3B3A 3D3C 3F3E
Block3 1 0 3 2 5 4 7 6 9 8 B A D C F E
7FC0 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
7FD0 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
7FE0 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
7FF0 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF
HEFEraseBlock
Syntax:
HEFEraseBlock ( block_number )
Command Availability:
Explanation:
HEFEraseBlock is used to erase all data locations within the HEFM block. HEFM data within the
HEFM block to the erase state value of the device. This Value is 0xFF and will read 0x3FFF if the
entire 14bit program memory word is displayed. Use Caution. Once the HEFM block is erased, the
HEFM data is gone forever and cannot be recovered unless it was previpusly saved.
116
The single parameter is as follows:
block_number represents the block to be erased. The block_number parameter is used to calculate the
physical memory location(s) that are updated.
Example 1:
EERAM
This is the EERAM section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
The 47xxx EERam device is a memory device is organized as 512 x 8 bits or 2,048 x 8 bits of memory
and utilizes the I2C serial interface.
The 47xxx provides infinite read and write cycles to the SRAM while EEPROM cells provide high-
endurance nonvolatile storage of data with more than one million store cycles to EEPROM & a Data
retention of > 200 years.
With an external capacitor (~10uF), SRAM data is automatically transferred to the EEPROM upon loss
of power, giving the advantages of NVRAM whilst eliminating the need for backup batteries.
Data can also be backed up manually by using either the Hardware Store pin (HS) or software control.
On power-up, the EEPROM data is automatically recalled to the SRAM. EEPROM data Recall can also
be initiated through software control.
117
Connectivity is shown below:
__ __
Vcap-->| U |<-- Vcc
A1 -->| |<-- HS
A2 -->| |<-> SCL
Vss -->|_____|<-> SDA
Modes of Operation
The SRAM allows for fast reads and writes and unlimited endurance. As long as power is present, the
data stored in the SRAM can be updated as often as desired.
To preserve the SRAM image, the AutoStore function copies the entire SRAM image to an EEPROM
array whenever it detects that the voltage drops below a predetermined level. The power for the
AutoStore process is provided by the externally connected VCAP capacitor. Upon power-up, the entire
memory contents are restored by copying the EEPROM image to the SRAM. This automatic restore
operation is completed in milliseconds after power-up, at the same time as when other devices would
be initializing.
There is no latency in writing to the SRAM. The SRAM can be written to starting at any random
address, and can be written continuously throughout the array, wrapping back to the beginning after
the end is reached. There is a small delay, specified as TWC in the data sheet, when writing to the
nonvolatile configuration bits of the STATUS Register (SR).
Besides the AutoStore function, there are two other methods to store the SRAM data to EEPROM:
• One method is the Hardware Store, initiated by a rising edge on the HS pin.
• The other method is the Software Store, initiated by writing the correct instruction to the command
register via I2C.
The_paragraph_above_is_copyright_Microchip:_AN2047
Explanation
The Great Cow BASIC constants and commands shown below control the configuration of the 47xxx
EE-RAM device. Great Cow BASIC supports I2C hardware and software connectivity - this is shown in
the tables below.
To use the 47xxx driver simply include the following in your user code. This will initialise the driver.
118
#include <47xxx_EERAM.H>
The device parameters for the device are shown in the table below.
Part Number Density (bits) VCC Range Max. I2C Tstore Delay Trecall Delay
Frequency
The Great Cow BASIC constants for control of the device are:
The Great Cow BASIC commands for control of the device are:
119
Command Context Example
Example:
'
#chip 16f18855,32
#option explicit
#include <47xxx_EERAM.H>
#startup InitPPS, 85
Sub InitPPS
UNLOCKPPS
'Module: EUSART
RC0PPS = 0x0010 'TX > RC0
TXPPS = 0x0008 'RC0 > TX (bi-directional)
'Module: MSSP1
SSP1DATPPS = 0x0013 'RC3 > SDA1
RC3PPS = 0x0015 'SDA1 > RC3 (bi-directional)
RC4PPS = 0x0014 'SCL1 > RC4
SSP1CLKPPS = 0x0014 'RC4 > SCL1 (bi-directional)
LOCKPPS
End Sub
120
#define USART_BAUD_RATE 115200
#define USART_TX_BLOCKING
HserPrintCRLF 2
next Idx
121
HserPrintCRLF : HserPrint "Control Byte = " Hex(EERAM_Status()) : HserPrintCRLF 2
end
SAFM
This is the SAFM section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
SAFM Overview
Introduction:
Some Advanced (18F) and some Enhanced Mid-Range (16F) Microchip PIC devices support Storage
Area Flash (SAF) memory. These devices also include EEPROM memory. SAF memory is not High
Endurance, meaning it does not have an endurance of 100K write cyces. SAF has the same endurance
as regular flash memmory, usually specified as 10K write cycles.
SAF memory appears at the top of program memory space and can be used for any purpose, like
regular flash program memory. Storage Area Flash is intended to be used to store data, such a device
calibration data, RF device register settings, and other data. SAFEM can be Read as frequently as
necessary. However, it is not intended to be written frequently like EEPROM. If non-volatile memory
need to be written frequenily, it is best to use the EEPROM on these devices.
As with all flash memory, data must be erased before it can be written and writing this memory will
stall the device for a few ms. Methods to read, write and erase the SAF memory are included in Great
Cow BASIC and they are described in this introduction.
The hefsaf.h library supports SAF operations for Great Cow BASIC.
Note: By default, Great Cow BASIC will use SAF memory for regular executable code unless it is told
otherwise. If you wish to store data here, you should reserve the SAF memory by using the compiler
option, as shown below to reserve 128 Words of SAF memory: This equates to 256 bytes on PIC 18F
microcontrollers and 128 Bytes on PIC 16F microcontrollers
SAF memory is a block of memory locations found at the top of the Flash program memory. Each
memory location can be used to hold a variable value, either a byte or a word dependent on the
specific device. The main difference between SAF memory and EEPROM is that EEPROM allows byte-
by-byte erase whereas the SAF memory does not. With SAF memory data must be erased before a
write and the erase can only be performed in blocks of memory. The blocks, also called rows, are a
122
fixed size associated with the specific device.
Great Cow BASIC handles the erase operation automatically. When a write operation is used by a user
the Great Cow BASIC library reads to a buffer, update the buffer, erase the block and finally write the
buffer back to SAFM. The complexity of using SAF memory is reduced with the automatically
handling of these operations.
123
SAFReadWord a SAFRead ( location , word_variable )
subroutine
with the
parameters:
location,
word_value
SAFEraseBlock a SAFEraseBlock ( 0 )
subroutine
with the A value of 0,1,2,3 etc.
parameters:
block_num
ber
The library also defines a set constants that are specific to the device. These may be useful in the user
program. These constants are used by the library. A user may use these public constants.
124
CHIPWORDS Word Device specific constant for the total
flash size
Warning
Whenever you update the hex file of your Microchip PIC micro-controller with your programmer you
MAY erase the data that are stored in SAF memory. If you want to avoid that you will have to flash
your Microchip PIC micro-controller with software that allows memory exclusion when flashing. This
is the case with Microchip PIC MPLAB IPE (Go to Advanced Mode/Enter password/Select Memory/Tick
“Preserve Flash on Program”/ Enter Start and End address of your SAFM). Or, simply use the
PICkitPlus suite of software to preserve SAF memory during programming.
SAFRead
Syntax:
'as a subroutine
SAFRead ( location, data )
'as a function
data = SAFRead ( location )
Command Availability:
Explanation:
SAFRead is used to read information, byte values, from SAFM, so that it can be accessed for use in a
user program.
location represents the location or relative address to read. The location will range from location 0 to
SAF_BYTES - 1. This cab be from 0-127 or 0-255m depending upon the specific device. HEF_BYTES is a
Great Cow BASIC constant that represents the number of bytes of SAF Memory.
data is the data that is to be read from the SAFM data storage area. This can be a byte value or a byte
125
variable.
This method reads data from SAFM given the specific relative location. This method is similar to the
EPRead method for EEPROM.
Example 1:
'The following example reads the SAFM data value into the byte variable “byte_value”
using a subroutine.
Example 2:
126
'... code preamble to select part
'... code to setup serial
'... code to setup PPS
'The following example reads the SAFM data value into the byte variable “byte_value”
using a function.
SAFReadWord
Syntax:
'as a subroutine
SAFReadWord ( location, data_word_variable )
'as a function
data_word_variable = SAFReadWord ( location )
Command Availability:
Explanation:
SAFReadWord is used to read information, word values, from SAFM so that it can be accessed for use
in a user program.
location represents the location or relative address to read. The location will range from 0 to
SAF_BYTES -1. Each data Word requires 2 SAF Locations, therefore the location will range from either
127
0 to 254 or 0 to 126 (in steps of 2), depending upon the device.
data is the word data that is to be read from the SAFM location. This must be a word variable.
This method reads word information from SAFM given the relative location in SAFM.
Example 1:
'The following example uses a subroutine to read an SAFM location into a word
variable.
Value = 4660
Example 2:
128
'... code preamble to select part
'... code to setup serial
'The following example uses a function to read an SAFM location into a word variable.
Value = 17185
SAFWrite
Syntax:
Command Availability:
Explanation:
SAFWrite is used to write information, byte values, to SAFM so that it can be accessed later for use in a
user program.
location represents the location or relative address to write. The location will range from location 0 to
SAF_BYTES - 1, or for all practical purposes 0-255 since all PIC Microcontrollers with SAFM support 256
129
bytes of SAF Memory. HEF_BYTES is a Great Cow BASIC constant that represents the number of bytes
of SAF Memory.
data is the data that is to be written to the SAFM location. This can be a byte value or a byte variable.
This method writes information to SAFM given the specific location. This method is similar to the
EPWrite method for EEPROM.
Example 1:
#chip 18F24K42, 16
'... code to setup PPS
'... code to setup serial
'The following example writes a byte value of 126 into HEFM location 34
SAFWrite( 34,126 )
Example 2:
#chip 18F24K42, 16
'... code to setup PPS
'... code to setup serial
'This example will populate the 256 bytes of SAF memory with a value that is same as
the SAFM location
SAFM_Dump
end
130
BlockNum = 0
HserPrint "Block"
HSerprint BlockNum
HSerprint " 1 0 3 2 5 4 7 6 9 8 B A D C F E"
BlockNum++
End if
IF NVM_Address % 16 = 0 then
HSerPrintCRLF
hserprint hex(NVM_Address_H)
hserprint hex(NVM_Address)
hserprint " "
end if
hserprint hex(DataWord_H)
hserprint hex(DataWord)
hserprint " "
131
Block0 1 0 3 2 5 4 7 6 9 8 B A D C F E
7F00 0100 0302 0504 0706 0908 0B0A 0D0C 0F0E
7F10 1110 1312 1514 1716 1918 1B1A 1D1C 1F1E
7F20 2120 2322 2524 2726 2928 2B2A 2D2C 2F2E
7F30 3130 3332 3534 3736 3938 3B3A 3D3C 3F3E
Block1 1 0 3 2 5 4 7 6 9 8 B A D C F E
7F40 4140 4342 4544 4746 4948 4B4A 4D4C 4F4E
7F50 5150 5352 5554 5756 5958 5B5A 5D5C 5F5E
7F60 6160 6362 6564 6766 6968 6B6A 6D6C 6F6E
7F70 7170 7372 7574 7776 7978 7B7A 7D7C 7F7E
Block2 1 0 3 2 5 4 7 6 9 8 B A D C F E
7F80 8180 8382 8584 8786 8988 8B8A 8D8C 8F8E
7F90 9190 9392 9594 9796 9998 9B9A 9D9C 9F9E
7FA0 A1A0 A3A2 A5A4 A7A6 A9A8 ABAA ADAC AFAE
7FB0 B1B0 B3B2 B5B4 B7B6 B9B8 BBBA BDBC BFBE
Block3 1 0 3 2 5 4 7 6 9 8 B A D C F E
7FC0 C1C0 C3C2 C5C4 C7C6 C9C8 CBCA CDCC CFCE
7FD0 D1D0 D3D2 D5D4 D7D6 D9D8 DBDA DDDC DFDE
7FE0 E1E0 E3E2 E5E4 E7E6 E9E8 EBEA EDEC EFEE
7FF0 F1F0 F3F2 F5F4 F7F6 F9F8 FBFA FDFC FFFE
SAFWriteWord
Syntax:
Command Availability:
Explanation:
SAFWriteWord is used to write information, word values, to the SAFM data storage, so that it can be
accessed later by a programmer on a Personal, or by the SAFRead commands.
location presents the location or relative address to write. The location will range from 0 to SAF_BYTES
132
-1. Each data Word requires 2 SAF Locations, therefore the location will range from either 0 to 254 or
0 to 126 (in steps of 2), depending upon the device.
data is the data that is to be written to the SAFM location. This can be a word value or a word variable.
This method writes information to SAFM given the specific location in SAFM. This method is similar
to the methods for EEPROM, but supports Word values.
Example 1:
'The following example stores in the word value of 0x1234 as SAFM location 34
Example 2:
133
#chip 18F24K42, 16
'... code to setup PPS
'... code to setup serial
'This example will write two word values to two specific locations.
If example 2 were displayed on a serial terminal. The result would show, where ---- is the existing
value.
Word_Variable1 = 4660
Word_Variable2 = 17185
SAFReadBlock
Syntax:
Command Availability:
134
Available on all PIC micro-controllers with SAFM memory.
Explanation:
HEFReadBlock is used to read information from the HEFM data storage into the buffer. Once the
buffer is populated it can be accessed for use within a user program.
block_number represents the block to be written to. The block_number parameter is used to calculate
the physical memory location(s) that are updated.
buffer() represents an array or string. The buffer will be used as the data target for the block read
operation. The buffer is handled as a buffer of bytes values. In most cases the buffer should be the
same size as a row/block of SAFM. For most PIC Microcontrollers with SAFM this will be 32 bytes.
num_bytes is an optional parameter, and can be used to specify number of bytes to read from SAFM,
starting at the first location in the selected SAFM block. This parameter is not normally required as
the default is set to the Great Cow BASIC constant SAF_ROWSIZE_BYTES.
Example 1:
#chip 18F24K42, 16
'... code preamble to setup PPS
'... code to setup serial
Dim My_Buffer(HEF_ROWSIZE_BYTES)
Dim index as byte
135
See also SAFM Overview, SAFRead, SAFReadWord, SAFWrite, SAFWriteWord, SAFReadBlock,
SAFWriteBlock, SAFEraseBlock
SAFWriteBlock
Syntax:
Command Availability:
Explanation:
SAFWriteBlock is used to write information from a user buffer to SAFM. Once the block is written it
can be accessed for use within a user program.
block_number represents the block to be written to. The block_number parameter is used to calculate
the physical memory location(s) that are updated.
buffer() represents an array or string. The buffer will be used as the data source that is written to the
SAFM block. The buffer is handled as a buffer of bytes values. In most cases the buffer should be the
same size as a row/block of SAFM. For most PIC Microcontrollers this will be 32 bytes. Best practice
is to size the buffer using the SAF_ROWSIZE_BYTES constant. If the size of the buffer exceeds the
device specific SAF_ROWSIZE_BYTES, the excess data will not be handled and the buffer will be
truncated at the SAF_ROWSIZE_BYTES limit.
num_bytes is an optional parameter, and can be used to specify the number of bytes to write to HEFM,
starting at the first location in the selected HEFM block. This parameter is not normally required as
the default is set to the Great Cow BASIC constant HEF_ROWSIZE_BYTES.
Example 1:
136
#chip 18F24K42, 16
'... code preamble to setup PPS
'... code to setup serial
Dim My_Buffer(HEF_ROWSIZE_BYTES)
Dim index as byte
SAFEraseBlock
Syntax:
SAFEraseBlock ( block_number )
Command Availability:
SAFEraseBlock is used to erase all data locations within the SAFM block. HEFM data within the HEFM
block to the erase state value of the device. This Value is 0xFF for each location and will read 0xFFFF
if the program memory word is displayed. Use Caution. Once the SAFM block is erased, the SAFEM data
is gone forever and cannot be recovered unless it was previpusly saved.
137
block_number represents the block to be erased. The block_number parameter is used to calculate the
physical memory location(s) that are updated.
Example 1:
138
Flow control
This is the Flow control section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Do
Syntax:
Command Availability:
Explanation:
The Do command will cause the code between the Do and the Loop to run repeatedly while condition is
true or until condition is true, depending on whether While or Until has been specified.
Note that the While or Until and the condition can only be specified once, or not at all. If they are not
specified, then the code will repeat endlessly.
Example 1:
139
'This code will flash a light until the button is pressed
#chip 12F629, 4
#config osc = int
Dir BUTTON In
Dir LIGHT Out
Do Until BUTTON = 1
PulseOut LIGHT, 1 s
Wait 1 s
Loop
Example 2:
This code will also flash a light until the button is pressed. This example uses EXIT DO within a
continuous loop.
#chip 12F629, 4
#config osc = int
Dir BUTTON In
Dir LIGHT Out
Do
PulseOut LIGHT, 1 s
Wait 1 s
if BUTTON = 1 then EXIT DO
Loop
End
Syntax:
End
Command Availability:
140
Available on all microcontrollers.
Explanation:
When the End command is used, the program will immediately stop running. There are very few cases
where this command is needed - generally, the program should be an endless loop.
Example:
'This program will turn on the red light, but not the green light
Set RED On
End
Set GREEN On
Exit
Syntax options:
Command Availability:
Explanation:
This command will make the program exit the routine it is currently in, as it would if it came to the end
of the routine.
Applies to Subroutines, Functions, For-Next loops, Do-Loop loops and Repeat loops.
Example:
141
#chip tiny13, 1
Do
Burglar
Loop
For more help, see Do, For, Sub, Functions and Repeat
For
Syntax:
Command Availability:
Explanation:
The For command is ideal for situations where a piece of code needs to be run a set number of times,
142
and where it is necessary to keep track of how many times the code has run. When the For command is
first executed, counter is set to start. Then, each successive time the program loops, increment is added
to counter, until counter is equal to end. Then, the program continues beyond the Next.
Step and increment are optionals. If Step is not specified, Great Cow BASIC will increment counter by 1
each time the code is run.
The Exit For is optional and can be used to exit the loop upon a specific condition.
Example 1:
#chip 16F88, 8
#config Osc = Int
For LoopCounter = 1 to 6
PulseOut Led, 1 s
Wait 1 s
Next
Example 2:
143
'This code will flash alternate LEDS until the switch is pressed.
#chip 16F88, 8
#config Osc = Int
Gosub
Syntax:
Gosub label
Command Availability:
Explanation:
The Gosub command is used to jump to a label as a subroutine, in a similar way to Goto. The difference
is that Return can then be used to return to the line of code after the Goto.
NOTE
Gosub should NOT be used if it can be avoided. It is not required to call a subroutine that has been
defined using Sub, just write the name of the subroutine.
Example:
144
'This program will flash an LED on portb bit 0 and play a beep on
'porta bit 4. until the microcontroller is turned off.
Do
'Flash Light
PulseOut LIGHT, 1 s
Wait 1 s
'Beep
Gosub PlayBeep
Loop
PlayBeep:
Tone 200, 10
Tone 100, 10
Return
Goto
Syntax:
Goto label
Command Availability:
Explanation:
The Goto command will make the microcontroller jump to the line specified, and continue running the
program from there. The Goto command is mainly useful for exiting out of loops - if you need to create
an infinite loop, use the Do command instead.
Be careful how you use Goto. If used too much, it can make programs very hard to read.
To define a label, put the name of the label alone on a line, with just a colon (:) after it.
Example:
145
'This program will flash the light until the button is pressed
'off. Notice the label named SWITCH_OFF.
Do
PulseOut LIGHT, 500 ms
If BUTTON = 1 Then Goto SWITCH_OFF
Wait 500 ms
If BUTTON = 1 Then Goto SWITCH_OFF
Loop
SWITCH_OFF:
Set LIGHT Off
'Chip will enter low power mode when program ends
If
Syntax:
146
Short form:
Long form:
If condition Then
...
program code
...
End If
Using Else:
If condition Then
code to run if true
Else
code to run if false
End If
Using If Else:
If condition Then
code to run if true
Else if nextcondition then
code to run if nextcondition true
Else
code to run if false
End If
Command Availability:
Explanation:
The If command is the most common command used to make decisions. If condition is true, then
command (short) or program code (long) will be run. If it is false, then the microcontroller will skip to the
code located on the next line (short) or after the End If (long form).
If Else is used, then the condition between If and Else will run if the condition is true, and the code
between Else and End If will run if the condition is false.
If Else if is used, then the condition after the Else if will run if the condition is true.
147
Supported:
Example:
#chip 12F683, 8
#config osc = int
Do
If ReadAD(SENSOR) > 128 Then
Set LIGHT Off
Else
Set LIGHT On
End If
Loop
IndCall
Syntax:
IndCall Address
Command Availability:
148
Explanation:
IndCall provides a basic implementation of function pointers. Address is the program memory location
of the subroutine that is to be called. There are two ways to specify this - either by providing a direct
reference to the subroutine using the @ operator, or by specifying a word variable that contains the
address.
This command is useful for callbacks. For example, a particular subroutine might read bytes from a
serial connection, but different actions may need to be taken at different times. A different subroutine
could be created for each action, and then the subroutine for the appropriate action could be passed to
the serial connection reading routine each time it is called.
Note: Calling subroutines that have parameters using IndCall is not supported. Errors may occur. If
data needs to be passed, use a variable instead.
Example:
'Main loop
Do
'Indirect call to subroutine at location FlashingSub
IndCall FlashingSub
Loop
Pause
Syntax:
Command Availability:
149
Available on all microcontrollers.
Explanation:
The Pause command will cause the program to pause for a specified time in milliseconds. The only unit
of time permitted is milliseconds.
Repeat
Syntax:
Repeat times
...
program code
...
<condition> Exit Repeat
...
End Repeat
Command Availability:
Explanation:
The Repeat command is ideal for situations where a piece of code needs to be run a set number of
times. It uses less memory and runs faster than the For command, and should be used wherever it is
not necessary to count how many times the code has run.
Example:
150
'This code will flash a green light 6 times.
#chip 16F88, 20
Repeat 6
PulseOut LED, 1 s
Wait 1 s
End Repeat
Select
Syntax:
Case value1
code1
Case value2
code2
Case Else
code4
End Select
Command Availability:
Explanation:
The Select Case control structure is used to select and run a particular section of code, based on the
value of var. If var equals value1 then code1 will be run. Once code1 has run, the chip will jump to the
End Select command and continue running the program. If none of the other conditions are true, then
the code under the Case Else section will be run.
151
Case var TO var is a range of values. If the value is within the range the code section will be executed.
Case Else is optional, and the program will function correctly without it.
If there is only one line of code after the Case, the code may look neater if the line is placed after the
Case. This is shown below in the example, for cases 3, 4 and 5.
It is important to note that only one section of code will be run when using Select Case.
Example 1:
#chip 16F877a, 4
DIR PORTA.0 IN
Do
Temp = ReadAD(AN0) / 20
CLS
Select Case Temp
Case 0
Print "None!"
Case 1
Print "One"
Case 2
Print "Two"
Case 3: Print "Three"
Case 4: Print "Four"
Case 5: Print "Five"
Case Else
Print "A lot!"
End Select
Wait 250 ms
Loop
152
Example 2:
This code demonstrates how to receive codes from a handheld remote control unit. This has been
tested and supports a Sony TV remote and also a universal remote set to Sony TV mode.
The program gets both the device number and the key number, and also translates the key number to
English. The received results are displayed on an LCD.
The circuit for the IR receiver and the chip is shown below.
'----- Constants
'----- Variables
'----- Program
do
getIR, cmd 'wait for IR signal
153
printCmd 'show device and command
printKey 'show key label
wait 10 mS 'ignore any repeats
loop 'repeat forever
'----- Subroutines
sub getIR
tarry1:
count = 0 'wait for start bit
do while IR = 0 'measure width (active low)
wait 100 uS '24 X 100 uS = 2.4 mS
count += 1
loop
if count < 20 then goto tarry1 'less than this so wait
154
print device
155
case 17
button = "Chan-"
case 18
button = "Vol+"
case 19
button = "Vol-"
case 20
button = "Mute"
case 21
button = "Power"
case else
button = " "
end select
print button
end sub
Wait
Syntax:
156
Conditional Delay:
Command Availability:
Explanation:
The Wait command will cause the program to wait for either a specified amount of time (such as 1
second), or while/until a condition is true.
When using the fixed-length delay, there is a variety of units that are available:
us 1 microsecond 1 us - 65535 us
ms 1 millisecond 1 ms - 65535 ms
s 1 second 1 s - 255 s
At one stage, Great Cow BASIC variables could not hold more than 255. The 10us and 10ms units were
added as a way to work around this limit. There is now no such limit (Wait 1000 ms will work for
example), so these are not really needed. However, you may see them in some older examples or
programs, and the 10us units are sometimes the shortest delay that will work accurately.
Example:
157
'This code will wait until a button is pressed, then it will flash
'a light every half a second and produce a 440 Hz tone.
#chip 16F819, 8
#config osc = int
Do
'Flash the light
Set LIGHT On
Wait 500 ms
Set LIGHT Off
158
Fixed Voltage Reference
This is the Fixed Voltage Reference section of the Help file. Please refer the sub-sections for details
using the contents/folder view.
FVRInitialize
Syntax:
Command Availability:
Available on all Microchip microcontrollers with the Fixed Voltage Reference (FVR) module.
Explanation:
The 1.024V FVR value exists for usage with other modules not just the ADC module.
Example:
159
'// use FVR 4096 as Reference
FVRInitialize ( FVR_4x )
wait while FVRIsOutputReady = false
ADVal = ReadAd(AN0)
FVRIsOutputReady
Syntax:
user_var = FVRIsOutputReady()
Command Availability:
Available on all Microchip microcontrollers with the Fixed Voltage Reference (FVR) module.
Explanation:
The method is a function that returns the state of the FVR. The returned value can be assigned to a
variable to used as function.
Example:
160
Interrupts
This is the Interrupt section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Interrupts overview
Introduction
Interrupts are a feature of many microcontrollers. They allow the microcontroller to temporarily
pause (interrupt) the code it is running and then start running another piece of code when some event
occurs. Once it has dealt with the event, it will return to where it was and continue running the
program.
Many events can trigger an interrupt, such as a timer reaching its limit, a serial message being
received, or a special pin on the microcontroller receiving a signal.
Using Interrupts
There are two ways to use interrupts in Great Cow BASIC. The first way is to use the On Interrupt
command. This will automatically enable a given interrupt, and run a particular subroutine when the
interrupt occurs.
The other way to deal with interrupts is to create a subroutine called Interrupt. Great Cow BASIC will
call this subroutine whenever an interrupt occurs, and then your code can check the "flag" bits to
determine which interrupt has occured, and what should be done about it. If you use this approach,
then you’ll need to enable the desired interrupts manually. It is also essential that your code clears the
flag bits, or else the interrupt routine will be called repeatedly.
Some combination of these two methods is also possible - the code generated by On Interrupt with
check to see if the interrupt is one it recognises. If the interrupt is recognised, On Interrupt will deal
with it - if not, the Interrupt subroutine will be called to deal with the interrupt.
The recommended way is to use On Interrupt, as it is both more efficient and easier to set up.
During some sections of code, it is desirable not to have any interrupts occur. If this is the case, then
use the IntOff command to disable interrupts at the start of the section, and IntOn to re-enable them at
the end. If any interrupt events occur while interrupts are disabled, then they will be processed as
soon as interrupts are re-enabled. If the program does not use interrupts, IntOn and IntOff will be
removed automatically by Great Cow BASIC.
IntOff
Syntax:
161
IntOff
Command Availability:
Available on Microchip PIC and Atmel AVR microcontrollers with interrupt support. Will be
automatically removed on chips without interrupts.
Explanation:
IntOff is used to disable interrupts on the microcontroller. It should be used at the start of code which
is timing-sensitive, and which would not function correctly if paused and restarted.
It is essential that IntOn is used to turn interrupts on again after the timing-sensitive code has finished
running. If not, no interrupts will be handled.
It is recommended that IntOff be placed before all code that is timing sensitive, in case interrupts are
implemented later.
IntOn
Syntax:
IntOn
Command Availability:
Available on Microchip PIC and Atmel AVR microcontrollers with interrupt support. Will be
automatically removed on chips without interrupts.
Explanation:
IntOn is used to enable interrupts on the microcontroller after IntOff has disabled them. It should be
used at the end of code which is timing-sensitive.
On Interrupt
Syntax:
162
On Interrupt event Call handler
On Interrupt event Ignore
Command Availability:
Available on Microchip PIC and Atmel AVR microcontrollers with interrupt support.
Explanation:
On Interrupt will add code to call the subroutine handler whenever the interrupt event occurs. When
Call is specified, Great Cow BASIC will enable the interrupt, and call the interrupt handler when it
occurs. When Ignore is specified, Great Cow BASIC will disable the interrupt handler and prevent it
from being called when the event occurs. If the event occurs while the handler is disabled, then the
handler will be called as soon as it is re-enabled. The only way to prevent this from happening is to
manually clear the flag bit for the interrupt.
There are many possible interrupt events that can occur, and the events vary greatly from chip to chip.
Great Cow BASIC will display an error if a given chip cannot support the specified event.
On Interrupt may require the setting or clearing of the interrupt register bit(s), and, On Interrupt may
require setting of explicit enable register bits. You should always consult the device datasheet for these
On Interrupt additional specific settings of register bits. Typically, you will need define the 1) source
event register bit(s) in the main program, and, 2) clear or set the register bit at the start of the of the
interrupt handler subroutine.
Great Cow BASIC has many demonstrations showing how to set and enable appropiate interrupt
register bits to support the On Interrupt method.
If On Interrupt is used to handle an event, then the Interrupt() subroutine will not be called for that
event. However, it will still be called for any events not dealt with by On Interrupt.
Events:
Great Cow BASIC supports the events shown on the table below. Some events are only implemented on
a few specialised chips. Events in grey are supported by Microchip PIC and Atmel AVR
microcontrollers, events in blue are only supported by some Microchip PIC microcontrollers, and
events in red are only supported by Atmel AVR microcontrollers.
Note that Great Cow BASIC doesn’t fully support all of the hardware which can generate interrupts -
some work may be required with various system variables to control the unsupported peripherals.
163
Event Name Description Supported
BatteryFail The battery has failed in some way. This is only implemented on the AVR
ATmega406
164
Event Name Description Supported
Ethernet The Ethernet module has generated an interrupt. This must be dealt Microchip
within the handler.
OscillatorFail The external oscillator has failed, and the microcontroller is running Microchip
from an internal oscillator.
165
Event Name Description Supported
PORTChange The pins on ports ABCEDEF have changed. This is generic port change Microchip
interrupt. You must inspect the source to ensure you are handlign the
correct interrupt.
PSC0Capture The counter for Power Stage Controller 0 matches the value in a AVR
compare register, the value of the counter has been captured, or a
synchronisation error has occurred
PSC0EndCycle Power Stage Controller 0 has reached the end of its cycle AVR
PSC1Capture The counter for Power Stage Controller 1 matches the value in a AVR
compare register, the value of the counter has been captured, or a
synchronisation error has occurred
PSC1EndCycle Power Stage Controller 1 has reached the end of its cycle AVR
PSC2Capture The counter for Power Stage Controller 2 matches the value in a AVR
compare register, the value of the counter has been captured, or a
synchronisation error has occurred
PSC2EndCycle Power Stage Controller 2 has reached the end of its cycle AVR
PWMTimeBase The PWM time base matches the PWM Time Base Period register Microchip
(PTPER)
SPIReady The SPI module has finished the previous transfer AVR
SPMReady A write to program memory by the spm instruction has finished AVR
166
Event Name Description Supported
Timer0Capture An input event on the pin ICP0 has caused the value of Timer 0 to be AVR
captured in the ICR0 register
Timer0Match1 Timer 0 matches the Timer 0 output compare register A (OCR0A) AVR
Timer0Match2 Timer 0 matches the Timer 0 output compare register B (OCR0B) AVR
Timer1Capture An input event on the pin ICP1 has caused the value of Timer 1 to be AVR
captured in the ICR1 register
Timer1Error The Timer 1 Fault Protection unit has been detected by an input on the AVR
INT0 pin
Timer1Match1 Timer 1 matches the Timer 1 output compare register A (OCR1A) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer1Match2 Timer 1 matches the Timer 1 output compare register B (OCR1B) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer1Match3 Timer 1 matches the Timer 1 output compare register C (OCR1C) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer1Match4 Timer 1 matches the Timer 1 output compare register D (OCR1D) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer2Match Timer 2 matches the Timer 2 output compare register (PR2) Microchip
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer2Match1 Timer 2 matches the Timer 2 output compare register A (OCR2A) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer2Match2 Timer 2 matches the Timer 2 output compare register B (OCR2B) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer3Capture An input event on the pin ICP3 has caused the value of Timer 3 to be AVR
captured in the ICR3 register
167
Event Name Description Supported
Timer3Match1 Timer 3 matches the Timer 3 output compare register A (OCR3A) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer3Match2 Timer 3 matches the Timer 3 output compare register B (OCR3B) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer3Match3 Timer 3 matches the Timer 3 output compare register C (OCR3C) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer4Capture An input event on the pin ICP4 has caused the value of Timer 4 to be AVR
captured in the ICR4 register
Timer4Match Timer 4 matches the Timer 4 output compare register (PR4) Microchip
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer4Match1 Timer 4 matches the Timer 4 output compare register A (OCR4A) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer4Match2 Timer 4 matches the Timer 4 output compare register B (OCR4B) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer4Match3 Timer 4 matches the Timer 4 output compare register C (OCR4C) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer5CAP1 An input on the CAP1 pin has caused the value of Timer 5 to be Microchip
captured in CAP1BUF
Timer5CAP2 An input on the CAP2 pin has caused the value of Timer 5 to be Microchip
captured in CAP2BUF
Timer5CAP3 An input on the CAP3 pin has caused the value of Timer 5 to be Microchip
captured in CAP3BUF
Timer5Capture An input event on the pin ICP5 has caused the value of Timer 5 to be AVR
captured in the ICR5 register
Timer5Match1 Timer 5 matches the Timer 5 output compare register A (OCR5A) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
168
Event Name Description Supported
Timer5Match2 Timer 5 matches the Timer 5 output compare register B (OCR5B) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer5Match3 Timer 5 matches the Timer 5 output compare register C (OCR5C) AVR
Within the Interrupt handling sub routine ensure the timer reset and
cleartimer is set appropriately.
Timer6Match Timer 6 matches the Timer 6 output compare register (PR6) Microchip
Timer8Match Timer 8 matches the Timer 8 output compare register (PR8) Microchip
Timer10Match Timer 10 matches the Timer 10 output compare register (PR10) Microchip
Timer12Match Timer 12 matches the Timer 12 output compare register (PR12) Microchip
TWIConnect The Atmel AVR has been connected to or disconnected from the TWI Microchip&
(I2C) bus AVR
TWIReady The TWI has finished the previous transmission and is ready to send or Microchip&
receive more data AVR
169
Event Name Description Supported
USB The USB module has generated an interrupt. This must be dealt with in Microchip&
the handler. AVR
Example 1:
170
'This program increments a counter every time Timer1 overflows
#chip 16F877A, 20
Wait 100 ms
Print "Int Test"
Do
CLS
Print CounterValue
Wait 100 ms
Loop
Sub IncCounter
CounterValue ++
End Sub
Example 2:
171
'This example reflects the input signal on the output port.
#chip mega328p, 16
#option explicit
'maintain a loop
do
loop
sub togglePin
portb.5 = !pinb.5
end sub
Example 3:
172
'This example reflects the input signal on the output port from the external
interrupt port.
#Chip mega328p, 16
#option explicit
'maintain a loop
do
loop
sub togglePin
portb.5 = !pinb.5
end sub
For more help, see InitTimer0 article contains an example of using Timer 0 and On Interrupt to
generate a Pulse Width Modulation signal to control a motor.
173
On Interrupt: The default handler
Introduction
1. You can define the interrupt flags and the default handler (a sub routine) will executed
2. You can define an On Interrupt event Call handler where the handler is executed that matches the
event and where all other define/valid events are handled by the default handler (a sub routine),
The easiest way to write an interrupt handler is to write it in Great Cow BASIC in conjunction with
the On Interrupt statement. On Interrupt tells microcontroller to activate its internal interrupt
handling and to jump to a predetermined interrupt handler (a sub routine that has been defined)
when the interrupt handler (the sub routine) has completed processing returns to correct address
in the program. See On Interrupt.
This method of supports the handling interrupts by enabling a default interrupt subroutine.
Example 1
This example shows if an event occurs the microcontroller will be program to jump to the interrupt
vector and the program will not know the event type, it will simple execute the Interrupt subroutine.
This code is not intended as a meaningful solution and intended to show the functionality only. An LED
is attached to PORTB.1 via a suitable resistor. It will light up when the Interrupt event has occurred.
#chip 16f877a, 4
dir PORTB.1 out
Set PORTB.1 Off
Sub Interrupt
Set PORTB.1 On
TMR1IF = 0
End Sub
Example 2
Any events that are not dealt with by On Interrupt will result in the code in the Interrupt subroutine
executing. This example shows the operation of two interrupt handlers - is not intended as a
174
meaningful solution.
LEDs are attached to PORTB.1 and PORTB.2 via suitable resistors. They will light up when the
Interrupt events occur.
#chip 16f877a, 4
On Interrupt Timer1Overflow call Overflowed
Sub Interrupt
Set PORTB.2 On
TMR2IF = 0
End Sub
Sub Overflowed
Set PORTB.1 On
TMR1IF = 0
End Sub
175
Keypad
This is the Keypad section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Keypad Overview
Introduction
The keypad routines allow for a program to read from a 4 x 4 matrix keypad.
There are two ways that the keypad routines can be set up. One option is to connect the wires from the
keypad in a particular order, and then to set the KeypadPort constant. The other option is to connect
the keypad in whatever way is easiest, and then set the KEYPAD_ROW_x and KEYPAD_COL_x constants. The
option (setting KeypadPort) will generate slightly more efficient code.
KEYPAD_ROW_1 The pin on the microcontroller that connects to the Row 1 pin on the N/A
keypad
KEYPAD_ROW_2 The pin on the microcontroller that connects to the Row 2 pin on the N/A
keypad
KEYPAD_ROW_3 The pin on the microcontroller that connects to the Row 3 pin on the N/A
keypad
KEYPAD_ROW_4 The pin on the microcontroller that connects to the Row 4 pin on the N/A
keypad
KEYPAD_COL_1 The pin on the microcontroller that connects to the Col 1 pin on the N/A
keypad
KEYPAD_COL_2 The pin on the microcontroller that connects to the Col 2 pin on the N/A
keypad
KEYPAD_COL_3 The pin on the microcontroller that connects to the Col 3 pin on the N/A
keypad
KEYPAD_COL_4 The pin on the microcontroller that connects to the Col 4 pin on the N/A
keypad
176
When setting up the keypad code using the KeypadPort constant, only KeypadPort needs to be set.
Pull-ups or pull-downs go on the columns only, and are typically 4.7k to 10k in value.
KeypadPort The port on the microcontroller chip that the keypad is connected to. N/A
The keypad routine has a feature when using pull-down resistors, simply add the constant to your
program and the and the scan logic will be inverted appropriately.
0 Row 1
1 Row 2
2 Row 3
3 Row 4
4 Column 1
5 Column 2
6 Column 3
7 Column 4
Note: To use a 3 x 3 keypad in this mode, the pins on the microcontroller for any unused columns must
be pulled up.
KeypadData
Syntax:
var = KeypadData
Command Availability:
177
Explanation:
This function will return a value corresponding to the key that is pressed on the keypad. Note that if
two or more keys are pressed, then only one value will be returned. var can have one of the following
values:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 KEY_A A
11 KEY_B B
12 KEY_C C
13 KEY_D D
Example:
178
'Program to show the value of the last pressed key on the LCD
#chip 18F4550, 20
'Main loop
Do
'Get key
Temp = KeypadData
KeypadRaw
Syntax:
largevar = KeypadRaw
Command Availability:
Explanation:
This function will return a 16 bit value, in which each bit corresponds to a key on the keypad. If the key
is pressed its bit will hold 1, and if it is released its bit will contain a 0.
179
This table shows the key that each bit corresponds to:
15 1,1 1
14 1,2 2
13 1,3 3
12 1,4 A
11 2,1 4
10 2,2 5
9 2,3 6
8 2,4 B
7 3,1 7
6 3,2 8
5 3,3 9
4 3,4 C
3 4,1 *
2 4,2 0
1 4,3 #
0 4,4 D
Example:
180
'Program to show the keypad status using LEDs
#chip 16F877A, 20
'LEDs
#define LED1 PORTC
#define LED2 PORTD
Dir LED1 Out
Dir LED2 Out
'Main loop
Do
'Get key
KeyStatus = KeypadRaw
'Display
LED1 = KeyStatus_H 'High Byte
LED2 = KeyStatus 'Low Byte
Loop
181
Graphical LCD
This is the GLCD section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
GLCD Overview
The GLCD commands are used to control a Graphical Liquid Crystal Display (GLCD) based on the a
number of GLCD chipsets. These are often 128x64 pixel displays but the size can vary. GLCD devices
draw graphical elements by enabling or disabling pixels.
A GLCD is an upgrade from the popular 16x2 LCDs (see Liquid Crystal Display Overview ) but the GLCD
allows full graphical control of the display.
• Require memory in the microcontroller to support graphial operations or can be used in text and
jpg mode only.
Great Cow BASIC makes this type of device easier to control with the commands for the GLCD.
• Memory required: For full GLCD capabilites you will require 1k or more, for text only and JPG
mode low memory devices are supported
Review your choice of microcontroller and GLCD carefully before commencing your project.
182
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
1 KS0 H 2.9 128 Lar Mon LCD 8-bit Typically Bit 7 of the Requires 12 These are
108 el inch * 64 ge o typica parallel operates at bus is ports/conne low cost
p and lly PIC and VCC 5. read/write – ctions. mono
less. with AVR: Always this could devices.
vari backli Softwar check cause
ous ght e voltage potential
sizes device specificatio lockup –
specific ns 8-bit bus this is low
protoco required. risk. Uses
l the
KS0107B (or
NT7107C) a
64-channel
common
driver
which
generates
the timing
signal to
control the
two
KS0108B
segment
drivers.
2 ILI H 3.2in 320 Lar Colo TFT PIC: Set +VCC from UNO shield SPI requires Good GLCD
948 el ch * ge r LCD per bit. 2.7 to 5. is excellent. 4 ports plus with very
1 p 240 8-bit AVR: Always Very fast 2 ports. good GLCD
parall via check display. Typically 6 performanc
el Shield voltage in total. e.
set via specificatio
AND ns
PORT
comma
nd
183
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
3 PC H 1.77i Nok 160 Sma SPI PIC and Display can Minimum SPI requires Good for
D85 el nch ia * ll AVR: operate in RAM 4 ports plus cost and
44 p 331 128 Mon Device text mode required is 2 ports. performanc
0 or o specific only for low 512 bytes Typically 6 e
511 LCD SPI RAM then add in total.
0 with comma microcontr user
LED nd, all ollers as full variables
in GLCD for graphics
softwar capabilities mode, this
e. requires display can
512bytes of operate in
RAM. +VCC text mode
3.3. Always only for low
check RAM
voltage microcontr
specificatio ollers.
ns Nice
display.
Sensitive to
operating
voltages.
4 ILI H 2.8 320 Me Colo TFT SPI PIC Typically Very nice SPI requires Good for
934 el Inch * diu r LCD and operates at display. 4 ports plus cost and
1 p or 240 m AVR: VCC 5. 2 ports. performanc
3.2 Hardw Always Typically 6 e
Inch are and check in total.
softwar voltage
e SPI specificatio
ns
5 SSD H 3.2in 240 Lar Colo TFT 16-bit Typically Mega2560 Connectivit Good for
128 el ch * ge r LCD parallel operates at shield y requires Mega2560
9 p 320 AVR: VCC 5. required. 20 ports. type shields
Softwar Always
e check
device voltage
specific specificatio
protoco ns
l.
184
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
6 ST7 H 1.8 128 Lar Colo TFT SPI PIC and Typically SPI requires Good for
735 el Inch * 64 ge r LCD AVR: operates at 4 ports plus cost and
p Hardware VCC 5. 2 ports. performanc
and Always Typically 6 e
software check in total.
SPI voltage
specificatio
ns Very nice
display.
7 ILI H 2.2 240 Me Mon TFT SPI PIC and Typically SPI requires Good for
934 el Inch * diu o LCD AVR: operates at 4 ports plus cost and
0 p 320 m Hardware VCC 5. 2 ports. performanc
and Always Typically 6 e
software check in total.
SPI voltage
specificatio
ns
8 ILI H 4inc RPI Lar Colo TFT SPI and PIC and Typically SPI requires An
948 el h 240 ge r LCD 8Bit AVR: operates at 4 ports plus expensive
6L p * Bus Hardware VCC 5. 2 ports. option
or 320 and Always Typically 6
ILI software check in total. 8Bit
948 SPI AVR: voltage Bus
6 8Bit Bus specificatio requires 8
using an ns Great ports plus 4
UNO Shield. pixel control
PIC: 8bit display. ports.
port Typically 13
suppported. in total
using an
8bit bus
solution.
185
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
9 Nex H ITEA 240 Lar Colo TFT Serial - Nextion Typically 2 ports for A very nice
ion el D * ge - r LCD hardwa specfic and operates at the option but
p Nexi 320 2.4 re or GLCD VCC 5 with read/write if you need
on to to softwar command external serial flexibility
800 7in e serial set power operations. then the
* che is supply. best!
480 s support Always
ed. check
voltage
specificatio
ns Great
command
set, you
need to
learn the
GUI and
then
interface to
Great Cow
BASIC.
1 SH H 1.3 128 Sm Mon I2C PIC and Always at RAM for I2C requires Good OLED
0 110 el inch * 64 all o AVR: 3.3v. Always Full Mode 2 ports. display,
6 p or OLE Hardw check GLCD is excellent
0.96i D are and voltage 1024 bytes value for
nch softwar specificatio or Low money
e I2C ns Memory
GLCD is 128
bytes or 0
bytes for
Text GLCD
Mode then
add user
variables
for graphics
mode.
186
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
1 SD H 0.96i 128 Sm Mon OLED I2C and PIC and RAM for SPI requires Good OLED
1 D13 el nch * 64 all o SPI AVR: Full Mode 4 ports plus display,
06 p Hardware GLCD is 2 ports. excellent
and 1024 bytes Typically 6 value for
software or Low in total. I2C money
I2C, and Memory requires 2
software GLCD is 128 ports.
SPI bytes or 0
bytes for
Text GLCD
Mode then
add user
variables
for graphics
mode.
Typically
operates at
VCC 5.
Always
check
voltage
specificatio
ns Very
good OLED
display.
Driver
supports
gaming.
Minimum
RAM
required is
1024 bytes
then add
user
variables
for graphics
mode.
Display can
operate in
text mode
only for low
RAM
microcontr
ollers
187
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
1 SD H 0.96i 128 Ver Mon OLED I2C and PIC and RAM for SPI requires Good OLED
2 D13 el nch * 32 y o SPI AVR: Full Mode 4 ports plus display,
06_ p sm Hardware GLCD is 512 2 ports. excellent
32 all and bytes or Typically 6 value for
software Low in total. I2C money
I2C, and Memory requires 2
software GLCD is 128 ports.
SPI bytes or 0
bytes for
Text GLCD
Mode then
add user
variables
for graphics
mode.
Typically
operates at
VCC 5.
Always
check
voltage
specificatio
ns Best
small OLED
display.
Driver
supports
gaming.
Minimum
RAM
required is
512 bytes
then add
user
variables
for graphics
mode, this
display can
operate in
text mode
only for low
RAM
microcontr
ollers
188
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
1 ST7 H 2.9in 128 Lar Mon LCD PIC and Typically 8-bit bus Requires 12 A very slow
3 920 el ch * 64 ge o typica AVR: operates at required. ports. device.
p lly Softwar VCC 5. Bit 7 of the
with e Always bus is
backli device check read/write –
ght 8- specific voltage this could
bit protoco specificatio cause
parall l. ns potential
el lockup –
this is low
risk. This
looks like a
KS0108 but
it is NOT!
Supports
Chinese
font set.
1 HX H 2.2in 240 Lar Colo TFT SPI AVR 8 bit Typically Controller An very
4 834 el ch * ge r LCD bus operates at requires 8 nice display
7G p 320 VCC 5. ports plus 5
Always control
check ports.
voltage Typically 13
specificatio in total with
ns Great an UNO
pixel shield.
display.
1 SD H 0.96i 96 * Sm Colo OLED SPI PIC and Typically SPI requires Very good
5 D13 el nch 48 all r AVR: operates at typically 6 color OLED
31 p Hardware VCC 5. in total. display,
and Always excellent
software check value for
I2C, and voltage money
software specificatio
SPI ns
189
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
1 ILI H 3.00i 400 Lar Colo OLED 8 bit PIC and Typically Requires Good color
6 932 el nch * ge r bus AVR: 8 bit operates at typically 13 OLED
6 p 320 bus VCC 3.3. in total plus display,
Always 0v, VCC and good value
check LED. for money
voltage as it is fast.
specificatio But, the
ns rotate is all
executed in
software
and this
does slow
down
processing.
The LED
connected
is typically
to ground.
You can
solder the
GND
connect to
make the
backlite
permanentl
y on.
190
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
1 NT H 2.9 128 Lar Mon LCD 8-bit Typically Look Requires 12 These are
7 710 el inch * 64 ge o typica parallel operates at similar to ports/conne medium
8C p and lly PIC and VCC 5. KS0108, but, ctions. cost mono
less. with AVR: Always it is NOT the devices.
vari backli Softwar check same, hence
ous ght e voltage this
sizes device specificatio driver. Use
specific ns 8-bit bus s the
protoco required. Winstar’s
l WDG0151-
TMI
module,
which is a
128×64
pixel
monochrom
atic
display. Th
is uses two
Neotic
display
controller
chips: NT71
08C and
NT7107C.
The
WDG0151
module
contains
two sets of
it to drive
128
segments.
On the
other hand,
the
KS0107B (or
NT7107C) is
a 64-
channel
common
driver
which
generates
the timing
signal to
control the
two 191
KS0108B
# Chi D Size Pix De Typ I/O Suppor Operating Comments Requireme Assessmen
pSe oc els pth e t nts t
t s
1 T69 H 4inc 240 Lar Mon LCD 8-bit Typically Operating Requires 12 These are
9 63_ el hes * 64 ge o typica parallel operates at similar to ports/conne medium
64 p by lly PIC and VCC 5. KS0108 and ctions. cost mono
2inc with AVR: Always an LCD. devices.
hes backli Softwar check segment
ght e voltage drivers.
device specificatio
specific ns 8-bit bus
protoco required.
l
2 T69 H 4inc 240 Lar Mon LCD 8-bit Typically Operating Requires 12 These are
0 63_ el hes * ge o typica parallel operates at similar to ports/conne medium
128 p by 128 lly PIC and VCC 5. KS0108 and ctions. cost mono
2inc with AVR: Always an LCD. devices.
hes backli Softwar check segment
ght e voltage drivers.
device specificatio
specific ns 8-bit bus
protoco required.
l
Setup:
You must include the glcd.h file at the top of your program. The file needs to be in brackets as shown
below.
#include <GLCD.h>
Defines:
There are several connections that must be defined to use the GLCD commands with a GLCD display.
The I/O pin is the pin on the Microchip PIC or the Atmel AVR microcontroller that is connected to that
specific pin on the graphical LCD.
192
#define GLCD_RW _I/O pin_ ‘Read/Write pin connection
#define GLCD_RESET _I/O pin_ ‘Reset pin connection
#define GLCD_CS1 _I/O pin_ ‘CS1 pin connection
#define GLCD_CS2 _I/O pin_ ‘CS2 pin connection
#define GLCD_RS _I/O pin_ ‘RS pin connection
#define GLCD_ENABLE _I/O pin_ ‘Enable pin Connection
#define GLCD_DB0 _I/O pin_ ‘Data pin 0 Connection
#define GLCD_DB1 _I/O pin_ ‘Data pin 1 Connection
#define GLCD_DB2 _I/O pin_ ‘Data pin 2 Connection
#define GLCD_DB3 _I/O pin_ ‘Data pin 3 Connection
#define GLCD_DB4 _I/O pin_ ‘Data pin 4 Connection
#define GLCD_DB5 _I/O pin_ ‘Data pin 5 Connection
#define GLCD_DB6 _I/O pin_ ‘Data pin 6 Connection
#define GLCD_DB7 _I/O pin_ ‘Data pin 7 Connection
#define GLCD_PROTECTOVERRUN 'prevent screen overdrawing 'SSD1306 GLCD only
#define GLCDDirection 'Invert GLCD Y axis 'KS0108 GCD only
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode )
Char set
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1, Xposition2,
Yposition2, [Optional In LineColour as 0 or 1]
)
FilledBo Draw a box on the GLCD to a specific size FilledBox (Xposition1, Yposition1, Xposition2,
x that is filled with the foreground colour. Yposition2, [Optional In LineColour 0 or 1] )
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2, [Optional In LineColour 0 or 1] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour 0 or
position that is set with the specific 1)
attribute.
193
Public variable supported across the range of supported GLCDs are shown in the table below. These
variables control the user definable parameters of a specific GLCD.
GLCDfntDefau Size of the current Default is 0.+ This equates to the standard GCB font set.
lt GLCD font.
GLCDfntDefau Size of the current Default is 1.+ This equates to the 8 pixel high.
ltsize GLCD font.
For color TFT displays any color can be defined using a valid hexidecimal word value between 0x0000
to 0xFFFF., see http://www.barth-dev.de/online/rgb565-color-picker/ for a wider range of color
parameters.
TFT_BLACK 0x0000
TFT_NAVY 0x000F
TFT_DARKGREEN 0x03E0
TFT_DARKCYAN 0x03EF
TFT_MAROON 0x7800
TFT_PURPLE 0x780F
TFT_OLIVE 0x7BE0
TFT_LIGHTGREY 0xC618
TFT_DARKGREY 0x7BEF
TFT_BLUE 0x001F
TFT_GREEN 0x07E0
TFT_CYAN 0x07FF
TFT_RED 0xF800
TFT_MAGENTA 0xF81F
TFT_YELLOW 0xFFE0
TFT_WHITE 0xFFFF
TFT_ORANGE 0xFD20
TFT_GREENYELLOW 0xAFE5
TFT_PINK 0xF81F
194
This example shows how to drive a KS0108 based Graphic LCD module with the built in commands of
Great Cow BASIC. See Graphic LCD for details, this is an external web site.
Example:
;Chip Settings
#chip 16F886,16
'#config MCLRE = on 'enable reset switch on CHIPINO
#include <GLCD.h>
;Defines (Constants)
#define GLCD_RW PORTB.1 'D9 to pin 5 of LCD
#define GLCD_RESET PORTB.5 'D13 to pin 17 of LCD
#define GLCD_CS1 PORTB.3 'D12 to actually since CS1, CS2 can be inverted
#define GLCD_CS2 PORTB.4 'D11 to actually since CS1, CS2 can be inverted
#define GLCD_RS PORTB.0 'D8 to pin 4 D/I pin on LCD
#define GLCD_ENABLE PORTB.2 'D10 to Pin 6 on LCD
#define GLCD_DB0 PORTC.7 'D0 to pin 7 on LCD
#define GLCD_DB1 PORTC.6 'D1 to pin 8 on LCD
#define GLCD_DB2 PORTC.5 'D2 to pin 9 on LCD
#define GLCD_DB3 PORTC.4 'D3 to pin 10 on LCD
#define GLCD_DB4 PORTC.3 'D4 to pin 11 on LCD
#define GLCD_DB5 PORTC.2 'D5 to pin 12 on LCD
#define GLCD_DB6 PORTC.1 'D6 to pin 13 on LCD
#define GLCD_DB7 PORTC.0 'D7 to pin 14 on LCD
Start:
GLCDCLS
GLCDPrint 0,10,"Hello" 'Print Hello
wait 5 s
GLCDPrint 0,10, "ASCII #:" 'Print ASCII #:
Box 18,30,28,40 'Draw Box Around ASCII Character
for char = 15 to 129 'Print 0 through 9
GLCDPrint 17, 20 , Str(char)+" "
GLCDdrawCHAR 20,30, char
wait 125 ms
next
line 0,50,127,50 'Draw Line using line command
for xvar = 0 to 80 'Draw line using Pset command
pset xvar,63,on '
next '
Wait 1 s
GLCDPrint 0,10,"End " 'Print Hello
wait 1 s
Goto Start
195
For more help, see Graphical LCD Demonstration, GLCDCLS, GLCDDrawChar, GLCDPrint,
GLCDReadByte, GLCDWriteByte, Pset
HX8347G Controllers
This section covers GLCD devices that use the HX8347G graphics controller.
HX8347G is a 262,144-color single-chip SoC driver for a-TFT liquid crystal display with resolution of
240 RGB x 320 dots.
The HX8347-G is designed to provide a single-chip solution that combines a gate driver, a source
driver, power supply circuit for 262,144 colors to drive a TFT panel with 240RGBx320 dots at
maximum.
The HX8347-G can be operated in low-voltage (1.4V) condition for the interface and integrated internal
boosters that produce the liquid crystal voltage, breeder resistance and the voltage follower circuit for
liquid crystal driver. In addition, The HX8347-G also supports various functions to reduce the power
consumption of a LCD system via software control.
The Great Cow BASIC constants shown below control the configuration of the HX8347G controller.
The Great Cow BASIC constants for control and data line connections are shown in the table below.
Connectivity is via an 8-bit bus. Where 8 pins are connected between the microcontroller and the
GLCD to control the data bus plus 5 control pins. This is simple when using an Arduino GLCD Shield.
To use the HX8347G driver simply include the following in your user code. This will initialise the
driver.
8-bit mode
196
'This GLCD driver supports 8 bit only. UNO ports can be replaced with porta.b
constants.
#include <glcd.h>
#include <UNO_mega328p.h >
#define GLCD_TYPE GLCD_TYPE_HX8347
'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
#define GLCD_RD ANALOG_0 ' read command line
#define GLCD_WR ANALOG_1 ' write command line
#define GLCD_RS ANALOG_2 ' Command/Data line
#define GLCD_CS ANALOG_3 ' Chip select line
#define GLCD_RST ANALOG_4 ' Reset line
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
GLCD_TYPE GLCD_TYPE_HX8347
GLCD_DB0.. Specifies the pin that is connected to DB0..7 IO pin on the GLCD (8 bit DBI). Require
7 d
GLCD_RST Specifies the output pin that is connected to Reset IO pin on the GLCD. Require
d
GLCD_CS Specifies the output pin that is connected to Chip Select (CS) on the GLCD. Require
d
GLCD_RS Specifies the output pin that is connected to Data/Command pin on the GLCD. Require
d
GLCD_WR Specifies the output pin that is connected to Data In (RW or WDR) pin on the Require
GLCD. d
197
Constants Controls Options
GLCD_RD Specifies the output pin that is connected to Data Out (RD or RDR) pin on the Require
GLCD. d
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCD_OLED Specifies the use of the optional OLED font set. The Optional
_FONT GLCDfntDefaultsize can be set to 1 or 2 only. GLCDfntDefaultsize=
1. A small 8 height pixel font with variable width.
GLCDfntDefaultsize= 2. A larger 10 width * 16 height pixel font.
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
GLCDPrin Print string of characters on GLCD using GLCDPrint( Xposition, Yposition, Stringvariable
t GCB font set )
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode
Char set [,Optional LineColour] )
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable [,Optional LineColour] )
Box Draw a box on the GLCD to a specific Box ( Xposition1, Yposition1, Xposition2,
size Yposition2 [,Optional LineColour]
198
Comma Purpose Example
nd
FilledBo Draw a box on the GLCD to a specific FilledBox (Xposition1, Yposition1, Xposition2,
x size that is filled with the foreground Yposition2 [,Optional LineColour] )
colour.
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2 [,Optional LineColour] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour)
position that is set with the specific
attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
eByte datasheet for usage.
HX8347G_ Specify color as a parameter for many Color constants for this device are shown in the
[color] GLCD commands list below.
Any color can be defined using a valid
hexidecimal word value between 0x0000 to
0xFFFF.
199
HX8347G_BLACK 'hexidecimal value 0x0000
HX8347G_RED 'hexidecimal value 0xF800
HX8347G_GREEN 'hexidecimal value 0x0400
HX8347G_BLUE 'hexidecimal value 0x001F
HX8347G_WHITE 'hexidecimal value 0xFFFF
HX8347G_PURPLE 'hexidecimal value 0xF11F
HX8347G_YELLOW 'hexidecimal value 0xFFE0
HX8347G_CYAN 'hexidecimal value 0x07FF
HX8347G_D_GRAY 'hexidecimal value 0x528A
HX8347G_L_GRAY 'hexidecimal value 0x7997
HX8347G_SILVER 'hexidecimal value 0xC618
HX8347G_MAROON 'hexidecimal value 0x8000
HX8347G_OLIVE 'hexidecimal value 0x8400
HX8347G_LIME 'hexidecimal value 0x07E0
HX8347G_AQUA 'hexidecimal value 0x07FF
HX8347G_TEAL 'hexidecimal value 0x0410
HX8347G_NAVY 'hexidecimal value 0x0010
HX8347G_FUCHSIA 'hexidecimal value 0xF81F
These examples show how to drive a HX8347G based Graphic LCD module with the built in commands
of Great Cow BASIC. The 8 bit DBI example uses a UNO shield, this can easily adapted to Microchip
architecture. The 16 bit DBI example uses a Mega2560 board.
Example:
200
#chip mega328p, 16
#option explicit
#include <glcd.h>
#include <UNO_mega328p.h >
'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
#define GLCD_RD ANALOG_0 ' read command line
#define GLCD_WR ANALOG_1 ' write command line
#define GLCD_RS ANALOG_2 ' Command/Data line
#define GLCD_CS ANALOG_3 ' Chip select line
#define GLCD_RST ANALOG_4 ' Reset line
GLCDRotate ( Portrait )
GLCDCLS HX8347_RED
GLCDPrint(0, 0, "Test of the HX8347G Device")
end
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
ILI9326 Controllers
This section covers GLCD devices that use the ILI9326 graphics controller. The ILI9326 is a TFT LCD
Single Chip Driver with 400RGBx320 Resolution and 262K colors.
201
The Great Cow BASIC constants shown below control the configuration of the ILI9326 controller.
Great Cow BASIC supports 8 bit bus connectivity - this is shown in the tables below.
To use the ILI9326 driver simply include the following in your user code. This will initialise the driver.
#include <glcd.h>
#DEFINE GLCD_TYPE GLCD_TYPE_ILI9326
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
GLCD_TYPE GLCD_TYPE_ILI9326
GLCD_RD Specifies the output pin that is connected to RD IO pin on the GLCD. Required
GLCD_WR Specifies the output pin that is connected to WR on the GLCD. Required
GLCD_RS Specifies the output pin that is connected to RS pin on the GLCD. Required
GLCD_CS Specifies the output pin that is connected to CS pin on the GLCD. Required
GLCD_RST Specifies the output pin that is connected to RST pin on the GLCD. Required
GLCD_DataPort Specifies the output port that is connected to DB0 to DB7 pins on the GLCD. Required
The Great Cow BASIC constants for control display characteristics are shown in the table below.
202
Constant Controls Default
s
GLCD_OLED Specifies the use of the optional OLED font set. The Optional
_FONT GLCDfntDefaultsize can be set to 1 or 2 only. GLCDfntDefaultsize=
1. A small 8 height pixel font with variable width.
GLCDfntDefaultsize= 2. A larger 10 width * 16 height pixel font.
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
GLCDDraw Print character on GLCD using GCB font set GLCDDrawChar( Xposition, Yposition,
Char CharCode [,Optional LineColour] )
GLCDDraw Print characters on GLCD using GCB font set GLCDDrawString( Xposition, Yposition,
String Stringvariable [,Optional LineColour] )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1, Xposition2,
Yposition2 [,Optional LineColour]
FilledBo Draw a box on the GLCD to a specific size FilledBox (Xposition1, Yposition1,
x that is filled with the foreground colour. Xposition2, Yposition2 [,Optional
LineColour] )
Line Draw a line on the GLCD to a specific length Line ( Xposition1, Yposition1, Xposition2,
that is filled with the specific attribute. Yposition2 [,Optional LineColour] )
PSet Set a pixel on the GLCD at a specific position PSet(Xposition, Yposition, Pixel Colour)
that is set with the specific attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
eByte datasheet for usage.
GLCDRead Read a byte value from the controller, see bytevariable = GLCDReadByte
Byte the datasheet for usage.
ILI9326_ Specify color as a parameter for many GLCD Color constants for this device are shown in
[color] commands the list below.
203
ILI9326_BLACK 'hexidecimal value 0x0000
ILI9326_RED 'hexidecimal value 0xF800
ILI9326_GREEN 'hexidecimal value 0x07E0
ILI9326_BLUE 'hexidecimal value 0x001F
ILI9326_WHITE 'hexidecimal value 0xFFFF
ILI9326_PURPLE 'hexidecimal value 0xF11F
ILI9326_YELLOW 'hexidecimal value 0xFFE0
ILI9326_CYAN 'hexidecimal value 0x07FF
ILI9326_D_GRAY 'hexidecimal value 0x528A
ILI9326_L_GRAY 'hexidecimal value 0x7997
ILI9326_SILVER 'hexidecimal value 0xC618
ILI9326_MAROON 'hexidecimal value 0x8000
ILI9326_OLIVE 'hexidecimal value 0x8400
ILI9326_LIME 'hexidecimal value 0x07E0
ILI9326_AQUA 'hexidecimal value 0x07FF
ILI9326_TEAL 'hexidecimal value 0x0410
ILI9326_NAVY 'hexidecimal value 0x0010
ILI9326_FUCHSIA 'hexidecimal value 0xF81F
This example shows how to drive a ILI9326 based Graphic LCD module with the built in commands of
Great Cow BASIC.
Example #1
;Chip Settings
#chip 16F1789,32
#config MCLRE=on
#option explicit
#include <glcd.h>
#define GLCD_TYPE GLCD_TYPE_ILI9326
204
Example #2 This example shows how to drive a ILI3941 with the OLED fonts. Note the use of the
GLCDfntDefaultSize to select the size of the OLED font in use.
'Chip Settings
#chip 16F1789,32
#config MCLRE=on
#option explicit
#include <glcd.h>
#define GLCD_TYPE GLCD_TYPE_ILI9326
GLCDfntDefaultSize = 2
GLCDFontWidth = 5
GLCDPrint ( 40, 0, "OLED" )
GLCDPrint ( 0, 18, "Typ: ILI9326" )
GLCDPrint ( 0, 34, "Size: 400 x 240" )
GLCDfntDefaultSize = 1
GLCDPrint(20, 56,"https://goo.gl/gjrxkp")
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
ILI9340 Controllers
This section covers GLCD devices that use the ILI9340 graphics controller. The ILI9340 is a TFT LCD
Single Chip Driver with 240RGBx320 Resolution and 262K colors.
The Great Cow BASIC constants shown below control the configuration of the ILI9340 controller.
Great Cow BASIC supports SPI hardware and software connectivity - this is shown in the tables
below.
To use the ILI9340 driver simply include the following in your user code. This will initialise the driver.
205
#include <glcd.h>
#DEFINE GLCD_TYPE GLCD_TYPE_ILI9340
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
GLCD_T GLCD_TYPE_ILI9340
YPE
GLCD_D Specifies the output pin that is connected to Required
C Data/Command IO pin on the GLCD.
GLCD_C Specifies the output pin that is connected to Chip Select Required
S (CS) on the GLCD.
GLCD_R Specifies the output pin that is connected to Reset pin on Required
eset the GLCD.
GLCD_D Specifies the output pin that is connected to Data Out Required
O (GLCD in) pin on the GLCD.
GLCD_S Specifies the output pin that is connected to Clock (CLK) Required
CK pin on the GLCD. #define GLCD_SCK porta.5
'example port setting
HWSPIM User can specify the hardware SPI mode. Must be one of Optional. Defaults to Masterfast
ode MasterSlow, Master, Masterfast when chipMhz is less than 64mhz
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCDFontWidth Specifies the font width of the Great Cow BASIC font set. 6
206
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
GLCDPrin Print string of characters on GLCD using GLCDPrint( Xposition, Yposition, Stringvariable
t GCB font set )
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode
Char set [,Optional LineColour] )
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable [,Optional LineColour] )
Box Draw a box on the GLCD to a specific Box ( Xposition1, Yposition1, Xposition2,
size Yposition2 [,Optional LineColour]]
FilledBo Draw a box on the GLCD to a specific FilledBox (Xposition1, Yposition1, Xposition2,
x size that is filled with the foreground Yposition2 [,Optional LineColour] )
colour.
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2 [,Optional LineColour] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour)
position that is set with the specific
attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
eByte datasheet for usage.
ILI9340_ Specify color as a parameter for many Color constants for this device are shown in the
[color] GLCD commands list below.
Any color can be defined using a valid
hexidecimal word value between 0x0000 to
0xFFFF.
207
ILI9340_BLACK 'hexidecimal value 0x0000
ILI9340_RED 'hexidecimal value 0xF800
ILI9340_GREEN 'hexidecimal value 0x07E0
ILI9340_BLUE 'hexidecimal value 0x001F
ILI9340_WHITE 'hexidecimal value 0xFFFF
ILI9340_PURPLE 'hexidecimal value 0xF11F
ILI9340_YELLOW 'hexidecimal value 0xFFE0
ILI9340_CYAN 'hexidecimal value 0x07FF
ILI9340_D_GRAY 'hexidecimal value 0x528A
ILI9340_L_GRAY 'hexidecimal value 0x7997
ILI9340_SILVER 'hexidecimal value 0xC618
ILI9340_MAROON 'hexidecimal value 0x8000
ILI9340_OLIVE 'hexidecimal value 0x8400
ILI9340_LIME 'hexidecimal value 0x07E0
ILI9340_AQUA 'hexidecimal value 0x07FF
ILI9340_TEAL 'hexidecimal value 0x0410
ILI9340_NAVY 'hexidecimal value 0x0010
ILI9340_FUCHSIA 'hexidecimal value 0xF81F
This example shows how to drive a ILI9340 based Graphic LCD module with the built in commands of
Great Cow BASIC.
Example:
208
;Chip Settings
#chip 16F1937,32
#config Osc = INT, VCAPEN_OFF, MCLRE_ON, PLLEN_ON, CLKOUTEN_OFF 'microcontroller
specific configuration
#include <glcd.h>
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
ILI9341 Controllers
This section covers GLCD devices that use the ILI9341 graphics controller. The ILI9341 is a TFT LCD
Single Chip Driver with 240RGBx320 Resolution and 262K colors.
The Great Cow BASIC constants shown below control the configuration of the ILI9341 controller.
Great Cow BASIC supports SPI hardware and software connectivity - this is shown in the tables
below.
To use the ILI9341 driver simply include the following in your user code. This will initialise the driver.
209
#include <glcd.h>
#DEFINE GLCD_TYPE GLCD_TYPE_ILI9341
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
GLCD_T GLCD_TYPE_ILI9341
YPE
GLCD_D Specifies the output pin that is connected to Required
C Data/Command IO pin on the GLCD.
GLCD_D Specifies the output pin that is connected to Data Out Required
O (GLCD in) pin on the GLCD.
HWSPIM User can specify the hardware SPI mode. Must be Optional. Defaults to Masterfast when
ode one of MasterSlow, Master, Masterfast chipMhz is less than 64mhz
The Great Cow BASIC constants for control display characteristics are shown in the table below.
210
Constant Controls Default
s
GLCDFontW Specifies the font width of the Great Cow BASIC font set. 6 for GCB fonts, and 5
idth for OLED fonts.
GLCD_OLED Specifies the use of the optional OLED font set. The Optional
_FONT GLCDfntDefaultsize can be set to 1 or 2 only. GLCDfntDefaultsize=
1. A small 8 height pixel font with variable width.
GLCDfntDefaultsize= 2. A larger 10 width * 16 height pixel font.
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
GLCDPri Print string of characters on GLCD using GCB GLCDPrint( Xposition, Yposition,
nt font set Stringvariable )
GLCDDra Print character on GLCD using GCB font set GLCDDrawChar( Xposition, Yposition,
wChar CharCode [,Optional LineColour] )
GLCDDra Print characters on GLCD using GCB font set GLCDDrawString( Xposition, Yposition,
wString Stringvariable [,Optional LineColour] )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1, Xposition2,
Yposition2 [,Optional LineColour]
FilledB Draw a box on the GLCD to a specific size that FilledBox (Xposition1, Yposition1,
ox is filled with the foreground colour. Xposition2, Yposition2 [,Optional
LineColour] )
Line Draw a line on the GLCD to a specific length Line ( Xposition1, Yposition1, Xposition2,
that is filled with the specific attribute. Yposition2 [,Optional LineColour] )
PSet Set a pixel on the GLCD at a specific position PSet(Xposition, Yposition, Pixel Colour)
that is set with the specific attribute.
GLCDWri Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
teByte datasheet for usage.
GLCDRea Read a byte value from the controller, see the bytevariable = GLCDReadByte
dByte datasheet for usage.
ILI9341 Specify color as a parameter for many GLCD Color constants for this device are shown in
_[color commands the list below.
]
211
Comma Purpose Example
nd
ReadPix Read the pixel color at the specified XY ReadPixel( Xosition , Yposition ) or
el coordination. Returns long variable with ReadPixel_ILI9341( Xosition , Yposition ) Any
Red, Green and Blue encoded in the lower 24 color can be defined using a valid
bits. hexidecimal word value between 0x0000 to
0xFFFF.
This example shows how to drive a ILI9341 based Graphic LCD module with the built in commands of
Great Cow BASIC.
Example #1
212
;Chip Settings
#chip 16F1937,32
#config Osc = INT, VCAPEN_OFF, MCLRE_ON, PLLEN_ON, CLKOUTEN_OFF 'microcontroller
specific configuration
#include <glcd.h>
Example #2 This example shows how to drive a ILI3941 with the OLED fonts. Note the use of the
GLCDfntDefaultSize to select the size of the OLED font in use.
GLCDfntDefaultSize = 2
GLCDFontWidth = 5
GLCDPrint ( 40, 0, "OLED" )
GLCDPrint ( 0, 18, "Typ: ILI9341" )
GLCDPrint ( 0, 34, "Size: 320 x 240" )
GLCDfntDefaultSize = 1
GLCDPrint(20, 56,"https://goo.gl/gjrxkp")
Example #2 This example shows how to disable the large OLED Fontset. This disables the font to
reduce memory usage.
When the extended OLED fontset is disabled every character will be shown as a block character.
213
#define GLCD_OLED_FONT 'The constant is required to support OLED fonts
#define GLCD_Disable_OLED_FONT2 'The constant to disable the extended OLED
fontset.
GLCDfntDefaultSize = 2
GLCDFontWidth = 5
GLCDPrint ( 40, 0, "OLED" )
GLCDPrint ( 0, 18, "Typ: ILI9341" )
GLCDPrint ( 0, 34, "Size: 320 x 240" )
GLCDfntDefaultSize = 1
GLCDPrint(20, 56,"https://goo.gl/gjrxkp")
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
ILI9481 Controllers
This section covers GLCD devices that use the ILI9481 graphics controller.
ILI9481 is a 262,144-color single-chip SoC driver for a-TFT liquid crystal display with resolution of
320 RGB x 480 dots, comprising a 960-channel source driver, a 480-channel gate driver, 345,600 bytes
GRAM for graphic data.
The Great Cow BASIC constants shown below control the configuration of the ILI9481controller. The
Great Cow BASIC constants for control and data line connections are shown in the table below. Two
options are available for connectivity:
1) The 8-bit mode where 8 pins are connected between the microcontroller and the GLCD to control
the data bus.
2) The 16-bit mode where two data ports (8 pins each) are connected between the microcontroller and
the GLCD to control the data bus.
To use the ILI9481 driver simply include the following in your user code. This will initialise the driver.
8-bit mode
214
'Pin mappings for Data Bus Interface (DBI)
'this GLCD driver supports 8 bit and 16 bit parallel data lines
16-bit mode
'16 bit control and dual data port lines (Mega2560 Board)
#define ILI9481_GLCD_CS PortG.1 'Chip Select line
#define ILI9481_GLCD_RS PortD.7 'DC data command line
#define ILI9481_GLCD_WR PortG.2 'Write command line
#define ILI9481_GLCD_RST PortG.0 'Reset line
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
215
Constants Controls Options
GLCD_TYPE GLCD_TYPE_ILI9481
GLCD_ILI9481_16b Specifies 16 bit DBI mode
it
GLCD_DB0..7 Specifies the pin that is connected to DB0..7 IO pin on the GLCD (8 bit Require
DBI). d
ILI9481_DataPort Specifies the port DB[15:8] pins on the GLCD (16 bit DBI). Require
H d
ILI9481_DataPort Specifies the port DB[7:0] pins on the GLCD (16 bit DBI). Require
L d
GLCD_RST Specifies the output pin that is connected to Reset IO pin on the GLCD. Require
d
GLCD_CS Specifies the output pin that is connected to Chip Select (CS) on the GLCD. Require
d
GLCD_RS Specifies the output pin that is connected to Data/Command pin on the Require
GLCD. d
GLCD_WR Specifies the output pin that is connected to Data In (RW or WDR) pin on Require
the GLCD. d
GLCD_RD Specifies the output pin that is connected to Data Out (RD or RDR) pin on Require
the GLCD. d
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCDFontWidth Specifies the font width of the Great Cow BASIC font set. 6
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
216
Comma Purpose Example
nd
GLCDPrin Print string of characters on GLCD using GLCDPrint( Xposition, Yposition, Stringvariable
t GCB font set )
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode
Char set [,Optional LineColour] )
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable [,Optional LineColour] )
Box Draw a box on the GLCD to a specific Box ( Xposition1, Yposition1, Xposition2,
size Yposition2 [,Optional LineColour]
FilledBo Draw a box on the GLCD to a specific FilledBox (Xposition1, Yposition1, Xposition2,
x size that is filled with the foreground Yposition2 [,Optional LineColour] )
colour.
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2 [,Optional LineColour] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour)
position that is set with the specific
attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
eByte datasheet for usage.
ILI9481_ Specify color as a parameter for many Color constants for this device are shown in the
[color] GLCD commands list below.
Any color can be defined using a valid
hexidecimal word value between 0x0000 to
0xFFFF.
217
ILI9481_BLACK 'hexidecimal value 0x0000
ILI9481_RED 'hexidecimal value 0xF800
ILI9481_GREEN 'hexidecimal value 0x0400
ILI9481_BLUE 'hexidecimal value 0x001F
ILI9481_WHITE 'hexidecimal value 0xFFFF
ILI9481_PURPLE 'hexidecimal value 0xF11F
ILI9481_YELLOW 'hexidecimal value 0xFFE0
ILI9481_CYAN 'hexidecimal value 0x07FF
ILI9481_D_GRAY 'hexidecimal value 0x528A
ILI9481_L_GRAY 'hexidecimal value 0x7997
ILI9481_SILVER 'hexidecimal value 0xC618
ILI9481_MAROON 'hexidecimal value 0x8000
ILI9481_OLIVE 'hexidecimal value 0x8400
ILI9481_LIME 'hexidecimal value 0x07E0
ILI9481_AQUA 'hexidecimal value 0x07FF
ILI9481_TEAL 'hexidecimal value 0x0410
ILI9481_NAVY 'hexidecimal value 0x0010
ILI9481_FUCHSIA 'hexidecimal value 0xF81F
These examples show how to drive a ILI9481 based Graphic LCD module with the built in commands of
Great Cow BASIC. The 8 bit DBI example uses a UNO shield, this can easily adapted to Microchip
architecture. The 16 bit DBI example uses a Mega2560 board.
Examples:
218
'8 bit DBI
#include <glcd.h>
#include <UNO_mega328p.h >
'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
#define GLCD_RD ANALOG_0 ' read command line
#define GLCD_WR ANALOG_1 ' write command line
#define GLCD_RS ANALOG_2 ' Command/Data line
#define GLCD_CS ANALOG_3 ' Chip select line
#define GLCD_RST ANALOG_4 ' Reset line
219
#define ILI9481_BlueViolet 0x895C
GLCDCLS_ILI9481 ILI9481_Black
wait 1 s
GLCDCLS_ILI9481 ILI9481_White
wait 1 s
GLCDfntDefaultsize = 3
GLCDBackground = ILI9481_BlueViolet
GLCDForeground = ILI9481_Yellow1
GLCDCLS
wait 1 s
Start:
GLCDRotate (Landscape)
GLCDCLS
GLCDDrawString ( ILI9481_GLCD_WIDTH/2 - 24, ILI9481_GLCD_HEIGHT/2 - 62, "GCB")
GLCDDrawString ( ILI9481_GLCD_WIDTH/2 - 120, ILI9481_GLCD_HEIGHT/2 -24, "ILI9481
Driver")
wait 5 s
GLCDRotate (Portrait_REV)
GLCDCLS
GLCDDrawString ( ILI9481_GLCD_WIDTH/2 - 24, ILI9481_GLCD_HEIGHT/2 - 62, "GCB")
GLCDDrawString ( ILI9481_GLCD_WIDTH/2 - 120, ILI9481_GLCD_HEIGHT/2 - 24, "ILI9481
Driver")
wait 5 s
GLCDRotate (Landscape_REV)
GLCDCLS
GLCDDrawString ( ILI9481_GLCD_WIDTH/2 - 24, ILI9481_GLCD_HEIGHT/2 - 62, "GCB")
GLCDDrawString ( ILI9481_GLCD_WIDTH/2 - 120, ILI9481_GLCD_HEIGHT/2 - 24, "ILI9481
Driver")
wait 5 s
goto Start
220
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
ILI9486(L) Controllers
This section covers GLCD devices that use the ILI9486(L) graphics controller.
The ILI9486(L) is a 262,144-color single-chip SoC driver for a-Si TFT liquid crystal display with
resolution of 320RGBx480 dots, comprising a 960-channel source driver, a 480-channel gate driver,
345,600bytes GRAM for graphic data of 320RGBx480 dots.
The Great Cow BASIC constants shown below control the configuration of the ILI9486(L) controller.
Great Cow BASIC supports 1) SPI using the SPI hardware module, 2) software SPI, 3) UNO shields and
4) an 8bit port bus - this is detailed in the tables below.
To use the ILI9486(L) driver simply include the following in your user code. This will initialise the
driver.
#include <glcd.h>
#DEFINE GLCD_TYPE GLCD_TYPE_ILI9486L
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
GLCD_DC Specifies the output pin that is connected to Data/Command IO pin on the GLCD. Required
GLCD_CS Specifies the output pin that is connected to Chip Select (CS) on the GLCD. Required
GLCD_Reset Specifies the output pin that is connected to Reset pin on the GLCD. Required
GLCD_DI Specifies the output pin that is connected to Data In (GLCD out) pin on the Required
GLCD.
GLCD_DO Specifies the output pin that is connected to Data Out (GLCD in) pin on the Required
GLCD.
GLCD_SLK Specifies the output pin that is connected to Clock (CLK) pin on the GLCD. Required
The Great Cow BASIC constants for the communicaton protocol for the controller are shown in the
table below.
221
Communications Options SELECT ONLY ONE OF THE FOLLLOWING
ILI9486L_HardwareSPI Specifies that SPI ports MUST be defined that match the SPI
hardware SPI will module for each specific microcontroller
be used #define ILI9486L_HardwareSPI
UNO_8bit_Shield Specifies that a UNO The shield will use 13 ports. These ports are pre-
shield will be used defined by the shield. These ports must be
specified. #define UNO_8bit_Shield
GLCD_DataPort Specifies that a full The microcontroller will use 13 ports. These port
8 port will be used is defined as 8 contigous bits. These control port
and the data port must be specified. #define
GLCD_DataPort portb
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCDFontWidth Specifies the font width of the Great Cow BASIC font set. 6
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
GLCDPrin Print string of characters on GLCD using GLCDPrint( Xposition, Yposition, Stringvariable
t GCB font set )
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode
Char set [,Optional LineColour] )
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable [,Optional LineColour] )
Box Draw a box on the GLCD to a specific Box ( Xposition1, Yposition1, Xposition2,
size Yposition2 [,Optional LineColour]
222
Comma Purpose Example
nd
FilledBo Draw a box on the GLCD to a specific FilledBox (Xposition1, Yposition1, Xposition2,
x size that is filled with the foreground Yposition2 [,Optional LineColour] )
colour.
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2 [,Optional In LineColour] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour)
position that is set with the specific
attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
eByte datasheet for usage.
ILI9486L Specify color as a parameter for many Color constants for this device are shown in the
_[color] GLCD commands list below.
Any color can be defined using a valid
hexidecimal word value between 0x0000 to
0xFFFF.
223
For a ILI9486L datasheet, please refer to Google.
This example shows how to drive a ILI9486L based Graphic LCD module with the built in commands of
Great Cow BASIC.
Example:
#chip mega328p, 16
#option explicit
#include <glcd.h>
#include <UNO_mega328p.h >
'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
#define GLCD_DC DIGITAL_8 ' Data command line
#define GLCD_CS DIGITAL_10 ' Chip select line
#define GLCD_RST DIGITAL_9 ' Reset line
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
KS0108 Controllers
This section covers GLCD devices that use the KS0108 graphics controller.
The KS0108 is an LCD is driven by on-board 5V parallel interface chipset KS0108 and KS0107. They are
extremely common and well documented
The Great Cow BASIC constants shown below control the configuration of the KS0108 controller. The
only connectivity option is the 8-bit mode where 8 connections (for the data) are required between the
microcontroller and the GLCD to control the data bus.
224
To use the KS0108 driver simply include the following in your user code. This will initialise the driver.
#include <glcd.h>
#DEFINE GLCD_TYPE GLCD_TYPE_KS0108
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
GLCD_TY GLCD_TYPE_KS0108
PE
GLCD_RS Specifies the output pin that is connected to Required
Register Select on the GLCD.
GLCD_RW Specifies the output pin that is connected to Must be defined (unless R/W is disabled)
Read/Write on the GLCD. The R/W pin can be see GLCD_NO_RW
disabled.
225
Consta Controls Options
nts
GLCD_NO Disables read/write inspection of the device Optional, but recommend NOT to set. The R/W
_RW during read/write operations pin can be disabled by setting the GLCD_NO_RW
constant. If this is done, there is no need for
the R/W to be connected to the chip, and no
need for the LCD_RW constant to be set. Ensure
that the R/W line on the LCD is connected to
ground if not used.
The Great Cow BASIC constants defined for the controller type are shown in the table below.
GLCD_WIDTH The width parameter of the 128 This constant cannot be changed
GLCD
226
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCDfntDefault Size of the current GLCD font. Default is 0. This equates to the standard GCB font
set.
GLCDfntDefaultsiz Size of the current GLCD font. Default is 1. This equates to the 8 pixel high.
e
The Great Cow BASIC commands supported for this GLCD are shown in the table below.
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode )
Char set
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1, Xposition2,
Yposition2, [Optional In LineColour as 0 or 1]
)
FilledBo Draw a box on the GLCD to a specific size FilledBox (Xposition1, Yposition1, Xposition2,
x that is filled with the foreground colour. Yposition2, [Optional In LineColour 0 or 1] )
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2, [Optional In LineColour 0 or 1] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour 0 or
position that is set with the specific 1)
attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
eByte datasheet for usage.
GLCDRead Read a byte value from the controller, see bytevariable = GLCDReadByte
Byte the datasheet for usage.
This example shows how to drive a KS0108 based Graphic LCD module with the built in commands of
Great Cow BASIC. See Graphic LCD for details, this is an external web site.
227
;Chip Settings
#chip 16F886,16
'#config MCLRE = on 'enable reset switch on CHIPINO
#include <GLCD.h>
;Defines (Constants)
#define GLCD_RW PORTB.1 'D9 to pin 5 of LCD
#define GLCD_RESET PORTB.5 'D13 to pin 17 of LCD
#define GLCD_CS1 PORTB.3 'D12 to actually since CS1, CS2 can be reversed on some
devices
#define GLCD_CS2 PORTB.4 'D11 to actually since CS1, CS2 can be reversed on some
devices
#define GLCD_RS PORTB.0 'D8 to pin 4 D/I pin on LCD
#define GLCD_ENABLE PORTB.2 'D10 to Pin 6 on LCD
#define GLCD_DB0 PORTC.7 'D0 to pin 7 on LCD
#define GLCD_DB1 PORTC.6 'D1 to pin 8 on LCD
#define GLCD_DB2 PORTC.5 'D2 to pin 9 on LCD
#define GLCD_DB3 PORTC.4 'D3 to pin 10 on LCD
#define GLCD_DB4 PORTC.3 'D4 to pin 11 on LCD
#define GLCD_DB5 PORTC.2 'D5 to pin 12 on LCD
#define GLCD_DB6 PORTC.1 'D6 to pin 13 on LCD
#define GLCD_DB7 PORTC.0 'D7 to pin 14 on LCD
Do forever
GLCDCLS
GLCDPrint 0,10,"Hello" 'Print Hello
wait 5 s
GLCDPrint 0,10, "ASCII #:" 'Print ASCII #:
Box 18,30,28,40 'Draw Box Around ASCII Character
for char = 15 to 129 'Print 0 through 9
GLCDPrint 17, 20 , Str(char)+" "
GLCDdrawCHAR 20,30, char
wait 125 ms
next
line 0,50,127,50 'Draw Line using line command
for xvar = 0 to 80 'draw line using Pset command
pset xvar,63,on '
next '
Wait 1 s
GLCDPrint 0,10,"End " 'Print Hello
wait 1 s
Loop
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
228
NEXTION Controllers
This section covers GLCD devices that use the serially attached Nextion graphics displays.
Nextion includes hardware part (a series of TFT boards) and software part (the Nextion editor
(http://nextion.itead.cc/)).
The Nextion TFT board uses only one serial port to communicate. It lets you avoid the hassle of
wiring. Nextion editor has mass components such as button, text, progress bar, slider, instrument
panel etc. to enrich your interface design. And, the drag-and-drop function ensures that you spend
less time in programming
The Nextion displays are 2.4 to 7.0 inches and range from 320*240 to 800*480 pixels. The connections
are 5v, 0v, SerialIn and SerialOut. Great Cow BASIC supports hardware and software serial
connectivity.
See GITHUB for the set of Great Cow BASIC demonstrations fro the Nextion displays. See Nextion
demonstrations on GITHUB.
To use the Nextion driver simply include the following in your user code. This will initialise the driver.
;VERY IMPORTANT!!
;Change the width and height to match the rotation in the Nextion Editor
#define GLCD_WIDTH 320 'could be 320 | 400 | 272 | 480 but any valid dimension
will work.
#define GLCD_HEIGHT 240 'could be 240 | 480 | 800 but any valid dimension will work.
;VERY IMPORTANT!!
;Fonts installed in the Nextion MUST match the fonts parameters loading to the GLCD.
229
;Obtain parameters from Nextion Editor/Font dialog.
#define NextionFont0 0, 8, 16 'Arial 8x16
#define NextionFont1 1, 12, 24 '24point 12x24 charset
#define NextionFont2 2, 16, 32 '32point 16x32 charset
;VERY IMPORTANT!!
;These two are optional. These constants are set in the library.
#define GLCD_NextionSerialPrint HSerPrint
#define GLCD_NextionSerialSend HSerSend
Sub InitPPS
'Module: EUSART
RXPPS = 0x0016 'RC6 > RX
'Module: EUSART
RC0PPS = 0x0010 'TX > RC0
TXPPS = 0x0010 'RC0 > TX (bi-directional)
RC5PPS = 0x0010 'TX > RC5
TXPPS = 0x0015 'RC5 > TX (bi-directional)
End Sub
'Template comment at the end of the config file
230
Setup for Software Serial
231
' ----- Configuration
'Chip Settings.
#chip 16f18855,32
#option explicit
;VERY IMPORTANT!!
;Change the width and height to match the rotation in the Nextion Editor
#define GLCD_WIDTH 320 'could be 320 | 400 | 272 | 480 but any valid dimension
will work.
#define GLCD_HEIGHT 240 'could be 240 | 480 | 800 but any valid dimension will work.
;VERY IMPORTANT!!
;Fonts installed in the Nextion MUST match the fonts parameters loading to the GLCD.
;Obtain parameters from Nextion Editor/Font dialog.
#define NextionFont0 0, 8, 16 'Arial 8x16
#define NextionFont1 1, 12, 24 '24point 12x24 charset
#define NextionFont2 2, 16, 32 '32point 16x32 charset
' ----- Set up for Software Serial - this is optional - shown to explain the method.
;Remove Hardware Serial before using Software serial
;You MUST also remove PPS setup, for hardware serial, when using Software serial
#include <SoftSerial.h>
;VERY IMPORTANT!!
;These two constants are required to support the the library.
#define GLCD_NextionSerialPrint Ser1Print
#define GLCD_NextionSerialSend Ser1Send
'
' ----- End of set up for Serial
232
The Great Cow BASIC constants shown below control the configuration of the Nextion controller. The
Great Cow BASIC constants for control and data line connections are shown in the table below.
GLCD_TYPE GLCD_TYPE_Nextion
GLCD_NextionSerialP Default is HSerPrint for hardware serial can be SernPrint when using Require
rint software serial. d
GLCD_NextionSerialS Default is HSerSend for hardware serial can be SernSend when using Require
end software serial. d
The Great Cow BASIC constants for control display characteristics are shown in the table below.
The Great Cow BASIC Nextion specific commands supported for this GLCD are shown in the table
below. Always review the appropiate library for the latest full set of supported commands.
233
Command Purpose Example
The Great Cow BASIC commonn commands supported for this GLCD are shown in the table
below. Always review the appropiate library for the latest full set of supported commands.
234
Comma Purpose Example
nd
GLCDDraw Print character on GLCD using GCB font set GLCDDrawChar( Xposition, Yposition, CharCode
Char [,Optional LineColour] )
GLCDDraw Print characters on GLCD using GCB font set GLCDDrawString( Xposition, Yposition,
String Stringvariable [,Optional LineColour] )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1, Xposition2,
Yposition2 [,Optional LineColour]
FilledBo Draw a box on the GLCD to a specific size FilledBox (Xposition1, Yposition1,
x that is filled with the foreground colour. Xposition2, Yposition2 [,Optional
LineColour] )
Line Draw a line on the GLCD to a specific length Line ( Xposition1, Yposition1, Xposition2,
that is filled with the specific attribute. Yposition2 [,Optional LineColour] )
235
For more help, see GLCDCLS
Supported in <GLCD.H>
NT7108C Controllers
This section covers GLCD devices that use the NT7108C graphics controller.
The NT7108C is an GLCD is driven by on-board 5V parallel interface chipset NT7108C. They are
similar to the KS0108.
The GLCD controller is the Winstar WDG0151-TMI module, which is a 128×64 pixel monochromatic
display. It uses two Neotic display controller chips: NT7108C and NT7107C, which are similar with
Samsung KS0108B and KS0107B controllers. The controller uses a dot matrix LCD segment driver
with 64 channel output, and therefore, the WDG0151 module contains two sets of it to drive 128
segments.
The Great Cow BASIC constants shown below control the configuration of the NT7108C controller.
The only connectivity option is the 8-bit full port mode where a full data port (for the data) is required
between the microcontroller and the GLCD to control the data bus.
To use the NT7108C driver simply include the following in your user code. This will initialise the
driver.
The NT7108C is very sensitive to clock timings. You may to adjust the clock timing to ensure the
display operates correctly.
#include <glcd.h>
#DEFINE GLCD_TYPE GLCD_TYPE_NT7108C
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
236
Constants Controls Options
GLCD_TYPE GLCD_TYPE_NT7108C
GLCD_RS Specifies the output pin that is connected to Register Select on the GLCD. Required
GLCD_RW Specifies the output pin that is connected to Read/Write on the GLCD. Required
GLCD_CS1 Specifies the output pin that is connected to CS1 on the GLCD. Required
GLCD_CS2 Specifies the output pin that is connected to CS2 on the GLCD. Required
GLCD_ENABLE Specifies the output pin that is connected to Enable on the GLCD. Required
GLCD_DATA_PORT Specifies the port that is connected to 8 connections on the GLCD. Required
The Great Cow BASIC constants defined for the controller type are shown in the table below.
GLCD_WIDTH The width parameter of the 128 This constant cannot be changed
GLCD
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCDfntDefault Size of the current GLCD font. Default is 0. This equates to the standard GCB font
set.
GLCDfntDefaultsiz Size of the current GLCD font. Default is 1. This equates to the 8 pixel high.
e
The Great Cow BASIC commands supported for this GLCD are shown in the table below.
237
Comma Purpose Example
nd
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode )
Char set
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1, Xposition2,
Yposition2, [Optional In LineColour as 0 or 1]
)
FilledBo Draw a box on the GLCD to a specific size FilledBox (Xposition1, Yposition1, Xposition2,
x that is filled with the foreground colour. Yposition2, [Optional In LineColour 0 or 1] )
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2, [Optional In LineColour 0 or 1] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour 0 or
position that is set with the specific 1)
attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
eByte datasheet for usage.
GLCDRead Read a byte value from the controller, see bytevariable = GLCDReadByte
Byte the datasheet for usage.
This example shows how to drive a NT7108C based Graphic LCD module with the built in commands of
Great Cow BASIC. See Graphic LCD for details, this is an external web site.
238
;Chip Settings
#chip 16F1939,32
#option explicit
#config Osc = INT, MCLRE_On
#include <glcd.h>
#define GLCD_TYPE GLCD_TYPE_NT7108C ' Specify the GLCD type
#define GLCDDirection 0 ' Flip the GLCD 0 do not flip, 1
flip
Box 0, 0, 127, 10
Line 63, 10, 63, 63
Line 0, 37, 127, 37
Circle 63, 37, 15
End
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
PCD8544 Controllers
This section covers GLCD devices that use the PCD844 graphics controller.
The PCD8544 is a low power CMOS LCD controller/driver, designed to drive a graphic display of 48
rows and 84 columns. All necessary functions for the display are provided in a single chip, including
on-chip generation of LCD supply and bias voltages, resulting in a minimum of external components
and low power consumption. The PCD8544 interfaces to microcontrollers through a serial bus
interface.
The Great Cow BASIC constants shown below control the configuration of the PCD844 controller.
Great Cow BASIC supports SPI software connectivity only - this is shown in the tables below.
239
The PCD8544 is a monochrome device.
The PCD844can operate in two modes. Full GLCD mode and Text/JPG mode the full GLCD mode
requires a minimum of 512 bytes. For microcontrollers with limited memory the text only can be
selected by setting the correct constant.
To use the PCD844 driver simply include the following in your user code. This will initialise the driver.
#include <glcd.h>
#define GLCD_TYPE GLCD_TYPE_PCD8544
' Pin mappings for software SPI for Nokia 3310 Device
#define GLCD_DO portc.5 'example port setting
#define GLCD_SCK portc.3 'example port setting
#define GLCD_DC portc.2 'example port setting
#define GLCD_CS portc.1 'example port setting
#define GLCD_RESET portc.0 'example port setting
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
GLCD_TYPE GLCD_TYPE_PCD8544
GLCD_DC Specifies the output pin that is connected to Data/Command IO pin on the GLCD. Required
GLCD_CS Specifies the output pin that is connected to Chip Select (CS) on the GLCD. Required
GLCD_Reset Specifies the output pin that is connected to Reset pin on the GLCD. Required
GLCD_D0 Specifies the output pin that is connected to Data Out (GLCD in) pin on the Required
GLCD.
GLCD_SCK Specifies the output pin that is connected to Clock (CLK) pin on the GLCD. Required
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCD_TYPE_PCD8544_CHA Specifies that the display controller will operate in text Optional
RACTER_MODE_ONLY mode and BMP draw mode only.
For microcontrollers with less then 1kb of RAM this will
be set be default.
PCD8544ClockDelay Specifies the clock delay, if required for slower Optional. Set to 0 as
microcontroller, the default value
PCD8544WriteDelay Specifies the write delay, if required for slower Optional. Set to 0 as
microcontroller, the default value
240
Constants Controls Default
GLCDFontWidth Specifies the font width of the Great Cow BASIC font set. 6
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode )
Char set
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1, Xposition2,
Yposition2, [Optional In LineColour as 0 or 1]
)
FilledBo Draw a box on the GLCD to a specific size FilledBox (Xposition1, Yposition1, Xposition2,
x that is filled with the foreground colour. Yposition2, [Optional In LineColour 0 or 1] )
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2, [Optional In LineColour 0 or 1] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour 0 or
position that is set with the specific 1)
attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
eByte datasheet for usage.
GLCDRead Read a byte value from the controller, see bytevariable = GLCDReadByte
Byte the datasheet for usage.
This example shows how to drive a PCD8544 based Graphic LCD module with the built in commands of
241
Great Cow BASIC.
Example:
#chip 16lf1939,32
#option Explicit
#config Osc = INT, MCLRE_ON, PLLEN_Off, CLKOUTEN_OFF
#include <glcd.h>
' Pin mappings for software SPI for Nokia 3310 Device
#define GLCD_DO portc.5
#define GLCD_SCK portc.3
#define GLCD_DC portc.2
#define GLCD_CS portc.1
#define GLCD_RESET portc.0
DO forever
for CCount = 31 to 127
GLCDPrint (0, 0, "PrintStr")
GLCDDrawString (0, 9, "DrawStr")
GLCDPrint ( 44 , 21, " ")
GLCDPrint ( 44 , 29, " ") ' word value
GLCDPrint ( 44 , 37, " ") ' Byte value
box 46,9,57,19
GLCDDrawChar(48, 9, CCount )
outString = str( CCount )
' draw a box to overwrite existing strings
FilledBox(58,9,GLCD_WIDTH-1,17,GLCDBackground )
242
GLCDDrawString(58, 9, outString )
longNumber = longNumber + 7
wordNumber = wordNumber + 3
byteNumber++
NEXT
LOOP
end
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
SDD1289 Controllers
This section covers GLCD devices that use the SDD1289 graphics controller. The SDD1289 is a 240 x 320
single chip controller driver IC for 262k color (RGB) amorphous TFT LCD.
The Great Cow BASIC constants shown below control the configuration of the SDD1289 controller.
Great Cow BASIC supports SPI hardware and software connectivity - this is shown in the tables
below.
To use the SDD1289 driver simply include the following in your user code. This will initialise the
driver.
#include <glcd.h>
#DEFINE GLCD_TYPE GLCD_TYPE_SDD1289
'Pin mappings for SDD1289
#define GLCD_DC porta.0 'example port setting
#define GLCD_CS porta.1 'example port setting
#define GLCD_RESET porta.2 'example port setting
#define GLCD_DI porta.3 'example port setting
#define GLCD_DO porta.4 'example port setting
#define GLCD_SCK porta.5 'example port setting
243
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCD_TYPE GLCD_TYPE_SDD1289
GLCD_DC Specifies the output pin that is connected to Data/Command IO pin on the GLCD. Required
GLCD_CS Specifies the output pin that is connected to Chip Select (CS) on the GLCD. Required
GLCD_Reset Specifies the output pin that is connected to Reset pin on the GLCD. Required
GLCD_DI Specifies the output pin that is connected to Data In (GLCD out) pin on the Required
GLCD.
GLCD_DO Specifies the output pin that is connected to Data Out (GLCD in) pin on the Required
GLCD.
GLCD_SCK Specifies the output pin that is connected to Clock (CLK) pin on the GLCD. Required
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCDFontWidth Specifies the font width of the Great Cow BASIC font set. 6
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
GLCDPrin Print string of characters on GLCD using GLCDPrint( Xposition, Yposition, Stringvariable
t GCB font set )
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode )
Char set
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable )
Box Draw a box on the GLCD to a specific Box ( Xposition1, Yposition1, Xposition2,
size Yposition2, [Optional In LineColour as 0 or 1]
FilledBo Draw a box on the GLCD to a specific FilledBox (Xposition1, Yposition1, Xposition2,
x size that is filled with the foreground Yposition2, [Optional In LineColour 0 or 1] )
colour.
244
Comma Purpose Example
nd
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2, [Optional In LineColour 0 or 1] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour 0 or 1)
position that is set with the specific
attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte ( LCDByte)
eByte datasheet for usage.
SDD1289_ Specify color as a parameter for many Color constants for this device are shown in the
[color] GLCD commands list below.
Any color can be defined using a valid
hexidecimal word value between 0x0000 to
0xFFFF.
This example shows how to drive a SDD1289 based Graphic LCD module with the built in commands of
Great Cow BASIC.
Example:
245
;Chip Settings
#chip 16F1937,32
#config Osc = INT, VCAPEN_OFF, MCLRE_ON, PLLEN_ON, CLKOUTEN_OFF
#include <glcd.h>
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte OR Pset
Supported in <GLCD.H>
SH1106 Controllers
This section covers GLCD devices that use the SH1106 graphics controller. THe SH1106 is a single-chip
CMOS OLED/PLED driver with controller for organic/polymer light emitting diode dot-matrix graphic
display system. SH1106 consists of 132 segments, 64 commons that can support a maximum display
resolution of 132 X 64. It is designed for Common Cathode type OLED panel.
The Great Cow BASIC constants shown below control the configuration of the SH1106 controller.
Great Cow BASIC supports i2C hardware and software connectivity - this is shown in the tables
below.
To use the SH1106 driver simply include the following in your user code. This will initialise the driver.
You can select Full Mode GLCD, Low Memory Mode GLCD or Text mode these require 1024, 128 or 0
byte GLCD buffer respectively - you microcontroller requires sufficient RAM to support the selected
mode of GLCD operation.
246
#include <glcd.h>
The Great Cow BASIC constants for control display characteristics are shown in the table below.
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCDFontWidth Specifies the font width of the Great Cow BASIC font set. 6
GLCD_TYPE_SH1106_CHARACT Specifies that the display controller will operate in text mode and Optio
ER_MODE_ONLY BMP draw mode only. nal
For microcontrollers with low RAM this will be set be default.
When selected ONLY text related commands are suppored. For
grapical commands you must have sufficient memory to use Full
GLCD mode or use GLCD_TYPE_SH1106_LOWMEMORY_GLCD_MODE
GLCD_TYPE_SH1106_LOWMEMO Specifies that the display controller will operate in Low Memory Optio
RY_GLCD_MODE mode. nal
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
247
review the appropiate library for the latest full set of supported commands.
GLCDPrint Print string of characters on GLCD using GCB GLCDPrint( Xposition, Yposition,
font set Stringvariable )
GLCDDrawChar Print character on GLCD using GCB font set GLCDDrawChar( Xposition, Yposition,
CharCode )
GLCDDrawStri Print characters on GLCD using GCB font set GLCDDrawString( Xposition, Yposition,
ng Stringvariable )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1,
Xposition2, Yposition2, [Optional In
LineColour as 0 or 1] )
FilledBox Draw a box on the GLCD to a specific size FilledBox (Xposition1, Yposition1,
that is filled with the foreground colour. Xposition2, Yposition2, [Optional In
LineColour 0 or 1] )
Line Draw a line on the GLCD to a specific length Line ( Xposition1, Yposition1,
that is filled with the specific attribute. Xposition2, Yposition2, [Optional In
LineColour 0 or 1] )
PSet Set a pixel on the GLCD at a specific position PSet(Xposition, Yposition, Pixel Colour
that is set with the specific attribute. 0 or 1)
GLCDWriteByt Set a byte value to the controller, see the GLCDWriteByte (LCDByte)
e datasheet for usage.
GLCDReadByte Read a byte value from the controller, see bytevariable = GLCDReadByte
the datasheet for usage.
The additional Great Cow BASIC commands for this GLCD are shown in the table below.
Command Purpose
248
Command Purpose
GLCDSetContrast ( Sets the constrast between 0 and 255. The contrast increases as the value
dim_state ) increases.
Parameter is dim value
This example shows how to drive a SH1106 based Graphic LCD module with the built in commands of
Great Cow BASIC.
; ----- Configuration
#chip mega328p,16
#include <glcd.h>
GLCDCLS
GLCDPrint 0, 0, "Great Cow BASIC"
GLCDPrint (0, 16, "Anobium 2016")
wait 3 s
GLCDCLS
DO forever
for CCount = 31 to 127
GLCDPrint ( 64 , 36, hex(longNumber_E ) ) ; Print a HEX string
249
GLCDPrint ( 76 , 36, hex(longNumber_U ) ) ; Print a HEX string
GLCDPrint ( 88 , 36, hex(longNumber_H ) ) ; Print a HEX string
GLCDPrint ( 100 , 36, hex(longNumber ) ) ; Print a HEX string
GLCDPrint ( 112 , 36, "h" ) ; Print a HEX string
This example shows how to drive a SH1106 based Graphic I2C LCD module with the built in commands
of Great Cow BASIC using Low Memory Mode GLCD.
Note the use of GLCD_Open_PageTransaction and GLCD_Close_PageTransaction to support the Low Memory
Mode of operation and the contraining of all GLCD commands with the transaction commands. The
use Low Memory Mode GLCD the two defines GLCD_TYPE_SH1106_LOWMEMORY_GLCD_MODE and
GLCD_TYPE_SH1106_CHARACTER_MODE_ONLY are included in the user program.
#chip mega328p,16
#include <glcd.h>
250
dim outString as string * 21
GLCDCLS
GLCD_Open_PageTransaction 0,7
GLCDPrint 0, 0, "Great Cow BASIC"
GLCDPrint (0, 16, "Anobium 2019")
GLCD_Close_PageTransaction
wait 3 s
GLCDCLS
DO forever
GLCD_Open_PageTransaction 0,7
251
line 0,63,64,31 ; Draw a line
GLCD_Close_PageTransaction
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
SSD1306 Controllers
This section covers GLCD devices that use the SSD1306 graphics controller.
The SSD1306 is a single-chip CMOS OLED/PLED driver with controller for organic / polymer light
emitting diode dot-matrix graphic display system. It consists of 128 segments and 64 commons. This IC
is designed for Common Cathode type OLED panel.
The SSD1306 embeds with contrast control, display RAM and oscillator, which reduces the number of
external components and power consumption. It has 256-step brightness control. Data/Commands are
sent from general MCU through the hardware selectable 6800/8000 series compatible Parallel
Interface, I2C interface or Serial Peripheral Interface. It is suitable for many compact portable
applications, such as mobile phone sub-display, MP3 player and calculator, etc.
The Great Cow BASIC constants shown below control the configuration of the SSD1306 controller.
Great Cow BASIC supports SPI and I2C hardware & software connectivity - this is shown in the tables
below.
To use the SSD1306 driver simply include the following in your user code. This will initialise the
driver.
The SSD1306 library supports 128 * 64 pixels or 128 * 32 pixels. The default is 128 * 64 pixels.
The SSD1306 can operate in three modes. Full GLCD mode, Low Memory GLCD mode or Text/JPG mode
the full GLCD mode requires a minimum of 1k bytes or 512 bytes for the 128x64 and the 128x32
devices respectively in Full GLCD mode. For microcontrollers with limited memory the third mode of
operation - Text mode. These can be selected by setting the correct constant.
To use the SSD1306 drivers simply include one of the following configuration. You can select Full Mode
GLCD, Low Memory Mode GLCD or Text mode these require 1024, 128 or 0 byte GLCD buffer
respectively - you microcontroller requires sufficient RAM to support the selected mode of GLCD
252
operation.
or,
The Great Cow BASIC constants for control display characteristics are shown in the table below.
The Great Cow BASIC constants for SPI/S4Wire control display characteristics are shown in the table
below.
253
Constan Controls Options
ts
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCD_PROTECTOVERRUN Define this constant to restrict pixel operations with the pixel Not
limits defined
GLCD_TYPE_SSD1306_CHARAC Specifies that the display controller will operate in text mode and Optiona
TER_MODE_ONLY BMP draw mode only. l
For microcontrollers with low RAM this will be set be default.
When selected ONLY text related commands are suppored. For
grapical commands you must have sufficient memory to use Full
GLCD mode or use GLCD_TYPE_SSD1306_LOWMEMORY_GLCD_MODE
GLCD_TYPE_SSD1306_LOWMEM Specifies that the display controller will operate in Low Memory Optiona
ORY_GLCD_MODE mode. l
GLCD_OLED_FONT Specifies the use of the optional OLED font set. The Optiona
GLCDfntDefaultsize can be set to 1 or 2 only. GLCDfntDefaultsize= l
1. A small 8 height pixel font with variable width.
GLCDfntDefaultsize= 2. A larger 10 width * 16 height pixel font.
The Great Cow BASIC variables for control display characteristics are shown in the table below. These
254
variables control the user definable parameters of a specific GLCD.
GLCDfntDefault Size of the current GLCD Default is 0. This equates to the standard GCB font set.
font.
GLCDfntDefault Size of the current GLCD Default is 1. This equates to the 8 pixel high.
size font.
The Great Cow BASIC commands supported for this GLCD are shown in the table below.
GLCDPrint Print string of characters on GLCD using GCB GLCDPrint( Xposition, Yposition,
font set Stringvariable )
GLCDDrawChar Print character on GLCD using GCB font set GLCDDrawChar( Xposition, Yposition,
CharCode )
GLCDDrawStri Print characters on GLCD using GCB font set GLCDDrawString( Xposition, Yposition,
ng Stringvariable )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1,
Xposition2, Yposition2, [Optional In
LineColour as 0 or 1] )
FilledBox Draw a box on the GLCD to a specific size FilledBox (Xposition1, Yposition1,
that is filled with the foreground colour. Xposition2, Yposition2, [Optional In
LineColour 0 or 1] )
Line Draw a line on the GLCD to a specific length Line ( Xposition1, Yposition1,
that is filled with the specific attribute. Xposition2, Yposition2, [Optional In
LineColour 0 or 1] )
PSet Set a pixel on the GLCD at a specific position PSet(Xposition, Yposition, Pixel Colour
that is set with the specific attribute. 0 or 1)
GLCDWriteByt Set a byte value to the controller, see the GLCDWriteByte (LCDByte)
e datasheet for usage.
GLCDReadByte Read a byte value from the controller, see bytevariable = GLCDReadByte
the datasheet for usage.
255
Command Purpose Example
The Great Cow BASIC specific commands for this GLCD are shown in the table below.
Command Purpose
Startscrollright_SSD1306 ( start , Activate a right handed scroll for rows start through stop Hint,
stop [,scrollspeed] ) the display is 16 rows tall. To scroll the whole display, execute:
startscrollright_SSD1306(0x00, 0x0F)
Parameters are Start row, End row, optionally Scrollspeed
Startscrollleft_SSD1306 ( start , Activate a left handed scroll for rows start through stop Hint,
stop [,scrollspeed] ) the display is 16 rows tall. To scroll the whole display, execute:
startscrollleft_SSD1306(0x00, 0x0F)
Parameters are Start row, End row, optionally Scrollspeed
Startscrolldiagright_SSD1306 ( start Activate a diagright handed scroll for rows start through stop
, stop [,scrollspeed] ) Hint, the display is 16 rows tall. To scroll the whole display,
execute:
startscrolldiagright_SSD1306(0x00, 0x0F)
Parameters are Start row, End row, optionally Scrollspeed
Startscrolldiagleft_SSD1306 ( start Activate a diagleft handed scroll for rows start through stop
, stop [,scrollspeed] ) Hint, the display is 16 rows tall. To scroll the whole display,
execute:
startscrolldiagleft_SSD1306(0x00, 0x0F)
Parameters are Start row,End row, optionally Scrollspeed
GLCDSetContrast ( dim_state ) Sets the constrast between 0 and 255. The contrast increases
as the value increases.
Parameter is dim value
This example shows how to drive a SSD1306 based Graphic I2C LCD module with the built in
commands of Great Cow BASIC using Full Mode GLCD
#chip mega328p,16
#include <glcd.h>
256
; ----- Define Hardware settings
' Define I2C settings
#define HI2C_BAUD_RATE 400
#define HI2C_DATA
HI2CMode Master
GLCDCLS
GLCDPrint 0, 0, "Great Cow BASIC"
GLCDPrint (0, 16, "Anobium 2015")
wait 3 s
GLCDCLS
DO forever
for CCount = 31 to 127
GLCDPrint ( 64 , 36, hex(longNumber_E ) ) ; Print a HEX string
GLCDPrint ( 76 , 36, hex(longNumber_U ) ) ; Print a HEX string
GLCDPrint ( 88 , 36, hex(longNumber_H ) ) ; Print a HEX string
GLCDPrint ( 100 , 36, hex(longNumber ) ) ; Print a HEX string
GLCDPrint ( 112 , 36, "h" ) ; Print a HEX string
257
filledbox 3,43,11,51, wordNumber ; Draw a filled box
This example shows how to drive a SSD1306 based Graphic I2C LCD module with the built in
commands of Great Cow BASIC using Low Memory Mode GLCD.
Note the use of GLCD_Open_PageTransaction and GLCD_Close_PageTransaction to support the Low Memory
Mode of operation and the contraining of all GLCD commands with the transaction commands. The
use Low Memory Mode GLCD the two defines GLCD_TYPE_SSD1306_LOWMEMORY_GLCD_MODE and
GLCD_TYPE_SSD1306_CHARACTER_MODE_ONLY are included in the user program.
#chip mega328p,16
#include <glcd.h>
GLCDCLS
258
GLCDPrint (0, 16, "Anobium 2019")
GLCD_Close_PageTransaction
wait 3 s
DO forever
GLCD_Open_PageTransaction 0,7
GLCD_Close_PageTransaction
259
end
This example shows how to drive a SSD1306 based Graphic SPI LCD module with the built in
commands of Great Cow BASIC.
260
'Chip model
#chip mega328p, 16
#include <glcd.h>
Do
SET led ON
wait 1 s
SET led OFF
GLCDCLS
GLCDPrint (30, 0, "Hello World!")
Circle (18,24,10)
FilledCircle (48,24,10)
Box (70,14,90,34)
FilledBox (106,14,126,34)
GLCDDrawString (32,35,"Draw String")
GLCDPrint (0,46,longnumber)
GLCDPrint (94,46,wordnumber)
GLCDPrint (52,55,bytenumber)
Line (0,40,127,63)
Line (0,63,127,40)
wait 3 s
Loop
261
This example shows how to drive a SSD1306 based Graphic I2C LCD module with 128 * 32 pixel
support.
#chip mega328p,16
#include <glcd.h>
GLCDCLS
GLCDPrint 0, 0, "Great Cow BASIC"
GLCDPrint (0, 16, "Anobium 2019")
This example shows how to drive a SSD1306 with the OLED fonts. Note the use of the
GLCDfntDefaultSize to select the size of the OLED font in use.
#define GLCD_OLED_FONT
GLCDfntDefaultSize = 2
GLCDFontWidth = 5
GLCDPrint ( 40, 0, "OLED" )
GLCDPrint ( 0, 18, "Typ: SSD1306" )
GLCDPrint ( 0, 34, "Size: 128x64" )
GLCDfntDefaultSize = 1
GLCDPrint(20, 56,"https://goo.gl/gjrxkp")
This example shows how to set the SSD1306 OLED the lowest constrast level by using a OLED chip
specific command.
262
'Use the GCB command to set the lowest constrast
GLCDSetContrast ( 0 )
'Then, use the Write command to set the output between 0 and 255
Write_Command_SSD1306(SSD1306_SETVCOMDETECT)
Write_Command_SSD1306(15) ' 0x40 default, to lower the contrast, put 0 for
lowest and 255 for highest.
GLCDfntDefaultSize = 2
GLCDFontWidth = 5
GLCDPrint ( 40, 0, "OLED" )
GLCDPrint ( 0, 18, "Typ: SSD1306" )
GLCDPrint ( 0, 34, "Size: 128x64" )
GLCDfntDefaultSize = 1
GLCDPrint(20, 56,"https://goo.gl/gjrxkp")
This example shows how to disable the large OLED Fontset. This disables the font to reduce memory
usage.
When the large OLED fontset is disabled every character will be shown as a block character.
GLCDfntDefaultSize = 2
GLCDFontWidth = 5
GLCDPrint ( 40, 0, "OLED" )
GLCDPrint ( 0, 18, "Typ: SSD1306" )
GLCDPrint ( 0, 34, "Size: 128x64" )
GLCDfntDefaultSize = 1
GLCDPrint(20, 56,"https://goo.gl/gjrxkp")
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
SSD1331 Controllers
This section covers GLCD devices that use the SSD1331 graphics controller. The SSD1331 is a single-
chip controller/driver for 262K-color, graphic type OLED-LCD.
263
The Great Cow BASIC constants shown below control the configuration of the SSD1331 controller.
Great Cow BASIC supports SPI, hardware and software SPI, connectivity. This is shown in the tables
below.
To use the SSD1331 driver simply include the following in your user code. This will initialise the
driver.
#include <glcd.h>
#include <UNO_mega328p.h >
'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
#define GLCD_DC portb.0 ' Data command line
#define GLCD_CS portb.2 ' Chip select line
#define GLCD_RESET portb.1 ' Reset line
#define GLCD_DO portb.3 ' Data out | MOSI
#define GLCD_SCK portb.5 ' Clock Line
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCD_TYPE GLCD_TYPE_SSD1331
GLCD_DC Specifies the output pin that is connected to Data/Command IO pin on the GLCD. Required
GLCD_CS Specifies the output pin that is connected to Chip Select (CS) on the GLCD. Required
GLCD_Reset Specifies the output pin that is connected to Reset pin on the GLCD. Required
GLCD_DO Specifies the output pin that is connected to Data Out (GLCD in) pin on the Required
GLCD.
GLCD_SCK Specifies the output pin that is connected to Clock (CLK) pin on the GLCD. Required
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCDFontWidth Specifies the font width of the Great Cow BASIC font set. 6 or 5 for the OLED font set.
264
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
GLCDPri Print string of characters on GLCD using GLCDPrint( Xposition, Yposition, Stringvariable
nt GCB font set )
GLCDDra Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode )
wChar set
GLCDDra Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
wString set Stringvariable )
Box Draw a box on the GLCD to a specific Box ( Xposition1, Yposition1, Xposition2,
size Yposition2, [Optional In LineColour as 0 or 1] )
FilledB Draw a box on the GLCD to a specific FilledBox (Xposition1, Yposition1, Xposition2,
ox size that is filled with the foreground Yposition2, [Optional In LineColour 0 or 1] )
colour.
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2, [Optional In LineColour 0 or 1] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour 0 or 1)
position that is set with the specific Any color can be defined using a valid
attribute. hexidecimal word value between 0x0000 to
0xFFFF.
Example:
265
#chip mega328p, 16
#option explicit
#include <glcd.h>
#include <UNO_mega328p.h >
'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
#define GLCD_DC portb.0 ' Data command line
#define GLCD_CS portb.2 ' Chip select line
#define GLCD_RESET portb.1 ' Reset line
#define GLCD_DO portb.3 ' Data out | MOSI
#define GLCD_SCK portb.5 ' Clock Line
#define SSD1331_HardwareSPI ' remove/comment out if you want to use software SPI.
GLCDCLS
GLCDPrintStringLN ("Great Cow BASIC")
GLCDPrintStringLN ("")
GLCDPrintStringLN ("Test of the SSD1331")
GLCDPrintStringLN ("")
GLCDPrintStringLN ("Anobium 2019")
end
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
ST7735 Controllers
This section covers GLCD devices that use the ST7735 graphics controller. The ST7735 is a single-chip
controller/driver for 262K-color, graphic type TFT-LCD.
The Great Cow BASIC constants shown below control the configuration of the ILI9340 controller.
Great Cow BASIC supports an 8 bit bus connectivity. The 8 bit must be a single port of consective bits
- this is shown in the tables below.
To use the ST7735 driver simply include the following in your user code. This will initialise the driver.
266
#include <glcd.h>
GLCD_TYPE GLCD_TYPE_ST7735
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCD_TYPE GLCD_TYPE_ST7735
GLCD_DATA_PO Not Available for this controller. Not
RT applicable.
GLCD_DC Specifies the output pin that is connected to Data/Command IO pin on Required
the GLCD.
GLCD_CS Specifies the output pin that is connected to Chip Select (CS) on the Required
GLCD.
GLCD_Reset Specifies the output pin that is connected to Reset pin on the GLCD. Required
GLCD_DI Specifies the output pin that is connected to Data In (GLCD out) pin on Required
the GLCD.
GLCD_D0 Specifies the output pin that is connected to Data Out (GLCD in) pin on Required
the GLCD.
GLCD_SLK Specifies the output pin that is connected to Clock (CLK) pin on the Required
GLCD.
The Great Cow BASIC constants for control display characteristics are shown in the table below.
GLCDFontWidth Specifies the font width of the Great Cow BASIC font set. 6
The Great Cow BASIC commands supported for this GLCD are shown in the table below. Always
review the appropiate library for the latest full set of supported commands.
267
Comma Purpose Example
nd
GLCDPri Print string of characters on GLCD using GLCDPrint( Xposition, Yposition, Stringvariable
nt GCB font set )
GLCDDra Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode )
wChar set
GLCDDra Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
wString set Stringvariable )
Box Draw a box on the GLCD to a specific Box ( Xposition1, Yposition1, Xposition2,
size Yposition2, [Optional In LineColour as 0 or 1] )
FilledB Draw a box on the GLCD to a specific FilledBox (Xposition1, Yposition1, Xposition2,
ox size that is filled with the foreground Yposition2, [Optional In LineColour 0 or 1] )
colour.
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2, [Optional In LineColour 0 or 1] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour 0 or 1)
position that is set with the specific
attribute.
GLCDWri Set a byte value to the controller, see the GLCDWriteByte (LCDByte)
teByte datasheet for usage.
ST7735_ Specify color as a parameter for many Any color can be defined using a valid
[color] GLCD commands hexidecimal word value between 0x0000 to
0xFFFF., see http://www.barth-
dev.de/online/rgb565-color-picker/ for a wider
range of color parameters.
Example:
268
;Chip Settings
#chip 16F1937,32
#config Osc = INT, VCAPEN_OFF, MCLRE_ON, PLLEN_ON, CLKOUTEN_OFF
#include <glcd.h>
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
ST7920 Controllers
This section covers GLCD devices that use the ST7920 graphics controller.
The Great Cow BASIC constants for control of the connectivity are shown in the table below. The only
connectivity option the 8-bit mode where 8 pins are connected between the microcontroller and the
GLCD to control the data bus.
ST7920 LCD controller/driver IC can display alphabets, numbers, Chinese fonts and self-defined
characters. It supports 3 kinds of bus interface, namely 8-bit, 4-bit and serial. Great Cow BASIC is
currently supports 8-bit only. For LCD only operations (text characters only) you can use the Great Cow
BASIC LCD routines.
All functions, including display RAM, Character Generation ROM, LCD display drivers and control
circuits are all in a one-chip solution. With a minimum system configuration, a Chinese character
display system can be easily achieved.
The ST7920 includes character ROM with 8192 16x16 dots Chinese fonts and 126 16x8 dots half-width
alphanumerical fonts. It supports 64x256 dots graphic display area for graphic display
(GDRAM). Mix-mode display with both character and graphic data is possible. ST7920 has built-in
CGRAM and provide 4 sets software programmable 16x16 fonts.
269
To use the ST7920 driver simply include the following in your user code. This will initialise the driver.
#include <glcd.h>
#DEFINE GLCD_TYPE GLCD_TYPE_ST7920
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
GLCD_RW Specifies the output pin that is connected to Must be defined unless R/W is disabled), see
Read/Write on the GLCD. The R/W pin can be GLCD_NO_RW
disabled*.
ST7920Wr Set the time delay between data Required, set to 20 us for 32Mhz support. Can
iteDelay transmissions. be reduced for slower chip speeds.
GLCD_NO_ Disables read/write inspection of the device Optional, but recommend NOT to set. The R/W
RW during read/write operations pin can be disabled by setting the GLCD_NO_RW
constant. If this is done, there is no need for
the R/W to be connected to the chip, and no
need for the LCD_RW constant to be set. Ensure
that the R/W line on the LCD is connected to
ground if not used.
The Great Cow BASIC constants for control display characteristics are shown in the table below.
270
Constants Controls Default
GLCDFontWidth Specifies the font width of the Great Cow BASIC font set. 6
The Great Cow BASIC commands supported for this GLCD are shown in the table below. For device
specific see the commands with the prefix of ST7920*.
GLCDDraw Print character on GLCD using GCB font GLCDDrawChar( Xposition, Yposition, CharCode )
Char set
GLCDDraw Print characters on GLCD using GCB font GLCDDrawString( Xposition, Yposition,
String set Stringvariable )
Box Draw a box on the GLCD to a specific size Box ( Xposition1, Yposition1, Xposition2,
Yposition2, [Optional In LineColour as 0 or 1]
)
FilledBo Draw a box on the GLCD to a specific size FilledBox (Xposition1, Yposition1, Xposition2,
x that is filled with the foreground colour. Yposition2, [Optional In LineColour 0 or 1] )
Line Draw a line on the GLCD to a specific Line ( Xposition1, Yposition1, Xposition2,
length that is filled with the specific Yposition2, [Optional In LineColour 0 or 1] )
attribute.
PSet Set a pixel on the GLCD at a specific PSet(Xposition, Yposition, Pixel Colour 0 or
position that is set with the specific 1)
attribute.
GLCDWrit Set a byte value to the controller, see the GLCDWriteByte (LCDByte)
eByte datasheet for usage.
GLCDRead Read a byte value from the controller, see bytevariable = GLCDReadByte
Byte the datasheet for usage.
This example shows how to drive a ST7920 based Graphic LCD module with the built in commands of
Great Cow BASIC. See Graphic LCD for details, this is an external web site.
271
Example 1:
;Chip Settings
#chip 16F1937,32
#config Osc = INT, VCAPEN_OFF, MCLRE_ON, PLLEN_ON, CLKOUTEN_OFF
#include <glcd.h>
' read delay of 25 is required at 32mhz, this can be reduced to 0 for slower clock
speeds
#define ST7920ReadDelay 25
' write delay of 2 is required at 32mhz. this can be reduced to 1 for slower clock
speeds
#define ST7920WriteDelay 2
ST7920GLCDEnableGraphics
ST7920GLCDClearGraphics
GLCDPrint 0, 1, "Great Cow BASIC "
wait 1 s
GLCDCLS
ST7920GLCDClearGraphics
rrun = 0
dim msg1 as string * 16
Do forever
GLCDCLS
ST7920GLCDClearGraphics ;clear screen
GLCDDrawString 30,0,"ChipMhz@" ;print string
GLCDDrawString 78,0, str(ChipMhz) ;print string
272
Circle(10,10,10,0) ;upper left
Circle(117,10,10,0) ;upper right
Circle(63,31,10,0) ;center
Circle(63,31,20,0) ;center
Circle(10,53,10,0) ;lower left
Circle(117,53,10,0) ;lower right
GLCDDrawString 30,54,"PIC16F1937" ;print string
wait 1 s ;wait
FilledBox( 0,0,128,63) ;create box
for ypos = 0 to 63 ;draw row by row
ST7920lineh 0,ypos,128, 0 ;draw line
next
wait 1 s ;wait
ST7920GLCDClearGraphics ;clear
loop
Example 2:
;Chip Settings
#chip 16F1937,32
#config Osc = INT, VCAPEN_OFF, MCLRE_ON, PLLEN_ON, CLKOUTEN_OFF
#include <glcd.h>
' read delay of 25 is required at 32mhz, this can be reduced to 0 for slower clock
speeds
#define ST7920ReadDelay 25
' write delay of 2 is required at 32mhz. this can be reduced to 1 for slower clock
speeds
#define ST7920WriteDelay 2
WAIT 1 S
ST7920GLCDEnableGraphics
ST7920GLCDClearGraphics
ST7920Tile "A"
273
GLCDPrint 0, 1, "Great Cow BASIC "
GLCDCLS
rrun = 0
dim msg1 as string * 16
do forever
ST7920GLCDEnableGraphics
ST7920GLCDClearGraphics
ST7920gTile 0x55, 0x55
wait 1 s
ST7920GLCDClearGraphics
ST7920Lineh(0, 0, GLCD_WIDTH)
ST7920Lineh(0, GLCD_HEIGHT - 1, GLCD_WIDTH)
ST7920LineV(0, 0, GLCD_HEIGHT)
ST7920LineV(GLCD_WIDTH - 1, 0, GLCD_HEIGHT)
Box 18,30,28,40
WAIT 2 S
FilledBox 18,30,28,40
ST7920GLCDClearGraphics
Start:
274
ST7920GLCDClearGraphics
workingGLCDDrawChar:
ST7920GLCDEnableGraphics
dim gtext as string
gtext = "ST7920 @QC12864B"
ST7920GLCDClearGraphics
ST7920GLCDDisableGraphics
GLCDCLS
ST7920Tile ( 0xa9 )
wait 1 s
GLCDCLS
'ST7920 can display half-width HCGROM fonts, user- defined CGRAM fonts and full 16x16
275
CGROM fonts. The
'character codes in 0000H~0006H will use user- defined fonts in CGRAM. The character
codes in 02H~7FH will use
'half-width alpha numeric fonts. The character code larger than A1H will be treated
as 16x16 fonts and will be
'combined with the next byte automatically. The 16x16 BIG5 fonts are stored in
A140H~D75FH while the 16x16 GB
'fonts are stored in A1A0H~F7FFH. In short:
'1. To display HCGROM fonts:
'Write 2 bytes of data into DDRAM to display two 8x16 fonts. Each byte represents 1
character.
'The data is among 02H~7FH.
'2. To display CGRAM fonts:
'Write 2 bytes of data into DDRAM to display one 16x16 font.
'Only 0000H, 0002H, 0004H and 0006H are acceptable.
'3. To display CGROM fonts:
'Write 2 bytes of data into DDRAM to display one 16x16 font.
'A140H~D75FH are BIG5 code, A1A0H~F7FFH are GB code.
linetest1:
ST7920GLCDEnableGraphics
ST7920gTile(0x55, 0x55)
wait 1 s
ST7920GLCDClearGraphics
'linehtest:
'
ST7920LineH(0, 0, GLCD_WIDTH)
276
ST7920LineH(0, GLCD_HEIGHT - 1, GLCD_WIDTH)
ST7920LineV(0, 0, GLCD_HEIGHT)
ST7920LineV(GLCD_WIDTH - 1, 0, GLCD_HEIGHT)
box test
ST7920LineH(10 ,0 , 118 )
ST7920LineH(0 ,8 , 128)
ST7920LineH(16 ,16 , 96 )
ST7920LineH(10 ,32 , 108 )
ST7920LineH(0, 16, GLCD_WIDTH)
ST7920LineH(0, 24, GLCD_WIDTH)
ST7920LineH(0, 32, GLCD_WIDTH)
ST7920LineH(0, 40, GLCD_WIDTH)
ST7920LineH(0, 48, GLCD_WIDTH)
ST7920LineH(0, 56, GLCD_WIDTH)
ST7920LineH(0, 63, GLCD_WIDTH)
ST7920LineV(16, 0, GLCD_HEIGHT)
ST7920LineV(17, 0, GLCD_HEIGHT)
ST7920LineV(15, 0, GLCD_HEIGHT)
ST7920LineV(46, 0, GLCD_HEIGHT)
ST7920LineV(47, 0, GLCD_HEIGHT)
ST7920LineV(48, 0, GLCD_HEIGHT)
ST7920LineV(46, 0, GLCD_HEIGHT)
ST7920LineV(47, 0, GLCD_HEIGHT)
ST7920LineV(48, 0, GLCD_HEIGHT)
ST7920LineV(96, 0, GLCD_HEIGHT)
ST7920LineV(97, 0, GLCD_HEIGHT)
ST7920LineV(98, 0, GLCD_HEIGHT
GraphicTestPlace:
ST7920GLCDClearGraphics
ST7920GraphicTest
ST7920GLCDClearGraphics
ST7920GLCDClearGraphics
277
ST7920GLCDDisableGraphics
GLCDCLS
ST7920SetIcon( 1, 0x55 )
loop
sub ST7920CallBuiltinChar
' 0xA140 ~ 0xA15F
for ii = 0 to 31
ST7920WriteData( 0xA1)
ST7920WriteData( 0x40 + ii)
next
wait 1 s
GLCDCLS
ST7920WriteData( 0xA1)
ST7920WriteData( 0xb0 + ii)
next
wait 1 s
GLCDCLS
ST7920WriteData( 0xA4)
ST7920WriteData( 0x40 + ii)
next
wait 1 s
GLCDCLS
end sub
For more help, see GLCDCLS, GLCDDrawChar, GLCDPrint, GLCDReadByte, GLCDWriteByte or Pset
Supported in <GLCD.H>
278
ST7920GLCDClearGraphics
Syntax:
ST7920GLCDClearGraphics
Explanation:
Example usage:
ST7920GLCDDisableGraphics
Syntax:
ST7920GLCDDisableGraphics
Explanation:
Example usage:
ST7920GLCDEnableGraphics
Syntax:
ST7920GLCDEnableGraphics
Explanation:
Example usage:
279
ST7920GraphicTest
Syntax:
ST7920GraphicTest
Explanation:
Example usage:
ST7920LineHs
Syntax:
Explanation:
This command draws a line with a specific style. The style is based on the bits value of the byte passed
to the routine.
Example usage:
ST7920Locate
Syntax:
Explanation:
This command locates the pixel at the specific X and Y location of the text screen. Subsequent printing
to the GLCD will place a character to the GLCD controller on the specified row and column. Due to the
design of the ST7920 controller (to accomodate Mandarin and Cyrillic), you must place the text on the
column according to the numbers above the diagram below. The addressing is handle by the
command.
280
|--0--|--1--|--2--|... ...|--7--|
+--+--+--+--+--+---------------------+
+--+--+--+--+--+---------------------+
+--+--+--+--+--+---------------------+
|' |' |' |' |' |' ... | <- row 2 (address 0x88)
+--+--+--+--+--+---------------------+
+--+--+--+--+--+---------------------+
|--0--|--1--|--2--|... ...|--7--|
+--+--+--+--+--+---------------------+
+--+--+--+--+--+---------------------+
+--+--+--+--+--+---------------------+
+--+--+--+--+--+---------------------+
+--+--+--+--+--+---------------------+
Example usage:
281
ST7920Locate ( 64, 31) ‘the pixel at the mid screen point
ST7920Tile
Syntax:
Explanation:
This command tiles the screen with the word value provided.
Example usage:
ST7920cTile
Syntax:
Explanation:
This required 2 bytes of data into DDRAM to display one 16x16 font from memory location
A140H~D75FH are BIG5 code, A1A0H~F7FFH are GB code.
Example usage:
ST7920gLocate
Syntax:
282
ST7920gLocate ( Xpos, Ypos)
Explanation:
This command locates the pixel at the specific X and Y location of the graphical screen.
Example usage:
ST7920gTile
Syntax:
Explanation:
Example usage:
ST7920gTile (0x55, 0x85) ‘tile the screen with an odd cross hatch
ST7920lineh
Syntax:
Explanation:
This command draws a horizontal line with the specific style. The style can be ON or OFF. Default is
ON.
Example usage:
283
ST7920linev
Syntax:
Explanation:
This command draws a vertical line with the specific style. The style can be ON or OFF. Default is ON
Example usage:
ST7920GLCDReadByte
Syntax:
byte_variable = ST7920GLCDReadByte
Explanation:
This function return the word value (16 bits) of the GLCD display for the current XY position.
Example usage:
ST7920WriteByte( SysCalcPositionY )
ST7920WriteByte( SysCalcPositionX )
' read data
GLCDDataTempWord = ST7920GLCDReadByte
GLCDDataTempWord = ST7920GLCDReadByte
GLCDDataTempWord = (GLCDDataTempWord*256) + ST7920GLCDReadByte
ST7920WriteByte
Syntax:
284
ST7920GLCDWriteByte
Explanation:
This command write to the appropriate location as specified by the current XY position.
Example usage:
...
ST7920WriteByte( SysCalcPositionY )
ST7920WriteByte( SysCalcPositionX )
' read data
GLCDDataTempWord = ST7920GLCDReadByte
GLCDDataTempWord = ST7920GLCDReadByte
GLCDDataTempWord = (GLCDDataTempWord*256) + ST7920GLCDReadByte
...
ST7920WriteCommand
Syntax:
ST7920GWriteCommand ( byte_variable)
Explanation:
Example usage:
...
ST7920WriteCommand(0x36) ' set the graphics mode on
GLCD_TYPE_ST7920_GRAPHICS_MODE = true
...
285
ST7920WriteData
Syntax:
ST7920GWriteData ( byte_variable)
Explanation:
Example usage:
...
for yy = 0 to ( GLCD_HEIGHT - 1 )
ST7920gLocate(0, yy)
for xx = 0 to ( GLCD_COLS -1 )
ST7920WriteData( 0x55 )
T7920WriteData( 0x55 )
next
next
...
ST7920gReaddata
Syntax:
byte_variable = ST7920gReaddata
Explanation:
This function return the word value (16 bits) of the GLCD display for the current XY position.
Example usage:
...
' Read a word from the display device.
word_variable = ST7920GLCDReadData
286
T6963 Controllers
This section covers Graphical Liquid Crystal Display (GLCD) devices that use the Toshiba T6963
graphics controller. The T6963 is a monochrome device that typically is blue or white. The GLCD can
be provided in a number of pixels sizes - 240 * 64 or 240 * 128.
The Toshiba T6963 is a very popular LCD controller for use in small graphics modules. It is capable of
controlling displays with a resolution up to 240x128. Because of its low power and small outline it is
most suitable for mobile applications such as PDAs, MP3 players or mobile measurement equipment.
A number of GLCD modules have this controller built-in these include the SP12N002 & SP14N001.
Although this controller is small, it has the capability of displaying and merging text and graphics and
it manages all the interfacing signals to the displays Row and Column drivers. The Great Cow BASIC
library supports the complex capabilities of the T6963.
The T6963 is an LCD is driven by on-board 5V parallel interface chipset T6963. For the specific
operating voltage always verify the operating voltages in the device specific datasheet.
The Great Cow BASIC connectivity option is the 8-bit mode - where 8 connections (for the data) are
required between the microcontroller and the GLCD to control the data bus.
To use the T6963 driver simply include the following in your user code. This will initialise the driver.
#chip 16f1939,32
#option explicit
'****************************************************************************************
*******
'Specify this GLCD - a 240 x 64 pixels display
#include <glcd.h>
#define GLCD_TYPE GLCD_TYPE_T6963_64
'****************************************************************************************
*******
'define the connectivity - the 8bit port
#define GLCD_DATA_PORT PORTD 'Library support contigous 8-bit port
' or
' #define GLCD_DB0 PORTD.0 'chip specific configuration where the
ibrary supports 8-bit port defined via 8 constants
' #define GLCD_DB1 PORTD.1 'chip specific configuration
' #define GLCD_DB2 PORTD.2 'chip specific configuration
' #define GLCD_DB3 PORTD.3 'chip specific configuration
287
' #define GLCD_DB4 PORTD.4 'chip specific configuration
' #define GLCD_DB5 PORTD.5 'chip specific configuration
' #define GLCD_DB6 PORTD.6 'chip specific configuration
' #define GLCD_DB7 PORTD.7 'chip specific configuration
'****************************************************************************************
*******
'*
'* Note : The T6963 controller's RAM address space from $0000 - $7FFF, total
32kbyte RAM, or it could be 64kbyte RAM best check!!
'*
'****************************************************************************************
*******
#define TEXT_HOME_ADDR 0x0000
'This is specific to the GLCD display
#define GRH_HOME_ADDR 0x3FFF
'This is specific to the GLCD display
#define CG_HOME_ADDR 0x77FF
'This is specific to the GLCD display
#define COLUMN 40 'Set column number to be 40 , 32, 30 etc.
This is specific to the GLCD display
#define MAX_ROW_PIXEL 64 'MAX_ROW_PIXEL the physical matrix length (y
direction) This is specific to the GLCD display
#define MAX_COL_PIXEL 240 'MAX_COL_PIXEL the physical matrix width (x
direction) This is specific to the GLCD display
'****************************************************************************************
*******
'* End of configuration
'****************************************************************************************
*******
288
The Great Cow BASIC constants for the interface to the controller are shown in the table below.
GLCD_RD Specifies the output pin that is connected to Read on the GLCD. Required
GLCD_WR Specifies the output pin that is connected to Write on the Required
GLCD.
GLCD_RESE Specifies the output pin that is connected to Reset on the Required
T GLCD.
GLCD_FS Specifies the output pin that is connected to FS on the GLCD. Required
The FS specifies the font size. Please set to 6 setting
GLCD_FS_SELECT = 1
GLCD_FS_S Specifies the output pin that is connected to FS on the GLCD. Required. Can be 1 or 0.
ELECT The FS specifies the font size. Please set to 6 setting Default setting is 1.
GLCD_FS_SELECT = 1
The T6963 differs from most other GLCD controllers in its use of the display RAM. Where a fixed area
of memory is normally allocated for text, graphics and the external character generator, but with the
T6963 the size for each area MUST be set by software commands. This means that the area for text,
graphics and external character generator can be freely allocated within the external memory, up to 64
kByte. Check the specific device for the amount of memory available. This can range from 4 kbyte to
64 kbyte.
For more information on memory management refer the device specific datasheet.
The Great Cow BASIC constants control the memory configuration of the T6963 controller.
289
Constants Value Comments
COLUMN 40 Set column number to be 40 , 32, 30 etc. This is specific to the GLCD display
MAX_ROW_PIXE 64 MAX_ROW_PIXEL the physical matrix length (y direction) This is specific to the
L GLCD display
MAX_COL_PIXE 240 MAX_COL_PIXEL the physical matrix width (x direction) This is specific to the
L GLCD display
The Great Cow BASIC library supports the following capabilities. Please refer to the relevant Help
section and the device specific demonstrations.
The GLCD command set covers the standard GLCDCLS, Line, Circle and all the GLCD methods and the
LCD command set: CLS, Locate, Print, LCDHEX etc. The demonstrations show how to load BMP loading
via external data sources and GLCD and LCD page swapping.
The table below shows the specific implementations of the command set for this device. Refer to the
GLCD and LCD in the Help for the generic GLCD and LCD commands.
Commands Usage
290
Commands Usage
To support GLCD and LCD page swapping - this can be used to support fixed pages of information,
BMPs or scrolling the following constants have are available to the user.
GLCDPage0_T6963
GLCDPage1_T6963
GLCDPage2_T6963
... etc
GLCDPage10_T6963
Ten pages are automatically created but the number of pages available is constrained by the memory
configuration.
LCDPage0_T6963
LCDPage1_T6963
LCDPage2_T6963
...etc
LCDPage10_T6963
Ten pages are automatically created but the number of pages available is constrained by the memory
configuration.
To use add the following to you user program. See the demonstration programs for more detailed
usage. After calling the SelectGLCDPage or SelectLCDPage methods all GLCD or LCD commands will be
applied to the current GLCD or LCD page.
291
'Select the GLCD page 1 memory
SelectGLCDPage ( GLCDPage1_T6963 )
The SelectLCDPage and SelectLCDPage and "Set Text Home Address" methods change the screen being
viewed on the device.
The key is to establish what you want your memory map to look like. Below is a map for one of my
240 x 64 pixel device. The default is for 10 screen pages (some newer LCD’s may have more RAM
for more screens). If you write the appropriate value (0x1000, or 0x11b0, or 0x1360, etc) to the text
home address, the display will instantly change to that screen - using SelectLCDPage and SelectLCDPage
method with the appropiate constant as parameter.
You can write your screens "ahead of time", in my case during the "splash screen" delay interval, and
instantly change to them later as desired. You can do this by setting current_grh_home_addr to the
appropiate page. And, then execute the GLCD commands you would normal use.
The graphic and text screens are independant but can be overlaid for a variety of useful effects.
Although, not tested, the LCD text screens can be scrolled 1 full text line at a time, while the GLCD
screens can be scrolled 1 pixel row at a time, provided you’ve set up your memory map accordingly
with adequate RAM for the graphic area.
292
'
'*******************************************************
'
' LCD MEMORY MAP
'
'*******************************************************
'
'
'
' --------------------
' | | 0x0000
' | |
' | TEXT RAM AREA | Each page has the numnbers of bytes + extra
' | ( 10 SCREENs ) | few bytes need to attributes. This is
' | | mentioned in the datasheet but imperical
' | | testing shows... you need the extra bytes
' --------------------
' | |
' | xx bytes unused |
' | |
' --------------------
' | | 0x3fff
' | |
' | |
' | GCLD RAM AREA |
' | ( 10 SCREENS ) |
' | |
' | |
' | |
' --------------------
' | | 0x77ff
' | CG RAM AREA | (Sacrosanct)
' | |
' -------------------- 0x7ffff
'
Supported in <GLCD.H>
293
Box
Syntax:
Explanation:
Draws a box on a graphic LCD from the upper corner of pixel position X1, Y1 location to pixel position
X2,Y2 location.
LineColour can be specified. Typical the value is 0 or 1 for GLCDForeGround and GLCDBackGround
respectively.
Circle
Circle:
Explanation:
The constant GLCD_PROTECTOVERRUN can be added to prevent circles from re-drawing at the screen edges.
Ensure the GLCD_Width and GLCD_HEIGHT constants are set correctly when using this additional constant.
Example:
#include <glcd.h>
294
Ellipse
Ellipse:
Explanation:
Draws a Ellipse on a GLCD at XPixelPosition, YPixelPosition with a specific vertex of XRadius and
YRadius.
The constant GLCD_PROTECTOVERRUN can be added to prevent Ellipses from re-drawing at the screen
edges. Ensure the GLCD_Width and GLCD_HEIGHT constants are set correctly when using this additional
constant.
Example:
#include <glcd.h>
FilledBox
Syntax:
Explanation:
Draws a filled box on a graphic LCD from the upper corner of pixel X1, Y1 location to pixel X2,Y2
location.
295
FilledCircle
Circle:
Explanation:
Example:
#include <glcd.h>
FilledEllipse
FilledEllipse:
Explanation:
Draws a FilledEllipse on a GLCD at XPixelPosition, YPixelPosition with a specific vertex of XRadius and
YRadius.
296
The constant GLCD_PROTECTOVERRUN can be added to prevent FilledEllipses from re-drawing at the screen
edges. Ensure the GLCD_Width and GLCD_HEIGHT constants are set correctly when using this additional
constant.
Example:
#include <glcd.h>
FilledTriangle
FilledTriangle:
Explanation:
The constant GLCD_PROTECTOVERRUN can be added to prevent FilledTriangles from re-drawing at the
screen edges. Ensure the GLCD_Width and GLCD_HEIGHT constants are set correctly when using this
additional constant.
Example:
#include <glcd.h>
GLCDCLS
Syntax:
GLCDCLS [GLCDBackground]
Explanation:
Clears the screen of a Graphic LCD. This command is supported by all GLCD displays.
For colour GLCD displays only. The optional parameter can be used to clear the screen to a specific
colour. Using this additional parameter will also change the GLCDBackground colour to this same
297
colour.
Specific to the ST7920 GLCD devices. This command supports the clearing the GLCD to either text mode
or graphics mode.
GLCDDrawChar
Syntax:
Colour can be ON or OFF. For the ST7735 devices this an be any word value that represents the color
palette.
Explanation:
X = 1 to 128
Y = 1 to 64
GLCDDrawString
Syntax:
Explanation:
298
GLCDPrint
Syntax:
Explanation:
GLCDLocateString
Syntax:
GLCDLocateString(PrintLocX, PrintLocY )
Explanation:
Moves the GLCD string pointer to the specified location on the GLCD screen.
For the purpose of this command. The screen addressing is the first line equates to the parameter 1, the
second line equates to the paramter 2 etc.
An example:
Example:
299
GLCDPrintStringLn ( "1.First Ln" )
GLCDPrintStringLn ( "2.Second Ln" )
GLCDPrintStringLn ( "" )
GLCDPrintStringLn ( "4.Forth Ln" )
GLCDLocateString( 0, 5 )
GLCDPrintString ( "5." )
GLCDPrintStringLn ( "Fifth Ln" )
GLCDPrintString
Syntax:
GLCDPrintString( String )
Explanation:
This command will NOT move the to start of the next line after the string has been displayed
Example:
300
GLCDPrintStringLn
Syntax:
GLCDPrintStringLn( String )
Explanation:
This command will move to the start of the next line after the string has been displayed
Example:
GLCDRotate
Syntax:
Explanation:
GLCD rotation needs to be supported by the GLCD chipset. NOT all GLCD chipset support these
commands.
301
LANDSCAPE
PORTRAIT_REV
LANDSCAPE_REV
PORTRAIT
The command will rotate the screen and set the following variables using the global variables shown
below.
GLCD_WIDTH
GLCD_HEIGHT
#define LANDSCAPE 1
#define PORTRAIT_REV 2
#define LANDSCAPE_REV 3
#define PORTRAIT 4
GLCDReadByte
Syntax:
byte_variable = GLCDReadByte
Explanation:
GLCDTimeDelay
Syntax:
GLCDTime
Explanation:
This will call the delay routine that delays data transmissions. By default this is set to 20, which equate
to 20 us. GLCDTimeDelay default of 20us is for 32Mhz support. The can be reduced for slower chip speeds
by change the constant ST7920WriteDelay.
Example usage:
302
GLCDTime 'call the delay routine
#define ST7920WriteDelay 1 'set the delay to 1 us
GLCDWriteByte
Syntax:
GLCDWriteByte (LCDByte)
Explanation:
Line
Syntax:
Explanation:
Draws a line on a GLCD from pixel X1, Y1 location to pixel X2,Y2 location.
Example:
#include <glcd.h>
line 0,0,127,63
line 0,63,127,0
line 40,0,87,63
line 40,63,87,0
303
Pset
Syntax:
Explanation:
Sets or Clears a Pixel at the specified XPosition, YPosition. Use GLCDState set to 1 to set the pixel and a
0 clears the pixel.
Triangle
Triangle:
Explanation:
The constant GLCD_PROTECTOVERRUN can be added to prevent Triangles from re-drawing at the screen
edges. Ensure the GLCD_Width and GLCD_HEIGHT constants are set correctly when using this additional
constant.
Example:
#include <glcd.h>
304
Touch Screen
This is the Touch Screen section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Syntax:
ADS7843_Init
ADS7843_GetXY
ADS7843_SetPrecision
Command Availability:
#include <ADS7843.h>
Explanation:
The ADS7843 device is a 12-bit sampling Analog-to-Digital Converter (ADC) with a synchronous serial
interface and low on resistance switches for driving touch screens.
The Great Cow Basic driver is integrated with the SDD1289 GLCD driver. To use the ADS7843 driver
the following is required to added to the Great Cow BASIC source file.
ADS7843_SetPrecision this sub-routine sets the level of precision of the touch screen.
Required Constants:
305
Constants Controls/Direction Default Value
The Great Cow Basic commands supported for this chip are:
Precision can be set to four values as shown in the table below. Passing a parameter of
ADS7843_SetPrecision changes the precision controls.
#define PREC_LOW 1
#define PREC_MEDIUM 2
#define PREC_HI 3
#define PREC_EXTREME 4 Default Value
Example:
This example shows how to drive a SDD1289 based Graphic LCD module with ADS7843 touch
controller.
'Chip Settings
#chip mega2560, 16
'Include
#include <glcd.h>
#include <ADS7843.h>
306
#define GLCD_WR PORTG.2
#define GLCD_CS PORTG.1
#define GLCD_RS PORTD.7
#define GLCD_RST PORTG.0
#define GLCD_DB0 PORTC.0
#define GLCD_DB1 PORTC.1
#define GLCD_DB2 PORTC.2
#define GLCD_DB3 PORTC.3
#define GLCD_DB4 PORTC.4
#define GLCD_DB5 PORTC.5
#define GLCD_DB6 PORTC.6
#define GLCD_DB7 PORTC.7
#define GLCD_DB8 PORTA.0
#define GLCD_DB9 PORTA.1
#define GLCD_DB10 PORTA.2
#define GLCD_DB11 PORTA.3
#define GLCD_DB12 PORTA.4
#define GLCD_DB13 PORTA.5
#define GLCD_DB14 PORTA.6
#define GLCD_DB15 PORTA.7
Wait 100 ms
num=0
Pset 1, 1, SSD1289_YELLOW
Do Forever
if ADS7843_IRQ=0 then
num++
GLCDPrint 10, 15, str(num),SSD1289_YELLOW, 2
ADS7843_GetXY ( TP_X , TP_Y )
if TP_X>=100 then GLCDPrint 100, 50, Str(TP_X),SSD1289_YELLOW, 2
if TP_X>=10 and TP_X<100 then GLCDPrint 100, 50, Str(TP_X)+" ",SSD1289_YELLOW,
2
if TP_X<10 then GLCDPrint 100, 50, Str(TP_X)+" ",SSD1289_YELLOW, 2
if TP_Y>=100 then GLCDPrint 100, 70, Str(TP_Y),SSD1289_YELLOW, 2
if TP_Y>=10 and TP_Y<100 then GLCDPrint 100, 70, Str(TP_Y)+" ", SSD1289_YELLOW,
2
if TP_Y<10 then GLCDPrint 100, 70, Str(TP_Y)+" ",SSD1289_YELLOW, 2
Pset TP_X, TP_Y, SSD1289_YELLOW
end if
307
Wait 1 ms
Loop
308
Liquid Crystal Display
This is the LCD section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
LCD Overview
Introduction:
The LCD routines in this section allow Great Cow BASIC programs to control an alphanumeric Liquid
Crystal Displays based on the HD44780 IC. This covers most 16 x 1, 16 x 2, 20 x 4 and 40 x 4 LCD
displays.
These methods allow the displays to be connected to the microcontroller in many different ways:
1 Uses a combined data and clock line. This mode is used when the LCD is connected
through a shift register 74HC595, as detailed at here.
This method of driving an LCD requires an additional integrated circuit and other
passive components. This is not recommended for the beginner.
2 Uses separated Data and Clock lines. This mode is used when the LCD is connected
through a 74LS174 shift register IC, as detailed at here
This method of driving an LCD requires additional integrated circuits and other
passive components. This is not recommended for the beginner.
3 DB, CB, EB are connected to the microcontroller as the Data, Clock and Enable Bits.
This a common method to connect a microcontroller to an LCD. This requires 3 data
ports on the microcontroller.
4 R/W, RS, Enable and the highest 4 data lines (DB4 through DB7) are connected to the
microcontroller. The use of the R/W line is optional.
This a common method to connect a microcontroller to an LCD. This requires 7(6)
data ports on the microcontroller.
8 R/W, RS, Enable and all 8 data lines. The data lines must all be connected to the same
I/O port, in sequential order. For example, DB0 to PORTB.0, DB1 to PORTB.1 and so on,
with`DB7` going to PORTB.7.
This is a common method to connect a microcontroller to a LCD. This requires 11(10)
data ports on the microcontroller.
309
Connection Required Connections
Mode
10 The LCD is controlled via I2C. A type 10 LCD 12C adapter. Set LCD_10 to 10 for the
YwRobot LCD1602 IIC V1 or the Sainsmart LCD_PIC I2C adapter
This is a common method and requires two data ports on the microcontroller.
12 The LCD is controlled via I2C. A type 12 LCD 12C adapter. Set LCD_10 to `12`for the
Ywmjkdz I2C adapter with a potentiometer (variable resistance) bent over top of
chip.
This is a common method and requires two data ports on the microcontroller.
The support of the types of LCD displays is shown in the following table.
4 40 x 4 LCD displays,
also known as 4004 type LCD displays.
To change the performance (communications speed) of the LCD use #define LCD_SPEED and/or to
change the LCD width characteristics use #define LCD_WIDTH
See the separate sections of the Help file for the specifics of each Connection Mode.
For more help, see LCD_IO 0, LCD_IO 1, LCD_IO 2, LCD_IO 3, LCD_IO_2 74xx164, LCD_IO_2 74xx174,
LCD_IO 4, LCD_IO 8, LCD_IO 10 or LCD_IO 12
and,
LCD_Width, lcd_speed
LCD_IO 0
To use connection mode 0, a subroutine to write a byte to the LCD must be provided.
310
Optionally, another subroutine to read a byte from the LCD can also be defined. If the LCD was to be
read, the function LCDReadByte would be set to the name of a function that reads the LCD and returns
the data byte from the LCD. If there is no way (or no requirement) to read from the LCD, then the
LCD_NO_RW constant must be set.
In connection mode 0, the LCD_RS constant will be set automatically to an unused bit variable. The
higher level LCD commands (such as Print and Locate) will set it, and the subroutine is responsible for
writing to the LCD. The subroutine should handle the process and then set the RS pin on the LCD
appropriately.
Relevant Constants:
Specific constants are used to control settings for the Liquid Crystal Display routines included with
Great Cow BASIC. To set these constants the main program should specific constants to support the
connection mode using #define.
When using connection mode 0 only one constant must be set - all others are optional or can be
ignored.
See the separate sections of the Help file for the specifics of each Connection Mode.
For more help, see LCD_IO 1, LCD_IO 2, LCD_IO 2_74xx164, LCD_IO 2_74xx174, LCD_IO 4,LCD_IO 8,
LCD_IO 10 or LCD_IO 12
LCD_IO 1
This approach uses a single connectivity line that supports a combined data and clock signal between
the microcontroller and the LCD display. This approach is used when the LCD is connected through a
shift register 74HC595, as detailed at here. This connection method is also called a 1-wire connection.
This solution approach recognises the original work provided in the Elektor Magazine.
Relevant Constants:
Specific constants are used to control settings for the Liquid Crystal Display routines included with
Great Cow BASIC. To set these constants the main program should specific constants to support the
connection mode using #define.
When using connection mode 1, only two constants must be set - all others are optional or can be
ignored.
311
How to connect and control the LCD background led: see LCDBacklight.
LCD.h supports in 1-wire mode the control of pin 4 of the 74HC595 for the background led.
See for further code examples see 0,1 and 2 Wire LCD Solutions.
See the separate sections of the Help file for the specifics of each Connection Mode.
For more help, see LCD_IO 0, LCD_IO 2 LCD_IO 2_74xx164, LCD_IO 2_74xx174 LCD_IO 4, LCD_IO 8,
LCD_IO 10 or LCD_IO 12
LCD_IO 2_74xx164
Use a Data and a Clock line. This manner is used when the LCD is connected through a shift register IC
either using a 74HC164 or a 74LS164, as detailed at here. This connection method is also called a 2-wire
connection.
This is the preferred two wire method to connect via a shift register to an LCD display.
Relevant Constants:
Specific constants are used to control settings for the Liquid Crystal Display routines included with
Great Cow BASIC. To set these constants the main program should specific constants to support the
connection mode using #define.
When using connection mode 2_74XX164 only three constants must be set - all others are optional or
can be ignored.
LCD.h supports in connection mode 2_74XX164 via the control of pin 11 of the 74HC164 / 74LS164 the
background led/backlight.
312
http://gcbasic.sourceforge.net/help/_lcdbacklight.html
See for further code examples see Two Wire LCD Solutions.
See the separate sections of the Help file for the specifics of each Connection Mode.
For more help, see LCD_IO 0, LCD_IO 1, LCD_IO 2, LCD_IO 2_74xx74, LCD_IO 4, LCD_IO 8, LCD_IO 10 or
LCD_IO 12
LCD_IO 2
This method uses a Data and a Clock line via a shift register to control the LCD display. This method is
used when the LCD is connected through a shift register IC either using a 74HC164 or a 74LS174, as
detailed at here. This connection method is also called a 2-wire connection.
This is a deprecated method mode to connect an LCD display to a microcontroller via a shift registry
either a 74LS174 (or a 74LS164 with diode connected to pin 11). This method does not support
backlight control and has no additional input/output pin.
If you have used the 2-wire mode prior to August 2015, please choose this method for your existing
code.
See LCD_IO 2 74xx164 for the preferred method to connect an LCD display to a microcomputer via a
shift register.
Relevant Constants:
Specific constants are used to control settings for the Liquid Crystal Display routines included with
Great Cow BASIC. To set these constants the main program should specific constants to support the
connection mode using #define. When using 2-bit mode only three constants must be set - all others
are optional or can be ignored.
See for further code examples see Two Wire LCD Solutions.
See the separate sections of the Help file for the specifics of each Connection Mode.
313
For more help, see LCD_IO 0, LCD_IO 1, LCD_IO 2_74xx164, LCD_IO 2_74xx174, LCD_IO 4, LCD_IO 8,
LCD_IO 10 or LCD_IO 12
LCD_IO 3
This method uses a Data and a Clock line via a shift register to control the LCD display plus an Enable
line. This method is used when the LCD is connected through a shift register IC using a LS74574.
314
315
Relevant Constants:
Specific constants are used to control settings for the Liquid Crystal Display routines included with
Great Cow BASIC. To set these constants the main program should specific constants to support the
connection mode using #define. When using 3-bit mode only three constants must be set.
316
Constant Name Controls Default Value
Example:
317
#chip 16f628a, 4
#option explicit
Dim BV as Byte
'Program Start
DO Forever
CLS
WAIT 2 s
PRINT "Test LCDHex "
wait 3 s
CLS
wait 1 s
for bv = 0 to 16
locate 0,0
Print "DEC " : Print BV
locate 1,0
Print "HEX "
LCDHex BV
Locate 1, 8
LCDHEX BV, LeadingZeroActive
wait 500 ms
next
CLS
wait 1 s
Print "END TEST"
LOOP
318
See the separate sections of the Help file for the specifics of each Connection Mode.
For more help, see LCD_IO 0, LCD_IO 1, LCD_IO 2_74xx164, LCD_IO 2_74xx174, LCD_IO 4, LCD_IO 8,
LCD_IO 10 or LCD_IO 12
LCD_IO 2_74xx174
LCD_IO 2_74xx174 has been deprecated as preferred method mode to connect an LCD display to a
microcontroller via a shift register either a 74LS174 (or a 74LS164 with diode connected to pin 11). This
method does not support backlight control and has no additional input/output pin.
See LCD_IO 2_74xx164 for the preferred method to connect an LCD display to a microcontroller via a
shift register.
For more help, see LCD_IO 1, LCD_IO 2, LCD_IO 2_74xx164, LCD_IO 4, LCD_IO 8, LCD_IO 10 or LCD_IO
12
LCD_IO 4
To use connection mode 4 the R/W, RS, Enable control lines and the highest 4 data lines (DB4 through
DB7) must be connected to the microcontroller.
Relevant Constants:
Specific constants are used to control settings for the Liquid Crystal Display routines included with
Great Cow BASIC. To set these constants the main program should specific constants to support the
connection mode using #define. Constants required for connection mode 4.
LCD_IO Must be 4 4
LCD_RS Specifies the output pin that is connected to Register Select on Must be defined
the LCD.
LCD_RW Specifies the output pin that is connected to Read/Write on the Must be defined (unless
LCD. The R/W pin can be disabled*. R/W is disabled)
LCD_Enable Specifies the output pin that is connected to Read/Write on the Must be defined
LCD.
LCD_DB4 Output pin used to interface with bit 4 of the LCD data bus Must be defined
LCD_DB5 Output pin used to interface with bit 5 of the LCD data bus Must be defined
319
Constant Controls Default Value
Name
LCD_DB6 Output pin used to interface with bit 6 of the LCD data bus Must be defined
LCD_DB7 Output pin used to interface with bit 7 of the LCD data bus Must be defined
The R/W pin can be disabled by setting the LCD_NO_RW constant. If this is done, there is no need for the R/W
to be connected to the chip, and no need for the LCD_RW constant to be set. Ensure that the R/W line on
the LCD is connected to ground if not used.
Also see for further code examples see Four Wire LCD Solutions.
See the separate sections of the Help file for the specifics of each Connection Mode.
For more help, see LCD_IO 0, LCD_IO 1, LCD_IO 2, LCD_IO 2_74xx164, LCD_IO 2_74xx174, LCD_IO 8,
LCD_IO 10 or LCD_IO 12
LCD_IO 8
Using connection mode will require R/W, RS, Enable and all 8 data lines.
The data lines must all be connected to the same I/O port, in sequential order. For example, DB0 to
PORTB.0, DB1 to PORTB.1 and so on, with DB7 going to PORTB.7.
Relevant Constants:
These constants are used to control settings for the Liquid Crystal Display routines included with Great
Cow BASIC. To set them, place a line in the main program file that uses #define to assign a value to the
particular constant.
LCD_RS Specifies the output pin that is connected to Register Select on Must be defined
the LCD.
LCD_RW Specifies the output pin that is connected to Read/Write on the Must be defined (unless
LCD. The R/W pin can be disabled*. R/W is disabled)
320
Constant Controls Default Value
Name
LCD_Enable Specifies the output pin that is connected to Read/Write on the Must be defined
LCD.
LCD_DATA_P Output port used to interface with LCD data bus Must be defined
ORT
LCD_LAT Drives the port with LATx support. Resolves issues with faster Optional
Mhz and the Microchip PIC read/write/modify feature. See
example below.
The R/W pin can be disabled by setting the LCD_NO_RW constant. If this is done, there is no need for the R/W
to be connected to the chip, and no need for the LCD_RW constant to be set. Ensure that the R/W line on
the LCD is connected to ground if not used.
See the separate sections of the Help file for the specifics of each Connection Mode.
For more help, see LCD_IO 0, LCD_IO 1, LCD_IO 2, LCD_IO 2_74xx164, LCD_IO 2_74xx174, LCD_IO 4,
LCD_IO 10 or LCD_IO 12
LCD_IO 10
The LCD is controlled via I2C of a type 10 LCD 12C adapter. Use LCD_IO 10 for the YwRobot LCD1602 IIC
V1 or the Sainsmart LCD_PIC I2C adapter. To use mode 10 you must define the I2C ports as normal in
your Great Cow BASIC code. Then, define the LCD type, set the I2C_address of the LCD adapter and the
LCD speed, if required. Finally, set the backlight control, if required.
Relevant Constants:
Specific constants are used to control settings for the Liquid Crystal Display routines included with
Great Cow BASIC. To set these constants the main program should specific constants to support the
connection mode using #define.
321
Constant Name Controls Value
See the separate sections of the Help file for the specifics of each Connection Mode.
For more help, see LCD_IO 0, LCD_IO 1, LCD_IO 2, LCD_IO 2_74xx164, LCD_IO 2_74xx174, LCD_IO 4,
LCD_IO 8, LCD_IO 12
Using mode 10
When using I2C LCD mode 10 the target I2C device address is setup as shown below. Each bit of the the
variable i2c_lcd_byte is defined to address the correct LCD display port.
i2c_lcd_e = i2c_lcd_byte.2
i2c_lcd_rw = i2c_lcd_byte.1
i2c_lcd_rs = i2c_lcd_byte.0
i2c_lcd_bl = i2c_lcd_byte.3
i2c_lcd_d4 = i2c_lcd_byte.4
i2c_lcd_d5 = i2c_lcd_byte.5
i2c_lcd_d6 = i2c_lcd_byte.6
i2c_lcd_d7 = i2c_lcd_byte.7
If you have an I2C LCD display adapter with a different set of connection of the adapter then change
this configuration to suit the specific of the adapter as follows. This should be done in the your main
program code.
LCD_IO 12
The LCD is controlled via I2C. A type 12 is the Ywmjkdz I2C adapter with potentiometer variable
resistor) bent over top of chip. To use mode 12 you must define the I2C ports as normal in your GCB
322
code. Then, define the LCD type, set the I2C_address of the LCD adapter and the LCD speed, if required.
Relevant Constants:
These constants are used to control settings for the Liquid Crystal Display routines included with Great
Cow BASIC. To set them, place a line in the main program file that uses #define to assign a value to the
particular constant.
When using 2-bit mode only three constants must be set - all others can be ignored.
See the separate sections of the Help file for the specifics of each Connection Mode.
323
For more help, see LCD_IO 0, LCD_IO 1, LCD_IO 2 LCD_IO 2_74xx164, LCD_IO 2_74xx174, LCD_IO 4,
LCD_IO 8, LCD_IO 10
When using I2C LCD mode 12 the target I2C device address is setup as shown below. Each bit of the the
variable i2c_lcd_byte is defined to address the correct LCD display port.
i2c_lcd_e = i2c_lcd_byte.4
i2c_lcd_rw = i2c_lcd_byte.5
i2c_lcd_rs = i2c_lcd_byte.6
i2c_lcd_bl = i2c_lcd_byte.7
i2c_lcd_d4 = i2c_lcd_byte.0
i2c_lcd_d5 = i2c_lcd_byte.1
i2c_lcd_d6 = i2c_lcd_byte.2
i2c_lcd_d7 = i2c_lcd_byte.3
If you have an I2C LCD display adapter with a different set of connection of the adapter then change
this configuration to suit the specific of the adapter as follows. This should be done in the your main
program code.
LCD_SPEED
Using LCD_SPEED:
The communication performance of a LCD display can be controlled via a #DEFINE. This method allows
the timing to be optimised.
Example
324
Define Required Connections
LCD_WIDTH
Using LCD_WIDTH:
This constant changes the width characteristics of a LCD display. The standard width is assumed to
be 20 characters.
This constant allows the width to be optimised for specific LCD chipsets.
Example
#DEFINE LCD_WIDTH 16
LCD_WIDTH Default is 20
16 - Set the WIDTH 16 characters
CLS
Syntax:
CLS
Command Availability:
Explanation:
The CLS command clears the contents of the LCD, and returns the cursor to the top left corner of the
screen
Example :
325
'A Flashing Hello World program for Great Cow BASIC
'Main routine
Do
Print "Hello World"
Wait 1 sec
CLS
Wait 1 sec
Loop
Supported in <LCD.H>
Get
Syntax:
Command Availability:
Available on all microcontrollers with the LCD R/W line (pin 5) connected and if the following constant
definition is used; #define LCD_RW. Not available if the LCD is connected using the 0 or 2 bit mode or if
the constant definition #define LCD_NO_RW is used.
Explanation:
The Get function reads the ASCII character code at a given location on the LCD.
Supported in <LCD.H>
326
LCDBacklight
Syntax:
LCDBacklight ( On | Off )
Command Availability:
Explanation:
Do not connect the LCD backlight directly to the microcontroller! Always refer to the datasheet for the
correct method to drive the LCD backlight.
For 0, 4, 8, 404 LCD types you must define the controlling port.pin for the LCD backlight.
You may need to invert the state of the LCD backlight control port. This can be achieved by setting the
following constants.
327
The diagram below shows a method to connect the LCD backlight to a microcontroller.
The
diagram above was provided by William Roth, January 2015.
Supported in <LCD.H>
LCDCreateChar
Syntax:
Command Availability:
Explanation:
Each character on the LCD is made up from an 8 row by 5 column (5x8) matrix of pixels. The data to be
sent to the LCD is composed of an 8 element array, where each element corresponds to a row. Inside
each element, the 5 lowest bits make up the data for the corresponding row. When a bit is set a dot will
be drawn at the matching location; when it is cleared, no dot will appear.
An array of more than 8 elements may be used, but only the first 8 will be read.
328
char is the ASCII value of the character to create. ASCII codes 0 through 7 are usually used to store
custom characters.
chardata() is an array containing the data for the character.
Example:
Supported in <LCD.H>
LCDCreateGraph
Syntax:
329
LCDCreateGraph value
Command Availability:
Explanation:
The LCDCreateGraph command will create a graph like character which can then be displayed on the
LCD
Example :
;Chip Settings
#chip 16F88,8
#config osc = intrc
;Defines (Constants)
#define LCD_IO 4
#define LCD_RS PORTA.6
#define LCD_NO_RW
#define LCD_Enable PORTA.7
#define LCD_DB4 PORTB.4
#define LCD_DB5 PORTB.5
#define LCD_DB6 PORTB.6
#define LCD_DB7 PORTB.7
Locate 0,0
Print "Reset"
wait 1 s
cls
Graph_Tests:
cls
'Draw the custom character - fill the LCD
repeat 64
LCDWriteChar 0
end Repeat
330
Supported in <LCD.H>
LCDCmd
Syntax:
LCDCMD value
Command Availability:
Explanation:
This command set LCD specific instructions to the LCD display. As shown in the table below.
Clear Screen 1 01
Set pointer in character-generator RAM (CG RAM address) 64 + addr 40+ addr
Example :
;Chip Settings
#chip 16F88,8
#config osc = intrc
331
;Defines (Constants)
#define LCD_IO 4
#define LCD_RS PORTA.6
#define LCD_NO_RW
#define LCD_Enable PORTA.7
#define LCD_DB4 PORTB.4
#define LCD_DB5 PORTB.5
#define LCD_DB6 PORTB.6
#define LCD_DB7 PORTB.7
Locate 0,0
Print "Reset"
wait 1 s
cls
LCD_Command_Tests:
locate 0,8
print "123456"
'Scroll display one character right (all lines) 28
'
lcdcmd 28
wait 1 s
lcdcmd 28
wait 1 s
lcdcmd 28
wait 1 s
lcdcmd 28
wait 1 s
332
'Move cursor one character left 16
'
lcdcursor flash
locate 0,8
lcdcmd 16
wait 1 s
lcdcmd 16
wait 1 s
lcdcmd 16
wait 1 s
lcdcmd 16
wait 1 s
Supported in <LCD.H>
LCDCursor
Syntax:
LCDCursor value
Command Availability:
Explanation:
FLASH, and ON/OFF have been retained for backward compatibility with older releases of GCB.
333
LCDOFF will turn off (hide) the LCD display.
CURSORON will turn on the cursor.
CURSOROFF will turn off the cursor.
FLASHON will flash the cursor.
FLASHOFF will stop flashing the cursor.
Example :
#config osc = intrc
#chip 16f877a, 8
;Defines (Constants)
#define LCD_IO 4
#define LCD_RS PORTA.6
#define LCD_NO_RW
#define LCD_Enable PORTA.7
#define LCD_DB4 PORTB.4
#define LCD_DB5 PORTB.5
#define LCD_DB6 PORTB.6
#define LCD_DB7 PORTB.7
Start:
CLS
WAIT 3 s
PRINT "START DEMO"
locate 1,0
PRINT "DISPLAY ON"
wait 3 s
CLS
Locate 0,0
Print "Cursor ON"
Locate 1,0
LCDcursor CursorOn
wait 3 S
CLS
LCDcursor CursorOFF
locate 0,0
Print "Cursor OFF"
wait 3 s
CLS
Locate 0,0
Print "FLASH ON"
Locate 1,0
334
LCDcursor FLASHON
wait 3 s
CLS
locate 0,0
Print "FLASH OFF"
LCDCURSOR FLASHOFF
wait 3 sec
Locate 0,0
Print "CURSOR&FLASH ON" 'Both are on at the same time
locate 1,0
LCDCURSOR CURSORON
LCDCURSOR FLASHON
Wait 3 sec
Locate 0,0
Print "CURSOR FLASH OFF"
locate 1,0
LCDCURSOR CursorOFF
LCDCURSOR FLASHOFF
Wait 3 sec
CLS
Locate 0,4
PRINT "Flashing"
Locate 1,4
Print "Display"
wait 500 ms
repeat 5
LCDCURSOR LCDOFF
wait 500 ms
LCDCURSOR LCDON
wait 500 ms
end repeat
CLS
Locate 0,0
Print "DISPLAY OFF"
Locate 1,0
Print "FOR 5 SEC"
Wait 2 SEC
LCDCURSOR LCDOFF
WAIT 5 s
CLS
Locate 0,0
335
LCDCURSOR LCDON
Print "END DEMO"
wait 3 s
goto start
Supported in <LCD.H>
LCDHex
Syntax:
LCDHex value
Command Availability:
Explanation:
The LCDHex will display the byte value as a 1 or 2 character HEX string.
Example :
'Change as necessary
#define LCD_RS PortC.0
#define LCD_Enable PortC.1
#define LCD_DB4 PortC.2
#define LCD_DB5 PortC.3
#define LCD_DB6 PortC.4
#define LCD_DB7 PortC.5
336
' #define LCD_IO 4
' #define LCD_NO_RW
' #define LCD_Speed fast 'FAST IS OK ON 16f877a
'
' ;Change as necessary
' #define LCD_RS PortB.2
' #define LCD_Enable PortB.3
' #define LCD_DB4 PortB.4
' #define LCD_DB5 PortB.5
' #define LCD_DB6 PortB.6
' #define LCD_DB7 PortB.7
'Program Start
DO Forever
CLS
WAIT 2 s
PRINT "Test LCDHex "
wait 3 s
CLS
wait 1 s
for bv = 0 to 255
locate 0,0
Print "DEC " : Print BV
locate 1,0
Print "HEX "
LCDHex BV, LeadingZeroActive ; dislay leading Zero
' LCDHex BV ; do not display leading zero
wait 1 s
next
CLS
wait 1 s
Print "END TEST"
LOOP
Supported in <LCD.H>
LCDHome
Syntax:
LCDHome
Command Availability:
337
Explanation:
Example:
;Chip Settings
#chip 16F88,8
#config osc = intrc
;Defines (Constants)
#define LCD_IO 4
#define LCD_RS PORTA.6
#define LCD_NO_RW
#define LCD_Enable PORTA.7
#define LCD_DB4 PORTB.4
#define LCD_DB5 PORTB.5
#define LCD_DB6 PORTB.6
#define LCD_DB7 PORTB.7
Locate 0,0
Print "Reset"
wait 1 s
ClS
Cursor_Home_Tests:
cls
lcdcursor flash
print "Test Home Cmd"
LCDHome
wait 3 s
Supported in <LCD.H>
LCDDisplayOn
Syntax:
LCDDisplayOn
Explanation:
338
See also LCDCursor
Supported in <LCD.H>
LCDDisplayOff
Syntax:
LCDDisplayOff
Explanation:
Supported in <LCD.H>
LCDSpace
Syntax:
LCDSpace value
Command Availability:
Explanation:
The LCDSpace command will print the required number of spaces on the LCD display
value is a byte value from 1 to 255. Where the value is the number of spaces required.
Example :
339
Locate 0,0
Print "Reset"
wait 1 s
cls
LCD_Space_Tests:
lcdcursor flash
lcdspace 12
print "*"
Supported in <LCD.H>
LCDWriteChar
Syntax:
LCDWriteChar char
Command Availability:
Explanation:
The LCDWriteChar command will show the specified character on the LCD, at the current cursor
position.
char is the ASCII value of the character to show. On most LCDs, characters 0 through 7 are user defined,
and can be set using the LCDCreateChar command.
Example :
340
'This program draws a smiling face character
Supported in <LCD.H>
Locate
Syntax:
Command Availability:
Explanation:
The Locate command is used to move the cursor on the LCD to the given location.
Line is line number on the LCD display. A byte value from 0 to 255.
Column is column number on the LCD display. A byte value from 0 to 255.
Example :
341
'A Hello World program for Great Cow BASIC.
'Uses Locate to show "World" on the second line
'Main routine
Print "Hello"
Locate 1, 5
Print "World"
Supported in <LCD.H>
Syntax:
Print string
Print byte
Print word
Print long
Print integer
Command Availability:
Explanation:
The Print command will show the contents of a variable on the LCD. It can display string, word, byte,
long or integer variables.
Example:
342
'A Light Meter program.
CLS
Print "Light Meter"
Locate 1,2
Print "A GCBASIC Demo"
Wait 2 s
Do
CLS
Print "Light Level: "
Print ReadAD(LightSensor)
Wait 250 ms
Loop
Supported in <LCD.H>
Put
Syntax:
Command Availability:
Explanation:
The Put command writes the given ASCII character code to the current location on the LCD.
Line is line number on the LCD display. A byte value from 0 to 255.
343
Column is column number on the LCD display. A byte value from 0 to 255.
Example :
'Misc Settings
#define SCROLL_DELAY 250 ms
'Main routine
For StarPos = 0 To 16
If StarPos = 0 Then
Put 0, 16, 32
Put 0, 0, 42
Else
Put 0, StarPos - 1, 32
Put 0, StarPos, 42
End If
Wait SCROLL_DELAY
Next
Supported in <LCD.H>
344
Examples
LCD_IO 2 Example
This a connection mode 2 Serial Driver to demonstrate LCD features. This for the 16F877A, but, it can
easily be adapted for other microcontrollers.
A 2 by 16 LCD is assumed.
Based on the works by Thomas Henry and then revised Evan R. Venn
#chip 16F877A,20
#define LCD_IO 2
#define LCD_DB portb.2
#define LCD_CB portb.0
#define LCD_NO_RW
;Here are various LCD commands which can be used.
;These are the LCD commands for the HD44780 controller
345
do forever
printMsg(0) ;print first message
wait 3 S ;pause 3 seconds
printMsg(2) ;print next message
wait 3 S ;pause 3 seconds
repeat 5 ;blink it five times
LCDCmd(lcdOff) ;display off
wait 500 mS ;pause
LCDCmd(lcdOn) ;display on
wait 500 mS ;pause
end repeat
wait 1 S ;pause before next demo
;demonstrate panning
printMsg(4) ;print next message
wait 3 S ;pause 3 seconds
repeat 16
LCDCmd(panL) ;pan left a step at a time
wait 300 mS ;slow down to avoid blur
end repeat
repeat 16
LCDCmd(panR) ;then pan right
wait 300 mS
end repeat
wait 1 S ;pause before next demo
;demonstrate moving the cursor
printMsg(6) ;print next message
wait 3 S ;pause 3 seconds
doHome ;home cursor
LCDCmd(under) ;choose underline cursor
for ii = 0 to 15 ;move cursor across first line
LCDCmd(line1+i)
wait 200 mS
next i
for ii = 0 to 15 ;move cursor across second line
LCDCmd(line2+i)
wait 200 mS
next i
for ii = 15 to 0 step -1 ;move cursor back over second line
LCDCmd(line2+i)
wait 200 mS
next i
for ii = 15 to 0 step -1 ;move cursor back over first line
LCDCmd(line1+i)
wait 200 mS
next i
wait 3 S
;demonstrate blinking block cursor
printMsg(8) ;print next message
346
doHome ;home the cursor
LCDCmd(block) ;choose blinking block cursor
wait 4 S ;pause 4 seconds
LCDCmd(mode1) ;change to one long line mode
doHome ;home the cursor again
LCDCmd(curOff) ;and disable it
end
347
;----- Home the cursor
sub doHome
LCDCmd(home)
wait 5 mS ;and so does this one
end sub
for ii = 0 to StringLength
index = row*16+ii
EPread index, char ;fetch next character and
print chr(char) ;transmit to the LCD
next
LCDCmd(line2) ;get set for second line
for ii = 0 to StringLength
index = (row+1)*16+ii
EPread index, char ;fetch next character and
print chr(char) ;transmit to the LCD
next
end sub
sub loadEeprom
' Strings for EEPROM, Strings should be limited to 16 characters for the first 13
sstrings, then a long string to fill eeprom
location = 0
WriteEeprom "First we'll show"
WriteEeprom "this message. "
WriteEeprom "Then we'll blink"
WriteEeprom "five times. "
WriteEeprom "Now lets pan "
WriteEeprom "left and right. "
WriteEeprom "Watch the line "
WriteEeprom "cursor move. "
WriteEeprom "A block cursor "
WriteEeprom "is available. "
WriteEeprom "Characters: "
WriteEeprom "Bye! "
WriteEeprom "in one line mode"
WriteEeprom "Next well scroll this long message as a marquee"
end sub
348
for ee = 1 to len ( Estring )
HSersend Estring(ee)
epwrite location, Estring(ee)
location++
next
end sub
LCD_IO 4 Example
This is a Serial connection mode 4 Driver to demonstrate LCD features. This for the 16F877A, but, it can
easily be adapted for other microcontrollers.
A 2 by 16 LCD is assumed.
Based on the works by Thomas Henry and then revised Evan R. Venn
#chip 16F877A,20
349
#define panL = 28 ;pan viewing window left
#define bus4 = 32 ;4-bit data bus mode
#define bus8 = 48 ;8-bit data bus mode
#define mode1 = 32 ;one-line mode (alias)
#define mode2 = 40 ;two-line mode
#define line1 = 128 ;go to start of line 1
#define line2 = 192 ;go to start of line 2
;----- Variables
dim char, msn, lsn, index, ii as byte
;----- Main Program
LoadEeprom ;load the EEprom with strings
do forever
printMsg(0) ;print first message
wait 3 S ;pause 3 seconds
printMsg(2) ;print next message
wait 3 S ;pause 3 seconds
repeat 5 ;blink it five times
LCDCmd(lcdOff) ;display off
wait 500 mS ;pause
LCDCmd(lcdOn) ;display on
wait 500 mS ;pause
end repeat
wait 1 S ;pause before next demo
;demonstrate panning
printMsg(4) ;print next message
wait 3 S ;pause 3 seconds
repeat 16
LCDCmd(panL) ;pan left a step at a time
wait 300 mS ;slow down to avoid blur
end repeat
repeat 16
LCDCmd(panR) ;then pan right
wait 300 mS
end repeat
wait 1 S ;pause before next demo
;demonstrate moving the cursor
printMsg(6) ;print next message
wait 3 S ;pause 3 seconds
doHome ;home cursor
LCDCmd(under) ;choose underline cursor
for ii = 0 to 15 ;move cursor across first line
LCDCmd(line1+i)
wait 200 mS
next i
for ii = 0 to 15 ;move cursor across second line
LCDCmd(line2+i)
wait 200 mS
350
next i
for ii = 15 to 0 step -1 ;move cursor back over second line
LCDCmd(line2+i)
wait 200 mS
next i
for ii = 15 to 0 step -1 ;move cursor back over first line
LCDCmd(line1+i)
wait 200 mS
next i
wait 3 S
;demonstrate blinking block cursor
printMsg(8) ;print next message
doHome ;home the cursor
LCDCmd(block) ;choose blinking block cursor
wait 4 S ;pause 4 seconds
LCDCmd(mode1) ;change to one long line mode
doHome ;home the cursor again
LCDCmd(curOff) ;and disable it
351
printMsg(11) ;print next message
doHome ;home the cursor
loop
end
for ii = 0 to StringLength
index = row*16+ii
EPread index, char ;fetch next character and
print chr(char) ;transmit to the LCD
next
LCDCmd(line2) ;get set for second line
for ii = 0 to StringLength
index = (row+1)*16+ii
EPread index, char ;fetch next character and
print chr(char) ;transmit to the LCD
next
end sub
sub loadEeprom
' Strings for EEPROM, Strings should be limited to 16 characters for the first 13
sstrings, then a long string to fill eeprom
location = 0
WriteEeprom "First we'll show"
WriteEeprom "this message. "
WriteEeprom "Then we'll blink"
WriteEeprom "five times. "
WriteEeprom "Now lets pan "
WriteEeprom "left and right. "
352
WriteEeprom "Watch the line "
WriteEeprom "cursor move. "
WriteEeprom "A block cursor "
WriteEeprom "is available. "
WriteEeprom "Characters: "
WriteEeprom "Bye! "
WriteEeprom "in one line mode"
WriteEeprom "Next well scroll this long message as a marquee"
end sub
LCD_IO 8 Example
This is an connection mode 8 Driver to demonstrate LCD features. This for the 16F877A, but, it can
easily be adapted for other microcontrollers.
A 2 by 16 LCD is assumed.
Based on the works by Thomas Henry and then revised Evan R. Venn
#chip 16F877A,20
353
#define lcdOff = 8 ;LCD screen off
#define lcdOn = 12 ;LCD screen on, no cursor
#define curOff = 12 ;an alias for the above
#define block = 13 ;LCD screen on, block cursor
#define under = 14 ;LCD screen on, underline cursor
#define undblk = 15 ;LCD screen on, blinking and underline cursor
#define CLeft = 16 ;cursor left
#define CRight = 20 ;cursor right
#define panR = 24 ;pan viewing window right
#define panL = 28 ;pan viewing window left
#define bus4 = 32 ;4-bit data bus mode
#define bus8 = 48 ;8-bit data bus mode
#define mode1 = 32 ;one-line mode (alias)
#define mode2 = 40 ;two-line mode
#define line1 = 128 ;go to start of line 1
#define line2 = 192 ;go to start of line 2
;----- Variables
dim char, msn, lsn, index, ii as byte
;----- Main Program
LoadEeprom ;load the EEprom with strings
do forever
printMsg(0) ;print first message
wait 3 S ;pause 3 seconds
printMsg(2) ;print next message
wait 3 S ;pause 3 seconds
repeat 5 ;blink it five times
LCDCmd(lcdOff) ;display off
wait 500 mS ;pause
LCDCmd(lcdOn) ;display on
wait 500 mS ;pause
end repeat
wait 1 S ;pause before next demo
;demonstrate panning
printMsg(4) ;print next message
wait 3 S ;pause 3 seconds
repeat 16
LCDCmd(panL) ;pan left a step at a time
wait 300 mS ;slow down to avoid blur
end repeat
repeat 16
LCDCmd(panR) ;then pan right
wait 300 mS
end repeat
wait 1 S ;pause before next demo
;demonstrate moving the cursor
printMsg(6) ;print next message
wait 3 S ;pause 3 seconds
354
doHome ;home cursor
LCDCmd(under) ;choose underline cursor
for ii = 0 to 15 ;move cursor across first line
LCDCmd(line1+i)
wait 200 mS
next i
for ii = 0 to 15 ;move cursor across second line
LCDCmd(line2+i)
wait 200 mS
next i
for ii = 15 to 0 step -1 ;move cursor back over second line
LCDCmd(line2+i)
wait 200 mS
next i
for ii = 15 to 0 step -1 ;move cursor back over first line
LCDCmd(line1+i)
wait 200 mS
next i
wait 3 S
;demonstrate blinking block cursor
printMsg(8) ;print next message
doHome ;home the cursor
LCDCmd(block) ;choose blinking block cursor
wait 4 S ;pause 4 seconds
LCDCmd(mode1) ;change to one long line mode
doHome ;home the cursor again
LCDCmd(curOff) ;and disable it
355
wait 500 mS
next i
for ii = 161 to 255 ;print next batch of ASCII characters
LCDCmd(line1+12)
print chr(ii)
wait 500 mS
next i
;say good-bye
LCDCmd(line2)
printMsg(11) ;print next message
doHome ;home the cursor
loop
end
for ii = 0 to StringLength
index = row*16+ii
EPread index, char ;fetch next character and
print chr(char) ;transmit to the LCD
next
LCDCmd(line2) ;get set for second line
for ii = 0 to StringLength
index = (row+1)*16+ii
EPread index, char ;fetch next character and
print chr(char) ;transmit to the LCD
next
end sub
sub loadEeprom
' Strings for EEPROM, Strings should be limited to 16 characters for the first 13
sstrings, then a long string to fill eeprom
356
location = 0
WriteEeprom "First we'll show"
WriteEeprom "this message. "
WriteEeprom "Then we'll blink"
WriteEeprom "five times. "
WriteEeprom "Now lets pan "
WriteEeprom "left and right. "
WriteEeprom "Watch the line "
WriteEeprom "cursor move. "
WriteEeprom "A block cursor "
WriteEeprom "is available. "
WriteEeprom "Characters: "
WriteEeprom "Bye! "
WriteEeprom "in one line mode"
WriteEeprom "Next well scroll this long message as a marquee"
end sub
LCD_IO 10 Example
This is an connection mode 10 I2C Driver to demonstrate LCD features. This for the 16F877A, but, it can
easily be adapted for other microcontrollers.
A 2 by 16 LCD is assumed with the LCD being driven using an LCD I2C adapter. Two types are
supported the YwRobot LCD1602 IIC V1 / a Sainsmart LCD_PIC I2C adapter or the Ywmjkdz I2C adapter
with pot bent over top of chip.
The demonstrates reading a DS18B20 and showing the results on the LCD.
Example:
#chip mega328p, 16
#include <DS18B20.h>
357
#define I2C_CLOCK PORTC.5
#define I2C_DISABLE_INTERRUPTS ON
'''Set up LCD
#define LCD_IO 10
#define LCD_I2C_Address_1 0x4E ; LCD 1
#define LCD_I2C_Address_2 0x4C ; LCD 2
; ----- Constants
' DS18B20 port settings - this is required
#define DQ PortC.3
'''Set LCD_10 to 10 for the YwRobot LCD1602 IIC V1 or the Sainsmart LCD_PIC I2C
adapter
'''Set LCD_10 to 12 for the Ywmjkdz I2C adapter with pot bent over top of chip
; ----- Variables
dim TempC_100 as word ' a variabler to handle the temperature calculations
dim DSdataRaw as Integer
ccount = 0
Do forever
' The function readtemp12 returns the raw value of the sensor.
' The sensor is read as a 12 bit value therefore each unit equates to 0.0625 of a
degree
DSdataRaw = readtemp12 ; save to this variable to prevent the delay bewtween
screen up dates
' The function readtemp returns the integer value of the sensor
DSdata = readtemp
358
DSdata = DSdataRaw ; Set the data
LCD_I2C_Address_Current = LCD_I2C_Address_1: DisplayInformation ( 3 ) ; update
LCD1
DSdata= DSdataRaw ; Set the data
LCD_I2C_Address_Current = LCD_I2C_Address_2: DisplayInformation ( 3 ) ; update
LCD2
ccount++
wait 1 s
loop
End
Case 1
CLS
print "GCBasic 2015"
locate 1,0
print "DS18B20 Demo"
Case 2
' Display the integer value of the sensor on the LCD
locate 0,0
print hex(ccount)
print " Ceil"
locate 0,8
print DSdata
print chr(223)+"C"+" "
Case 3
' Display the integer and decimal value of the sensor on the LCD
Positive:
359
TempC_100 = TempC_100 + DSdata
DisplayTemp:
locate 1,0
print hex(ccount)
print " Real"
locate 1,8
print str(Whole)
print "."
' To ensure the decimal part is two digits
Dig = Fract / 10
print Dig
Dig = Fract % 10
print Dig
print chr(223)
print "C"+" "
End Select
end sub
360
Pulse width modulation
This is the Pulse width modulation section of the Help file. Please refer the sub-sections for details
using the contents/folder view for the MicroChip PIC PWM capabilities and the ATMEL AVR PWM
capabilities.
Introduction:
The methods described in this section allow the generation of Pulse Width Modulation (PWM) signals.
PWM signals enables the microcontroller to control items like the speed of a motor, or the brightness
of a LED or lamp.
The methods can also be used to generate the appropriate frequency signal to drive an infrared LED
for remote control applications.
Great Cow BASIC support the four different method shown below:
- Two methods use the microcontroller CCP module
- One method uses the microcontroller PWM module, and
- One method is a software emulation of PWM.
Using PWM with the CCP module: This option requires a CCP module within the microcontroller.
Hardware PWM is only available through the "CCP" or "CCPx" pin. This is a hardware limitation of
Microchip PIC microcontrollers.
Microcontrollers with PPS can change the pin - use the PPS tool to set the desired output pin.
Using microcontroller PWM module. This option requires a PWM module within the microcontroller
Microcontrollers with PPS can change the pin - use the PPS tool to set the desired output pin.
361
Hardware PWM using the CCP1 in fixed mode
The fixed mode can use CCP1 only, and, the parameters of the PWM cannot be dynamically changed in
the user program. The parameters are fixed by the definition of two constants.
HPWMOn
wait 5 s
HPWMOff
Software PWM
The PWM parameters for duty and the number of pulses can be changed dynamically in the user
program.
The PWM is only operational for the number of cycles stated in the calling method.
'A call to use the software PWM on the specific port, with a duty of 127 for 100
cycles
; ----- Constants
'PWM constant. This is a required constant.
#define PWM_Out1 portb.0
Relevant Constants:
A number of constants are used to control settings for the PWM hardware module of the
microcontroller. To set them, place a line in the main program file that uses #define to assign a value to
the particular constant.
362
PWM Software Mode
Syntax:
Command Availability:
Available on all microcontrollers. This method does NOT require a PWM module within the
microcontroller.
This command uses a software PWM routine within Great Cow BASIC to produce a PWM signal on the
selected port of the chip.
The method PWMOut does not make use of any special hardware within the microcontroller. The PWM
signal is generated only while the PWMOut command is executing - therefore, when the PWMOut is not
executing by moving onto the next command, the PWM signal will stop.
PWMOut
Syntax:
Command Availability:
Available on all microcontrollers. This method does NOT require a PWM module within the
microcontroller.
This command uses a software PWM routine within Great Cow BASIC to produce a PWM signal on the
selected port of the chip.
The method PWMOut does not make use of any special hardware within the microcontroller. The PWM
signal is generated only while the PWMOut command is executing - therefore, when the PWMOut is not
executing by moving onto the next command, the PWM signal will stop.
Explanation :
channel sets the channel that the PWM is to be generated on. This must have been defined previously
by setting the constants PWM_Out1
duty cycle specifies the PWM duty cycle, and ranges from 0 to 255. 255 corresponds to 100%, 127 to
363
50%, 63 to 25%, and so on.
cycles is used to set the amount of PWM pulses to supply. This is useful for situations in which a pulse
of a specific length is required.
The formula for calculating the time taken for one cycle is:
where:
Example 1 :
364
'This program controls the brightness of an LED on PORTB.0
'using the software PWM routine and a potentiometer.
#chip 16f877a, 20
; ----- Constants
'PWM constant. This is a required constant.
#define PWM_Out1 portb.0
; ----- Variables
' No Variables specified in this example.
end
Example 2 :
365
'This program controls the brightness of an LED on gpio.1
'using the software PWM routine and a potentiometer.
#chip 12f675, 4
; ----- Constants
'PWM constant. This is a required constant.
#define PWM_Out1 gpio.1
; ----- Variables
' No Variables specified in this example.
HPWM CCP
Syntax:
Command Availability:
This command supports the CCP 8 bit support with Timer 2 only.
For CCP/PWM support for timers 2, 4 and 6, if the specific devices supports alternative CCP/PWM clock
sources - see here HPWM_CCPTimerN
For PWM 10 Bit support with other timers - see here HPWM 10 Bit
Explanation:
This command sets up the hardware PWM module of the Microchip PIC microcontroller to generate a
PWM waveform of the given frequency and duty cycle.
366
If you only need one particular frequency and duty cycle, you should use PWMOn and the constants
PWM_Freq and PWM_Duty instead.
channel is 1, 2, 3, 4 or 5, and corresponds to the pins CCP1, CCP2, CCP3, CCP4 and CCP5 respectively. On
chips with only one CCP port, pin CCP or CCP1 is always used, and channel is ignored. (It should be set
to 1 anyway to allow for future upgrades to more powerful microcontrollers.)
frequency sets the frequency of the PWM output. It is measured in KHz. The maximum value allowed is
255 KHz. The minimum value varies depending on the clock speed. 1 KHz is the minimum on chips 16
MHz or under and 2 Khz is the lowest possible on 20 MHz chips. In situations that do not require a
specific PWM frequency, the PWM frequency should equal approximately 1 five-hundredth the clock
speed of the microcontroller (ie 40 Khz on a 20 MHz chip, 16 KHz on an 8 MHz chip). This gives the best
duty cycle resolution possible.
duty cycle specifies the desired duty cycle of the PWM signal, and ranges from 0 to 255 where 255 is
100% duty cycle.
To stop the PWM signal use the HPWMOff method with the parameter of the channel.
The optional constant HPWM_FAST can be defined to enable the recalculation of the timer prescaler when
needed. This will provide faster operation, but uses extra byte of RAM and may cause problems if HPWM
and PWMOn are used together in a program. This will not cause any issue when using HPWM and PWMOff in
the same program with HPWM_FAST.
The optional constant DisableCCPFixedModePWM can be defined to prevent Timer2 from being enabled.
This constant may be required when you need to use Timer2 for non-CCP/PWM support.
Example:
367
'This program will alter the brightness of an LED using
'CCP/PWM.
'Main code
do
'Turn up brightness over the range
For Bright = 1 to 255
HPWM 1, 40, Bright
wait 10 ms
next
'Turn down brightness over the range
For Bright = 255 to 1
HPWM 1, 40, Bright
wait 10 ms
next
loop
HPWMOff
Syntax:
Command Availability:
Explanation:
This command will disable the output of the CCP1/PWM module on the Microchip PIC chip.
Example:
368
'Select chip model and speed
#chip 16F877A, 20
'Main code
do
'Turn up brightness over 2.5 seconds
For Bright = 1 to 255
HPWM 1, 40, Bright
wait 10 ms
next
wait 1 s
HPWMOff ( 1 )' turn off the PWM channel
loop
HPWM_CCPTimerN
Syntax:
Command Availability:
This command supports the CCP 8 bit support with selectable Timer 2, 4 or 6 only for CCP/PWM only.
Explanation:
This command sets up the hardware PWM module of the Microchip PIC microcontroller to generate a
PWM waveform of the given frequency and duty cycle.
If you only need one particular frequency and duty cycle, you should use PWMOn and the constants
PWM_Freq and PWM_Duty instead.
channel is 1, 2, 3, 4 or 5, and corresponds to the pins CCP1, CCP2, CCP3, CCP4 and CCP5 respectively. On
chips with only one CCP port, pin CCP or CCP1 is always used, and channel is ignored. (It should be set
to 1 anyway to allow for future upgrades to more powerful microcontrollers.)
369
frequency sets the frequency of the PWM output. It is measured in KHz. The maximum value allowed is
255 KHz. The minimum value varies depending on the clock speed. 1 KHz is the minimum on chips 16
MHz or under and 2 Khz is the lowest possible on 20 MHz chips. In situations that do not require a
specific PWM frequency, the PWM frequency should equal approximately 1 five-hundredth the clock
speed of the microcontroller (ie 40 Khz on a 20 MHz chip, 16 KHz on an 8 MHz chip). This gives the best
duty cycle resolution possible.
duty cycle specifies the desired duty cycle of the PWM signal, and ranges from 0 to 255 where 255 is
100% duty cycle.
To stop the PWM signal use the HPWMOff method with the parameter of the channel.
Example:
#chip 16F1825, 4
initialisation:
'Command as follows:
' HPWM_CCPTimerN CCP_Channel, Frequency, Duty, Timer Source. Timer source defaults
to timer 2, so, the timersource is optional.
do
loop
HPWMOff
Syntax:
370
HPWMOff ( channel ) 'selectively turn off the CCP channel
Command Availability:
Explanation:
This command will disable the output of the CCP1/PWM module on the Microchip PIC chip.
Example:
'Main code
do
'Turn up brightness over 2.5 seconds
For Bright = 1 to 255
HPWM 1, 40, Bright
wait 10 ms
next
wait 1 s
HPWMOff ( 1 )' turn off the PWM channel
loop
HPWM 10 Bit
Syntax:
Command Availability:
Only available on Microchip PIC microcontrollers with the 10-bit PWM module.
371
For the Capture/Compare/PWM (CCP) module, see here HPWM CCP
Explanation:
This command sets up the hardware PWM module of the Microchip PIC microcontroller to generate a
PWM waveform of the given frequency and duty cycle. Once this command is called, the PWM will be
emitted until PWMOff is called.
frequency sets the frequency of the PWM output. It is measured in KHz. The maximum value allowed is
255 KHz. The minimum value varies depending on the clock speed. 1 KHz is the minimum on chips 16
MHz or under and 2 Khz is the lowest possible on 20 MHz chips. In situations that do not require a
specific PWM frequency, the PWM frequency should equal approximately 1 five-hundredth the clock
speed of the microcontroller (ie 40 Khz on a 20 MHz chip, 16 KHz on an 8 MHz chip). This gives the best
duty cycle resolution possible.
duty cycle specifies the desired duty cycle of the PWM signal, and ranges from 0 to 1023 where 1023 is
100% duty cycle. This should be a WORD value. Note: Byte values are supported as a Byte value is
factorised to a Word value. To use a Byte value and to ensure the 10-bit resolution you should cast the
parameter as a Word, [WORD]byte_value or [WORD]constant_value
Optional resolution specifies the desired resolution to be used. These can be either 255 or 1023. The
rational of this optional parameter is to support the duty cycle with a BYTE or a WORD range. If you
call the method with a WORD the resolution will be set to 1023.
Notes:
PWM channels 1 and 2 are disable by default. You must enable using the constants USE_HPWMn
where n is the PWM channel you want to enable. You can disable any PWM channel by setting the
appropiatge change to FALSE.
On some microcontrollers you may need to set the port.pin as an output for PWM to operated as
desired.
372
Example 1:
Sub InitPPS
'Module: PWM6
RA2PPS = 0x000E 'PWM6OUT > RA2
End Sub
'Template comment at the end of the config file
'Main code
do
'Turn up brightness over the range
For Bright = 0 to 1023
HPWM 6, 40, Bright, 2
wait 10 ms
next
'Turn down brightness over the range
For Bright = 1023 to 0
HPWM 6, 40, Bright, 2
wait 10 ms
next
loop
Example 2:
373
'This program will alter the brightness of an LED using
'hardware PWM.
Sub InitPPS
'Module: PWM3
RA2PPS = 0x000E 'PWM3OUT > RA2
End Sub
'Template comment at the end of the config file
'Main code
do
'Turn up brightness over the range
For Bright = 0 to 1023
HPWM 3, 40, Bright, 2
wait 10 ms
next
'Turn down brightness over the range
For Bright = 1023 to 0
HPWM 3, 40, Bright, 2
wait 10 ms
next
loop
HPWMUpdate
Syntax:
374
HPWMUpdate ( channel, duty_cycle )
Command Availability:
Explanation:
• You MUST have previously called the HPWM 10 Bit command using the full command (see HPWM
10 Bit) to set the channel specific settings for frequency and timer source. See the example below
for the usage.
• You MUST have previously called the HPWM 10 Bit command with the same type of variable, or,
use casting to ensure the variable tpye is the same type.
This command only supports the previously called HPWM 10 Bit command, or, if you have set more
than one HPWM 10 Bit PWM channel then to use the command you must have set the channel to the
same frequency.
The command only supports the hardware PWM module of the Microchip PIC microcontroller to
generate a PWM waveform at the previously defined frequency and timer source.
duty cycle specifies the desired duty cycle of the PWM signal, and ranges from 0 to 1023 where 1023 is
100% duty cycle.
Example:
375
'This program will alter the brightness of an LED using
'hardware PWM.
Sub InitPPS
'Module: PWM6
RA2PPS = 0x000E 'PWM6OUT > RA2
End Sub
'Template comment at the end of the config file
'Setup PWM - this is mandated as this specifies the frequency and the clock source.
'Uses casting [word] to ensure the intialisation value of Zero (0) is a treated as a
word. The variable type MUST match the HPWMUpdate variable type.
HPWM 6, 40, [word]0, 2
'Main code
do
'Turn up brightness over 2.5 seconds
For Bright = 0 to 1023
HPWMUpdate 6, Bright
wait 10 ms
next
'Turn down brightness over 2.5 seconds
For Bright = 1023 to 0
HPWMUpdate 6, Bright
wait 10 ms
next
loop
376
HPWMOff
Syntax:
Command Availability:
Explanation:
This command will disable the output of the PWM module on the Microchip PIC chip.
Example:
377
'This program will alter the brightness of an LED using
'hardware PWM.
Sub InitPPS
'Module: PWM6
RA2PPS = 0x000E 'PWM6OUT > RA2
End Sub
'Template comment at the end of the config file
'Main code
For ForLoop = 1 to 4
'Turn up brightness over 2.5 seconds
For Bright = 1 to 255
HPWM 6, 40, Bright, 2
wait 10 ms
next
'Turn down brightness over 2.5 seconds
For Bright = 255 to 1
HPWM 6, 40, Bright, 2
wait 10 ms
next
next
HPWMOff 6, PWMHardware 'where PWMHardware is the defined constant or you can use
TRUE
HPWM 16 Bit
Syntax:
378
HPWM16 channel, frequency, duty cycle 'Enable a 16-bit PWM channel'
Command Availability:
Only available on Microchip PIC microcontrollers with the 16-bit PWM module. 16-bit PWM support
includes both dynamic mode and fixed mode operations. See the examples below for usage.
For the Capture/Compare/PWM (CCP) module or the 10-bit PWM module, see the other sections of the
Help.
Explanation:
This command sets up the hardware PWM module of the Microchip PIC microcontroller to generate a
PWM waveform of the given frequency and duty cycle. Once this command is called, the PWM will be
emitted until HPWM16Off method is called.
channel is 1, 2, 3.. 12. These corresponds to the 16-bit PWM channel respectively.
The 16-bit PWM channel MUST be supported by the microcontroller. Check the microcontroller
specific datasheet for the available channel.
frequency sets the frequency of the PWM output. It is measured in KHz. The maximum value allowed is
0xFFFF. The minimum value varies depending on the clock speed. 1 KHz is the minimum on chips 16
MHz or under and 2 Khz is the lowest possible on 20 MHz chips. In situations that do not require a
specific PWM frequency, the PWM frequency should equal approximately 1 five-hundredth the clock
speed of the microcontroller (ie 40 Khz on a 20 MHz chip, 16 KHz on an 8 MHz chip). This gives the best
duty cycle resolution possible.
duty cycle specifies the desired duty cycle of the PWM signal, and ranges from 0 to 0xFFFF where
0xFFFF is 100% duty cycle. This should be a WORD value.
Example 1:
379
' This program will enable dynamic mode PWM signals
'
' All the 12 PWM16 channels can configured at seperate dynamic frequencies dynamic
duty, the syntax is:
'
' HPWM16(xx, frequency, duty )
'
' xx can be 1 through 12, for this specifc microcontroller there are three PWM16
channels.
'
' To set the parameters of Great Cow BASIC PWM fixed mode for the channels use the
commands shown below::
#chip 12F1572, 32
#config mclr=on
do Forever
loop
The 16-bit library also supports fixed mode PWM operations. The following two examples show the
constants and the commands to control 16-bit PWM Fixed Mode operations.
Example 2:
380
' This program will enable fix mode PWM signals
'
' All the 12 PWM16 channels can configured at seperate fixed frequencies and fixed
duty, the syntax is:
'
' #define HPWM16_xx_Freq 38 'Set frequency in KHz on channel xx
' #define HPWM16_xx_Duty 50 'Set duty cycle to 50% on channel xx
'
' xx can be 1 through 12
'
' To set the parameters of Great Cow BASIC PWM fixed mode on channel 1 use the
following:
'
' #define HPWM16_1_Freq 0.1 to > 1000 'Set the frequency, but, the clock
speed must be low for low PWM frequency
' #define HPWM16_1_Duty 0.1 to 100 'Set duty cycle as percentage 0-
100%, just change the number
'
#chip 12F1572, 32
#config mclr=on
do Forever
loop
Example 3:
381
' This program will enable fix mode PWM signals
'
' All the 12 PWM16 channels can configured at seperate fixed frequencies and fixed
duty, the syntax is:
'
' #define HPWM16_xx_Freq 38 'Set frequency in KHz on channel xx
' #define HPWM16_xx_Duty 50 'Set duty cycle to 50% on channel xx
'
' xx can be 1 through 12, for this specifc microcontroller there are three PWM16
channels.
'
' To set the parameters of Great Cow BASIC PWM fixed mode for the three channels use
the following:
#chip 12F1572, 32
#config mclr=on
do Forever
loop
Syntax:
382
PWMOn 'only applies to CCP/PWM channel 1
'or
PWMOff
PWMOn( channel ) 'where the parameter can be any valid CCP/PWM channel, 1,
2, 3, 4 or 5
'or
PWMOff( channel )
Command Availability:
See here HPWM CCP for the method to change PWM parameters dynamically or to use other CCP
channels - this method support CCP1/PWM, CCP2/PWM, CCP3/PWM, CCP4/PWM and CCP5/PWM.
Explanation:
This command sets up the hardware PWM module of the Microchip PIC microcontroller to generate a
PWM waveform of the given frequency and duty cycle. Once this command is called, the PWM will be
emitted until PWMOff is called.
These constants are required to set the parameters for the PWM. The frequency and the duty applies
to all channels when using the method(s) or macro(s) shown above.
PWM_Duty Sets the duty cycle of the PWM module output. Given as percentage. 50
For CCP/PWM modules are also supported using a call to a method or a macro, as follows:
383
Method/Macro Controls Default Value
To set the Fixed Mode PWM for PWM Modules you need to set a timer frequency, a PWM module cycle
and the PWM model source clock.
The options for source clock are shown below. These are the PWM timers supported by the PWM
modules, where nn is the frequency.
PWM_Timer2_Freq `nn` or
PWM_Timer4_Freq `nn` or
PWM_Timer6_Freq `nn`.
The PWM module duty is set using PWM_`yy`_Duty xx' where `yy is between 1 and 9 and is a valid
PWM module, and, xx is the Duty cycle for specific channels
#define PWM_yy_Duty xx
The PMW module clock source us PWM_`zz`_Clock_Source tt. Where zz is channel and tt is the PWM
clock source.
#define PMW_zz_Clock_Source tt
You do not need to define all the timers and or all the channels, just define the constants you need.
The minimum is A timer with a frequency A PWM channel with a duty A PWM channel clock
source
384
Example: For PWM channel 6 with a frequency of 38Khz with a duty of 50% with a clock source of
timer 2, use
#define PWM_Timer2_Freq 38
#define PWM_7_Duty 50
#define PMW_7_Clock_Source 6
#define PWM_1_Duty 10 'Set duty cycle as percentage 0-100%, just change the
number
#define PMW_1_Clock_Source 2
#define PWM_2_Duty 20
#define PMW_2_Clock_Source 4
#define PWM_3_Duty 30
#define PMW_3_Clock_Source 6
#define PWM_4_Duty 40
#define PMW_4_Clock_Source 2
#define PWM_5_Duty 50
#define PMW_5_Clock_Source 6
#define PWM_6_Duty 60
#define PMW_6_Clock_Source 6
#define PWM_7_Duty 70
#define PMW_7_Clock_Source 4
385
#define PWM_8_Duty 80
#define PMW_8_Clock_Source 4
#define PWM_9_Duty 90
#define PMW_9_Clock_Source 6
Example #1:
#chip 16f877a,20
'Main code
wait 10 s 'Wait 10 s
do
loop
Example #2:
386
#chip 16f877a,20
'Main code
wait 10 s 'Wait 10 s
do
loop
Example #3:*
'A real simple and easy PWM setup for 8 and 10 bit PWM channels
#chip 18f25k42, 16
#startup InitPPS, 85
Sub InitPPS
'Module: PWM5
RA0PPS = 0x000D 'PWM5 > RA0
'Module: PWM6
RA1PPS = 0x000E 'PWM6 > RA1
'Module: PWM7
RA2PPS = 0x000F 'PWM7 > RA2
'Module: PWM8
RA3PPS = 0x0010 'PWM8 > RA3
End Sub
387
'This is the setup section for fixed mode PWM
#define PWM_5_Duty 50
#define PMW_5_Clock_Source 6
#define PWM_6_Duty 60
#define PMW_6_Clock_Source 6
#define PWM_7_Duty 70
#define PMW_7_Clock_Source 4
388
#define PWM_8_Duty 80
#define PMW_8_Clock_Source 4
End
For more help, see PWMOn and PWMOff or, for AVR see Fixed Mode PWM for AVR
PWMOn
Syntax:
PWMOn
389
Command Availability:
Explanation:
This command will enable the output of the CCP1/PWM module on the Microchip PIC microcontroller.
Example:
'This program will enable a 76 Khz PWM signal, with a duty cycle
'of 80%. It will emit the signal for 10 seconds, then stop.
#define PWM_Freq 76 'Set frequency in KHz
#define PWM_Duty 80 'Set duty cycle to 80 %
PWMOn 'Turn on the PWM
WAIT 10 s 'Wait 10 seconds
PWMOff 'Turn off the PWM
PWMOff
Syntax:
PWMOff
Command Availability:
Explanation:
This command will disable the output of the CCP1/PWM module on the Microchip PIC chip.
Example:
390
'This program will enable a 76 Khz PWM signal, with a duty cycle
'of 80%. It will emit the signal for 10 seconds, then stop.
#define PWM_Freq 76 'Set frequency in KHz
#define PWM_Duty 80 'Set duty cycle to 80 %
PWMOn 'Turn on the PWM
WAIT 10 s 'Wait 10 seconds
PWMOff 'Turn off the PWM
For compatibility all channels are supported by default. This is maintains backward compatibility.
To mimise the code, use the following to disable support for a specific Capture/Compare/PWM (CCP)
module, timers or the PWM module.
Setting a constant to FALSE will remove the support of the capability from the method and therefore
will reduce the program size.
To further mimise the code, use the following to disable support for a specific PWM channels. Only
PWM channels 5, 6 and 7 are supported.
To further mimise the code, use the following to disable support for a specific timers.
391
Example
This will save 335 bytes of program memory by removing support for CCP1, CCP2 and CCP4.
#chip 16f18855,32
#Config FEXTOSC_OFF, RSTOSC_HFINT32
#Config WRT_OFF, CPD_ON, MCLRE_ON
UNLOCKPPS
RC2PPS = 0x0A 'RC2->CCP2:CCP2;
LOCKPPS
'Setting the port an output is VERY important... LED will not work if you do not set
as an output.
dir portC.2 out ; CCP2
do forever
For Bright = 1 to 255
HPWM 2, 40, Bright
wait 10 ms
next
loop
Introduction:
The methods described in this section allow the generation of Pulse Width Modulation (PWM) signals.
PWM signals enables the microcontroller to control items like the speed of a motor, or the brightness
of a LED or lamp.
The methods can also be used to generate the appropriate frequency signal to drive an infrared LED
for remote control applications.
The AVR devices use a Timer/Counter and OCRnx module that has a variable period register. The
Hardware PWM is available through the OCnx pin.
392
The method uses three parameters to setup the HPWM.
Relevant Constants:
A number of constants are used to control settings for PWM hardware module of the microcontroller.
To set them, place a line in the main program file that uses #define to assign a value to the particular
constant.
Syntax:
Command Availability:
The HPWM command is available on Atmel AVR microcontrollers with an OCnx pin, and is compatible
with the PIC HPWM command method. Due to the the unique way of AVR PWM implementation, and
code efficiency, there are some notable differences in the HPWM initialization and its use.
This command supports the Fast PWM Mode and period registers for their respective devices.
Typically Timer0 and Timer2 do not have a period register and the "A" channel is sacrificed to provide
that function. Therefore, channel 1 and channel 6 will not be available, but are documented for
possible future use. Some device Timers do not have an adjustable period register, so this command is
not feasible (consult the datasheet).
Explanation:
The HPWM command sets up the hardware PWM module of the Atmel AVR microcontrollers to
generate a PWM waveform of the given frequency and duty cycle. Once this command is called, the
PWM will be emitted until the duty cycle parameter is written to zero.
If the need is just one particular frequency and duty cycle, one should use PWMOn and the constants
PWM_Freq and PWM_Duty instead. PWMOn for the AVR is uniquely assigned to the OC0B pin, or
channel 2. PWMOff will only shutdown the AVR HPWM channel 2.
393
channel described as 1, 2, 3,…16 correspond to the pins OCR0A, OCR0B….OCR5C as detailed in the
channel constant table. Channel 1 and channel 6 are not available.
frequency sets the frequency of the PWM output measured in Khz. The maximum value allowed is 255
KHz. In situations that do not require a specific PWM frequency, the PWM frequency should equal
approximately 4 times the clock speed (GCB chipMHz) of the microcontroller (ie 63 KHz on a 16 MHz
chip, 32 KHz on 8 MHz, 4 Khz on 1 MHz). This gives the best duty cycle resolution possible. Alternate
frequencies with good duty cycle resolution are 1Khz, and 4Khz with chipMhz values of 16 and 8
respectively.
duty cycle specifies the desired duty cycle of the PWM signal, and ranges from 0 to 255 where 255 is
100% duty cycle. The AVR fast PWM mode has a small spike at the extreme setting of 0x00, on most
devices, with each period register rollover. By using the HPWM command, and writing 0x00 to the
duty cycle parameter, the PWM signal will shutdown completely and avoid the spike. The PWM signal
can then be restarted again with a new HPWM command.
Note: Due to the AVR having a timer prescaler of just 1, 8, and 64; the AVR frequency and duty cycle
resolution will be different from the PIC frequency and duty cycle resolution. The AVR HPWM
parameters will likely need adjusting ,when substituted into an existing PIC program, and where
accuracy is required.
HPWM Constants:
The AVR HPWM timer constants for channel number control are shown in the table below. Each timer
constant needs to be defined for any one of the channels it controls.
AVRTC4 Specifies AVR TC4 associated with channel 11,12, and 13 Must be
defined
AVRTC5 Specifies AVR TC5 associated with channel 14, 15, and 16 Must be
defined
The Great Cow BASIC HPWM channel constants for output pin control are shown in the table below.
Each HPWM channel used needs to be defined. The Port pin associated with each OCnx must be set to
394
output.
AVRCHAN1 Specifies AVR HPWM channel 1 to the associated output pin OC0A N/A
OCR0A is used as period register and thus not available
AVRCHAN2 Specifies AVR HPWM channel 2 to the associated output pin OC0B Must be
defined
AVRCHAN3 Specifies AVR HPWM channel 3 to the associated output pin OC1A Must be
MUX’d with OC0A pin on some ATTiny’s defined
AVRCHAN4 Specifies AVR HPWM channel 4 to the associated output pin OC1B Must be
defined
AVRCHAN5 Specifies AVR HPWM channel 5 to the associated output pin OC1C Must be
On some larger pinout devices and MUX’d with OC0A pin defined
AVRCHAN6 Specifies AVR HPWM channel 6 to the associated output pin OC2A N/A
OCR2A is used as a period register and thus not available
AVRCHAN7 Specifies AVR HPWM channel 7 to the associated output pin OC2B Must be
defined
AVRCHAN8 Specifies AVR HPWM channel 8 to the associated output pin OC3A Must be
defined
AVRCHAN9 Specifies AVR HPWM channel 9 to the associated output pin OC3B Must be
defined
AVRCHAN10 Specifies AVR HPWM channel 9 to the associated output pin OC3C Must be
defined
AVRCHAN11 Specifies AVR HPWM channel 11 to the associated output pin OC4A Must be
defined
AVRCHAN12 Specifies AVR HPWM channel 12 to the associated output pin OC4B Must be
defined
AVRCHAN13 Specifies AVR HPWM channel 13 to the associated output pin OC4C Must be
defined
AVRCHAN14 Specifies AVR HPWM channel 14 to the associated output pin OC5A Must be
defined
AVRCHAN15 Specifies AVR HPWM channel 15 to the associated output pin OC5B Must be
defined
AVRCHAN16 Specifies AVR HPWM channel 16 to the associated output pin OC5C Must be
defined
Example:
395
'Using HPWM command to alternate ramping leds with the UNO board
#chip mega328,16
'************pwm************************
'Must define AVRTCx, AVRCHANx, and set OCnX pin dir to out
do
loop
396
HPWM Fixed Mode for AVR
Syntax:
PWMOn
'or
PWMOff
Command Availability:
This command is only available on the Atmel AVR microcontrollers with a Timer/Counter0 OC0B
register.
Explanation:
The PWMOn command will only enable the output of the OC0B/PWM module of the Atmel AVR
microcontroller.
This command sets up the hardware PWM module of the Atmel AVR microcontroller to generate a
PWM waveform of the given frequency and duty cycle. Once PWMON method is called, the PWM will
be emitted until PWMOff is called.
PWM_Duty Sets the duty cycle of the PWM module output. Given as percentage. 50
Example:
397
'This program demonstrates the PWMOn and PWMOff commands
'of the fixed mode HPWM on OC0B pin.
#chip mega328p,16
#define PWM_Freq 40
#define PWM_Duty 50
do
loop
For more help, see PWMOn and PWMOff or, for Microchip microcontrollers see Fixed Mode PWM
for Microchip
Syntax:
PWMOn
Command Availability:
This command is only available on the Atmel AVR microcontrollers with a Timer/Counter0 OC0B
register.
Explanation:
The PWMOn command will only enable the output of the OC0B/PWM module of the Atmel AVR
microcontroller.
398
This command is not available for any other OCnx/PWM modules.
Example:
#chip mega328p,16
#define PWM_Freq 40
#define PWM_Duty 50
do
loop
Syntax:
PWMOff
Command Availability:
This command is only available on the Atmel AVR microcontrollers with a Timer/Counter0 OC0B
register.
Explanation:
399
The PWMOff command will only disable the output of the OC0B/PWM module of the Atmel AVR
microcontrollers.
Example:
#chip mega328p,16
#define PWM_Freq 40
#define PWM_Duty 50
do
loop
400
Random Numbers
This is the Random Numbers section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Overview
Introduction:
The generator uses a 16 bit linear feedback shift register to produce pseudo-random numbers. The
most significant 8 bits of the LFSR are used to provide an 8 bit random number.
When compiling a program, Great Cow BASIC will generate an initial seed for the generator. However,
this seed will be the same every time the program runs, so the sequence of numbers produced by a
given program will always be the same. To work around this, there is a Randomize subroutine. It can
be provided with a new seed for the generator (which will cause the generator to move to a different
point in the sequence). Alternatively, Randomize can be set to obtain a seed from some other source
such as a timer every time it is run.
Relevant Constants:
These constants are used to control settings for the random number generation. To set them, place a
line in the main program file that uses #define to assign a value to the particular constant.
Example:
Random
Syntax:
var = Random
Command Availability:
401
Explanation:
The Random function will generate a pseudo-random number between 0 and 255 inclusive.
The numbers generated by Random will follow the same sequence every time, until Randomize is used.
Example:
Randomize
Syntax:
Randomize
Randomize seed
Command Availability:
Explanation:
Randomize is used to seed the pseudo random number generator, so that it will produce a different
sequence of numbers each time it is used.
The random number generator in Great Cow BASIC is a 16 bit linear feedback shift register, which is
explained here: http://en.wikipedia.org/wiki/Linear_feedback_shift_register
Generally, you will get the same sequence every time it is used. However, you can seed it so that it will
start at a different point at the sequence using the Randomize command.
If you wanted to use an analog reading to seed the generator, this would work:
402
Randomize ReadAD10(AN0)
If no seed is specified, then the RANDOMIZE_SEED constant will be used as the seed. If seed is
specified, then it will be used to seed the generator.
It is important that the seed is different every time that Randomize is used. If the seed is always the
same, then the sequence of numbers will always be the same. It is best to use a running timer, an
input port, or the analog to digital converter as the source of the seed, since these will normally
provide a different value each time the program runs.
Example:
403
7-Segment Displays
This is the 7-Segment Displays section of the Help file. Please refer the sub-sections for details using
the contents/folder view.
Introduction
The Great Cow BASIC 7 segment display methods make it easier for Great Cow BASIC programs to
display numbers and letters on 7 segment LED displays.
The Great Cow BASIC methods support up to four digit 7 segment display devices, common
anode/cathode and inversion of the port logic to support driving the device(s) via a transistor.
There are three ways that the 7 segment display routines can be set up.
Metho Description
d
1 Connect the microcontroller to the 7 segment display (via suitable resistors) using any eight
output bits. Use DISP_SEG_x and DISP_SEL_x constants to specify the outpout ports and the
select port(s) to be used.
2 Connect the microcontroller to the 7 segment display (via suitable resistors) using whole port
(8 bits) of the microcontroller. This implies the connections are consectutive in terms of the 8
output bits of the port. Use the DISPLAYPORTn and DISPSELECTn constants to specify the whole
port and the select port(s) to be used. This method will generate the most efficient code.
3 Connect the microcontroller to the 7 segment display (via suitable resistors) using whole port
(8 bits) of the microcontroller. This implies the connections are consectutive in terms of the 8
output bits of the port. Use the DISPLAYPORTn and DISP_SEL_n constants to specify the whole
port and the select port(s) to be used.
The Great Cow BASIC methods assume the 7 segment display(s) is to be connected to a common
parallel bus with a Common Cathode. See the sections Common Cathode and Common Anode for
examples of using Great Cow BASIC code to control these different configurations
Shown below are the differing constants that must be set for the three connectivity options.
1 DISP_SEG_x
&
DISP_SEL_x
404
Ind Method Descr Constants Default Value
ex iptio
n
DISP_S Specifies the output pin (output bit) Must be specified to use this
EG_x used to control a specific segment of connectivity option.
the 7 segment display. There are seven
constants that must be specified.
DISP_SEG_A through DISP_SEG_G. One
must be set for each segment. Typical
commands are: #define DISP_SEG_A
portA.0 #define DISP_SEG_B portA.1
#define DISP_SEG_C portA.2 #define
DISP_SEG_D portA.3 #define DISP_SEG_E
portA.4 #define DISP_SEG_F portA.5
#define DISP_SEG_G portA.6
DISP_S Specifies the output pin (output bit) Optional.
EG_DOT used to control the decimal point on
the 7 segment display. Typical
commands are: #define DISP_SEG_DOT
portA.7
DISP_S Specifies the output pin (output bit) A valid output pin (output bit) must be
EL_x used to control a specific 7 segment specified. Must be specified to use
display. These constants are used to this connectivity option.
control the specific 7 segment display
being addresses. Typical commands
are: #define DISP_SEL_1 portA.0
#define DISP_SEL_2 portA.1
2 DISPLAYPOR
Tn &
DISPSELECT
n
DISPLA Specifies the output port used to Can be DISPLAYPORTA and/or
YPORTn control the 7 segment display. Port.bit DISPLAYPORTB and/or DISPLAYPORTC
>> Segment port.0 >> A port.1 >> and/or DISPLAYPORTD Where
B port.2 >> C port.3 >> D port.4 DISPLAYPORTn can be A, B, C or D which
>> E port.5 >> F port.6 >> G corresponding to displays 1, 2, 3 and
4, respectively. Must be specified to
use this connectivity option.
DISPSE Specifies the output command used to Can be DISPSELECTA and/or DISPSELECTB
LECTn select a specific 7 segment display and/or DISPSELECTC and/or DISPSELECTD
addressed by DISPLAYPORT_n. Used to Must be specified to use this
control output pin (output bit) when connectivity option.
several displays are multiplexed.
Typical commands are: #define
DispSelectA Set portA.0 on #define
DispSelectB Set portA.1 on
405
Ind Method Descr Constants Default Value
ex iptio
n
3 DISPLAYPOR
Tn &
DISP_SEL_n
DISPLA Specifies the output port used to Can be DISPLAYPORTA and/or
YPORTn control the 7 segment display. Port.bit DISPLAYPORTB and/or DISPLAYPORTC
>> Segment port.0 >> A port.1 >> and/or DISPLAYPORTD Where
B port.2 >> C port.3 >> D port.4 DISPLAYPORTn can be A, B, C or D which
>> E port.5 >> F port.6 >> G corresponding to displays 1, 2, 3 and
4, respectively. Must be specified to
use this connectivity option.
DISP_S Specifies the output command used to Must be specified to use this
EL_n select a specific 7 segment display connectivity option. Can be specified
addressed by DISPLAYPORTn. Typical as DISP_SEL_1 and/or DISP_SEL_2
commands are: #define DISP_SEL_1 and/or DISP_SEL_3 and/or DISP_SEL_4
portA.0 #define DISP_SEL_2 portA.1
Example 1:
#chip 16F886, 8
406
#define DISP_SEG_A PORTB.0
#define DISP_SEG_B PORTB.1
#define DISP_SEG_C PORTB.2
#define DISP_SEG_D PORTB.3
#define DISP_SEG_E PORTB.4
#define DISP_SEG_F PORTB.5
#define DISP_SEG_G PORTB.6
#define DISP_SEG_DOT PORTB.7 '' available on some displays as dp or colon
end Repeat
Wait 100 ms
Next
Loop
407
Also, see DisplayChar, DisplayValue
408
Common Cathode
7Seg_Common Inverts controls for Common Anode Required for Common Cathode displays
Anode displays
7Seg_HighSi Support PFET or PNP high side Inverts Common Cathode addressing pin logic for
de driving of the display multiplexed displays
Example:
'Chip model
#chip 16f1783,8
' This is the usage of the SEG_DOT for decimal point support
' An optional third parameter of '1' will turn on the decimal point
' of that digit when using DisplayValue command
#define DISP_SEG_DOT PortC.7
Do Forever
For count = 0 to 999
number = count
Num2 = 0
409
Num3 = 0
If number >= 100 Then
Num3 = number / 100
'SysCalcTempX is the remainder after a division has been completed
number = SysCalcTempX
End if
If number >= 10 Then
Num2 = number / 10
number = SysCalcTempX
end if
Num1 = number
Repeat 10
DisplayValue 1, Num1,1 'Optional third parameter turns on the dp dot on
that digit
wait 5 ms
DisplayValue 2, Num2
wait 5 ms
DisplayValue 3, Num3
wait 5 ms
end Repeat
Next
Loop
Common Anode
When setting up the 7 segment Common Anode display you MUST use the 7Seg_CommonAnode constant.
You can optionally use the 7Seg_HighSide constant to support PFET or PNP high side driving of the
Common Anode displays as follows:
7Seg_Common Inverts controls for Common Anode Required for Common Cathode displays
Anode displays
7Seg_HighSi Support PFET or PNP high side Inverts Common Cathode addressing pin logic for
de driving of the display multiplexed displays
Example:
410
'Chip model
#chip 16f1783,8
Do Forever
For count = 0 to 999
number = count
Num2 = 0
Num3 = 0
If number >= 100 Then
Num3 = number / 100
'SysCalcTempX is the remainder after a division has been completed
number = SysCalcTempX
End if
If number >= 10 Then
Num2 = number / 10
number = SysCalcTempX
end if
Num1 = number
Repeat 10
DisplayValue 1, Num1, 1 'Optional third parameter turns on the dp on that
digit
wait 5 ms
DisplayValue 2, Num2
wait 5 ms
DisplayValue 3, Num3
wait 5 ms
411
end Repeat
Next
Loop
DisplayValue
Syntax:
Command Availability:
Explanation:
This command will display the given value on a seven segment LED display.
dot is an optional parameter. When it is 1 then the decimal point for that digit is turned on.
The command also support HEX characters in the range between 0x00 and 0x0F (0 to 15). See example
two below for usage.
Example 1:
412
'This program will count from 0 to 99 on two LED displays
#chip 16F819, 8
#config osc = int
Do
For Counter = 0 To 99
Example 2:
413
'This program will count from 0 to 0xff on two LED displays
#chip 16F819, 8
#config osc = int
Do
For Counter = 0 To 0xff
414
DisplayChar
Syntax:
Command Availability:
Explanation:
This command will display the given ASCII character on a seven segment LED display.
dot is an optional parameter. When it is 1 then the decimal point for that digit is turned on.
Example 1:
#chip 16F877A, 20
This is a Common Anode example There are three different methods for port specification Note the
ports are specified bit by bit in this case but could be specified like Example 1 See Overview for further
explanation.
415
Example 2:
' Constants
' You need to specify the port settings
#define DISP_SEG_A PORTB.0
#define DISP_SEG_B PORTB.1
#define DISP_SEG_C PORTB.2
#define DISP_SEG_D PORTB.3
#define DISP_SEG_E PORTB.4
#define DISP_SEG_F PORTB.5
#define DISP_SEG_G PORTB.6
DisplaySegment
Syntax:
416
DisplayValue (display, data)
Command Availability:
Explanation:
This command will display the given value on a seven segment LED display.
data is the value between 0 and 255. Where data is the representation of the segments to be set.
Example
#chip 16F690, 4
#config WDT_OFF
417
#define smallTCharacter 120 'raw character for 't' on 7 segment.
; ----- Variables
CountDownValue = 10
Main:
' Push number to 7 Segment Display
if sw1 = 0 then goto Countdown
num2 = 1
num3 = 0
cnt = 5
gosub display
goto main
Countdown:
num2 = CountDownValue/10
num3 = CountDownValue%10
cnt = 60
gosub display
if CountDownValue = 0 then
firingPort = 1
cnt = 200
gosub dispfire
firingPort = 0
CountDownValue = 10
goto main
end if
CountDownValue = CountDownValue - 1
goto Countdown
418
display:
Repeat cnt
DisplaySegment 1, smallTCharacter
wait 5 ms
Displaychar 2, "-"
DisplayValue 3, Num2
wait 5 ms
DisplayValue 4, Num3
wait 5 ms
end Repeat
return
hld:
if sw1 = 0 then goto hld
cnt = 5
gosub Display
if sw1 = 1 then goto hld
goto countdown
DispFire:
Repeat cnt
Displaychar 1, "F"
wait 5 ms
Displaychar 2, "i"
wait 5 ms
Displaychar 3, "r"
wait 5 ms
Displaychar 4, "E"
wait 5 ms
End Repeat
return
end
419
One Wire Devices
This is the One Wire Devices section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
DS18B20
The sensor reports degrees C with 9 to 12-bit precision from -55C to 125C (+/- 0.5C).
Each sensor has a unique 64-Bit Serial number etched into it. This allows for a number of sensors to be
used on one data bus. This sensor is used in many data-logging and temperature control projects.
To use the DS18B20 driver the following is required to added to the Great Cow BASIC source file.
#include <DS18B20.h>
Note the Great Cow BASIC commands do not work with the older DS1820 or DS18S20 as they have a
different internal resolution.
These commands are not designed to be used with parasitically powered DS18B20 sensors.
ReadDigitalTemp
Syntax:
ReadDigitalTemp
Command Availability:
Explanation:
Return the value of the sensor in two global variables. The following two lines must be included in the
Great Cow BASIC source file.
#include <DS18B20.h>
#define DQ PortC.3 ; change port configuration as required
This method returns whole part of the sensor value in the byte variable DSint, the method also returns
420
decimal part of the sensor value in the byte variable DSdec.
Example:
*#include <DS18B20.h>*
ReadDigitalTemp
ReadTemp
Syntax:
byte_var = ReadTemp
Command Availability:
Explanation:
421
ReadTemp is a function that returns the raw value of the sensor. The following two lines must be
included in the Great Cow BASIC source file.
#include <DS18B20.h>
#define DQ PortC.3 ; change port configuration as required
ReadTemp reads the sensor and stores in output variable. The conversion takes up to 750ms. Readtemp
carries out a full 12 bit conversion and then rounds the result to the nearest full degree Celsius.
To read the full 12 bit value of the sensor use the readtemp12 command.
The temperature is read back in whole degree steps, and the sensor operates from -55 to + 125 degrees
Celsius. Note that bit 7 is 0 for positive temperature values and 1 for negative values (ie negative values
will appear as 128 + numeric value).
Note the Readtemp command does not work with the older DS1820 or DS18S20 as they have a different
internal resolution. This command is not designed to be used with parasitically powered DS18B20
sensors, the 5V pin of the sensor must be connected.
Example:
422
'Chip Settings. Assumes the development board with with a 16F877A
#chip 16F877A,1
#include <DS18B20.h>
ccount = 0
CLS
do forever
' The function readtemp returns the integer value of the sensor
DSdata = readtemp
wait 2 s
ccount++
loop
ReadTemp12
Syntax:
byte_var = ReadTemp12
Command Availability:
423
Available on all microcontrollers.
Explanation:
ReadTemp12 is a function that returns the raw value of the sensor. The following two lines must be
included in the Great Cow BASIC source file.
#include <DS18B20.h>
#define DQ PortC.3 ; change port configuration as required
Reads sensor and stores in output variable. The conversion takes up to 750ms. Readtemp12 carries out a
full 12 bit conversion.
This command is for advanced users only. For standard ‘whole degree’ data use the Readtemp command.
The temperature is read back as the raw 12 bit data into a word variable (0.0625 degree resolution).
The user must interpret the data through mathematical manipulation. See the DS18B20 datasheet for
more information on the 12 bit temperature/data information construct.
The function readtemp12 does not work with the older DS1820 or DS18S20 as they have a different
internal resolution. This command is not designed to be used with parasitically powered DS18B20
sensors, the 5V pin of the sensor must be connected.
Example:
#include <DS18B20.h>
424
do forever
'Display the integer and decimal value of the sensor on the LCD
' The function readtemp12 returns the raw value of the sensor.
' The sensor is read as a 12 bit value. Each unit equates to 0.0625 of a degree
DSdata = readtemp12
SignBit = DSdata / 256 / 128
If SignBit = 0 Then goto Positive
' its negative!
DSdata = ( DSdata # 0xffff ) + 1 ' take twos comp
Positive:
' Convert value * 0.0625. Mulitple value by 6 then add result to multiplication of
the value with 25 then divide result by 100.
TempC_100 = DSdata * 6
DSdata = ( DSdata * 25 ) / 100
TempC_100 = TempC_100 + DSdata
DisplayTemp:
locate 1,0
print hex(ccount)
print " Real"
locate 1,8
print str(Whole)
print "."
' To ensure the decimal part is two digits
Dig = Fract / 10
print Dig
Dig = Fract % 10
print Dig
print chr(223)
print "C"
wait 2 s
ccount++
loop
425
Serial Communications
This is the Serial Communications section of the Help file. Please refer the sub-sections for details
using the contents/folder view.
RS232 (software)
This is the Software Serial Communications section of the Help file. Please refer the sub-sections for
details using the contents/folder view.
Introduction:
These routines allow the microcontroller to send and receive RS232 data.
All functions are implemented using software, so no special hardware is required on the
microcontroller. However, if the microcontroller has a hardware serial module (usually referred to as
UART or USART), and the serial data lines are connected to the appropriate pins, the hardware routines
should be used for smaller code, improved reliability and higher baud rates.
Relevant Constants:
These constants are used to control settings for the RS232 serial communication routines. To set them,
place a line in the main program file that uses #define to assign a value to the particular constant.
SendALow, SendBLow, These are used to define the commands used to send a low No Default
SendCLow (0) bit on serial channels A, B and C respectively. Must be defined
SendAHigh, These are used to define the commands used to send a high No Default
SendBHigh, (1) bit on serial channels A, B and C respectively. Must be defined
SendCHigh
RecALow, RecBLow, The condition that is true when a low bit is being received Sys232Temp.0 OFF
RecCLow Must be defined
RecAHigh, RecBHigh, The condition that is true when a high bit is being received Sys232Temp.0 ON
RecCHigh Must be defined
InitSer
Syntax:
Command Availability:
426
Available on all microcontrollers.
Explanation:
This command will set up the serial communications. The parameters are as follows:
channel is 1, 2 or 3, and refers to the I/O ports that are used for communication.
rate is the bit rate, which is given by the letter r and then the desiredrate in bps. Acceptable units are
r300, r600, r1200, r2400, r4800, r9600 and r19200.
start gives the number of start bits, which is usually 1. To make the microcontroller wait for the start
bit before proceeding with the receive, add 128 to start. (Note: it may be desirable to use the
WaitForStart constant here.)
data tells the program how many data bits are to be sent or received. In most situations t his is 8, but it
can range between 1 and 8, inclusive.
stop is the number of stop bits. If start bit 7 is on, then this number will be ignored.
parity refers to a system of error checking used by many devices. It can be odd (in which there must
always be an odd number of high bits), even (where the number of high bits must always be even), or
none (for systems that do not use parity).
invert can be either "normal" or "invert". If it in "invert", then high bits will be changed to low, and low
to high.
Example:
SerSend
Syntax:
Command Availability:
Explanation:
This command will send a byte given by data using the RS232 channel referred to as channel according
to the rules set using InitSer.
427
Example:
'This program will send a byte using PORTB.2, the value of which
'depends on whether a button is pressed. This can be used with the example for
SerReceive.
#chip 16F819, 8
#config Osc = Int
'Config Software-UART
#define SendAHigh Set RS232Out ON
#define SendALow Set RS232Out OFF
#define RecAHigh Set RS232In ON
#define RecALow Set RS232In OFF
Dir Button In
SerReceive
Syntax:
Command Availability:
Explanation:
This command will read a byte from the RS232 channel given by channel according to the rules set
428
using InitSer, and store the received byte in the variable output.
Example:
'This program will read a byte from PORTB.2, and set the LED on if
'the byte is more than 50. This can be used with the SerSend
'example program.
#chip 16F88, 8
#config Osc = Int
SerPrint
Syntax:
Command Availability:
Explanation:
SerPrint is used to send a value over the serial connection. value can be a string, integer, long, word or
byte.
SerPrint will not send any new line characters. If the chip is sending to a terminal, these commands
should follow SerPrint.
429
SerSend channel, 13
SerSend channel, 10
Example:
'This program will display any values received over the serial
'connection. If "pot" is received, the value of the analog sensor
'will be sent.
'Chip settings
#chip 18F2525, 8
#config Osc = Int
'LCD settings
#define LCD_IO 4
#define LCD_RS PORTC.7
#define LCD_RW PORTC.6
#define LCD_Enable PORTC.5
#define LCD_DB4 PORTC.4
#define LCD_DB5 PORTC.3
#define LCD_DB6 PORTC.2
#define LCD_DB7 PORTC.1
'Serial settings
#define RS232Out PORTB.0
#define RS232In PORTB.1
'Config Software-UART
#define SendAHigh Set RS232Out ON
#define SendALow Set RS232Out OFF
#define RecAHigh Set RS232In ON
#define RecALow Set RS232In OFF
set RS232Out On
Do
'Potentiometer
#define POT_PORT PORTA.0
#define POT_AN AN0
430
'Create buffer variables to store received messages
Dim Buffer As String
Dim OldBuffer As String
BufferSize = 0
'Main loop
'Get a byte from the terminal
SerReceive 1, Temp
BufferSize = 0
End If
'Backspace code, delete last character in buffer
If Temp = 8 Then
If BufferSize > 0 Then BufferSize -= 1
End If
'Received ASCII code between 32 and 127, add to buffer
If Temp >= 32 And Temp <= 127 Then
BufferSize += 1
Buffer(BufferSize) = Temp
End If
Loop
431
'Takes a sensor reading and sends it to terminal
Sub SendSensorReading
SerPrint 1, "Sensor Reading: "
SerPrint 1, ReadAD10(POT_AN)
SerSend 1, 13
SerSend 1, 10
End Sub
432
RS232 (software optimised)
This is the Software Serial Communications section of the Help file. Please refer the sub-sections for
details using the contents/folder view.
Introduction:
These routines allow the microcontroller to send and receive RS232 data.
SoftSerial is a library for the Creat Cow BASIC compiler and works on AVR and PIC microcontrollers.
These routines allow the microcontroller to send and receive RS232 data. All functions are
implemented using software, so no special hardware is required on the microcontroller. SoftSerial
uses ASM routines for minimal overhead. If the microcontroller has a hardware serial module (usually
referred to as UART or USART) the hardware routines can be used too.
Features
• 5 - 8 data bits
• 1 or 2 stop bits
Relevant Constants:
These constants are used to control settings for the RS232 serial communication routines. To set them,
place a line in the main program file that uses #define to assign a value to the particular constant.
433
Constant Name/s Controls Default value SER1_TXPORT,
Valid SER2_TXPORT,
Values SER3_TXPORT
These are used to define the port for PORTA - No Default Must SER1_TXPIN,
sending on serial channels 1, 2 and 3 PORTx be defined for SER2_TXPIN,
respectively. Note, that we also have to sending SER3_TXPIN
define a PortPin (see next line). It is not
necessary to define this, if we want to
receive only. Sample: #define
SER1_TXPORT PortB
These are used to define the pin (the 0-7 No Default Must SER1_RXPORT,
corresponding bit) for sending on serial be defined for SER2_RXPORT,
channels 1, 2 and 3 respectively. Sample: sending SER3_RXPORT
#define SER1_TXPIN 0
These are used to define the port for PORTA - Must be defined SER1_RXPIN,
receiving on serial channels 1, 2 and 3 PORTx No for receiving SER2_RXPIN,
respectively. Note, that we also have to Default SER3_RXPIN
define a PortPin (see next line). It is not
necessary to define this, if we want to
receive only. Sample: #define
SER1_RXPORT PortA
These are used to define the pin (the 0-7 No Default Must SER1_BAUD,
corresponding bit) for receiving on serial be defined for SER2_BAUD,
channels 1, 2 and 3 respectively. It is not receiving SER3_BAUD
necessary to define this, if we want to send
only. Sample: #define SER1_RXPIN 5
These are used to define the baudrate for 75 - 512000 No Default Must SER1_DATABITS,
sending and receiving on serial channels 1, be defined SER2_DATABITS,
2 and 3 respectively. It is not necessary to SER3_DATABITS
define this, if we want to send only. Sample:
#define SER1_BAUD 19200
These are used to define the databits for 5-8 Optional Default SER1_STOPBITS,
sending and receiving on serial channels 1, =8 SER2_STOPBITS,
2 and 3 respectively. Sample: #define SER3_STOPBITS
SER1_DATABITS 7
These are used to define the stopbits for 1, 2 Optional Default SER1_INVERT,
sending and receiving on serial channels 1, =1 SER2_INVERT,
2 and 3 respectively. Sample: #define SER3_INVERT
SER1_STOPBITS 2
434
Constant Name/s Controls Default value SER1_TXPORT,
Valid SER2_TXPORT,
Values SER3_TXPORT
These are used to define the polarity for On, Off Optional Default SER1_RXNOWAIT,
sending and receiving on serial channels 1, = Off SER2_RXNOWAIT,
2 and 3 respectively. If it is "On", then high SER3_RXNOWAIT
bits will be changed to low, and low to high.
This is useful for connection to a PCs native
serial port or USB-serial converters with
MAX232. Sample: #define SER1_INVERT On
SerNSend
Syntax:
Ser1Send data
Ser2Send data
Ser3Send data
Command Availability:
Explanation:
This command will send a byte given by data using the channel referred to as Ser1.. , Ser2… , Ser3…
according to the rules set by the related defines.
Example:
435
'This program will send one byte using PORTA.5
; ----- Configuration
#chip 12F1501, 1
SerNPrint
Syntax:
Ser1Print value
Ser2Print value
Ser3Print value
Command Availability:
Explanation:
This command will send a value using the channel referred to as Ser1.. , Ser2… , Ser3… according to the
rules set by the related defines. value can be a string, integer, long, word or byte.
Example:
436
'This program will send text and an icrementing value using PORTB.1
; ----- Configuration
#chip 16F886, 16
#option Explicit
; ----- Variables
Dim xx As Word
xx = 1000
SerNReceive
Syntax:
bytevar = Ser1Receive
bytevar = Ser2Receive
bytevar = Ser3Receive
Command Availability:
Explanation:
437
This function will read a byte using the channel referred to as Ser1.. , Ser2… , Ser3… according to the
rules set by the related defines. The received byte is stored in the variable bytevar. By default the
function waits for the startbit impulse edge before executing the following commands. See the sample
files how to realize timeout-functionality or interrupt-driven receiving.
Example:
'This program will receive bytes on PORTB.0 and send back using PORTB.1
; ----- Configuration
#chip 16F886, 16
#option Explicit
; ----- Variables
Dim RecByte As Byte
Do Forever
RecByte = Ser1Receive 'receive one byte - wait until detecting startbit
Ser1Send 13 'new line in Terminal
Ser1Send 10 '
Ser1Print "You sent: " 'send a text
Ser1Send RecByte 'send the sign representing the byte
Loop
438
Exposed in SoftSerial.h authored by Frank Steinberg
439
RS232 (hardware)
This is the RS232 (hardware) section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Introduction
Great Cow BASIC included microcontroller hardware-based serial routines are intended for use on
microcontrollers with built in serial communications modules - normally referred to in datasheets as
USART or UART modules. Check the microcontroller data sheet for the defined transmit and receive
(TX/Rx) pins. Make sure your program sets the Tx pin direction to Out and the Rx pin direction to In
respectively. If the RS232 lines are connected elsewhere, or the microcontroller has no USART module,
then the Great Cow BASIC software based RS232 routines must be used.
Initialization of the USART module is handled automatically from your program by defining the chip,
speed, and the baudrate. The baudrate generator values are calculated and set, usart is set to
asynchronous, usart is enabled , the receive and transmit are enabled. See the table below.
Example:
#chip mega328p, 16
#define USART_BAUD_RATE 9600
#define USART_TX_BLOCKING
Command Availability:
The following table explains the methods that can be implemented when using the Great Cow BASIC
serial routines.
Commands:
440
Command Parameters Example
441
Command Parameters Example
443
Command Parameters Example
444
Command Parameters Example
445
Command Parameters Example
Constants These constants affect the operation of the hardware RS232 routines:
USART_BAU Baud rate (in bps) for the routines to operate at. No default, user must enter a baud.
D_RATE Doesn’t have to be a standard baud.
USART_BLO If defined, this constant will cause the USART No parameter needed. Use “#defining”
CKING routines to wait until data can be sent or it implement the action.
received.
USART_TX_ If defined, this constant will cause the Transmit No parameter needed. Use “#defining”
BLOCKING USART routines to wait until Transmit register is it implement the action.
empty before writing the next byte which
prevents over running the register and losing
data.
USART2_BA Baud rate (in bps) for the routines to operate at. No default, user must enter a baud.
UD_RATE Doesn’t have to be a standard baud.
USART2_BL If defined, this constant will cause the USART No parameter needed. Use “#defining”
OCKING routines to wait until data can be sent or it implement the action.
received.
USART2_TX If defined, this constant will cause the Transmit No parameter needed. Use “#defining”
_BLOCKING USART routines to wait until Transmit register is it implement the action.
empty before writing the next byte which
prevents over running the register and losing
data.
USART3_BA Baud rate (in bps) for the routines to operate at. No default, user must enter a baud.
UD_RATE Doesn’t have to be a standard baud.
USART3_BL If defined, this constant will cause the USART No parameter needed. Use “#defining”
OCKING routines to wait until data can be sent or it implement the action.
received.
USART3_TX If defined, this constant will cause the Transmit No parameter needed. Use “#defining”
_BLOCKING USART routines to wait until Transmit register is it implement the action.
empty before writing the next byte which
prevents over running the register and losing
data.
446
Constant Controls Default Value
Name
USART4_BA Baud rate (in bps) for the routines to operate at. No default, user must enter a baud.
UD_RATE Doesn’t have to be a standard baud.
USART4_BL If defined, this constant will cause the USART No parameter needed. Use “#defining”
OCKING routines to wait until data can be sent or it implement the action.
received.
USART4_TX If defined, this constant will cause the Transmit No parameter needed. Use “#defining”
_BLOCKING USART routines to wait until Transmit register is it implement the action.
empty before writing the next byte which
prevents over running the register and losing
data.
USART_DEL This is the delay between characters. 1 ms To disable this delay between
AY characters … Use #define
USART_DELAY 0 MS
SerPrintC Causes a Carriage return to be sent after every No parameter needed. User “#defining”
R HserPrint automatically. it implements the action
SerPrintL Causes a LineFeed to be sent after every No parameter needed. User “#defining”
F HserPrint. Some communications require both CR it implements the action
and LF
HSerGetNum
Syntax:
Command Availability:
Explanation:
This command will read a multi digit number received as ascii chars followed by a CR from an external
447
serial source using a hardware serial module. The command checks that only numbers are input
disregarding other characters while waiting for the ending <CR>. It can be used only as a subroutine.
Example:
'This program receives a number and CR from a PC terminal and sends it back on both
usarts
#chip 18f26k22, 16
'Init pins
#define SerInPort PORTc.7 'usart1 in
#define SerOutPort PORTc.6 'usart2 out
'Set pin directions
Dir SerOutPort Out
Dir SerInPort In
Dir PORTB.6 Out 'USART2 out
Dir PORTB.7 In 'USArt2 in
Dir PORTB.0 Out 'leds for testing
Dir PORTB.1 Out 'leds for testing
Wait 100 Ms
'Variables
Dim myNum as Word
'Main body of program commences here.
'Message after reset
HSerPrint "18F26k22"
HSerPrintCRLF
'Main routine
Do forever
'wait for char from UART
'HSerReceive InChar
HSerGetNum myNum,2 'from usart 2
HSerPrint myNum,1 ' send out usart 1
HSerPrint myNum,2 'send out usart 2
HSerPrintCRLF 1,2 'send one CRLF out usart 2
HserPrintCRLF 1,1 ‘send one CRLF out usart 1
loop
Example: This program receives number on serial port 1 and displays. This example shows using a
448
Long as the input variable.
Therefore, the result is in the range of 0-99999. The example also shows how to detect a buffer
overrun by testing the HSerInByte variable.
#chip mega328p, 16
Do
HSerGetNum myNum
HSerPrint myNum
HSerGetString
Syntax:
Variable type is string and the routine checks for numbers,letters, and puctuation.
Command Availability:
449
Explanation:
This command will read a multi character string received as ascii input to the hardware serial module
followed by a CR from an external serial source. It can be used only as a subroutine. Variable type is
string and the routine checks for numbers,letters, and puctuation.
Example:
'This program receives char string and CR from a PC terminal, sends back the string
on the serial port, and turns Led’s on off by command
#chip 18f26k22, 16
'InitUSART
#define SerInPort PORTc.7 'USART 1 Rx Pin
#define SerOutPort PORTc.6 'USART 1 Tx Pin
Wait 100 Ms
; ----- Variables
' All byte variables are defined upon use.
Dim myNum as Word
Dim MyString as String
'Main routine
450
Do Forever
HSerGetString MyString
HSerPrint MyString
HSerSend(13)
If MyString = "LED1 ON" Then
Set PORTB.0 Off
End If
If MyString = "LED1 OFF" Then
Set PORTB.0 On
End If
If MyString = "LED2 ON" Then
Set PORTB.1 Off
End If
If MyString = "LED2 OFF" Then
Set PORTB.1 On
End If
Loop
HSerPrint
Syntax:
Command Availability:
Explanation:
HSerPrint is used to send a value over the serial connection. user_value can be a string, integer, long,
word or byte. HSerPrint is very similar to Print. The data will be sent out the hardware serial module.
HSerPrint will not send any new line characters. If the chip is sending to a terminal, these commands
should follow every HSerPrint :
451
HSerPrint 13
HSerPrint 10
Example:
'This program will display any values received over the serial
'connection. If "pot" is received, the value of the analog sensor
'will be sent.
'Note: This has been adapted from the SerPrint example.
'Chip settings
#chip 18F2525, 8
#config Osc = Int
'LCD settings
#define LCD_IO 4
#define LCD_RS PORTC.7
#define LCD_RW PORTC.6
#define LCD_Enable PORTC.5
#define LCD_DB4 PORTC.4
#define LCD_DB5 PORTC.3
#define LCD_DB6 PORTC.2
#define LCD_DB7 PORTC.1
'USART settings
#define USART_BAUD_RATE 9600
#define USART_TX_BLOCKING
'Potentiometer
#define POT_PORT PORTA.0
#define POT_AN AN0
452
Wait 1 s
'Main loop
Do
'Get a byte from the terminal
HSerReceive Temp
BufferSize = 0
End If
'Backspace code, delete last character in buffer
If Temp = 8 Then
If BufferSize > 0 Then BufferSize -= 1
End If
'Received ASCII code between 32 and 127, add to buffer
If Temp >= 32 And Temp <= 127 Then
BufferSize += 1
Buffer(BufferSize) = Temp
End If
Loop
453
'If received command is "pot", show potentiometer value
If CmdIn = "pot" Then
SendSensorReading
ExecCommand = True
End If
End Function
HSerPrintStringCRLF
Syntax:
Command Availability:
Explanation:
HSerPrintStringCRLF is used to send a string over the serial connection. The parameter can only be a
string. HSerPrintStringCRLF is very similar to HserPrint but HserPrint can handle all types of variables.
Example:
454
'This program will display string over the serial connection.
'Chip settings
#chip 18F2525, 8
#config Osc = Int
'USART settings
#define USART_BAUD_RATE 9600
#define USART_TX_BLOCKING
HSerReceive
Syntax:
Used as subroutine:
HSerReceive (user_byte_variable)
or, if other multiple comports are in use, set the comport before using HSerReceive.
Used as function:
Command Availability:
455
Explanation:
This command will read a byte from the hardware RS232 module. It can be used either as a subroutine
or as a function. If used as a subroutine, a variable must be supplied to store the received value in. If
used as a function, it will return the received value.
The subroutine HSerReceive can get a byte from any comport but must set the comport number
immediately before the call. If ”#define USART_BLOCKING” is defined then the HserReceive waits in a
loop until it receives a byte. If” #define USART_BLOCKING” is NOT defined then HserReceive returns
the new byte that was received OR returns 255 because of “DefaultUsartReturnValue = 255” was
defined. This is good because it don’t hold up your program from executing other commands and
your can check it for new data priodically.
Example:
'This program will read a value from the USART, and send it to PORTB.
#chip 16F877A, 20
'USART settings
#define USART_BAUD_RATE 9600 'sets up comport 1 for 9600 baud
'Main loop
Do
'Get serial data and output value to PortB as 8 bit binary
HSerReceive(InChar) 'Receive data as Subroutine from comport 1
'InChar = HSerReceive 'Could also be written as Function
If InChar <> 255 Then 'If value is 255 then it is old data
PortB = InChar 'If new data then it goes to PortB
End If
Loop
Example 2:
456
'If you choose no “Blocking” and comment both of them out.
'USART settings
#define USART_BAUD_RATE 9600
'#define USART_BLOCKING ' just none OR one of the blocking
'#define USART_TX_BLOCKING ' statements should be defined
'Main loop
Do
'Get and display value
'If there is no new data, HSerReceive will return default value.
comport = 1
HSerReceive tempvalue
If tempvalue <> 255 Then ‘ don’t change PortB if it is default
PortB = tempvalue
End If
Loop
Example 3:
457
'If you choose no “Blocking” and comment both of them out.
#chip mega328p, 16
Wait 1 s
'Main routine hook up FTDI232 usb to serial and use terminal program to check
Start:
comport = 1
HSerReceive(InChar) 'Subroutine needs the comport set
'InChar = HSerReceive ' This function will get from comport 1
If InChar <> 255 Then ' check if for received byte
'return 255 if old data
HSerSend InChar 'send back char to UART
End If
Goto Start
HSerReceiveFrom
Syntax:
Command Availability:
458
Microchip PIC supports USART1 and 2.
Atmel AVR supports USART 1,2,3 and 4.
Explanation:
This command will read a byte from the hardware RS232 module. It can be only be used as a function.
It will return the received value.
Example:
'This program will read a value from the USART, and display it on PORTB.
#chip 16F877A, 20
'USART settings
#define USART_BAUD_RATE 9600
#define USART_BLOCKING
#define USART_TX_BLOCKING
'Main loop
Do
'Get byte value
bytein = HSerReceiveFrom (2)
'do something useful
Loop
HSerSend
Syntax:
Command Availability:
459
Atmel AVR supports USART 1,2,3 and 4.
Explanation:
This command will send a byte given by user_byte using the hardware RS232 module.
Example:
'This program will send the status of PORTB through the hardware
'serial module.
#chip 16F877A, 20
'USART settings
#define USART_BAUD_RATE 9600 'Initializes USART port with 9600 baud
'#define USART_BLOCKING ' Both of these blocking statements will
#define USART_TX_BLOCKING ' wait for tx register to be empty
' use only one of the two
'Set PORTB to input
Dir PORTB In
'Set USART transmit pin to output
Dir PORTC.6 Out
'Main loop
Do
'Send PORTB value through USART
HSerSend PORTB
HSerSend(13) ' sends a CR
'Short delay for receiver to process message
Wait 10 ms 'probably not necessary with blocking statement
Loop
HserPrintByteCRLF
Syntax:
HserPrintByteCRLF user_data [, 1 | 2 | 3 | 4 ]
Command Availability:
Explanation:
460
This command will send a byte given by user_data using the hardware USART module and then send
the ASCII codes 13 and 10. ASCII codes 13 and 10 equate to a carriage return and line feed.
Example:
'This program will send the status of PORTB through the hardware serial module.
HserPrintCRLF
Syntax:
Command Availability:
Microchip PIC supports USART1 and 2.+ Atmel AVR supports USART 1,2,3 and 4.
Explanation:
This command will send ASCII codes 13 and 10 only using the hardware RS232 module. ASCII codes 13
and 10 equate to a carriage return and line feed.
Optionally, you can add a parameter. The number will determine the number of ASCII codes 13 and 10
set to the hardware RS232 module.
Also you can choose the comport with second optional parameter if it is not the default comport 1. If
there is no first optional parameter then you must have atleast acomma before it to indicate this is the
second parameter.
Examples:
461
'This Line will send 1 CR and LF
HserPrintCRLF ' Will send a CR & LF to the terminal
462
PS/2
This is the PS/2 section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
PS/2 Overview
PS2 Overview
These routines make it easier to communicate with a PS/2 device, particularly an external keyboard.
Relevant Constants
PS2_DELAY This constant can be set to a delay, such as 10 ms. If set, a delay will be Not set
added at the end of every byte sent or received.
Connections between the Keyboard and the Microcontroller The following diagram show a typical
connection between the keyboard and the microcontroller. The value of R1 and R2 is typically 4.7k for
a 5v system.
InKey
Syntax:
463
output = InKey
Command Availability:
Explanation:
The InKey function will read the last pressed key from a PS/2 keyboard, and return an ASCII value
corresponding to the key. If no key is pressed, then InKey will return 0.
It will also monitor Caps Lock, Num Lock and Scroll Lock keys, and update the status LEDs as
appropriate.
Example:
'Hardware settings
#chip 18F4620, 20
Main:
'Read the last pressed key
KeyIn = INKEY
'If no key pressed, try reading again
464
If KeyIn = 0 Then Goto Main
DisplayData:
'Display key buffer
'LCDWriteChar is used instead of Print for greater control
CLS
For DataPos = 1 to DataCount
If DataPos = 17 then Locate 1, 0
LCDWriteChar KeyLog(DataPos)
Next
Goto Main
PS2SetKBLeds
Syntax:
PS2SetKBLeds (LedStatus)
Command Availability:
465
Explanation:
This routine will turn the status LEDs on a keyboard on or off. LedStatus is a variable, of which the
lower 3 bits correspond to the 3 LEDs. Bit 0 is for Scroll Lock, bit 1 controls Num Lock and bit 2 controls
Caps Lock.
Note that this routine does not alter the status variables within the INKEY routine - so even if the Caps
Lock LED is turned on, Caps Lock will stay off.
Example:
'Hardware settings
#chip 16F88, 8
'Main Loop
Do
Loop
PS2ReadByte
Syntax:
output = PS2ReadByte
Command Availability:
466
Available on all microcontrollers.
Explanation:
PS2ReadByte will read a byte from the PS/2 bus. It will return the byte, or 0 if no data was returned by
the PS/2 device.
The PS/2 bus will normally be held in the inhibit state. PS2ReadByte will uninhibit the bus for 25 ms. If a
response is received, it will be read. Then, the bus will be placed back in the inhibit state.
Example:
PS2WriteByte
Syntax:
PS2WriteByte user_data
Command Availability:
Explanation:
PS2WriteByte will send a byte to a PS/2 device. Once the byte has been written, the PS/2 bus will be
placed in the inhibit state.
Example:
467
SPI
This is the Serial Peripheral Interface section of the Help file. Please refer the sub-sections for details
using the contents/folder view.
SPI Overview
Syntax:
The SPI interface allows for the transmission and receiption of data simultaneously on two lines (MOSI
and MISO).
The Clock polarity (CPOL) and clock phase (CPHA) are the main parameters that define a clock format
to be used by the SPI bus. Depending on CPOL parameter, SPI clock may be inverted or non-inverted.
CPHA parameter is used to shift the sampling phase. If CPHA=0 the data are sampled on the leading
(first) clock edge. If CPHA=1 the data are sampled on the trailing (second) clock edge, regardless of
whether that clock edge is rising or falling.
CPOL=0, CPHA=0
The data must be available before the first clock signal rising. The clock idle state is zero. The data on
MISO and MOSI lines must be stable while the clock is high and can be changed when the clock is low.
The data is captured on the clock’s low-to-high transition and propagated on high-to-low clock
transition.
CPOL=0, CPHA=1
468
The first clock signal rising can be used to prepare the data. The clock idle state is zero. The data on
MISO and MOSI lines must be stable while the clock is low and can be changed when the clock is high.
The data is captured on the clock’s high-to-low transition and propagated on low-to-high clock
transition.
CPOL=1, CPHA=0
The data must be available before the first clock signal falling. The clock idle state is one. The data on
MISO and MOSI lines must be stable while the clock is low and can be changed when the clock is high.
The data is captured on the clock’s high-to-low transition and propagated on low-to-high clock
transition.
CPOL=1, CPHA=1
The first clock signal falling can be used to prepare the data. The clock idle state is one. The data on
MISO and MOSI lines must be stable while the clock is high and can be changed when the clock is low.
The data is captured on the clock’s low-to-high transition and propagated on high-to-low clock
transition.
Key Commands
469
SPIMode ( _Mode_ [, SPIClockMode])
FastHWSPITransfer( _OutByte_ )
The Great Cow BASIC used the microcontrollers hardware module for SPI. The example below shows
an implementation of Hardware and Software SPI. Software SPI allows for a greater choice of ports to
be used to control the SPI operations.
Example
This example demonstrates the SPI capabilities for the mega328p. The process is similar of any
microcontroller..
This example show using the hardware SPI option and a sofware SPI option.
Using hardware SPI mode - make sure the #define SPI_HardwareSPI is not commented out. Using
software SPI mode - comment out #define SPI_HardwareSPI. The example code will then use software
SPI.
Usage
Hardware SPI mode the Data Out, Data In and Clock (DO/DI and SCK) cannot be moved but the
optional Data Command, Chip Select and Reset are all moveable.
Software SPI mode the Data Out, Data In and Clock (DO/DI and SCK), Data Command, Chip
Select and Reset are all moveable.
Code overview
InitSPIMode calls SPIMode if needed, when hardware mode, and set the lines.
The sub SendByteviaSPI is called to handle whether to call the Hardware or use Software
(bit-banging) SPI.
#chip mega328p, 16
#option explicit
#include <UNO_mega328p.h >
#define SPI_HardwareSPI 'comment this out to make into Software SPI but, you may
have to change clock lines
'Pin mappings for SPI - this SPI driver supports Hardware SPI
#define SPI_DC DIGITAL_8 ' Data command line
470
#define SPI_CS DIGITAL_4 ' Chip select line
#define SPI_RESET DIGITAL_9 ' Reset line
'If DIGITAL_10 is NOT used as the SPI_CS (sometimes called SS) the port must and
output or set as input/pulled high with a 10k resistor.
'As follows:
'If CS is configured as an input, it must be held high to ensure Master SPI
operation.
'If the CS pin is driven low by peripheral circuitry when the SPI is configured
as a Master with the SS pin defined as an input, the
'SPI system interprets this as another master selecting the SPI as a slave and
starting to send data to it!
'If CS is an output SPI communications will commence with no flow control.
dir DIGITAL_10 Out
byte1 = 100 ' temp values (will come from potentiometer later)
byte2 = 150
byte3 = 200
InitSPIMode
do forever
set SPI_CS OFF;
set SPI_DC OFF;
SendByteviaSPI (byte1)
set SPI_CS ON;
set SPI_DC ON
471
set SPI_DC ON
wait 10 ms
loop
sub InitSPIMode
#ifdef SPI_HardwareSPI
SPIMode ( MasterFast, SPI_CPOL_0 + SPI_CPHA_0 )
#endif
End sub
#ifdef SPI_HardwareSPI
FastHWSPITransfer SPISendByte
set SPI_CS ON;
exit sub
#endif
#ifndef SPI_HardwareSPI
repeat 8
if SPISendByte.7 = ON then
set SPI_DO ON;
else
set SPI_DO OFF;
end if
SET SPI_SCK On; ; therefore CPOL=0 ==ON, and, where CPOL=1==ON
rotate SPISendByte left
set SPI_SCK Off; ; therefore CPOL=0 =OFF, and, where CPOL=1==OFF
472
end repeat
set SPI_CS ON;
set SPI_DO OFF;
#endif
end Sub
SPIMode
Syntax:
Command Availability:
Explanation:
Mode sets the mode of the SPI module within the microcontroller. These are the possible SPI Modes:
MasterSlow Master mode, SPI clock is 1/64 of the frequency of the microcontroller.
Master Master mode, SPI clock is 1/16 of the frequency of the microcontroller.
MasterFast Master mode, SPI clock is 1/4 of the frequency of the microcontroller.
SPIClockMode is an optional parameter to set the mode of the SPI clock mode. This optional parameter
sets both the clock polarity and clock edge.
SPIClockMode Description
473
SPIMode ( MasterFast, SPI_CPOL_n + SPI_CPHA_n )
Where the following parameters can be used as a calculation to set the SPIClockMode.
SPI_CPOL_0 CPOL = 0
SPI_CPOL_1 CPOL = 1
SPI_CPHA_0 CPHA = 0
SPI_CPHA_1 CPHA = 1
Summary:
When using SPI setting the clock frequency is completed using SPIMode, and the master must also
configure the clock polarity and phase with respect to the data. Using the two options as CPOL and
CPHA.
The timing diagram is shown below. The timing is further described and applies to both the master and
the slave device.
When CPOL=0 the base value of the clock is zero, i.e. the active state is 1 and idle state is 0.
• For CPHA=0, data are captured on the clock’s rising edge (low→high transition) and data is output
on a falling edge (high→low clock transition).
• For CPHA=1, data are captured on the clock’s falling edge and data is output on a rising edge.
When CPOL=1 the base value of the clock is one (inversion of CPOL=0), i.e. the active state is 0 and idle
state is 1.
• For CPHA=0, data are captured on clock’s falling edge and data is output on a rising edge.
• For CPHA=1, data are captured on clock’s rising edge and data is output on a falling edge.
When CPHA=0 means sampling on the first clock edge and , while CPHA=1 means sampling on the
second clock edge, regardless of whether that clock edge is rising or falling. Note that with CPHA=0,
the data must be stable for a half cycle before the first clock cycle.
In other words, CPHA=0 means transmitting data on the active to idle state and CPHA=1 means that
data is transmitted on the idle to active state edge. Note that if transmission happens on a particular
edge, then capturing will happen on the opposite edge(i.e. if transmission happens on falling, then
reception happens on rising and vice versa). The MOSI and MISO signals are usually stable (at their
reception points) for the half cycle until the next clock transition. SPI master and slave devices may
well sample data at different points in that half cycle.
This adds more flexibility to the communication channel between the master and slave.
474
Example
This example demonstrates the SPI capabilities for the mega328p. The process is similar of any
microcontroller..
#chip mega328p, 16
#option explicit
#include <UNO_mega328p.h >
#define SPI_HardwareSPI 'comment this out to make into Software SPI but, you may
have to change clock lines
'Pin mappings for SPI - this SPI driver supports Hardware SPI
#define SPI_DC DIGITAL_8 ' Data command line
#define SPI_CS DIGITAL_4 ' Chip select line
#define SPI_RESET DIGITAL_9 ' Reset line
'If DIGITAL_10 is NOT used as the SPI_CS (sometimes called SS) the port must and
output or set as input/pulled high with a 10k resistor.
'As follows:
'If CS is configured as an input, it must be held high to ensure Master SPI
operation.
'If the CS pin is driven low by peripheral circuitry when the SPI is configured
as a Master with the SS pin defined as an input, the
475
'SPI system interprets this as another master selecting the SPI as a slave and
starting to send data to it!
'If CS is an output SPI communications will commence with no flow control.
dir DIGITAL_10 Out
do
set SPI_CS OFF; Select line
set SPI_DC OFF; Send Data if off, or, Data if On
SPITransfer ( outbyte, inbyte )
set SPI_CS ON; Deselect Line
set SPI_DC ON
wait 10 ms
loop
SPITransfer
Syntax:
SPITransfer tx, rx
Command Availability:
Explanation:
This command simultaneously sends and receives a byte of data using the SPI protocol. It behaves
differently depending on whether the microcontroller has been set to act as a master or a slave. When
operating as a master, SPITransfer will initiate a transfer. The data in tx will be sent to the slave, whilst
the byte that is buffered in the slave will be read into rx. In slave mode, the SPITransfer command will
pause the program until a transfer is initiated by the master. At this point, it will send the data in tx
whilst reading the transmission from the master into the rx variable.
Example:
There are two example programs for this command - one to run on the slave microcontroller , and one
on the master. A reading is taken from a sensor on the slave, and sent across to the master which
shows the data on its LCD screen.
476
Slave Program:
Master Program:
477
'General hardware configuration
#chip 16F877A, 20
'Main Loop
do
'Read a byte from the slave
'No data to send, so tx is 0
SPITransfer 0, Temp
'Display data
if Temp > 0 then
CLS
Print "Light: "
LCDInt Temp
Temp = 0
end if
'Wait to allow time for the LCD to show the given value
wait 100 ms
loop
FastHWSPITransfer
Syntax:
FastHWSPITransfer tx
Command Availability:
478
Available on Microchip PIC microcontrollers with Hardware SPI modules.
Explanation:
This command only sends a byte of data using the SPI protocol. This command only supports master
mode.
As a master, FastHWSPITransfer will initiate a transfer. The data in tx will be sent to the slave.
Example:
Master Program:
'Main Loop
do
loop
479
I2C Software
This is the I2C Software section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
I2C Overview
Introduction:
These software routines allow Great Cow BASIC programs to send and receive I2C messages. They can
be configured to act as master or slave, and the speed can also be altered.
No hardware I2C module is required for these routines - all communication is handled in software.
However, these routines will not work on 12-bit instruction Microchip PIC microcontrollers (10F,
12F5xx and 16F5xx chips).
Relevant Constants:
I2C_USE_TIMEOUT Set to true if the I2C routines should stop waiting for the I2c bus - the Not Set
routine will exit if a timeout occurs. Should be used when you need to
prevent system lockups on the I2C bus. Supports both software I2C
master and slave mode. Will return the variable I2CAck = FALSE when
a timeout has occurred.
I2C_DISABLE_INTE Disable interrupts during I2C routines. Important when an i2C clock is Not
RRUPTS part of your solution defined.
Example: This example examines the IC2 devices and displays on a terminal. This code will require
adaption but the code shows an approach to discover the IC2 devices.
' I2C Overview - using the ChipIno board, see here for information
#chip 16F886, 8
#config MCLRE_ON
480
' Define I2C settings
#define I2C_MODE Master
#define I2C_DATA PORTC.4
#define I2C_CLOCK PORTC.3
#define I2C_DISABLE_INTERRUPTS ON
HSerPrintCRLF 2
HSerPrint "I2C Discover using the ChipIno"
HSerPrintCRLF 2
wait 100 ms
dim DeviceID as byte
for DeviceID = 0 to 255
I2CStart
I2CSend ( deviceID )
I2CSend ( 0 )
I2CSend ( 0 )
i2cstop
HSerPrint "__"
HSerPrint "ID: 0x"
HSerPrint hex(deviceID)
HSerPrint " (d"
HSerPrint Str(deviceID)
HSerPrint ")"
HSerPrintCRLF
end if
next
HSerPrint "End of Device Search": HSerPrintCRLF 2
End
Supported in <I2C.H>
481
I2CAckPollState
Syntax:
Command Availability:
Available on all microcontrollers except 12 bit instruction Microchip PIC microcontrollers (10F,
12F5xx, 16F5xx chips)
Explanation:
Should only be used when I2C routines are operating in Master mode, this command will return the
last state of the acknowledge response from a specific I2C device on the I2C bus.
I2CACKPOLL sets the state of variable I2CAckPollState. I2CAckPollState can only read - it cannot be set.
Example:
...
' ACK polling removes the need to for the 24xxxxx device to have a 5ms
write time
I2CACKPOLL( eeprom_device )
' You check the exit state,
' Use I2CAckPollState to check the state of a target device
...
Supported in <I2C.H>
I2CAckpoll
Syntax:
I2CAckpoll ( I2C_device_address )
Command Availability:
Available on all microcontrollers except 12 bit instruction Microchip PIC microcontrollers (10F,
12F5xx, 16F5xx chips)
Explanation:
Should only be used when I2C routines are operating in Master mode, this command will look for a
482
specific I2C device on the I2C bus.
This sets a global variable I2CAckPollState that can be inspected in your calling routine.
Example:
...
' ACK polling removes the need to for the 24xxxxx device to have a 5ms write time
I2CACKPOLL( eeprom_device )
' You check the exit state, use I2CAckPollState to check the state of
' the acknowledge from the target device
...
Supported in <I2C.H>
I2CReceive
Syntax:
I2CReceive data
I2CReceive data, ack
Command Availability:
Available on all microcontrollers except 12 bit instruction Microchip PIC microcontrollers (10F,
12F5xx, 16F5xx chips)
Explanation:
The I2CReceive command will send data through the I2C connection. If ack is TRUE, or no value is given
for ack, then I2CReceive will send an ack.
If in slave mode, I2CReceive will wait for the master to send the data before reading. When the method
I2CReceive is used in Slave mode the global variable I2CMatch will be set to true when the received
value is equal to the constant I2C_ADDRESS.
483
' I2C Receive - using the ChipIno board, see here for information. ' This program
reads an I2C register and LED is set to on if the value is over 100.
' This program will read from address 83, register 1.
#chip 16F886, 8
#config MCLRE_ON
'I2C settings
#define I2C_MODE Master
#define I2C_DATA PORTC.4
#define I2C_CLOCK PORTC.3
'Misc settings
#define LED PORTB.5
dir LED Out
'Main loop
Do
'Send start
I2CStart
'Request value
I2CSend 83
I2CSend 1
'Read value
I2CReceive ValueIn
'Send stop
I2CStop
'Delay
Wait 20 ms
Loop
See the I2C Overview for the Master mode device to control this Slave mode device.
484
attached.
;----- Configuration
#chip 16F88, 8
#config INTRC_IO,MCLR_OFF
;----- Variables
;----- Program
#define LED0 porta.2 ;pin 1
#define LED1 porta.3 ;pin 2
#define LED2 porta.4 ;pin 3
do
I2CStart ;wait for Start signal
I2CReceive( addr ) ;then wait for an address
case 1:
if value then
set LED1 on
else
485
set LED1 off
end if
case 2:
if value then
set LED2 on
else
set LED2 off
end if
case else
;other register numbers are ignored
end select
else
I2CStop ;release bus in any event
end if
loop
Supported in <I2C.H>
I2CReset
Syntax:
I2CReset
Command Availability:
Available on all microcontrollers except 12 bit instruction Microchip PIC microcontrollers (10F,
12F5xx, 16F5xx chips)
Explanation:
This will attempt a reset of the I2C by changing the state of the I2C bus.
Example:
...
I2CReset
...
Supported in <I2C.H>
486
I2CRestart
Syntax:
I2CRestart
Command Availability:
Available on all microcontrollers except 12 bit instruction Microchip PIC microcontrollers (10F,
12F5xx, 16F5xx chips)
Explanation:
If the I2C routines are operating in Master mode, this command will send a start and restart condition
in a single command.
Example:
...
I2CRESTART
....
Supported in <I2C.H>
I2CSend
Syntax:
I2CSend data
I2CSend data, ack
Command Availability:
Available on all microcontrollers except 12 bit instruction Microchip PIC microcontrollers (10F,
12F5xx, 16F5xx chips)
Explanation:
The I2CSend command will send data through the I2C connection. If ack is TRUE, or no value is given
for ack, then I2CSend will wait for an Ack from the receiver before continuing. If in master mode,
I2CSend will send the data immediately. If in slave mode, I2CSend will wait for the master to request the
data before sending.
Example 1:
487
' I2CSend - using the ChipIno board, see here for information.
' This program send commands to a GCB Slave with three LEDs attached.
#chip 16F886, 8
#config MCLRE_ON
'I2C settings
#define I2C_MODE Master
#define I2C_DATA PORTC.4
#define I2C_CLOCK PORTC.3
#define I2C_BIT_DELAY 20 us
#define I2C_CLOCK_DELAY 30 us
;----- Program
do
488
loop
Example 2:
'Chip model
#chip 16F88, 8
'I2C settings
#define I2C_MODE Slave
#define I2C_CLOCK PORTB.0
#define I2C_DATA PORTB.1
#define I2C_DISABLE_INTERRUPTS ON
'Main loop
Do
'Wait for start condition
I2CStart
'Get address
I2CReceive Address
If Address = 83 Then
'If address was this device's address, respond
I2CReceive Register
OutValue = ReadAD(Register)
I2CSend OutValue
End If
I2CStop
Wait 5 ms
Loop
Supported in <I2C.H>
I2CStart
Syntax:
489
I2CStart
Command Availability:
Available on all microcontrollers except 12 bit instruction Microchip PIC microcontrollers (10F,
12F5xx, 16F5xx chips)
Explanation:
If the I2C routines are operating in Master mode, this command will send a start condition. If routines
are in Slave mode, it will pause the program until a start condition is sent by the master. It should be
placed at the start of every I2C transmission.
Example:
Supported in <I2C.H>
I2CStartoccurred
Syntax:
I2CStartoccurred
Command Availability:
Available on all microcontrollers except 12 bit instruction Microchip PIC microcontrollers (10F,
12F5xx, 16F5xx chips)
Explanation:
If the I2C routine IS operating in Slave mode, this function will check if a start condition has occurred
since the last run of this function. 'Only used in slave mode
Example:
Supported in <I2C.H>
I2CStop
Syntax:
490
I2CStop
Command Availability:
Available on all microcontrollers except 12 bit instruction microcontrollers (10F, 12F5xx, 16F5xx chips)
Explanation:
When in Master mode, this command will send an I2C stop condition, and re-enable interrupts if
I2CStart disabled them. In Slave mode, it will re- enable interrupts.
Example:
Supported in <I2C.H>
491
I2C/TWI Hardware Module
This is the I2C/TWI section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
HI2C Overview
Introduction:
These methods allow Great Cow BASIC programs to send and receive Inter- Integrated Circuit (I2C™)
messages via:
• Master Synchronous Serial Port (MSSP) module of the microcontroller for the Microchip PIC
architecture, or
• ATMEL 2-wire Serial Interface (TWI) for the Atmel AVR microcontroller architecture.
These methods are serial interfaces that are useful for communicating with other peripheral or
microcontroller devices. These peripheral devices may be serial EEPROMs, shift registers, display
drivers, A/D converters, etc.
• Master Mode, or
These methods fully implement all the I2C master and slave functions (including general call support)
and supports interrupts on start and stop bits in hardware to determine a free bus (multi-master
function).
These methods implement the standard mode specifications as well as 7-bit and 10-bit addressing. A
“glitch” filter is built into the SCL and SDA pins when the pin is an input. This filter operates in both
the 100 KHz and 400 KHz modes. In the 100 KHz mode, when these pins are an output, there is a slew
rate control of the pin that is independent of device frequency.
A hardware I2C/TWI module within the microcontroller is required for these methods.
The driver supports two hardware I2C ports. The second port is addressed by the suffix HI2C2. All
HI2C commands are applicable to the second HI2C2 port.
492
Frequenc Description Support
y
Up to 3.4 I2C high speed: Allowing up to 3.4 Mbit/s. Supported on I2C Module Only Requires
Mbit/s. alternative clock source to be set.
Relevant Constants:
HI2C_BAUD Operational speed of the microcontroller. For Microchip SSP or MSSP modules and AVR
_RATE Defaults to 100 kbit/s microcontrollers: #define HI2C_BAUD_RATE
400 or
#define HI2C_BAUD_RATE 100.
Where #define HI2C_BAUD_RATE 100 is the
default value and therefore does need to be
specified. For Microchip I2C module: 'define
HI2C_BAUD_RATE 125' is the default. You
can override this value if you set up an
alternative clock source.
HI2CITSCL Sets the TSCL period to Zero as the Stop #define HI2CITSCLWaitPeriod 70
WaitPerio condition must be held for TSCL after Stop
d
transition. Default to 70, some solutions can
use this set to 0. The clock source and clock
method must be reviewed before changing
this setting.
Port Settings:
The settings of the pin direction is critical to the operation of these methods.
For the Microchip SSP/MSSP modules both ports must be set as input.
For the Microchip I2C module bith ports must be set as output. And, configure the pins as open-drain
and set the I2C levels - see example below for usage.
In all case the data and clock line *must * be pulled up with an appropriate resistor (typically 4.k @
5.0v for 100Mkz transmissions).
493
Constant Controls Default Value
Clock Sources: The Microchip I2C can select one of ten clocks sources as shown in the table below.
I2C1Clock_MFINTOSC is the default which support 125hz.
To change the clock source, change the constant as follows:
Clock Constants
Alternative Clock Source: Using SMTClock source as the clock spurce, the following methods
implement the SMT as the clock source. The defintion of the constant, the include, setting of the SMT
period, initialisation and starting of the clock source are ALL required.
494
#define I2C1ClockSource I2C1Clock_SMT1
#Include <SMT_Timers.h>
Error Codes: This module has extensive error reporting. For the standard error report refer to the
appropriate datasheet. Great Cow BASIC also exposes the following error messages to enable the user
code to handle the errors appropriately. These are exposed via the variable HI2C1lastError - the bits of
the HI2C1lastError are set as in the table shown below.
I2C1_GOOD 0
I2C1_FAIL_TIMEOUT 1
I2C1_TXBE_TIMEOUT 2
I2C1_START_TIMEOUT 4
I2C1_RESTART_TIMEOUT 8
I2C1_RXBF_TIMEOUT 16
I2C1_ACK_TIMEOUT 32
I2C1_MDR_TIMEOUT 64
I2C1_STOP_TIMEOUT 128
Shown below are two examples of using Hardware I2C with Great Cow BASIC.
Example 1:
This example examines the IC2 modules using the Microchip SSP/MSSP module and the AVR
microcontrollers. This will display the result on a serial terminal. This code will require adaption but
the code shows an approach to discover the IC2 devices.
#chip mega328p, 16
#config MCLRE_ON
495
' Define I2C settings
#define HI2C_BAUD_RATE 400
#define HI2C_DATA PORTC.5
#define HI2C_CLOCK PORTC.4
'I2C pins need to be input for SSP module when used on Microchip PIC device
Dir HI2C_DATA in
Dir HI2C_CLOCK in
'MASTER MODE
HI2CMode Master
HSerPrintCRLF 2
HSerPrint "Hardware I2C Discover using the "
HSerPrint CHipNameStr
HSerPrintCRLF 2
end if
HI2CStop
next
HSerPrintCRLF
HSerPrint "End of Device Search"
HSerPrintCRLF 2
496
This example examines the IC2 devices and displays on a serial terminal for the I2C module only.
This code will require adaption but the code shows an approach to discover the IC2 devices.
This code will only operate on the Microchip I2C module.
#chip 18f25k42, 16
#option Explicit
#config mclr=on, LVP=on
#startup InitPPS, 85
Sub InitPPS
'Module: UART1
RC6PPS = 0x0013 'TX1 > RC6
U1RXPPS = 0x0017 'RC7 > RX1
End Sub
'For this solution we can set the TSCL period to Zero as the Stop condition must be
held for TSCL after Stop transition
#define HI2CITSCLWaitPeriod 0
497
'****************************************************************************************
*************
'Main program commences here.. everything before this is setup for the board.
do
HSerPrintCRLF
HSerPrint "Hardware I2C "
HSerPrintCRLF 2
498
'Do a stop.
HI2CStop
Else
HSerPrint "! "
end if
next
HSerPrintCRLF 2
HSerPrint "End of Search"
HSerPrintCRLF 2
wait 1 s
wait while SwitchIn = On
loop
Supported in <HI2C.H>
HI2CAckPollState
Syntax:
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
Should only be used when I2C routines are operating in Master mode, this command will return the
last state of the acknowledge response from a specific I2C device on the I2C bus.
Note:
This command is also available on microcontrollers with a second hardware I2C port.
499
Example:
This example code would display the devices on the I2C bus.
...
for deviceID = 0 to 255
HI2CStart
HI2CSend ( deviceID )
Supported in <HI2C.H>
HI2CReceive
Syntax:
HI2CReceive data
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
The HI2CReceive command will send data through the I2C connection. If ack is TRUE, or no value is
given for ack, then HI2CReceive will send an ack to the I2C bus.
If in master mode, HI2CReceive will read the data immediately. If in slave mode, HI2CReceive will wait
for the master to send the data before reading.
Note:
This command is also available on microcontrollers with a second hardware I2C port.
500
HI2C2Receive _data_
Example 1:
'This program reads an I2C register and sets an LED if it is over 100.
'It will read from I2C device with an address of 83, register 1.
' Change the processor
#chip 16F1937, 32
#config Osc = intOSC, MCLRE_ON, PLLEN_ON, VCAPEN_OFF
'Misc settings
#define LED PORTB.0
'Main loop
Do
'Send start
HI2CStart
'Request value
HI2CSend 83
HI2CSend 1
'Read value
HI2CReceive ValueIn
'Send stop
HI2CStop
501
'Turn on LED if received value > 100
Set LED Off
If ValueIn > 100 Then Set LED On
'Delay
Wait 20 ms
Loop
Example 2:
See the I2C Overview for the Master mode device to control this Slave mode device.
; This Slave device responds to address 0x60 and may only be written to.
; Within it, there are three registers, 0,1 and 2 corresponding to the three LEDs.
Writing a zero
; turns the respective LED off. Writing anything else turns it on.
#chip 16F88, 4
#config INTRC_IO,MCLR_Off
#define I2C_BIT_DELAY 20 us
#define I2C_CLOCK_DELAY 10 us
#define I2C_END_DELAY 10 us
'Serial settings
#define SerInPort PORTB.6
#define SerOutPort PORTB.7
502
InitSer 1, r2400, 1 + WaitForStart, 8, 1, none, INVERT
wait 1 s
;----- Variables
;----- Program
503
end Repeat
do forever
if reg <> oldreg then ; only process when the reg is a new value
oldreg = reg ; retain old value
show = 1 ; its time to show the LEDS!
if value <> oldvalue then ; logic for tracking old values. You only want to
update terminal once per change
oldvalue = value
show = 1
end if
end if
SerPrint 1, "0x"+hex(addr)
SerSend 1,9
SerPrint 1, STR(reg)
SerSend 1,9
SerPrint 1, STR(value)
SerSend 1,10
SerSend 1,13
show = 0
end if
loop
Sub I2C_Interrupt
' handle interrupt
IF SSPIF=1 THEN ; its a valid interrupt
504
mempointer = 0 ; set the memory pointer. This code emulates an
EEPROM!
end if
IF addr = ( I2C_ADDRESS | 1 ) THEN ; its our write address
CKP = 0 ; acknowledge command
; If the SDA line was low (ACK), the transmit data must be
loaded into
; the SSPBUF register which also loads the SSPSR
; register. Then, pin RB4/SCK/SCL should be enabled
; by setting bit CKP.
else
505
readpointer++ ' increment our counter
END IF
END IF
CKP = 1 ' acknowledge command
SSPOV = 0 ' acknowledge command
END IF
SSPIF=0
END SUB
sub UpdateLEDS
case 1
if value = 1 then
set LED1 on
else
set LED1 off
end if
case 2
if value = 1 then
set LED2 on
else
set LED2 off
end if
end select
End Sub
Supported in <HI2C.H>
HI2CRestart
Syntax:
506
HI2CRestart
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
If the HI2C routines are operating in Master mode, this command will send a start and restart
condition in a single command.
Note:
This command is also available on microcontrollers with a second hardware I2C port.
HI2C2Restart
Example:
do
HI2CReStart ;generate a start signal
HI2CSend(eepDev) ;inidcate a write
loop While HI2CAckPollState
Supported in <HI2C.H>
507
HI2CSend
Syntax:
HI2CSend data
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
The HI2CSend command will send data through the I2C connection. If in master mode, HI2CSend will
send the data immediately. If in slave mode, HI2CSend will wait for the master to request the data
before sending.
Note:
This command is also available on microcontrollers with a second hardware I2C port.
HI2C2Send data
Example:
This example code retrieves multiple bytes from an EEPROM memory device.
508
do
HI2CReStart ;generate a start signal
HI2CSend(eepDev) ;indicate a write
loop While HI2CAckPollState
Supported in <HI2C.H>
HI2CStart
Syntax:
HI2CStart
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
If the HI2C routines are operating in Master mode, this command will send a start condition. If
routines are in Slave mode, it will pause the program until a start condition is sent by the master. It
should be placed at the start of every I2C transmission.
Note:
This command is also available on microcontrollers with a second hardware I2C port.
509
HI2C2Start
Example:
Supported in <HI2C.H>
HI2CStartOccurred
Syntax:
HI2CStartOccurred
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
Check if a start condition has occurred since the last run of this function
Note:
This command is also available on microcontrollers with a second hardware I2C port.
HI2C2StartOccurred
Supported in <HI2C.H>
HI2CMode
Syntax:
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
510
Sets the microcontroller to either a Master device or a Slave device.
Note:
This command is also available on microcontrollers with a second hardware I2C port.
Supported in <HI2C.H>
HI2CSetAddress
Syntax:
HI2CSetAddress address_number
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
Note:
This command is also available on microcontrollers with a second hardware I2C port.
HI2C2SetAddress address_number
Supported in <HI2C.H>
HI2CStop
Syntax:
HI2CStop
Command Availability:
511
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
Note:
This command is also available on microcontrollers with a second hardware I2C port.
HI2C2Stop
Example:
Supported in <HI2C.H>
HI2CStopped
Syntax:
HI2CStopped
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
In Slave mode only. Check if start condition received since last used of HI2CStopped.
Note:
This command is also available on microcontrollers with a second hardware I2C port.
HI2C2Stopped
Supported in <HI2C.H>
HI2CWaitMSSP
Syntax:
512
HI2CWaitMSSP
Command Availability:
Only available for microcontrollers with the hardware I2C or TWI module.
Explanation:
The methods sets the global byte variable HI2CWaitMSSPTimeout to 255 ( or True ) if the MSSP module
has timeout during operations.
Note:
This command is also available on microcontrollers with a second hardware I2C port.
HI2C2WaitMSSP
Supported in <HI2C.H>
513
Sound
This is the Sound section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Sound Overview
Introduction:
These Great Cow BASIC methods generate tones of a given frequency and duration.
Method Controls
Tone Generate a specified tone for a specified duration in terms of a frequency of a specified
Mhz and units of 10ms
ShortTon Generate a specified tone for a specified duration in terms of a frequency of a 10Mhz and
e units of 1ms
Play Play a tune string. The format of the string is the QBASIC play command.
PlayRTTT Play a tune string. The format of the string is the Nokia cell phone RTTTL format.
L
Relevant Constants:
These constants are used to control settings for the tone generation routines. To set them, place a line
in the main program file that uses #define to assign a value to the particular constant.
Note: If an exact frequency is required, or a smaller program is needed, these routines should not be
used. Instead, you should use code like this:
Repeat count
PulseOut SoundOut, period us
Wait period us
End Repeat
514
Tone
Syntax:
Command Availability:
Explanation:
This command will produce the specified tone for the specified duration. Frequency is measured in Hz,
and Duration is in 10 ms units.
Please note that this command may not produce the exact frequency specified. While it is accurate
enough for error beeps and small pieces of monophonic music, it should not be used for anything that
requires a highly precise frequency.
Example:
Do
Tone 440, 1000
Loop
ShortTone
Syntax:
Command Availability:
Explanation:
This command will produce the specified tone for the specified duration. Frequency is measured in
515
units of 10 Hz, and Duration is in 1 ms units. Please note that this command may not produce the exact
frequency specified. While it is accurate enough for error beeps and small pieces of monophonic
music, it should not be used for anything that requires a highly precise frequency.
Example:
#chip 16F88, 20
#define SoundOut PORTB.1
Dir PORTA.0 In
Do
ShortTone ReadAD(AN0), 100
Loop
Play
Syntax:
Play SoundPlayDataString
You must specify the following include and the port of the sound device.
#include <songplay.h>
#define SOUNDOUT PORTN.N
Explanation: This command will plays a QBASIC sequence of notes. The SoundPlayDataString is a
string representing a musical note or notes to play where Notes are A to G.
Comman Description
d
A-G May be followed by length: 2 = half note, 4 = quarter, also may be followed by # or + (sharp)
or - (flat).
516
Comman Description
d
Comman Description
d
M Play mode
Example:
Play RTTTL
Syntax:
PlayRTTTL SoundPlayRTTTLDataString
You must specify the following include and the port of the sound device.
#include <songplay.h>
#define SOUNDOUT PORTN.N
517
Command Availability:: Available on all microcontrollers.
Explanation: This command will play a sequence of notes in the Nokia RTTTL string format.
The SoundPlayRTTTLDataString is a string representing a musical note or notes to play where Notes
are A to G. This format and information below is credited to WikiPedia, see here. To be recognized by
ringtone programs, an RTTTL/Nokring format ringtone must contain three specific elements: name,
settings, and notes. For example, here is the RTTTL ringtone for Haunted House:
HauntHouse: d=4,o=5,b=108: 2a4, 2e, 2d#, 2b4, 2a4, 2c, 2d, 2a#4, 2e., e, 1f4, 1a4, 1d#, 2e., d, 2c., b4, 1a4,
1p, 2a4, 2e, 2d#, 2b4, 2a4, 2c, 2d, 2a#4, 2e., e, 1f4, 1a4, 1d#, 2e., d, 2c., b4, 1a4
• Part 1: name of the ringtone (here: "HauntHouse"), a string of characters represents the name of
the ringtone
• Part 2: settings (here: d=4,o=5,b=108), where "d=" is the default duration of a note. In this case, the
"4" means that each note with no duration specifier (see below) is by default considered a quarter
note. "8" would mean an eighth note, and so on. Accordingly, "o=" is the default octave. There are
four octaves in the Nokring/RTTTL format. And "b=" is the tempo, in "beats per minute".
• Part 3: the notes. Each note is separated by a comma and includes, in sequence: a duration
specifier, a standard music note, either a, b, c, d, e, f or g, and an octave specifier. If no duration or
octave specifier are present, the default applies.
Example 1:
#chip 16f877a
#include <songplay.h>
Example 2:
518
#chip 16f877a
#include <songplay.h>
'Defines
#define SoundOut PORTC.0
wait 1 s
SoundPlayRTTTLDataString =
"Thegood,:d=4,o=6,b=63:32c,32f,32c,32f,c,8g_5,8a_5,f5,8p,32c,32f,32c,32f,c,8g_5,8a_5,d_"
PlayRTTTL(SoundPlayRTTTLDataString)
wait 1 s
SoundPlayRTTTLDataString
="LedZeppel:d=4,o=6,b=80:8g,16p,8f_,16p,8f,16p,8e,16p,8d,8a5,8c,16p,8b5,16p,a_5,8a5,16f5,
16e5,16d5,8p,16p,16a_5,16a_5,16a_5,8p,16p,16b5,16b5,16b5,8p,16p,16b5,16b5,16b5,8p,16p,16c
,16c,16c,8p,16p,16c,16c,16c"
PlayRTTTL(SoundPlayRTTTLDataString)
Do Forever
Loop
End
519
Timers
This is the Timers section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Timer Overview
Great Cow BASIC supports methods to set, clear, read, start and stop the microcontroller timers.
Timer 0
Timer 1
Timer 2
Timer 3
Timer 4
Timer 5
Timer 6
Timer 7
Timer 8
Timer 10
Timer 12
Not all of these timers available on all microcontrollers. For example, if a microcontroller has three
timers, then typically only Timer0, Timer1 and Timer2 will be available.
Please refer to the datasheet for your microcontroller to determine the supported timers and if a
specific timer is 8-bit or 16-bit.
To initialise and change the timers you may have to change the Prescaler.
A Prescaler is an electronic counting circuit used to reduce a high frequency electrical signal to a lower
frequency by integer division. The prescaler takes the basic timer clock frequency and divides it by
some value before being processed by the timer, according to how the Prescaler register(s) are
configured. The prescaler values that may be configured might be limited to a few fixed values, see the
timer specfic page in this Help file or refer to the datasheet.
To use a Prescaler some simple integer maths is required, however, when calculating the Prescaler
there is often be a tradeoff between resolution, where a high resolution requires a high clock
frequency and range where a high clock frequency willl cause the timer to overflow more quickly. For
example, achieving 1 us resolution and a 1 sec maximum period using a 16-bit timer may require some
clever thinking when using 8-bit timers. Please ask for advice via the Great Cow BASIC forum, or,
search for some of the many great resources on the internet to calculate a Prescaler value.
520
Common Langauge:
Using timers has the following terms /common langauge. This following paragraph is intended to
explain the common language.
The Oscillator (OSC) is the system clock, this can be sourced from an internal or external source, OSC is
same the as microcontroller Mhz. This is called the the Frequency of the OSCillator (FOSC) or the
System Clock.
On a Microchip PIC microcontroller, one machine code instruction is executed for every four system
clock pulses.
This means that instructions are executed at a frequency of FOSC/4.
The Microchip PIC datasheets call this FOSC/4 or FOSC4.
All Microchip PIC timer prescales are based on the FOSC/4, not the FOSC or the System Clock.
As Prescale are based upon FOSC/4, you must use FOSC/4 in your timer calculations to get the results
you expect.
All Prescale and Postscale values are integer numbers.
On Atmel AVR microcontroller, most machine code instructions will execute in a single clock pulse.
Initialising a timer for a Microchip PIC microcontroller may not operate as expected when using the
same code for an Atmel AVR microcontroller by simply changing the #chip definition. You must
recalculate the Prescaler of a timer when moving timer parameters between Microchip PIC and Atmel
AVR microcontrollers. And, of course, the same when moving timer parameters between Atmel AVR
and Microchip PIC microcontrollers.
Initialising microcontrollers with very limited RAM using Great Cow BASIC needs carefull
consideration. RAM may be need to be optimised by using ASM to control the timers. You can use Great
Cow BASIC to create the timer related Great Cow BASIC ASM code then manually edit the Great Cow
BASIC ASM to optmise RAM usage. Add your revised and optimised ASM back into your program and
then remove the no longer required calls the the Great Cow BASIC methods. If you need advice on this
subject please ask for advice via the Great Cow BASIC forum.
A Microchip PIC microcontroller can have one of two types of 8-bit timer 2/4/6/8.
The first type has only one clock source and that clock is the FOSC/4 source.
The second type is much more flexible and can have many different clock sources and supports more
prescale values.
The timer type for a Microchip PIC microcontroller can be determined by checking for the existence of
a T2CLKCON register, either in the Datasheet or in the Great Cow BASIC "dat file" for the specific
521
microcontroller.
If the microcontroller DOES NOT have a T2CLKCON register then ALL Timer 2/4/6/8 timers on that chip
are the first type, and are configured using:
If the microcontroller DOES have a T2CLKCON register then ALL Timer 2/4/6/8 timers on that chip are
the second type and are configured using:
The possible Source, PreScale and PostScale constants for each type are shown in the Great Cow BASIC
Help file. See each timer for the constants.
The "Period" of these timers is determined by the system clock speed, the prescale value and 8-bit
value in the respective timer period register. The timer period registers are PR2, PR4, PR6 or PR8 for
timer2, timer4, timer6 and timer8 respectively. These registers are also called PRx and TMRx where
the x refers to specific timer number.
When a specific timer is enabled/started the TMRx timer register will increment until the TMRx
register matches the value in the PRx register. At this time the TMRx register is cleared to 0 and the
timer continues to increment until the next match of the PRx register, and so on until the timer is
stopped. The lower the value of the PRx register, the shorter the timer period will be. The default
value for the PRX register at power up is 255.
The timer interrupt flag (TMRxIF) is set based upon the number of match conditions as determine by
the postscaler. The postscaler does not actually change the timer period, it changes the time between
interrupt conditions.
ClearTimer
Syntax:
ClearTimer TimerNo
Command Availability:
Available on all Microchip PIC and Atmel AVR microcontrollers with built in timer modules.
522
Explanation:
Cleartimer can be used on-the-fly if desired, so there is no requirement to stop the timer first.
Example:
.....
'Clear timer 1
ClearTimer 1
.....
InitTimer0
Syntax:
Command Availability:
See also see: InitTimer0 8bit/16bit for support for microcontrollers with a 8 bit/16 bit Timer 0 module.
Explanation:
Paramet Description
er
source The clock source for this specific timer. Can be either Osc or Ext where`Osc` is an internal
oscillator and Ext is an external oscillator.
Osc - Selects the clock source in use, as set by the microcontroller specific configuration
(fuses or #config). This could be an internal clock or an external clock source ( external
clock sources are typically attached to the XTAL pins). Ext - Selects the clock source
attached to a specific exernal interrupt input port. This allows a different clock frequency
than the main clock to be used, such as 32.768 kHz crystals commonly used for real time
circuits.
523
Paramet Description
er
prescaler The value of the prescaler for this specific timer. See the tables below for permitted values
for Microchip PIC or the Atmel AVR microcontrollers.
When the timer overflows from 255 to 0, a Timer0Overflow interrupt will be generated. This can be
used in conjunction with On Interrupt to run a section of code when the overflow occurs.
On Microchip PIC microcontrollers where the prescaler rate select bits are in the range of 2 to 256 you
should use one of the following constants. If the prescaler rate select bits are in the range of 1 to 32768
then see the subsequent table.
1:2 PS0_2 0
1:4 PS0_4 1
1:8 PS0_8 2
1:16 PS0_16 3
1:32 PS0_32 4
1:64 PS0_64 5
1:128 PS0_128 6
1:256 PS0_256 7
These correspond to a prescaler of between 1:2 and 1:256 of the oscillator speed where the oscillator
speed is (FOSC/4). The prescaler applies to both the internal oscillator or the external clock.
The prescaler will only apply when the timer is driven from the Osc the internal oscillator - the
prescaler has no effect when the external clock source is specified.
524
Prescaler Value Primary GCB Constant Secondary GCB Constant Constant Equates
to value
This code uses Timer 0 and On Interrupt to generate a Pulse Width Modulation signal, that will allow
the speed of a motor to be easily controlled.
#chip 16F88, 8
#config osc = int
'Main routine
Do
'Increase speed to full over 2.5 seconds
For Speed = 0 to 100
MotorSpeed = Speed
Wait 25 ms
Next
'Hold speed
Wait 1 s
'Decrease speed to zero over 2.5 seconds
For Speed = 100 to 0
MotorSpeed = Speed
Wait 25 ms
Next
'Hold speed
Wait 1 s
Loop
'Setup routine
Sub InitMotorControl
525
'Clear variables
MotorSpeed = 0
PWMCounter = 0
'Set up the timer using the internal oscillator with a prescaler of 1/2 (Equates
to 0)
'Timer 0 starts automatically on a Microchip PIC microcontroller, therefore,
StartTimer is not required.
InitTimer0 Osc, PS0_2
End Sub
'PWM sub
'This will be called when Timer 0 overflows
Sub PWMHandler
If MotorSpeed > PWMCounter Then
Set MOTOR On
Else
Set MOTOR Off
End If
PWMCounter += 1
If PWMCounter = 100 Then PWMCounter = 0
End Sub
#chip 16f18855,32
#option Explicit
'timer test Program
'Main routine
Do
'Increase speed to full over 2.5 seconds
For Speed = 0 to 100
526
MotorSpeed = Speed
Wait 25 ms
Next
'Hold speed
Wait 1 s
'Decrease speed to zero over 2.5 seconds
For Speed = 100 to 0
MotorSpeed = Speed
Wait 25 ms
Next
'Hold speed
Wait 1 s
Loop
'Setup routine
Sub InitMotorControl
'Clear variables
MotorSpeed = 0
PWMCounter = 0
End Sub
'PWM sub
'This will be called when Timer 0 overflows
Sub PWMHandler
End Sub
Supported in <TIMER.H>
InitTimer0 8bit/16bit
Syntax:
527
InitTimer0 source, prescaler, postscaler
Command Availability:
Timer 0 on Microchip PIC 18(L)F, as well as small number of 18C and 16(L)F microcontrollers, can be
configured for either 8-bit or 16-bit operation.
Refer to the datasheet for your microcontroller to determine if it supports both 8-bit and 16-bit
operation.
See also see: InitTimer0 for microcontroller with only an 8 bit Timer 0 module.
Explanation:
Paramet Description
er
source The clock source for this specific timer. Can be either Osc or Ext where`Osc` is an internal
oscillator and Ext is an external oscillator.
Osc - Selects the clock source in use, as set by the microcontroller specific configuration
(fuses or #config). This could be an internal clock or an external clock source ( external
clock sources are typically attached to the XTAL pins). Ext - Selects the clock source
attached to a specific exernal interrupt input port. This allows a different clock frequency
than the main clock to be used, such as 32.768 kHz crystals commonly used for real time
circuits.
prescaler The value of the prescaler for this specific timer. See the tables below for permitted values
for Microchip PIC or the Atmel AVR microcontrollers.
When the timer overflows to 0, a Timer0Overflow interrupt will be generated. This can be used in
conjunction with On Interrupt to run a section of code when the overflow occurs.
On Microchip PIC microcontrollers where the prescaler rate select bits are in the range of 2 to 256 you
should use one of the following constants. If the prescaler rate select bits are in the range of 1 to 32768
then see the subsequent table.
528
Prescaler Value Primary GCB Constant Constant Equates
to value
1:2 PS0_2 0
1:4 PS0_4 1
1:8 PS0_8 2
1:16 PS0_16 3
1:32 PS0_32 4
1:64 PS0_64 5
1:128 PS0_128 6
1:256 PS0_256 7
These correspond to a prescaler of between 1:2 and 1:256 of the oscillator speed where the oscillator
speed is (FOSC/4). The prescaler applies to both the internal oscillator or the external clock.
On Chips with 8/16-bit Timer0, when in 8-bit mode, Timer0 behaves much like Timers2/4/6. In 8-bit
mode the TMR0H register does not increment. It instead becomes the Period or Match register and is
aliased as "PR0" (Period Register 0).
In 8-bit mode, Timer0 does not technically overflow. Instead when TMR0L increments and matches the
value in the PR0 register, TMR0L is reset to 0. The interrupt flag bit (TMR0IF) bit is then set (based upon
Postscaler).
The default value in the PR0 "match register" is 255. This value can be set/changed in the user program
to set/change the timer period. This can be used to fine tune the timer period.
To configure PIC Timer 0 for 16_bit operation add the following line to the source code.
#define TMR0_16bit
As shown above, where Timer 0 supports 16bit you must use the following syntax:
529
An example is shown below using the Great Cow BASIC constants. See the tables below for the
constants.
In this example specfic values have been passed to the method. Great Cow BASIC supports passing
specfic values to setup Timer 0. These specfic values be obtained from the MicroChip Configurator.
InitTimer0 Osc, 0x91 , 0x48 ' Where these values are specfic to the timer
setup.
Paramet Description
er
source The clock source for this specific timer. Can be either Osc or Ext where Osc is an internal
oscillator and Ext is an external oscillator.
prescaler The value of the prescaler for this specific timer. See the tables below for permitted values
for Microchip PIC or the Atmel AVR microcontrollers. You may also be required to specify
one of the following clock sources.
TMR0_CLC1
TMR0_SOSC
TMR0_LFINTOSC
TMR0_HFINTOSC
TMR0_FOSC4
TMR0_T0CKIPPS_Inverted
TMR0_T0CKIPPS_True
You should use a simple addition to concatenate the prescaler with a specific clock source.
For example.
PRE0_16 + TMR0_HFINTOSC
postscale See the tables below for permitted values for Microchip.
r Also, refer to the specific datasheet postcaler values.
530
Microchip PIC microcontrollers where the prescaler rate select bits are in the range of 1 to 32768 you
should use one of the following constants.
1:1 PRE0_1 0
1:2 PRE0_2 1
1:4 PRE0_4 2
1:8 PRE0_8 3
1:16 PRE0_16 4
1:32 PRE0_32 5
1:64 PRE0_64 6
1:128 PRE0_128 7
1:256 PRE0_256 8
1:512 PRE0_512 9
1:1024 PRE0_1024 10
1:2048 PRE0_2048 11
1:4096 PRE0_4096 12
1:8192 PRE0_8192 13
1:16384 PRE0_16384 14
1:32768 PRE0_32768 15
These correspond to a prescaler of between 1:1 and 1:32768 of the oscillator speed where the oscillator
speed is (FOSC/4). The prescaler applies to both the internal oscillator or the external clock.
On Microchip PIC microcontroller that require postscaler is can be one of the following constants
where the Postscaler Rate Select bits are in the range of 1 to 16.
1:1 POST0_1 0
1:2 POST0_2 1
1:3 POST0_3 2
531
Postcaler Value Primary GCB Constant Use Numeric Constant
1:4 POST0_4 3
1:5 POST0_5 4
1:6 POST0_6 5
1:7 POST0_7 6
1:8 POST0_8 7
1:9 POST0_9 8
1:10 POST0_10 9
1:11 POST0_11 10
1:12 POST0_12 11
1:13 POST0_13 12
1:14 POST0_14 13
1:15 POST0_15 14
1:16 POST0_16 15
Example:
This code uses Timer 0 and On Interrupt to generate a Pulse Width Modulation signal, that will allow
the speed of a motor to be easily controlled.
#chip 16F88, 8
#config osc = int
'Main routine
Do
'Increase speed to full over 2.5 seconds
For Speed = 0 to 100
MotorSpeed = Speed
Wait 25 ms
Next
532
'Hold speed
Wait 1 s
'Decrease speed to zero over 2.5 seconds
For Speed = 100 to 0
MotorSpeed = Speed
Wait 25 ms
Next
'Hold speed
Wait 1 s
Loop
'Setup routine
Sub InitMotorControl
'Clear variables
MotorSpeed = 0
PWMCounter = 0
'Set up the timer using the internal oscillator with a prescaler of 1/2 (Equates
to 0)
'Timer 0 starts automatically on a Microchip PIC microcontroller, therefore,
StartTimer is not required.
InitTimer0 Osc, PS0_2
End Sub
'PWM sub
'This will be called when Timer 0 overflows
Sub PWMHandler
If MotorSpeed > PWMCounter Then
Set MOTOR On
Else
Set MOTOR Off
End If
PWMCounter += 1
If PWMCounter = 100 Then PWMCounter = 0
End Sub
Supported in <TIMER.H>
InitTimer1
Syntax:
533
InitTimer1 source, prescaler
Command Availability:
Explanation:
Paramet Description
er
source The clock source for this specific timer. Can be either Osc, Ext or ExtOsc where:
Osc is an internal oscillator.
Ext is an external oscillator.
Osc - Selects the clock source in use, as set by the microcontroller specific configuration
(fuses or #config). This could be an internal clock or an external clock source ( external
clock sources are typically attached to the XTAL pins). Ext - Selects the clock source
attached to a specific exernal interrupt input port. This allows a different clock frequency
than the main clock to be used, such as 32.768 kHz crystals commonly used for real time
circuits.
prescaler The value of the prescaler for this specific timer. See the tables below for permitted vales
for Microchip PIC or the Atmel AVR microcontrollers.
When the timer overflows an interrupt event will be generated. This interrupt event can be used in
conjunction with On Interrupt to run a section of code when the interrupt event occurs.
534
Microchip PIC microcontrollers:
1:1 PS1_1 0
1:2 PS1_2 16
1:4 PS1_4 32
1:8 PS1_8 48
These correspond to a prescaler of between 1:2 and 1:8 of the oscillator (FOSC/4) speed. The prescaler
will apply to either the oscillator or the external clock input.
On the majority of Atmel AVR microcontrollers prescaler must be one of the following constants:
The prescaler will only apply when the timer is driven from the Osc the internal oscillator - the
prescaler has no effect when the external clock source is specified.
Prescaler Value Primary GCB Constant Secondary GCB Constant Constant Equates
to value
535
The prescaler will only apply when the timer is driven from the Osc the internal oscillator - the
prescaler has no effect when the external clock source is specified.
1:0 PS_1_0 0
1:1 PS_1_1 1
1:2 PS_1_2 2
1:4 PS_1_4 3
1:8 PS_1_8 4
1:16 PS_1_16 5
1:32 PS_1_32 6
1:64 PS_1_64 7
1:128 PS_1_128 8
1:256 PS_1_256 9
1:512 PS_1_512 10
1:1024 PS_1_1024 11
1:2048 PS_1_2048 12
1:4096 PS_1_4096 13
1:8192 PS_1_8192 14
1:16384 PS_1_16384 15
Example 1 (Microchip):
This example will measure that time that a switch is depressed (or on) and will write the results to the
EEPROM.
536
#chip 16F819, 20
#define Switch PORTA.0
Dir Switch In
DataCount = 0
'Initilise Timer 1
InitTimer1 Osc, PS1_8
Do
ClearTimer 1
Wait Until Switch = On
StartTimer 1
Wait Until Switch = Off
StopTimer 1
This example will flash the yellow LED on an Arduino Uno (R3) once every second.
537
On Interrupt Timer1Overflow Call Flash_LED
Do
'Wait for interrupt
loop
Sub Flash_LED
Settimer 1, 3200 'Preload timer
pulseout LED, 100 ms
End Sub
Supported in <TIMER.H>
InitTimer2
or, where you required to state the clock source, use the following
Command Availability:
Available on all microcontrollers with a Timer 2 module. As shown above a Microchip microcontroller
can potentially support two types of methods for initialisation.
This the most common method to initialise a Microchip microcontroller timer. With this method the
timer has only one possible clock source, this mandated by the microcontrollers architecture, and that
clock source is the System Clock/4 also known as FOSC/4.
538
The second method is much more flexible in term of the clock source. Microcontrollers that support
this second method enable you to select different clock sources and to select more prescale values. The
method is shown below:
How do you determine which method to use for your specific Microchip microcontroller ?
The timer type for a Microchip microcontroller can be determined by checking for the existance of a
T2CLKCON register, either in the Datasheet or in the Great Cow BASIC "dat file" for the specific device.
If the Microchip microcontroller DOES NOT have a T2CLKCON register then timers 2/4/6/8 for that
specific microcontroller chip use the first method, and are configured using:
If the microcontroller DOES have a T2CLKCON register then ALL timers 2/4/6/8 for that specific
microcontroller chip use the second method, and are configured using:
InitTimer2 (Source,PreScale,PostScale)
The possible Source, Prescale and Postscale constants for each type are shown in the tables
below. These table are summary tables from the Microchip datasheets.
The Period of the timer is determined by the system clock speed, the prescale value and 8-bit value in
the respective timer period register. The timer period for timer 2 is held in register PR2.
When the timer is enabled, by starting the timer, it will increment until the TMR2 register matches the
value in the PR2 register. At this time the TMR2 register is cleared to 0 and the timer continues to
increment until the next match, and so on.
The lower the value of the PR2 register, the shorter the timer period will be. The default value for the
PR2 register at power up is 255.
The timer interrupt flag (TMR2IF) is set based upon the number of match conditions as determine by
the postscaler. The postscaler does not actually change the timer period, it changes the time between
interrupt conditions.
539
Timer constants for the MicroChip microcontrollers
Paramete Description
r
clocksourc This is an optional parameter. Please review the datasheet for specific usage.
e
Source can be one of the following numeric values:
prescaler The value of the prescaler for this specific timer. See the tables below for permitted
values.
postscaler The value of the postscaler for this specific timer. See the tables below for permitted
values.
prescaler can be one of the following settings, if you MicroChip microcontroller has the T2CKPS4 bit
then refer to table 3:
1:1 PS2_1 0
1:4 PS2_4 1
1:16 PS2_16 2
1:64 PS2_64 3
Note that a 1:64 prescale is only avaialable on certain midrange microcontrollers. Please refer to the
datasheet to determine if a 1:64 prescale is supported by a spectific microcontroller.
540
Prescaler Value Primary GCB Constant Constant Equates
to value
1:1 PS2_1 0
1:2 PS2_2 1
1:4 PS2_4 2
1:8 PS2_8 3
1:16 PS2_16 4
1:32 PS2_32 5
1:64 PS2_64 6
1:128 PS2_128 7
postscaler slows the rate of the interrupt generation (or WDT reset) from a counter/timer by dividing it
down.
On Microchip PIC microcontroller one of the following constants where the Postscaler Rate Select bits
are in the range of 1 to 16.
541
Postcaler Value GCB Constant Eqautes to
Explanation:(Atmel AVR)
source can be one of the following settings: Parameters for this timer are detailed in the table below:
Paramet Description
er
source The clock source for this specific timer. Can be either Osc or Ext where`Osc` is an internal
oscillator and Ext is an external oscillator.
prescaler for Atmel AVR Timer 2 is chip specific and can be selected from one of the two tables shown
below. Please refer to the datasheet determine which table to use and which prescales within that
table are supported by a specific Atmel AVR microcontroller.
Prescaler Value Primary GCB Constant Secondary GCB Constant Constant Equates
to value
542
Prescaler Value Primary GCB Constant Secondary GCB Constant Constant Equates
to value
Prescaler Value Primary GCB Constant Secondary GCB Constant Constant Equates
to value
Example:
This code uses Timer 2 and On Interrupt to flash an LED every 200 timer ticks.
543
#chip 16F1788, 8
#config OSC = INTOSC
Do
' Wating for interrupt on match val of 100
Loop
InitTimer3
Syntax:
Command Availability:
Explanation:
544
Paramet Description
er
source The clock source for this specific timer. Can be either Osc, Ext or ExtOsc where:
Osc is an internal oscillator.
Ext is an external oscillator.
Osc - Selects the clock source in use, as set by the microcontroller specific configuration
(fuses or #config). This could be an internal clock or an external clock source ( external
clock sources are typically attached to the XTAL pins). Ext - Selects the clock source
attached to a specific exernal interrupt input port. This allows a different clock frequency
than the main clock to be used, such as 32.768 kHz crystals commonly used for real time
circuits.
prescaler The value of the prescaler for this specific timer. See the tables below for permitted vales
for Microchip PIC or the Atmel AVR microcontrollers.
When the timer overflows an interrupt event will be generated. This interrupt event can be used in
conjunction with On Interrupt to run a section of code when the interrupt event occurs.
1:1 PS3_1 0
1:2 PS3_2 16
545
Prescaler Value Primary GCB Constant Constant Equates
to value
1:4 PS3_4 32
1:8 PS3_8 48
These correspond to a prescaler of between 1:2 and 1:8 of the oscillator (FOSC/4) speed. The prescaler
will apply to either the oscillator or the external clock input.
On the majority of Atmel AVR microcontrollers prescaler must be one of the following constants:
The prescaler will only apply when the timer is driven from the Osc the internal oscillator - the
prescaler has no effect when the external clock source is specified.
Prescaler Value Primary GCB Constant Secondary GCB Constant Constant Equates
to value
Supported in <TIMER.H>
InitTimer4
or, where you required to state the clock source, use the following
546
InitTimer4 source, prescaler
Command Availability:
Available on all microcontrollers with a Timer 4 module. As shown above a Microchip microcontroller
can potentially support two types of methods for initialisation.
This the most common method to initialise a Microchip microcontroller timer. With this method the
timer has only one possible clock source, this mandated by the microcontrollers architecture, and that
clock source is the System Clock/4 also known as FOSC/4.
The second method is much more flexible in term of the clock source. Microcontrollers that support
this second method enable you to select different clock sources and to select more prescale values. The
method is shown below:
How do you determine which method to use for your specific Microchip microcontroller ?
The timer type for a Microchip microcontroller can be determined by checking for the existance of a
T2CLKCON register, either in the Datasheet or in the Great Cow BASIC "dat file" for the specific device.
If the Microchip microcontroller DOES NOT have a T4CLKCON register then timers 2/4/6/8 for that
specific microcontroller chip use the first method, and are configured using:
If the microcontroller DOES have a T2CLKCON register then ALL timers 2/4/6/8 for that specific
microcontroller chip use the second method, and are configured using:
InitTimer4 (Source,PreScale,PostScale)
The possible Source, Prescale and Postscale constants for each type are shown in the tables
below. These table are summary tables from the Microchip datasheets.
547
Period of the Timers
The Period of the timer is determined by the system clock speed, the prescale value and 8-bit value in
the respective timer period register. The timer period for timer 4 is held in register PR4.
When the timer is enabled, by starting the timer, it will increment until the TMR4 register matches the
value in the PR4 register. At this time the TMR4 register is cleared to 0 and the timer continues to
increment until the next match, and so on.
The lower the value of the PR4 register, the shorter the timer period will be. The default value for the
PR4 register at power up is 255.
The timer interrupt flag (TMR4IF) is set based upon the number of match conditions as determine by
the postscaler. The postscaler does not actually change the timer period, it changes the time between
interrupt conditions.
Paramete Description
r
prescaler The value of the prescaler for this specific timer. See the tables below for permitted
values.
postscaler The value of the postscaler for this specific timer. See the tables below for permitted
values.
548
prescaler can be one of the following settings, if you MicroChip microcontroller has the T4CKPS4 bit
then refer to table 2:
1:1 PS4_1 0
1:4 PS4_4 1
1:16 PS4_16 2
1:64 PS4_64 3
Table 2
Note that a 1:64 prescale is only avaialable on certain midrange microcontrollers. Please refer to the
datasheet to determine if a 1:64 prescale is supported by a spectific microcontroller.
1:1 PS4_1 0
1:2 PS4_2 1
1:4 PS4_4 2
1:8 PS4_8 3
1:16 PS4_16 4
1:32 PS4_32 5
1:64 PS4_64 6
1:128 PS4_128 7
Table 3
postscaler slows the rate of the interrupt generation (or WDT reset) from a counter/timer by dividing it
down.
On Microchip PIC microcontroller one of the following constants where the Postscaler Rate Select bits
are in the range of 1 to 16.
1:1 Postscaler 0
549
Postcaler Value Use Numeric Constant
1:2 Postscaler 1
1:3 Postscaler 2
1:4 Postscaler 3
1:5 Postscaler 4
1:6 Postscaler 5
1:7 Postscaler 6
1:8 Postscaler 7
1:9 Postscaler 8
1:10 Postscaler 9
1:11 Postscaler 10
1:12 Postscaler 11
1:13 Postscaler 12
1:14 Postscaler 13
1:15 Postscaler 14
1:16 Postscaler 15
Explanation:(Atmel AVR)
source can be one of the following settings: Parameters for this timer are detailed in the table below:
Paramet Description
er
source The clock source for this specific timer. Can be either Osc or Ext where`Osc` is an internal
oscillator and Ext is an external oscillator.
prescaler for Atmel AVR Timer 4 can be selected from the table below.
550
Prescaler Value Primary GCB Constant Secondary GCB Constant Constant Equates
to value
Example:
This code uses Timer 4 and On Interrupt to generate a 1ms pulse 20 ms.
#chip 18F25K80, 8
#config OSC =INTIO2
Do
'Waiting for interrupt on match val of 154
Loop
Sub PulsePin3
pulseout Pin3, 1 ms
End Sub
InitTimer5
Syntax:
551
InitTimer5 source, prescaler
Command Availability:
Explanation:
Paramet Description
er
source The clock source for this specific timer. Can be either Osc, Ext or ExtOsc where:
Osc is an internal oscillator.
Ext is an external oscillator.
Osc - Selects the clock source in use, as set by the microcontroller specific configuration
(fuses or #config). This could be an internal clock or an external clock source ( external
clock sources are typically attached to the XTAL pins). Ext - Selects the clock source
attached to a specific exernal interrupt input port. This allows a different clock frequency
than the main clock to be used, such as 32.768 kHz crystals commonly used for real time
circuits.
prescaler The value of the prescaler for this specific timer. See the tables below for permitted vales
for Microchip PIC or the Atmel AVR microcontrollers.
When the timer overflows an interrupt event will be generated. This interrupt event can be used in
conjunction with On Interrupt to run a section of code when the interrupt event occurs.
552
Microchip PIC microcontrollers:
1:1 PS5_1 0
1:2 PS5_2 16
1:4 PS5_4 32
1:8 PS5_8 48
These correspond to a prescaler of between 1:2 and 1:8 of the oscillator (FOSC/4) speed. The prescaler
will apply to either the oscillator or the external clock input.
On the majority of Atmel AVR microcontrollers prescaler must be one of the following constants:
The prescaler will only apply when the timer is driven from the Osc the internal oscillator - the
prescaler has no effect when the external clock source is specified.
Prescaler Value Primary GCB Constant Secondary GCB Constant Constant Equates
to value
Supported in <TIMER.H>
InitTimer6
553
InitTimer6 prescaler, postscaler
or, where you required to state the clock source, use the following
Command Availability:
Available on all microcontrollers with a Timer 6 module. As shown above a Microchip microcontroller
can potentially support two types of methods for initialisation.
This the most common method to initialise a Microchip microcontroller timer. With this method the
timer has only one possible clock source, this mandated by the microcontrollers architecture, and that
clock source is the System Clock/4 also known as FOSC/4.
The second method is much more flexible in term of the clock source. Microcontrollers that support
this second method enable you to select different clock sources and to select more prescale values. The
method is shown below:
How do you determine which method to use for your specific Microchip microcontroller ?
The timer type for a Microchip microcontroller can be determined by checking for the existance of a
T2CLKCON register, either in the Datasheet or in the Great Cow BASIC "dat file" for the specific device.
If the Microchip microcontroller DOES NOT have a T2CLKCON register then timers 2/4/6/8 for that
specific microcontroller chip use the first method, and are configured using:
554
If the microcontroller DOES have a T2CLKCON register then ALL timers 2/4/6/8 for that specific
microcontroller chip use the second method, and are configured using:
InitTimer6 (Source,PreScale,PostScale)
The possible Source, Prescale and Postscale constants for each type are shown in the tables
below. These table are summary tables from the Microchip datasheets.
The Period of the timer is determined by the system clock speed, the prescale value and 8-bit value in
the respective timer period register. The timer period for timer 6 is held in register PR6.
When the timer is enabled, by starting the timer, it will increment until the TMR6 register matches the
value in the PR6 register. At this time the TMR6 register is cleared to 0 and the timer continues to
increment until the next match, and so on.
The lower the value of the PR6 register, the shorter the timer period will be. The default value for the
PR6 register at power up is 255.
The timer interrupt flag (TMR6IF) is set based upon the number of match conditions as determine by
the postscaler. The postscaler does not actually change the timer period, it changes the time between
interrupt conditions.
555
Paramete Description
r
clocksourc This is an optional parameter. Please review the datasheet for specific usage.
e
Source can be one of the following numeric values:
prescaler The value of the prescaler for this specific timer. See the tables below for permitted
values.
postscaler The value of the postscaler for this specific timer. See the tables below for permitted
values.
prescaler can be one of the following settings, if you MicroChip microcontroller has the T6CKPS4 bit
then refer to table 3:
1:1 PS6_1 0
1:4 PS6_4 1
1:16 PS6_16 2
1:64 PS6_64 3
Table 2
Note that a 1:64 prescale is only avaialable on certain midrange microcontrollers. Please refer to the
datasheet to determine if a 1:64 prescale is supported by a spectific microcontroller.
556
Prescaler Value Primary GCB Constant Constant Equates
to value
1:1 PS6_1 0
1:2 PS6_2 1
1:4 PS6_4 2
1:8 PS6_8 3
1:16 PS6_16 4
1:32 PS6_32 5
1:64 PS6_64 6
1:128 PS6_128 7
Table 3
postscaler slows the rate of the interrupt generation (or WDT reset) from a counter/timer by dividing it
down.
On Microchip PIC microcontroller one of the following constants where the Postscaler Rate Select bits
are in the range of 1 to 16.
1:1 Postscaler 0
1:2 Postscaler 1
1:3 Postscaler 2
1:4 Postscaler 3
1:5 Postscaler 4
1:6 Postscaler 5
1:7 Postscaler 6
1:8 Postscaler 7
1:9 Postscaler 8
1:10 Postscaler 9
1:11 Postscaler 10
1:12 Postscaler 11
1:13 Postscaler 12
557
Postcaler Value Use Numeric Constant
1:14 Postscaler 13
1:15 Postscaler 14
1:16 Postscaler 15
InitTimer7
Syntax:
Command Availability:
Explanation:
558
Paramet Description
er
source The clock source for this specific timer. Can be either Osc, Ext or ExtOsc where:
Osc is an internal oscillator.
Ext is an external oscillator.
Osc - Selects the clock source in use, as set by the microcontroller specific configuration
(fuses or #config). This could be an internal clock or an external clock source ( external
clock sources are typically attached to the XTAL pins). Ext - Selects the clock source
attached to a specific exernal interrupt input port. This allows a different clock frequency
than the main clock to be used, such as 32.768 kHz crystals commonly used for real time
circuits.
prescaler The value of the prescaler for this specific timer. See the tables below for permitted vales
for Microchip PIC or the Atmel AVR microcontrollers.
When the timer overflows an interrupt event will be generated. This interrupt event can be used in
conjunction with On Interrupt to run a section of code when the interrupt event occurs.
1:1 PS7_1 0
1:2 PS7_2 16
559
Prescaler Value Primary GCB Constant Constant Equates
to value
1:4 PS7_4 32
1:8 PS7_8 48
These correspond to a prescaler of between 1:2 and 1:8 of the oscillator (FOSC/4) speed. The prescaler
will apply to either the oscillator or the external clock input.
Supported in <TIMER.H>
InitTimer8
or, where you required to state the clock source, use the following
Command Availability:
Available on all microcontrollers with a Timer 8 module. As shown above a Microchip microcontroller
can potentially support two types of methods for initialisation.
This the most common method to initialise a Microchip microcontroller timer. With this method the
timer has only one possible clock source, this mandated by the microcontrollers architecture, and that
clock source is the System Clock/4 also known as FOSC/4.
The second method is much more flexible in term of the clock source. Microcontrollers that support
560
this second method enable you to select different clock sources and to select more prescale values. The
method is shown below:
How do you determine which method to use for your specific Microchip microcontroller ?
The timer type for a Microchip microcontroller can be determined by checking for the existance of a
T2CLKCON register, either in the Datasheet or in the Great Cow BASIC "dat file" for the specific device.
If the Microchip microcontroller DOES NOT have a T2CLKCON register then timers 2/4/6/8 for that
specific microcontroller chip use the first method, and are configured using:
If the microcontroller DOES have a T2CLKCON register then ALL timers 2/4/6/8 for that specific
microcontroller chip use the second method, and are configured using:
InitTimer8 (Source,PreScale,PostScale)
The possible Source, Prescale and Postscale constants for each type are shown in the tables
below. These table are summary tables from the Microchip datasheets.
The Period of the timer is determined by the system clock speed, the prescale value and 8-bit value in
the respective timer period register. The timer period for timer 8 is held in register PR8.
When the timer is enabled, by starting the timer, it will increment until the TMR8 register matches the
value in the PR8 register. At this time the TMR8 register is cleared to 0 and the timer continues to
increment until the next match, and so on.
The lower the value of the PR8 register, the shorter the timer period will be. The default value for the
PR8 register at power up is 255.
The timer interrupt flag (TMR8IF) is set based upon the number of match conditions as determine by
the postscaler. The postscaler does not actually change the timer period, it changes the time between
interrupt conditions.
561
Paramete Description
r
clocksourc This is an optional parameter. Please review the datasheet for specific usage.
e
Source can be one of the following numeric values:
prescaler The value of the prescaler for this specific timer. See the tables below for permitted
values.
postscaler The value of the postscaler for this specific timer. See the tables below for permitted
values.
1:1 PS8_1 0
1:4 PS8_4 1
1:16 PS8_16 2
1:64 PS8_64 3
Note that a 1:64 prescale is only avaialable on certain midrange microcontrollers. Please refer to the
datasheet to determine if a 1:64 prescale is supported by a spectific microcontroller.
postscaler slows the rate of the interrupt generation (or WDT reset) from a counter/timer by dividing it
down.
562
On Microchip PIC microcontroller one of the following constants where the Postscaler Rate Select bits
are in the range of 1 to 16.
1:1 Postscaler 0
1:2 Postscaler 1
1:3 Postscaler 2
1:4 Postscaler 3
1:5 Postscaler 4
1:6 Postscaler 5
1:7 Postscaler 6
1:8 Postscaler 7
1:9 Postscaler 8
1:10 Postscaler 9
1:11 Postscaler 10
1:12 Postscaler 11
1:13 Postscaler 12
1:14 Postscaler 13
1:15 Postscaler 14
1:16 Postscaler 15
InitTimer10
Syntax:
Command Availability:
Explanation:
563
Paramete Description
r
prescaler The value of the prescaler for this specific timer. See the tables below for permitted
values.
postscaler The value of the postscaler for this specific timer. See the tables below for permitted
values.
1:1 PS10_1 0
1:4 PS10_4 1
1:16 PS10_16 2
1:64 PS10_64 3
Note that a 1:64 prescale is only avaialable on certain midrange microcontrollers. Please refer to the
datasheet to determine if a 1:64 prescale is supported by a spectific microcontroller.
postscaler slows the rate of the interrupt generation (or WDT reset) from a counter/timer by dividing it
down.
On Microchip PIC microcontroller one of the following constants where the Postscaler Rate Select bits
are in the range of 1 to 16.
1:1 Postscaler 0
1:2 Postscaler 1
1:3 Postscaler 2
1:4 Postscaler 3
1:5 Postscaler 4
1:6 Postscaler 5
1:7 Postscaler 6
564
Postcaler Value Use Numeric Constant
1:8 Postscaler 7
1:9 Postscaler 8
1:10 Postscaler 9
1:11 Postscaler 10
1:12 Postscaler 11
1:13 Postscaler 12
1:14 Postscaler 13
1:15 Postscaler 14
1:16 Postscaler 15
InitTimer12
Syntax:
Command Availability:
Explanation:
Paramete Description
r
prescaler The value of the prescaler for this specific timer. See the tables below for permitted
values.
postscaler The value of the postscaler for this specific timer. See the tables below for permitted
values.
565
Prescaler Value Primary GCB Constant Constant Equates
to value
1:1 PS12_1 0
1:4 PS12_4 1
1:16 PS12_16 2
1:64 PS12_64 3
Note that a 1:64 prescale is only avaialable on certain midrange microcontrollers. Please refer to the
datasheet to determine if a 1:64 prescale is supported by a spectific microcontroller.
postscaler slows the rate of the interrupt generation (or WDT reset) from a counter/timer by dividing it
down.
On Microchip PIC microcontroller one of the following constants where the Postscaler Rate Select bits
are in the range of 1 to 16.
1:1 Postscaler 0
1:2 Postscaler 1
1:3 Postscaler 2
1:4 Postscaler 3
1:5 Postscaler 4
1:6 Postscaler 5
1:7 Postscaler 6
1:8 Postscaler 7
1:9 Postscaler 8
1:10 Postscaler 9
1:11 Postscaler 10
1:12 Postscaler 11
1:13 Postscaler 12
1:14 Postscaler 13
1:15 Postscaler 14
566
Postcaler Value Use Numeric Constant
1:16 Postscaler 15
Settimer
Syntax:
Command Availability:
Explanation:
Settimer will set the value of the specified timer with either byte value or a word value. 8-bit timers use
a byte value. 16-bit timers use a word value.
Settimer can be used on-the-fly, so there is no requirement to stop the timer first.
Example:
This example shows the operation of setting two timers - is not intended as a meaningful solution.
567
#chip 16f877a, 4
On Interrupt Timer1Overflow call Overflowed
Set PORTB.0 On
Sub Interrupt
Set PORTB.2 On
TMR2IF = 0
End Sub
Sub Overflowed
Set PORTB.1 On
TMR1IF = 0
End Sub
Supported in <TIMER.H>
StartTimer
Syntax:
StartTimer TimerNo
Command Availability:
Explanation:
Timer 0:
Please refer to the datasheet to determine if Timer 0 on specific Microchip PIC microcontroller can be
568
started and stopped with starttimer and stoptimer. If the Microchip PIC microcontroller has a register
named "T0CON" then it supports stoptimer and starttimer.
Example:
This example will measure that time that a switch is depressed (or on) and will write the results to the
EEPROM.
#chip 16F819, 20
#define Switch PORTA.0
Dir Switch In
DataCount = 0
'Initilise Timer 1
InitTimer1 Osc, PS1_8
Do
ClearTimer 1
Wait Until Switch = On
StartTimer 1
Wait Until Switch = Off
StopTimer 1
Supported in <TIMER.H>
569
StopTimer
Syntax:
StopTimer TimerNo
Command Availability:
On the Microchip PIC 18(L)Fxxx microcontrollers Timer 0 can be stopped with stopttimer.
With respect to Timer 0 on the Microchip PIC baseline and midrage range of microcontrollers
stoptimer (and starttimer) has no effect as Timer 0.
Example:
This example will measure that time that a switch is depressed (or on) and will write the results to the
EEPROM.
The example shows how to stop a timer when not in use.
570
#chip 16F819, 20
#define Switch PORTA.0
Dir Switch In
DataCount = 0
'Initilise Timer 1
InitTimer1 Osc, PS1_8
Do
ClearTimer 1
Wait Until Switch = On
StartTimer 1
Wait Until Switch = Off
StopTimer 1
Supported in <TIMER.H>
Reading Timers
Great Cow BASIC has the following functions to read the current timer value. They are:
Timer0()
Timer1()
Timer2()
Timer3()
Timer4()
Timer5()
Timer6()
Timer7()
Timer8()
Timer10()
Timer12()
571
Note that these functions should only be used to read the timer value. To write the timer value,
settimer should be used.
Not all of these functions are available on all microcontrollers. For example, if a microcontrollers has
three timers, then typically only Timer0, Timer1 and Timer2 will be available.
Please refer to the datasheet for your microcontroller to determine the supported timer numbers, and
if a specific timer is 8-bit or 16-bit.
SMT Timers
The Signal Measurement Timer (SMT) capability is a 24-bit counter with advanced clocking and gating
logic, which can be configured for measuring a variety of digital signal parameters such as pulse
width, frequency and duty cycle, and the time difference between edges on two signals.
Syntax:
InitSMT1(SMT_FOSC,SMTPres_1)
InitSMT2(SMT_FOSC4,SMTPres_8)
StartSMT1
StartSMT2
Command Availability:
This command set supports the use of the SMT as a 24-bit timer only.
Microchip PIC Microcontrollers have either 1 or 2 Signal Measurement Timers (SMT). A 24-bit timer
allows for very long timer periods/high resolution and can be quite useful for certain applications.
SMT timers support multiple clock sources and prescales. Interrupt on overflow/match is also
supported.
SMT timers will "overflow" when the 24-bit timer value "matches" the 24-bit period registers.
The timer period can be precisely adjusted/set by writing a period value to the respective period
register for eact timer.
572
The maximum period is achieved by a period register value of 16,777,215. 16,777,215 is the default
value at POR. The timer period is also affected by the ChipMhz, TimerSource and Timer Prescale.
The library supports "normal" timer operation of SMT1/SMT2. The library does not support the
advanced signal measurement features.
Explanation:
SetSMT1( Sets the timer to the specific value. The value can be 1 to SETSMT1(4045000)
TimerValue ) 16777215
573
Command Description Example
SetSMT2( Sets the timer to the specific value. The value can be 1 to SETSMT2(4045000)
TimerValue ) 16777215
SetSMT1Period ( Sets the timer period to the specific value. The value can be 1 SETSMT1PERIOD(4045
PeriodValue ) to 16777215 000)
SetSMT2Period ( Sets the timer period to the specific value. The value can be 1 SETSMT1PERIOD(9322
PeriodValue ) to 16777215 401)
#Chip 16F18855, 32
#option explicit
#Include <SMT_Timers.h>
#config CLKOUTEN_ON
'' -------------------LATA-----------------
'' Bit#: -7---6---5---4---3---2---1---0---
'' LED: ---------------|D5 |D4 |D3 |D1 |-
''-----------------------------------------
''
#define LEDD2 PORTA.0
#define LEDD3 PORTA.1
#define LEDD4 PORTA.2
#define LEDD5 PORTA.3
#define Potentiometer PORTA.4
574
SETSMT1PERIOD ( 4045000 ) ' 1.000s periodwith the parameters of SMT_FOSC and
SMTPres_1 within the clock variance of the interclock
' a perfect internal clock would be 4000000
InitSMT1(SMT_FOSC,SMTPres_1)
InitSMT2(SMT_FOSC4,SMTPres_8)
StartSMT1
StartSMT2
Do
'// Waiting for interrupts
LOOP
Sub BlinkLEDD2
LEDD2 = !LEDD2
End SUB
Sub BlinkLEDD3
LEDD3 = !LEDD3
End SUB
Supported in <SMT_Timers.h>
575
Variables Operations
This is the Variables Operations section of the Help file. Please refer the sub-sections for details using
the contents/folder view.
Using Variables
Explaination
Using and accessing bytes within word and long numbers etc may be required when you are creating
your solution. This can be done with some ease.
Example 1:
You can access the bytes within word and longs variables using the following as a guide using the
Suffixes _H, _U and _E
lowb = workvariable
highb = workvariable_H
upperb = workvariable_U
lastb = workvariable_E
Dim rB as Byte
Dim sW as Word
'To use the bits 7-0 [lower byte] in the Word variable sW
rB = sW
'For bits 15-8 [upper byte] in the Word variable sW, use sw_H
rB = sW_H
576
To extract the bytes from a LONG of 32 bits use the Suffixes _H, _U and _E, where
Dim rB as Byte
Dim tL as Long
Example 2:
Assigning values to Word and Long variables via the the Byte variable (the Least Significant Byte [.lsb])
of the same Word and Long variable.
Because a Long (or Word) variable and the Least Significant Byte, of the variable, have the same
variable assignments to specific byte elements (_e, _u and _h) assignment must be appropriate to the
element.
The code below uses a Long variable but the same principle is used for a Word.
Assigning two values, a byte and a word constant value, to the variable tL to compare resulting impact
on Long variable.
577
Dim tL as Long
tL = 255 'All bits of the value 255 will reside in the lowest byte of the Long
variable tL
tL = 286 'This assignment will flow into tL_H where tL_H =1 and tl=30.
'is same as the following assignment, we show the use of casting for clarification
only.
[Long] tL = 0xF7C5E300 The lower byte (tL) is empty (zero).
'This will assign the lowest byte with 0xA4 but this assignment will also clear the
upper 3 byte elements of the long variable.
tL = 0xA4
A method to check a variable is assigned as expected is to use HserPrint and HserPrint hex(), as
follows:
' HserPrint hex() only prints one byte so we need to handle the four elements
HserPrint " Print tL _E, tL_U, tL_H & tL as hex"
HserPrint hex (tL_E)
HserPrint hex (tL_U)
HserPrint hex (tL_H)
HserPrint hex (tL)
HserPrintCRLF
HserPrint "Variable tL = "
HserPrint tL
578
The user code above will result in an output as follows:
Explanation
Within Great Cow BASIC you can use regular variable assignments. But, you can also use C like maths
assignments.
GLCDPrintLoc += 6
CharCode -= 15
CharCode++
CharCode---
Within Great Cow BASIC you can define binary, hexidecimal and decimal constants, see Constants.
Please note what is and what is not support with respect to assigning numbers to constants. An
example program examines what is supported.
579
#chip 16F88, 4
#config Osc = INT, MCLRE_OFF
porta = test1
porta = test2
porta = test3
porta = test4
porta = test5
porta = test6
You can assigned values/numbers with all the methods shown above (for constants and variables) but
please be aware that you must Use '0' not '00'. One zero equates to zero and two zeros will give you an
unassigned variable.
Constants:
A few critical constants are defined within Great Cow BASIC , you can re-use these constants. They
include:
Setting Variables
Syntax:
580
Variable = data
Explanation:
Variable will be set to data.
data can be either a fixed value (such as 157), another variable, or a sum.
All unknown byte variables are assigned Zero. A variable with the name of Forever is not defined by
Great Cow BASIC and therefore defaults to the value of zero.
If data is a fixed value, it must be an integer between 0 and 255 inclusive.
If data is a calculation, then it may have any of the following operands:
+ (add)
- (subtract, or negate if there is no value before it)
* (multiply)
/ (divide)
% (modulo)
& (and)
| (or)
# (xor)
! (not)
= (equal)
<> (not equal)
< (less than)
> (greater than)
<= (less than or equal)
>= (more than or equal)
The final six operands are for checking conditions. They will return FALSE (0) if the condition is false,
or TRUE (255) if the condition is true.
The And, Or, Xor and Not operators function both as bitwise and logical operators.
Great Cow BASIC understands order of operations. If multiple operands are present, they will be
processed in this order:
Brackets
Unary operations (not and negate)
Multiply/Divide/Modulo
Add/Subtract
Conditional operators
And/Or/Xor
581
There are several modes in which variables can be set. Great Cow BASIC will automatically use a
different mode for each calculation, depending on the type of variable being set. If a byte variable is
being set, byte mode will be used; if a word variable is being set, word mode will be used. If a byte is
being set but the calculation involves numbers larger than 255, word mode can be used by adding
[WORD] to the start of one of the values in the calculation. This is known as casting - refer to the
Variables article for more information.
Divide or division
When using division you will get accurate results, within the limitations of integer numbers, by
completing any multiplication first and the division last. But, you may have issues with variables
overflowing - ensure your variable type are correct for the calculation type.
If you that calculation a division, the compiler will use the long division routine, if the value may
overflow, and then fit the result into a word. This code provides the correct result, again within the
limitations of integer numbers:
Division also sets the global system variable SysCalcTempX to the remainder of the division. However
the following simple rules apply.
• If both of the parameters of the division are constants, the compiler will do the calculation itself
and use the result rather than making the microcontroller work out the same thing every time. So,
if there are two constants used, the microcontroller division routine does not get used, and
SysCalcTempX does not get set.
• If either of the parameters of the division are variables, the compiler will ensure the
microcontroller does the calculation as the result could be different every time. So, in the this case
the microcontroller division routine does get used, and SysCalcTempX is set.
If you prefer, you can add Let to the start of the line. It will not alter the execution of the program, but
is included for those who are used to including it in other BASIC dialects.
Example:
582
'This program is to illustrate the setting of variables.
Chipmunk = 46 'Sets the variable Chipmunk to 46
Animal = Chipmunk 'Sets the variable Animal to the value of the variable Chipmunk
Bear = 2 + 3 * 5 'Sets the variable Bear to the result of 2 + 3 * 5, 17.
Sheep = (2 + 3) * 5 'Sets the variable Sheep to the result of (2 + 3) * 5, 25.
Animal = 2 * Bear 'Sets the variable Animal to twice the value of Bear.
Great Cow BASIC supports the method setting a specific bit of a variable or register. Use the following
method:
'variable.bit method
myByteVariable.0 = 1 'will set bit 0 to 1
myByteVariable.1 = 0 'will set bit 1 to 0
myByteVariable.2 = 1 'will set bit 2 to 1
To set more than one bit in one command Great Cow BASIC supports the bits method.
Great Cow BASIC also supports setting specific bits of a variable or register. Use the following method:
'variable.bitS method
SPLLEN, IRCF3, IRCF2, IRCF1, IRCF0 = b'01111'
' would generate ASM [for your specific microcontroller] similar to the following.
' bcf OSCTUNE,PLLEN,ACCESS
' bsf OSCCON,IRCF2,ACCESS
' bsf OSCCON,IRCF1,ACCESS
' bsf OSCCON,IRCF0,ACCESS
This method is limited to literal values. You cannot use value from another variable as the setting
value (at v0.98.00).
Great Cow BASIC supports the method setting a specific bit of a variable or register. Use the following
583
method:
'variable.bit method
myByteVariable.0 = 1 'will set bit 0 to 1
myByteVariable.1 = 0 'will set bit 1 to 0
myByteVariable.2 = 1 'will set bit 2 to 1
To set more than one bit in one command Great Cow BASIC supports the bits method.
Great Cow BASIC also supports setting specific bits of a variable or register. Use the following method:
'variable.bitS method
SPLLEN, IRCF3, IRCF2, IRCF1, IRCF0 = b'01111'
' would generate ASM [for your specific microcontroller like the following.
' bcf OSCTUNE,PLLEN,ACCESS
' bsf OSCCON,IRCF2,ACCESS
' bsf OSCCON,IRCF1,ACCESS
' bsf OSCCON,IRCF0,ACCESS
This method is limited to literal values. You cannot use value from another variable as the setting
value (at v0.98.00).
To set more than one bit in one command Great Cow BASIC supports the bits method.
Great Cow BASIC also supports setting specific bits of a variable or register with error handling. Use
this method to prevent errors when a specified bit does not exist.
The [canskip] prefix will handle the error condition when a specific bit or specific bits do not exist.
The following example shows the usage.
This method is limited to literal values. You cannot use value from another variable as the setting
value (at v0.98.00).
This example shows how the error handler compares to not have the [canskip] prefix
584
' Of these two lines, only the first compiles:
[canskip] SPLLEN, IRCF3, IRCF2, IRCF1, IRCF0 = b'01111' 'first line with error
handler
SPLLEN, IRCF3, IRCF2, IRCF1, IRCF0 = b'01111' 'second line with no
error handler
Variable Lifecycle
Explanation
Within Great Cow BASIC you can use variables. This section details the Variable Lifecycle when using
variables.
As shown below in the rule without #Option Explicit but ALL variables MUST be defined including
bytes variables.
Once a variable is defined, and then the variable it is used the variable persists.
Aliasing - You can reduce memory usage by Aliasing. Remember all variables are global so you must
be careful.
If there are two variables with the same name, they will be placed in the same memory location. You
can reuse the same variable name in two subs/functions, and you can make the variables different
types, but writing to the variable in one sub will overwrite the value from the other sub, see the
example below.
As a general guide define any shared variables near the start of the program for easier readability.
All variables should be initialised with a desired initialisation value. Do not assume the initialisation
value is Zero.
585
All variables are global. Bit variables defined in subs/function are global.
Byte variables do not need to be defined using the DIM statement. See #Option Explicit above. Just to
clarify byte is default type, this means:
All variables are global, but, if they are defined inside a particular subroutine then their type is not, see
the example below:
Example code:
MainVar = 128
OtherVar = 514
DemoSub
'At this point:
'MainVar is a byte, value 128
'OtherVar is a word, value 514
'Counter is a byte, value 2
'(Byte is default type, but location shared with that of Counter in DemoSub. High
byte ignored)
Sub DemoSub
Dim Counter As Word
Counter = 2050
'At this point:
'MainVar and OtherVar as byte and word, as in main routine
'Counter is a word, value 2050
End Sub
In DemoSub, Counter is a word. But anywhere else in the program it is a byte unless otherwise
specified. If the variable is used/read in the main routine, it will be treated as a byte, and only the low
8 bits will be returned. In this example the low 8 bits of 2050 are 2.
The main reason for keeping the type inside the subroutine was for the following scenario: A
subroutine uses a temporary variable of type byte, and relies on it overflowing.
Another subroutine uses a temporary variable of the same name, but of word type.
586
If the first subroutine is already in the program, and then the second one is added, the behaviour of the
first one will not change at all due to the addition of the second one.
The handling of variable types using this method minimises the size of the generated assembly code.
Dim
Syntax:
For Arrays:
Dim array(size) [At location]
Explanation:
Dim has two uses, both of which involve large variables. It can be used to define arrays, bit variables
and all variables larger than 1 byte.
Command Availability:
The Dim variable command is used to inform Great Cow BASIC of variables that are larger than 1 byte,
or to create alternate names for other variables.
The Dim array command also sets up array variables. The maximum array size is determined by the
parameter size is dynamically allocated by the compiler and depends on the specific chip used, as well
as the complexity of the program.
The limit on array size varies dependent on the chip type. The 12F/16F series of chips the array limit is
80 elements. For the Atmel AVR or an 18F there is no limit other than free RAM however Great Cow
BASIC limits the array size to 10,000 elements. If a memory limit is reached, the compiler will give an
error message.
type specifies the type of variable that is to be created. Different variable types can hold values over
different ranges, and use different amounts of RAM. See the Variables article for more information.
When multiple variables are included on the one line, Great Cow BASIC will set them all to the type
that is specified at the end of the line. If there is no type specified, then Great Cow BASIC will make the
variable a byte.
Alias creates a variable using the same memory location as one or more other variables. It is mainly
587
used internally in Great Cow BASIC to treat system variables as a word. For example, this command is
used to create a word variable, made up from the two memory locations used to store the result of an
A/D conversion:
A variable can be placed at a specific location in the data memory of the chip using the At option.
location will be used whether it is a valid location or not, but a warning will be generated if Great Cow
BASIC has already allocated the memory, or if the memory does not appear to be valid. This can be
used for peripherals that have multi byte buffers in RAM.
Example:
dim DataList(10)
dim Reading as word
Reading = 21978
DataList(1) = 15
For more help, see:SerPrint articles as these articales show how to use Dim to create string variables
and Variables for more details in creating and managing strings lengths.
BcdToDec_GCB
Syntax:
BcdToDec_GCB ( ByteVariable )
Command Availability:
Explanation:
You can add this function. Just add this to your Great Cow BASIC program and then call it when you
need it.
588
Example:
DecToBcd_GCB
Syntax:
DectoBcd( ByteVariable )
Command Availability:
Explanation:
Converts numbers from Decimal to Binary Coded Decimal format. Support Bytes only.
You can add this function. Just add this to your Great Cow BASIC program and then call it when you
need it.
Example:
Rotate
Syntax:
Command Availability:
589
Explanation:
The Rotate command will rotate variable one bit in a specified direction. The bit shifted will be placed
in the Carry bit of the Status register (STATUS.C). STATUS.C acts as a ninth bit of the variable that is being
rotated.
When a variable is rotated right, the bit in the STATUS.C location is placed into the MSB of the variable
being rotated, and the LSB of the variable is placed into STATUS.C location.
When rotated left the opposite occurs. The MSB of the variable is shifted to the STATUS.C bit and the
LSB of the variable will contain what was previously in the STATUS.C bit location.
As you may notice the STATUS.C bit added a 0 to the rotation. So this will take 9 shifts left to get back to
the original value.
Simple option
Many times you want to rotate the variable around like the STATUS.C bit wasn’t there so the MSB of the
variable fills the LSB of the variable on Rotate Left or the LSB fills the MSB on Rotate Right. That is
where the SIMPLE option comes in. It adds a hidden step that shifts the STATUS.C bit twice so the bit
moves from one end of the variable to the other.
Notes: The carry is also called SREG bit C, or simply C flag on AVR.
In some cases the Status.C or C flag may already be set because of prior operations in your program.
Therefore, it may be necessary to clear the C flag before using Rotate. Use Set C Off before using the
Rotate command to clear the flag.
590
Example:
#chip 16F819, 8
#config osc = int
'Chase
C = 0
Do
Rotate PORTB Right Simple
Wait 250 ms
Loop
Set
Syntax:
Command Availability:
Explanation:
The purpose of the Set command is to turn individuals bits on and off.
The Set command is most useful for controlling output ports, but can also be used to set variables.
Often when controlling output ports, Set is used in conjunction with constants. This makes it easier to
adapt the program for a new circuit later.
Example:
591
'Blink LED sample program for Great Cow BASIC
'Controls an LED on PORTB bit 0.
'Main routine
Do
Set LED On
Wait 1 sec
Set LED OFF
Wait 1 sec
Loop
SWAP4
Syntax:
SWAP4( VariableA)
Command Availability:
Explanation:
A function that swaps (or exchanges) nibbles (or the 8 bits of a byte in nibbles).
Example:
592
dim ByteVariable as Byte
HSerPrint hex(ByteVariable)
SWAP
Syntax:
Command Availability:
Explanation:
A function that swaps (or exchanges) one byte or word for another. SWAP support the use of byte and
word variables.
593
String Manipulation
This is the String Manipulation section of the Help file. Please refer the sub-sections for details using
the contents/folder view.
Asc, bytetobin, chr, fill, hex, instr, lcase, left, leftpad, len, mid, pad, right, rtrim, str, trim, ucase, val,
wordtobin or concatenation
Asc
Syntax:
Command Availability:
Explanation:
Returns the character code of the character at the specified position in a string.
ASC returns the character code of a particular character in the string. If the string is an ANSI string, the
returned value will be in the range of 0 to 255. This function DOES NOT support UNICODE.
The optional position parameter determines which character is to be checked. The first character is
one, the second two, etc. If the position parameter is missing, the first character is presumed.
CHR is the natural complement of ASC. CHR produces a one-character string corresponding to its ASCII.
Note:
If the string passed is null (zero-length) or the position is zero or greater than the length of the string
the returned value will be 0.
Example:
594
ByteToBin
Syntax:
stringvar = ByteToBin(bytevar)
Command Availability:
Explanation:
The ByteToBin function creates a string of a ANSI (8-byte) characters. The function converts a number
to a string consisting of ones and zeros that represents the binary value.
Note: Supports BYTE variables only. For WORD variables use WordToBin
Example:
Chr
Syntax:
stringvar = CHR(bytevar)
Command Availability:
Explanation:
Example:
595
string = CHR( 65 ) ' Returns "A"
Fill
Syntax:
Command Availability:
Explanation:
The Fill function is used to create string to a specific length that is of a specific character.
The length of the string is specified by the first parameter. The character used to pad the string is
specified by the second parameter, this parameter is optional as the " "(space) character is assumed.
Example:
596
Hex
Syntax:
stringvar = Hex(number)
Command Availability:
Explanation:
The Hex function will convert a number into hexadecimal format. The input number should be a byte
variable, or a fixed number between 0 and 255 inclusive. After running the function, the string
variable stringvar will contain a 2 digit hexadecimal number.
Example:
When using the functions Hex() do not leave space between the function call and the left brace. You
will get a compiler error that is meaningless.
597
' use this, note this is no space between the Hex and the left brace!
Hex(number_variable)
' do not use, note the space!
Hex (number_variable)
Instr
Syntax:
Command Availability:
Explanation:
The Instr function will search one string to find the location of another string within it. source is the
string to search inside, and find is the string to find. The function will return the location of find within
source, or 0 if source does not contain find.
Example:
598
'Set chip model
#chip 16F1936
LCase
Syntax:
output = LCase(source)
Command Availability:
Explanation:
The LCase function will convert all of the letters in the string source to lower case, and return the result.
Example:
599
'Set chip model
#chip 16F1936
Left
Syntax:
Command Availability:
Explanation:
The Left function will extract the leftmost count characters from the input string source, and return
them in a new string.
Example:
600
'Set chip model
#chip 16F1936
LeftPad
Syntax:
LeftPad(string_variable,byte_value_of_the_new_length,pad_character)
Command Availability:
Explanation:
The LeftPad function is used to create string to a specific length that is extended with a specific
character to the left hand side of the string.
The character used to pad the string is specified by the third parameter.
Example:
601
DIR PORTA 0x03
'Defines (Constants)
#define LCD_SPEED slow
#define LCD_IO 4
#define LCD_NO_RW
#define LCD_Enable PORTc.0
#define LCD_RS PORTc.1
#define LCD_DB4 PORTa.5
#define LCD_DB5 PORTa.4
#define LCD_DB6 PORTa.3
#define LCD_DB7 PORTa.2
'''--------------------------------------------
'''-------End of board-specific settings-------
'''--------------------------------------------
; ---- variables
DIM inString as string * 5
DIM outString1 as String
DIM outString2 as String
inString = "12345"
print instring
print " "
print outstring1
locate 1,0
602
print instring
print " "
print outstring2
end
Len
Syntax:
Command Availability:
Explanation:
The Len function returns an byte value which is the length of a phrase or a sentence, including the
empty spaces. The format is:
target_byte_variable = Len("Phrase")
or another example. This code will loop through the for-next loop 12 times as determined by the length
of the string:
teststring = "0123456789AB"
for loopthrustring = 1 to len(teststring)
hserprint mid(teststring, loopthrustring , 1)
next
Ltrim
Syntax:
stringvar = LTRIM(stringvar)
Command Availability:
603
Available on all microcontrollers
Explanation:
The Ltrim function will trim the 7-bit ASCII space character (value 32) from the LEFT hand side of a
string.
Use Ltrim on text that you have received from another source that may have irregular spacing at the
left hand end of the string.
Mid
Syntax:
Command Availability:
Explanation:
The Mid function is used to extract characters from the middle of a string variable. source is the
variable to extract from, start is the position of the first character to extract, and count is the number
of characters to extract. If count is not specified, all characters from start to the end of the source
string will be returned.
Example:
604
'Set chip model
#chip 16F1936
Pad
Syntax:
Command Availability:
Explanation:
The Pad function is used to create string to a specific length that is extended with a specific character.
The length of the string is specified by the second parameter. The character used to pad the string is
specified by the third parameter.
Example:
605
'Set chip model
#chip 16f877a
'Defines (Constants)
#define LCD_SPEED slow
#define LCD_IO 4
#define LCD_NO_RW
#define LCD_Enable PORTc.0
#define LCD_RS PORTc.1
#define LCD_DB4 PORTa.5
#define LCD_DB5 PORTa.4
#define LCD_DB6 PORTa.3
#define LCD_DB7 PORTa.2
'''--------------------------------------------
'''-------End of board-specific settings-------
'''--------------------------------------------
; ---- variables
'Define the string
Dim TestData As String * 16
TestData = "Location"
end
606
Right
Syntax:
Command Availability:
Explanation:
The Right function will extract the rightmost count characters from the input string source, and return
them in a new string.
Example:
Rtrim
Syntax:
stringvar = Rtrim(stringvar)
Command Availability:
Explanation:
607
The Rtrim function will trim the 7-bit ASCII space character (value 32) from the RIGHT hand side of a
string.
Use Rtrim on text that you have received from another source that may have irregular spacing at the
right hand end of the string.
Str
Syntax:
Command Availability:
Explanation:
The Str function will convert a number into a string. number can be any byte or word variable, or a
fixed number between 0 and 65535 inclusive. For Long numbers use Str32 and for Integer numbers
use StrInteger.
The string variable stringvar will contain the same number, represented as a string. The length of the
string returned is 5, 10 or 6 characters for Byte & Word, Long and Integer respectively.
This function is especially useful if a number needs to added to the end of a string, or if a custom data
sending routine has been created but only supports the output of string variables.
Example1:
608
'Set chip model
#chip 16F1936
'Send
HSerPrint OutVar
HSerPrintCRLF
When using the functions STR() do not leave space between the function
call and the left brace. You will get a compiler error that is
meaningless.
' use this, note this is no space between the STR and the left brace!
STR(number_variable)
' do not use, note the space!
STR (number_variable)
Example2:
'''
'''
'''
'''
'''************************************************************************
'''
''' PIC: 16F18855
''' Compiler: GCB
''' IDE: GCB@SYN
'''
''' Board: Xpress Evaluation Board
''' Date: June 2016
609
'''
' ----- Configuration
'Chip Settings.
#chip 16f18855,32
#Config FEXTOSC_OFF, RSTOSC_HFINT32
#Config WRT_OFF, CPD_ON, MCLRE_ON
#option Explicit
'' -------------------LATA-----------------
'' Bit#: -7---6---5---4---3---2---1---0---
'' LED: ---------------|D5 |D4 |D3 |D2 |-
''-----------------------------------------
''
; ----- Constants
#define USART_BAUD_RATE 19200
#define USART_TX_BLOCKING
#define SWITCH_DOWN 0
#define SWITCH_UP 1
#define SWITCH PORTA.5
Dir SWITCH In
; ----- Variables
dim bytevar as Byte
dim wordvar as Word
dim longvar as long
610
dim integervarP, integervarN,integervar as Integer
do
wait 100 ms
wait 1 s
loop
end
Trim
Syntax:
stringvar = Trim(stringvar)
Command Availability:
611
Available on all microcontrollers
Explanation:
The Trim function will trim the 7-bit ASCII space character (value 32) from text.
Trim removes all spaces from text except for single spaces between words. Use Trim on text that you
have received from another source that may have irregular spacing at the left or right hand ends of the
string.
UCase
Syntax:
output = UCase(source)
Command Availability:
Explanation:
The UCase function will convert all of the letters in the string source to upper case, and return the
result.
Example:
612
See Also LCase
Val
Syntax:
Command Availability:
Explanation:
The Val function will extract a number from a string variable, and store it in a word variable. One
potential use is reading numbers that are sent in ASCII format over a serial connection.
The Val32 function will extract a long number from a string variable, and store it in a long variable.
Example1:
613
'Program for an RS232 controlled dimmer
'Set chip model
#chip 16F1936
'Main Loop
Do
'Get serial byte
Wait Until USARTHasData
HSerReceive InByte
'Output
HPWM 1, 32, OutputLevel
'Number?
If InByte >= 48 and InByte <= 57 Then
'Add to end of DataIn string
DataInCount += 1
DataIn(DataInCount) = InByte
DataIn(0) = DataInCount
End If
Loop
614
Example2:
'' -------------------LATA-----------------
'' Bit#: -7---6---5---4---3---2---1---0---
'' LED: ---------------|D5 |D4 |D3 |D2 |-
''-----------------------------------------
''
; ----- Constants
#define USART_BAUD_RATE 19200
#define USART_TX_BLOCKING
#define SWITCH_DOWN 0
#define SWITCH_UP 1
#define SWITCH PORTA.5
Dir SWITCH In
615
; ----- Variables
dim bytevar as Byte
dim wordvar as Word
dim longvar as long
bytevar = 0
wordvar = 0
longvar = 0
#option Explicit
do
wait 100 ms
wait 1 s
loop
end
WordToBin
Syntax:
stringvar = WordToBin(bytevar)
616
Explanation:
The WordToBin function creates a string of a ANSI (8-byte) characters. The function converts a number
to a string consisting of ones and zeros that represents the binary value.
Example:
Concatenation
Syntax:
Command Availability:
Explanation:
This method does not change the existing strings, but returns a new string containing the text of the
joined variables, see Concatenated String Constraint below.
Concatenation joins the elements of a specified values using the specified separator between each
variable.
Example:
timevariable = 999
stringvar = "Time = " + timevariable ' Returns Time = 999
When concatenating a string you cannot assign a string to itself. You must assign the result of string
concatenation to another string. See below.
617
Example to resolve string handling constraint:
outnumber = 24
outstring1 = "Result = "
'This concatenation will yield an incorrect string
outstring1 = outstring1 +str(outnumber)
'This concatenation will yield an the correct string. With tmpstring1 containing the
correct concatenated string
tmpstring1 = outstring1 +str(outnumber)
To resolve the constraint simply assign the source string to another string.
618
Miscellaneous Commands
This is the Miscellaneous Commands section of the Help file. Please refer the sub-sections for details
using the contents/folder view.
Dir
Syntax:
Command Availability:
Explanation:
The Dir command is used to set the direction of the ports of the microcontroller chip. The individual
form sets the direction of one pin at a time, whereas the entire port form will set all bits in a port.
In the individual form, specify the port and bit (ie. PORTB.4), then the direction, which is either In or
Out.
The entire port form is similiar to the TRIS instruction offered by some Microchip PIC microcontrollers.
To use it, give the name of the port (i.e. PORTA), and then a byte is to be written into the TRIS variable.
This form of the command is for those who are familiar with the Microchip PIC microcontrollers
internal architecture.
Note: Entire port form will work differently on Atmel AVR microcontrollers when a value other than
IN or OUT is used. Atmel AVR microcontrollers use 0 to indicate in and 1 to indicate out, whereas
Microchip PIC microcontrollers use 0 for out and 1 for in. When IN and OUT are used there are no
compatibility issues.
Example:
619
'This program sets PORTA bits 0 and 1 to in, and the rest to out.
'It also sets all of PORTB to output, except for B1.
'Individual form is used for PORTA:
DIR PORTA.0 IN
DIR PORTA.1 IN
DIR PORTA.2 OUT
DIR PORTA.3 OUT
DIR PORTA.4 OUT
DIR PORTA.5 OUT
DIR PORTA.6 OUT
DIR PORTA.7 OUT
'Entire port form used for B:
DIR PORTB b'00000010'
The compiler will set the automatic pin DIRection using the following logic.
Any time that the user program reads a pin or port, the compiler records that. Any time that the user
program writes to a pin or entire port, the compiler also records that.
Once all input code has been compiled, the compiler examinies the list of reads and writes.
If a pin is only ever read, the compiler does not know if the intent is to read the latch or an input value,
so it sets that pin to be an input.
If the compiler sees a pin being read and written to, the compiler does not know if you are using a pin
for some sort of bidirectional communication, or if you are just reading the latch. To avoid making
incorrect assumptions, the compiler will expect you to set the pin direction manually.
If you use "portA.2 = 1", you’ve only written to the pin, so the compiler knows it must be an output.
If you use "portA.2 = not portA.2", the compiler sees that you are reading and writing to the pin, and
will expect the user program set the direction instead of trying to guess what you are doing.
The compiler also records any use of the Dir command, and will not do any automatic direction setting
on a pin if Dir has been used on that pin anywhere in the user program..
GetUserID
Syntax:
620
Command Availability:
Explanation:
Reads the memory location and returns the ID for a specific microcontroller.
If the microcontroller does not support GetUSerID then the following message will be issued during
compilation Warning: GetUserID not supported by this microcontroller.
The method reads the memory location 0x8000 + Index and returns it as a Word value, where the
Index 0x00 to 0x0B as follows:
Example:
621
#chip 16F1455
#Config WRT_OFF, MCLRE_ON
#include <GetUserID.h>
'Implement ANSI escaope code for serial terminal NOT using a LCD!
#define ESC chr(27)
#define CLS HSerPrint(ESC+"[2J")
#define HOME HSerPrint(ESC+"[H")
#define Print HSerPrint
CLS
HOME
End
Pot
Syntax:
Command Availability:
Explanation:
Pot makes it possible to measure an analog resistance with a digital port, with the addition of a small
capacitor. This is the required circuit:
622
The command works by using the microcontroller pin to discharge the capacitor, then measuring the
time taken for the capacitor to charge again through the resistor.
The value for the capacitor must be adjusted depending on the size of the variable resistor. The
charging time needs to be approximately 2.5 ms when the resistor is at its maximum value. For a
typical 50 k potentiometer or LDR, a 50 nf capacitor is required.
This command should be used carefully. Each time it is inserted, 20 words of program memory are
used on the chip, which as a rough guide is more than 15 times the size of the Set command.
pin is the port connected to the circuit. The direction of the pin will be dealt with by the Pot command.
output is the name of the variable that will receive the value.
Example:
623
'This program will beep whenever a shadow is detected
'A potentiometer is used to adjust the threshold
#chip 16F628A, 4
#config INTOSC_OSC_NOCLKOUT
Do
Pot ADJUST, Threshold
Pot LDR, LightLevel
If LightLevel > Threshold Then
Tone 1000, 100
End If
Loop
PulseOut
Syntax:
Command Availability:
Explanation:
The PulseOut command will set the specified pin high, wait for the specified amount of time, and then
set the pin low again. The pin is specified in the same way as it is for the Set command, and the time is
the same as for the Wait command.
Example:
624
'This program flashes an LED on GPIO.0 using PulseOut
#chip 12F629, 4
#config INTRC_OSC_NOCLKOUT
'The DIRection of the port is set to show the command. It is not required to set the
DIRection when using the PulseOut command.
Dir GPIO.0 Out
Do
PulseOut GPIO.0, 1 sec 'Turn LED on for 1 sec
Wait 1 sec 'Wait 1 sec with LED off
Loop
PulseOutInv
Syntax:
Command Availability:
Explanation:
The PulseOutInv command will set the specified pin low, wait for the specified amount of time, and
then set the pin high. The pin is specified in the same way as it is for the Set command, and the time is
the same as for the Wait command.
Example:
PulseIn
Syntax:
625
PulseIn pin, user_variable, time units
Command Availability:
Explanation:
The PulseIn command will monitor the specified pin when the pin is high, and then measure the high
time. It will store the time in the user variable. The user variable must be a WORD if returned units are
expected to be > 255 (Example: Pulse is 500 ms)
Example:
#chip 12F629, 4
#config INTRC_OSC_NOCLKOUT
Dir GPIO.0 In
Dim TimeResult as WORD
Do while GPIO.0 = Off 'Wait for next positive edge to start measuring
Loop
PulseInInv
Syntax:
Command Availability:
Explanation:
The PulseIn command will monitor the specified pin when the pin is low, and then measure the low
time. It will store the time in the user variable. The user variable must be a WORD if returned units are
expected to be > 255 (Example: Pulse is 500 ms)
626
Example:
#chip 12F629, 4
#config INTRC_OSC_NOCLKOUT
Dir GPIO.0 In
Dim TimeResult as WORD
Peek
Syntax:
Command Availability:
Explanation:
The Peek function is used to read information from the on-chip RAM of the microcontroller.
location is a word variable that gives the address to read. The exact range of valid values varies from
chip to chip.
This command should not normally be used, as it will make the porting of code to another chip very
difficult.
Example #1 :
Example #2
627
' This subroutine will toggle the pin state.
' You must change the parameters for your specific chip.
' Usage as show in examples below.
'
' Toggle @PORTE, 2 ' equates to RE1.
' Wait 100 ms
' Toggle @PORTE, 2
' Wait 100 ms
Poke
Syntax:
Poke(location, value)
Command Availability:
Explanation:
The Poke command is used to write information to the on-chip RAM of the microcontroller.
location is a word variable that gives the address to write. The exact range of valid values varies from
chip to chip. value is the data to write to the location.
This command should not normally be used, as it will make the porting of code to another chip very
difficult.
Example 1:
628
'This program will set all of the PORTB pins high
POKE (6, 255)
Example 2:
;Chip Settings
#chip 16F88
Do Forever
FlashPin @PORTB, 8
Wait 1 s
Loop
Using @ before the name of a variable (including a special function register) will give you the address
of that variable, which can then be stored in a word variable and used by Peek and Poke to indirectly
access the location.
Weak Pullups
Weak pullups provide a method within many microcontrollers such as the Atmel AVR and Microchip
PIC microcontrollers to support internal/selectable pull-ups for convenience and reduced parts count.
If you require Weak pullups these internal pullups can provide a simple solution. For example, you can
use them to ground input pins with a switch closure - with the pullup enabled, the pin is held in a high
state until the input line pulls it to ground. Be aware of possible EMI interference and also make sure
to use a debounce routine.
If you need your weak pullups to exactly control current (rare for most microcontroller applications),
then you should consider 10k resistors (5V/10K = 500uA) Why? If you review in the microcontroller
data sheet, there is no resistance given for the weak pullups. That is because they are not weak pull-
resistors they are weak pullups consisting of what appear to be high-resistance channel pFETs. Their
channel resistance will vary with temperature and between parts; not easy to characterize.
The data sheet gives a current range for the internals as 50-400uA (at 5V).
629
PORTs can have an individually controlled weak internal pullup. When set, each bit of the appropriate
Microchip PIC register enables the corresponding pin pullup. There is a master bit within a specific
register bit that enables pullups on all pins which also have their corresponding weak pull bit set.
Also when set, there is a weak pull register bit to disable all weak pullups.
The weak pullup is automatically turned off when the port pin is configured as an output. The pullups
are disabled on a Power-on Reset.
You should review the datasheet for the method for a specific microcontroller.
The following code demonstrates how to set the weak pullups available on port B of an 18F25K20.
Example:
Dir Portb in
Dir Portc out
do
portc.1 = portb.1 'in pin 22, out pin 12
portc.2 = portb.2 'in pin 23, out pin 13
portc.3 = portb.3 'in pin 24, out pin 14
portc.4 = portb.4 'in pin 25, out pin 15
Also, see I2C Slave Hardware for an example using a 16F microcontroller.
630
Maths
This is the Maths section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Abs
Syntax:
Command Availability:
Explanation:
The Abs function will compute the absolute value of a integer number therefore in the range of −32767
to +32767.
Example:
Average
Syntax:
Command Availability:
Explanation:
A function that returns the average of two numbers. This only supports byte variables.
Provides a very fast way to calculate the average of two 8 bit numbers.
Example:
631
average_value = Average(8,4) ' Will return 6
Difference
Syntax:
Command Availability:
Explanation:
A function that returns the difference between of two numbers. This only supports byte or word
variables.
Example:
Logarithms
Explanation:
Great Cow BASIC support logarithmic functions through the include file <maths.h>.
These functions compute base 2, base e and base 10 logarithms accurate to 2 decimal places, +/- 0.01.
The values returned are fixed-point numbers, with two decimal places assumed on the right. Or if you
prefer, think of the values as being scaled up by 100.
The input arguments are word-sized integers, 1 to 65535. Remember, logarithms are not defined for
non-positive numbers. It is the calling program’s responsibility to avoid these. Output values are also
word-sized.
Local variables consume 9 bytes, while the function parameters consume another 4 bytes, for a grand
total of 13 bytes of RAM used. The lookup table takes 35 words of program memory.
Supported in <MATHS.H>
632
Log2
Syntax:
Command Availability:
Explanation:
The Log2 command will return the base-2 logarithm, to 2 decimal places.
The values returned are fixed-point numbers, with two decimal places assumed on the right. or if you
prefer, think of the values as being scaled up by 100.
Example:
Supported in <MATHS.H>
Loge
Syntax:
Command Availability:
Explanation:
The Loge command will return the base-e logarithm, to 2 decimal places.
The values returned are fixed-point numbers, with two decimal places assumed on the right. or if you
prefer, think of the values as being scaled up by 100.
Example:
633
dim log_value as word
log_value = loge ( 10 )
Supported in <MATHS.H>
Log10
Syntax:
Command Availability:
Explanation:
The Log10 command will return the base-10 logarithm, to 2 decimal places.
The values returned are fixed-point numbers, with two decimal places assumed on the right. or if you
prefer, think of the values as being scaled up by 100.
Example:
Supported in <MATHS.H>
Power
Syntax:
Explanation:
This function raises a base to an exponent, i.e, power(base,exponent). Calculation powers will become
large, in terms of long numbers, you must ensure the program manage numbers remain within range
of the defined variables.
The base and exponent are Byte sized numbers in this method.
The returned result is a Long.
634
Non-negative numbers are assumed throughout.
Note: 0 raised to 0 is meaningless and should be avoided, but, any other non-zero base raised to 0 is
handled correctly.
Example:
;----- Configuration
;----- Constants
;----- Variables
dim i, j as byte
;----- Program
635
Supported in <MATHS.H>
Scale
Syntax:
Command Availability:
value: the number to scale. A value between 0 and 0xFFFFF - all values passed will be treated as Word
variables.
fromLow: the lower bound of the value’s current range. An Integer value between -32767 and 32767.
fromHigh: the upper bound of the value’s current range. An Integer value between -32767 and 32767.
toLow: the lower bound of the value’s target range. An Integer value between -32767 and 32767.
toHigh: the upper bound of the value’s target range. An Integer value between -32767 and 32767.
calibration: optional calibration offset value. An Integer value between -32767 and 32767.
This is also an overloaded method. You can also use word variables to provide a returned result of 0-
65535.
value: the number to scale. A value between 0 and 0xFFFFF - all values passed will be treated as Word
variables.
fromLow: the lower bound of the value’s current range. A word value.
fromHigh: the upper bound of the value’s current range. A word value.
toLow: the lower bound of the value’s target range. A word value.
toHigh: the upper bound of the value’s target range. A word value.
636
calibration: optional calibration offset value. An Integer value between -32767 and 32767.
Explanation:
Scales, re-maps, a number from one range to another. That is, a value of fromLow would gets scaled
to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.
The method does not constrain values to within the integer range returned, because out-of-range
values are sometimes intended and useful.
Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the
scale() method may be used to reverse a range of numbers, for example:
The method also handles negative integer numbers well, so that this example:
Sqrt
Syntax:
Explanation:
A square root routine for Great Cow BASIC. The function only involves bit shifting, addition and
subtraction, which makes it fast and efficient.
This method required a word variable as the input and a word variable as the output. The method will
handle arguments of up to 4294.
Command Availability:
Example:
637
;----- Configuration
;----- Constants
;----- Variables
;----- Program
638
print outStr ;display results
wait 2 S
next i
Supported in <MATHS.H>
Syntax:
Explanation:
Great Cow BASIC supports the following functions, sin(x), cos(x), tan(x), where x is a signed integer
representing an angle measured in a whole number of degrees. The output values are also integers,
represented as fixed point decimal fractions.
Details:
The sine, cosine and tangent functions are available for your programs simply by including the header
file offering the precision you need.
In fixed point representation, the decimal point is assumed. For example, with two places of accuracy,
sin(60) returns 87, which you would interpret as 0.87. With three places, 866 is returned, to be
interpreted as 0.866, and so on. Another way of thinking of this is to consider the two-place values as
scaled up by 100, the three-place values scaled up by 1000 and the four-place values scaled up by
10,000.
Sine and cosine are always defined, but remember that tangent fails to exist at 90 degrees, 270 degrees
and all their coterminal angles. It is the responsibility of the calling program to avoid these special
values.
Note that the tangent function is not available to four decimal places, since its value grows so rapidly,
exceeding what the Integer data type can represent.
639
These routines are completely general. The input argument may be positive, negative or zero, with no
restriction on the size. Further observe that lookup tables are used, so the routines are very fast,
efficient and accurate.
;----- Configuration
#chip 16F88, 8 ;PIC16F88 running at 8 MHz
#config mclr=off ;reset handled internally
#config osc=int ;use internal clock
#include <Trig3Places.h>
;----- Constants
;----- Variables
dim i as integer
dim outStr, valStr as string
;----- Program
640
wait 500 mS ;pause to view
cls ;do likewise for tangent
print "tan("
print i
print ")="
locate 1,0
printTrig(tan(i))
wait 500 mS ;pause to view
next i
valStr = str(value)
length = len(valStr)
select case length
case 1:
outStr = outStr + "0.00" + valStr
case 2:
outStr = outStr + "0.0" + valStr
case 3:
outStr = outStr + "0." + valStr
case 4:
outStr = outStr + left(valStr,1) + "." + right(valStr,3)
case 5:
outStr = outStr + left(valStr,2) + "." + right(valStr,3)
end select
print outStr
end sub
Trigonometry ATAN
Syntax:
Explanation:
641
Details:
Great Cow BASIC supports the following functions ATan( x, y) where x and y are the vectors. The
function returns a word representing the angle measured in a whole number of degrees.
The function also returns a global variable NegFlag with returns the quadrant of the angle.
Quadrant 1 = 0 to 89 +
Quadrant 2 = 90 to 179 +
Quadrant 3 = 180 to 269 +
Quadrant 4 = 270 to 359 +
This ATan function is a fast XY vector to integer degree algorithm developed in Jan 2011, see
www.RomanBlack.com and sSee http://www.romanblack.com/integer_degree.htm
The function converts any XY values including 0 to a degree value that should be within +/- 1 degree of
the accurate value without needing large slow trig functions like ArcTan() or ArcCos().
At least one of the X or Y values must be non-zero. This is the full version, for all 4 quadrants and will
generate the angle in integer degrees from 0-360. Any values of X and Y are usable including negative
values provided they are between -1456 and 1456 so the 16bit multiply does not overflow.
642
Peripheral Pin Select
This is the Peripheral Pin Select section of the Help file. Please refer the sub-sections for details using
the contents/folder view.
Introduction:
Peripheral Pin Select (PPS) enables the digital peripheral I/O pins to be changed to support mapping of
external pins to different pins.
In older 8-bit Microchip devices, a peripheral was hard-wired to a specific pin (example: PWM1 output
on pin RC5).
PPS allows you to choose from a number of output and input pins to connect to the digital peripheral.
There are cases where a change of I/O position can make a circuit board easier to route Sometimes
mistakes are found too late to fix so having the option to change a pinout mapping in software rather
than creating a new printed circuit board can be very helpful.
You must use both commands UnLockPPS and LockPPS to complete any PPS changes. Great Cow BASIC
includes these two macros to ensure this process is handled correctly.
Example:
643
'Please check configuration before using on an alternative microcontroller.
#chip 16f18855,32
#option explicit
#config RSTOSC_HFINT32
UnLockPPS
Syntax:
UNLOCKPPS
Explanation:
Peripheral Pin Select (PPS) has an operation mode in which all input and output selections can be
prevented to stop inadvertent changes.
PPS selections are unlocked by setting by the use of the UnLockPPS command.
Using this command will ensure the special sequence of Microchip assembler is handled correctly.
Command Availability:
644
#chip 16f18855,32
#option explicit
#config RSTOSC_HFINT32
LockPPS
Syntax:
LOCKPSS
Explanation:
Peripheral Pin Select (PPS) has an operation mode in which all input and output selections can be
prevented to stop inadvertent changes.
PPS selections are locked by setting by the use of the LockPPS command.
Using this command will ensure the special sequence of Microchip assembler is handled correctly.
Command Availability:
645
#chip 16f18855,32
#option explicit
#config RSTOSC_HFINT32
646
Compiler Directives
This is the Compiler Directives section of the Help file. Please refer the sub-sections for details using
the contents/folder view.
#asmraw
Syntax:
#asmraw [label]
#asmraw [Mnemonics | Directives | Macros] [Operands] ['comments]
Explanation:
The #asmraw directive is used to specify the assembly that Great Cow BASIC will use.
Anything following this directive will be inserted into ASM source file with no changes other than
trimming spaces - no replacement of constants.
Assembly is a programming language you may use to develop the source code for your application.
The directive must conform to the following basic guidelines. Each line of the source file may contain
up to four types of information:
• Labels
• Operands
• Comments
The order and position of these are important. For ease of debugging, it is recommended that labels
start in column one and mnemonics start in column two or beyond. Operands follow the
mnemonic.
Comments may follow the operands, mnemonics or labels, and can start in any column. The maximum
column width is 255 characters.
White space or a colon must separate the label and the mnemonic, and white space must separate the
mnemonic and the operand(s). Multiple operands must be separated by commas.
White space is one or more spaces or tabs. White space is used to separate pieces of a source line.
White space should be used to make your code easier for people to read.
647
Example
This example will generate the following in the ASM source file.
lds SysValueCopy,TCCR0B
andi SysValueCopy, 0xf8
inc SysValueCopy
sts TCCR0B, SysValueCopy
#chip
Syntax:
Explanation:
The #chip directive is used to specify the chip model and frequency that Great Cow BASIC will use.
The frequency is the frequency of the chip in MHz, and is required for the delay and PWM routines.
If frequency is not present the compiler will select a frequency default frequency that should work for
the microcontroller.
1. If the chip has an internal oscillator, the compiler will use that and pick the highest frequency it
supports.
2. If the chip does not have an internal oscillator, then Great Cow BASIC will assume that the chip is
being run at its maximum possible clock frequency using an external crystal.
3. If you are using an external crystal then you must specify a chip frequency.
When using an AVR, there is no need to specify "AT" before the name.
648
Examples:
#chip 12F509, 4
#chip 18F4550, 48
#chip 16F88, 0.125
#chip tiny2313, 1
#chip mega8, 16
Setting Other Clock frequencies: Some alternative compilers allow value of the clock frequency to be
set with the numerical value in Hertz (i.e. 24576000). This can be useful when using the clock
frequencies other than standard frequencies.
Great Cow BASIC requires clock frequencys to be specified in MHz, but will accept decimal points. For
example, if you wanted to run a 16F1827 at 24576000 Hz, you would write the following:
Each microcontroller has a microcontroller data file. This file is located in /GreatCowBasic/chipdata/
folder when installed.
An example is 12F1840.dat
The there are two sections in the microcontroller data file that control the "chip frequency", they are:
ChipData section
The ChipData section for 12F1840 microcontroller. The 12F1840 is used an example
649
[ChipData]
Prog=4096
EEPROM=256
RAM=256
I/O=6
ADC=4
MaxMHz=32
IntOsc=32, 16, 8, 4, 2, 1, 0.5, 0.25, 0.125
Pins=8
Family=15
ConfigWords=2
PSP=0
MaxAddress=4095
The 12F1840 microcontroller supports nine internal frequencies (ChipMHz). #Chip is used as follows:
#chip 12F1840, 32
1. When using the internal oscillator, it tells the compiler to set the chip clock frequency (FOSC) to
32MHz
2. It tells the compiler to calculate all delays (wait times) based upon FOSC of 32 MHz. Unlike Picaxe
Basic (and other compilers) Great Cow delays ("wait") are correct regardless of the setting of FOSC.
If you set the internal oscillator to 4 MHz a "wait 1 ms" will still be 1 ms.
If you set chipMHz to something other than the valid options in the [ChipData] IntOsc section of the
microcontroller specific dat file, then, the compiler assumes that you are using an external oscillator
and will calculate the delays according to the value you use. The wait times will be incorrect if you are
not using an external oscillator at the same frequency as ChipMhz.
Since "12" is not a valid internal osc frequency, the microcontroller FOSC will default to 8 MHz because
there is no external crystal installed. However, the wait times will be incorrect as they will be
calculated by the compiler based upon a 12 Mhz clock.
ConfigOps section
The [ConfigOps] section of 12F1840.dat is towards the end of the chip data file. For the 12F1940 is looks
like this
650
[ConfigOps]
OSC=LP,XT,HS,EXTRC,INTOSC,ECL,ECM,ECH
WDTE=OFF,SWDTEN,NSLEEP,ON
PWRTE=ON,OFF
MCLRE=OFF,ON
CP=ON,OFF
CPD=ON,OFF
BOREN=OFF,SBODEN,NSLEEP,ON
CLKOUTEN=ON,OFF
IESO=OFF,ON
FCMEN=OFF,ON
WRT=ALL,HALF,BOOT,OFF
PLLEN=OFF,ON
STVREN=OFF,ON
BORV=HI,LO,19
LVP=OFF,ON
OSC specifies which oscillator options are available for the specific microcontroller. INTOSC is the
internal oscillator. All others are some form of external clock source. PLLEN sets the internal Phase Lock
Loop either on or off. With this chip the default clock frequency is 8 Mhz. The PLL multiplies this by 4.
So to get 32 Mhz the basic internal oscillator will be 8 Mhz then multiplied by 4. For 16 MHz it wil be 4
multiplied by 2.
GCB sets the PLL automatically, so this option should generally be left alone. IF PLLEN is set to ON, then
GCB may not be able to set the correct frewuency of the internal oscillator. Only set PLL = ON if you
know what you are doing.
It is always a good practice to set the oscillator source in #config at the beginning of your code. This
prevents potential errors. Example:
#Chip 12F1840, 16
#Config OSC = INTOSC
In this example above, Great Cow BASIC will automatically set the necessary OSC bits for the
microcontroller. Frequency bits will be set to 4 MHz and the PLL will be turned on and wait times will
be calculated on an FOSC of 16.
You can set the clock to other frequencies but you have to put the PIC into EC or External Clock mode
and then supply that specific clock frequency to the OSC1 pin.
651
ECL - 0 MHz - 0.5 MHz
ECM - 0.5 MHz - 4 MHz
ECH = 4 MHz - 32MHz
Example: For a 2.1 MHz clock you would need to set the #config and the clock frequency, and, provide
the OSC1 pin with a 2.1 MHz signal.
#chip 12f1840,2.1
#config OSC = ECM
Notes
When "#config osc=" is not specified in the source code, most microcontrollers will default to an
external oscillator source. This means at runtime the chip is expecting an external clock signal. If the
external clock signal is not present, the chip detects a "failure" of the external clock and will "falls
back" to the default internal oscillator setting.
The PLLEN bit defaults to OFF. The PLL is enabled depending upon the ChipMhz in #Chip xxxxxx,
ChipMhz.
The Great Cow BASIC defaults - This is how the bits are set if there is no #config in the source code,
Great Cow BASIC does set certain bits. To examine what bits are set on a particular chip you can omit
#config in the source code, thenm compile the code and then use "Open ASM" in the Great Cow BASIC
IDE. The bits that are set will be in the config section. All other bits ( those not specifically set) with
#Config will be at the POR setting as described below, The POR settings are shown in the datasheet for
each microcontroller.
Currently Great Cow BASIC sets the LVP bit OFF by default on many chips. This does not affect normal
HV programming like a with a PicKit3. The default of LVP = OFF will prevent the microcontroller
from being programmed with Low Voltage Programmer. This means that if a PIC microcontroller has
previously been programmed with with "LVP = OFF", then it must be erased or reprogrammed with
LVP = ON using a HVP programmer prior to using certain programming devices e.g. Curiosity
development boards, or "NS" programmers as these required that LVP = ON.
When LVP = ON, the MCLR pin is automatically set to EXTERNAL MCLR. This means that the MCLRE
pin CANNOT be sue for general purpose I/O functions.
The native POR (Power On Reset) defaults. This is the state of the config bits after Power on if the ASM
code has no configuration entries or on a blank factory chip. The only way to power up in this state
with GCB code is to use " #option NoConfig" in the Great Cow BASIC source code.
#config
Syntax:
652
#config option1, option2, ... , optionN
Explanation:
The #config directive is used to specify configuration options for the chip. There is a detailed
explanation of #config in the Configuration section of help.
#define
Syntax:
Explanation:
#define will search through the program for Find, and replace it with the value given for Replace.
#if
Syntax:
#if Condition
...
#endif
Explanation:
The #if directive is used to prevent a section of code from compiling unless Condition is true.
Condition has the same syntax as the condition in a normal Great Cow BASIC if command. The only
difference is that it uses constants instead of variables and does not use "then".
Example:
653
'This program will pulse an adjustable number of pins on PORTB
'The number of pins is controlled by the FlashPins constant
#chip 16F88, 8
'Initialise
Dir PORTB Out
'Main loop
Do
#if FlashPins >= 1
PulseOut PORTB.0, 250 ms
#endif
#if FlashPins >= 2
PulseOut PORTB.1, 250 ms
#endif
#if FlashPins >= 3
PulseOut PORTB.2, 250 ms
#endif
#if FlashPins >= 4
PulseOut PORTB.3, 250 ms
#endif
Loop
#ifdef
Syntax:
Explanation:
654
• Checking if a system bit has been defined
The advantage of using #ifdef rather than an equivalent series of IF statements is the amount of code
that is downloaded to the chip. #ifdef controls what code is compiled and downloaded, IF controls
what is run once on the chip. #ifdef should be used whenever the value of a constant is to be checked.
Great Cow BASIC also supports the #ifndef directive - this is the opposite of the #ifdef directive - it will
remove code that #ifdef leaves, and vice versa.
Note: The code in the following sections will not compile, as it is missing #chip directives and Dir
commands. It is intended to act as an example only.
#define Blink1
#ifdef Blink1
PulseOut PORTB.0, 1 sec
Wait 1 sec
#endif
#ifdef Blink2
PulseOut PORTB.1, 1 sec
Wait 1 sec
#endif
This code will pulse PORTB.0, but not PORTB.1. This is because Blink1 has been defined, but Blink2 has
not. If the line was added at the start of the program, then both pins would be pulsed.
#define Blink2
The value of the constant defined is not important and can be left off of the #define.
655
#define PinsToFlash 2
This program uses a constant called PinsToFlash that controls how many lights are pulsed. PORTB.0 is
pulsed when PinsToFlash is equal to 1, 2 or 3, PORTB.1 is pulsed when PinsToFlash equals 2 or 3, and
PORTB.2 is flashed when PinsToFlash is 3.
#ifdef NoVar(ANSEL)
SET ADCON1.PCFG3 OFF
SET ADCON1.PCFG2 ON
SET ADCON1.PCFG1 ON
SET ADCON1.PCFG0 OFF
#endif
#ifdef Var(ANSEL)
ANSEL = 0
#endif
The above section of code has been copied directly from a-d.h. It is used to disable the A/D function of
pins, so that they can be used as standard digital I/O ports. If ANSEL is not declared as a system variable
for a particular chip, then the program uses ADCON1 to control the port modes. If ANSEL is defined, then
the chip is newer and its ports can be set to digital by clearing ANSEL.
Similar to above, except with Bit and NoBit in the place of Var and NoVar respectively.
#ifndef
Syntax:
656
#ifndef Constant | Constant Value | Var(VariableName)
...
#endif
Explanation:
The #ifndef directive is used to selectively enable sections of code. It is the opposite of the #ifdef
directive - it will delete code in cases where #ifdef would leave it, and will leave code where #ifdef
would delete it.
#include
Syntax:
#include filename
Explanation:
#include tells Great Cow BASIC to open up another file, read all of the subroutines and constants from
it, and then copy them into the current program.
Absolute is used to refer to files in the ..\GreatCowBASIC\include directory. The name of the file is
specified in between < and > symbols. For instance, to include the file srf04.h, the directive is:
#include <srf04.h>
Relative is used to read files in the same folder as the currently selected program. Filenames are given
enclosed in quotation marks, such as where mycode.h is the name of the file that is to be read.
#include "mycode.h"
NOTES: It is not essential that the include file name ends in .h - the important thing is that the name
given to Great Cow BASIC is the exact name of the file to be included.
Those who are familiar with #include in assembly or C should bear in mind that #include in Great Cow
BASIC works differently to #include in most other languages - code is not inserted at the location of the
#include, but rather at the end of the current program.
657
#script
Syntax:
#script
[scriptcommand1]
[scriptcommand2]
...
[scriptcommandn]
#endscript
Explanation:
The #script block is used to create small sections of code which Great Cow BASIC runs during
compilation. A detail explanation and example are included in the Scripts article.
#startup
Syntax:
Explanation:
#startup is used in include files to automatically insert initialization routines. If a define or subroutine
from the file is used in the program, then the specified subroutine will be called.
The priority to #startup support the setting of the priority of the subroutines for all the libraries in a
project.
InitSys has priority 80, lowlevel communication routines have the priority of 90
All other subroutines defaults to 100.
658
Example 1:
This example from the hardware I2C library set the subroutine with the priority of 90.
#startup HIC2Init, 90
Example 2:
This example from would be included in user code to ensure the PPS setting are set prior to use of the
MSSP or USART.
#chip 16f18855,32
#option explicit
#config RSTOSC_HFINT32
#mem
This directive is obsolete.
Great Cow BASIC determines the amount of memory on a chip automatically, and will ignore the #mem
directive.
Other directives
The built-in #defines are used to support the #IFDEF command set are as follows. The table also shows
which #defines are supported as string in HSerPrint, SerPrint and other string related commands.
659
Directive Descri Output Usage Returns
ption
ChipADC String #IFDEF & Output The number of A/D inputs on the current chip
commands
ChipEEprom Numbe #IFDEF & Output The number of Bytes in EEPROM memmory
r commands
ChipIO String #IFDEF & Output The number of general purpose IO pins
commands
ChipReserve Numbe #Scripts, #IFDEF & The value of the words reserved
HighProg r Output commands
ChipWords Numbe #IFDEF & Output The number of WORDS in Flash memmory
r commands
Var() Functio Not applicable True if a register is declared (or false if not declared) in
n the currently specficied microcontroller’s .dat file.
Var(register_name)
NoVar() Functio Not applicable True if a register is NOT declared (or false if declared) in
n the currently specficied microcontroller’s .dat file.
NoVar(register_name)
Bit() Functio Not applicable True if a bit is declared (or false if not declared) in the
n currently specficied microcontroller’s .dat file.
Bit(bit_name)
NoBit() Functio Not applicable True if a bit is NOT declared (or false if declared) in the
n currently specficied microcontroller’s .dat file.
NoBit(bit_name)
Allof() Functio Not applicable True if all defines are declared: AllOf(define1, define2,
n …)
660
Directive Descri Output Usage Returns
ption
OneOf() Functio Not applicable True if one of the defines is declared: OneOf(define1,
n define2, …)
The table below shows two special directives that support the mapping for one variable or bit to
anoher variable or bit. This is useful when creating portable code or libraries to ensure Great Cow
BASIC
#samebit The compiler checks each item in the list to #samebit PLLEN, SPLLEN, SPLLMULT
see which ones are implemented on the Set SPLLEN On
current microcontroller.
If any of the bits do not exist, the compiler
will create a constant mapping to the name
of the first parameter in the list of
parameters that does exist. + If none of the
bits exist the no constant is created.
#sameva The compiler checks each item in the list to #samevar CMCON, CMCON0, CMCONbob
r see which ones are implemented on the #ifdef Var(CMCONbob)
current microcontroller. CMCONbob = 7
If any of the variables do not exist, the #endif
compiler will create a constant mapping to
the name of the first parameter in the list of Compiles to:
parameters that does exist. + If none of the ;CMCONbob = 7
variables exist the no constant is created. movlw 7
movwf CMCON,ACCESS
This table shows the ChipFamily constants mapped to the microcontroller architecture.
661
ChipFamily Value Microcontroller Characteristics
130 AVR core version V3E class microcontrollers but essentially the mega32u6 only
15 Mid-range core devices. 14 Bit instruction set with enhanced instruction set class
15 plus Mid-range core devices. 14 Bit instruction set with enhanced instruction set and
familyVariant=1 with large memory capability class
16 High end core devices. 16 Bit instruction set, memory addressing architecture
and an extended instruction set.
662
Compiler Options
This is the Compiler Options section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
#Option Explicit
Syntax:
#option explicit
This option ensures that all variables are dimensioned in the user program. The scope is the user code
only and no other code space like .h or include files.
#option explicit requires all variables,including bytes, in the user program to be defined.
Variables can be defined and not used within your user program. Unused variables will not allocate
memory.
Introduction:
Example:
'Example command
#option explicit
myuserflag = true
#Option NoConfig
Syntax:
#option NoConfig
This option will prevent the generated assembler from generating _Config items.
663
#option NoConfig is used when using a bootloader.
Introduction:
Example:
'Example command
#option NoConfig
'User Code......
#Option Bootloader
Syntax:
Explanation:
#option bootloader prevents the overwriting of any pre-loaded bootloader code, vectors, etc. below the
address and have all Great Cow BASIC code start at address.
A bootloader is a program that stays in the microcontroller and communicates with the PC, typically
through the serial interface. The bootloader receives a user program from the PC and writes it in the
flash memory, then launches this program in execution. Bootloaders can only be used with those
microcontrollers that can write their flash memory through software.
The bootloader itself must be written into the flash memory with an external programmer.
In order for the bootloader to be launched after each reset, a goto bootloader instruction must exist
somewhere in the first 4 instructions; There are two types of bootloaders, some that require that the
user reallocate the code and others that by themselves reallocate the first 4 instructions of the user
program to another location and execute them when the bootloader exits.
The diagram below shows the architecture of a bootloader. The left hand is the operation of the
instructions without a bootloader. The right hand shows the initial instruction of goto the bootoader,
then, when the bootloader has initialised the execution of the start code.
664
See example bootload software.
Example:
#Option NoContextSave
Syntax:
#option NoContextSave
Explanation:
Interrupts can occur at almost any time, and may interrupt another command as it runs. To ensure
that the interrupted command can continue properly after the interrupt, some temporary variables
(the context) must be saved. Normally Great Cow BASIC will do this automatically, but in some cases it
may be necessary to prevent this. If porting some existing assembly code to Great Cow BASIC, or
creating a bootloader using Great Cow BASIC that will call another program,
NoContextSave can be used to prevent the context saving code from being added automatically.
Be very careful using this option - it is very easy to cause random corruption of variables. If creating
your own context saving code, you may need to save several variables. These are:
665
For Atmel AVR microcontrollers: All 32 registers
Other variables may also need to be saved, depending on what commands are used inside the
interrupt handler. Everything that is saved will also need to be restored manually when the interrupt
handler finishes.
Example:
' This shows an example that could be used by a bootloader to call some application
code.
' The application code must deal with context save and restore
' Suppose that application code starts at location 0x100, with interrupt vector at
0x108
'Chip model
#chip 18F2620
#Option NoLatch
Syntax:
#option nolatch
Introduction:
The Great Cow BASIC compiler will redirect all I/O pin writes from PORTx to LATx registers on
16F1/18F
3. Microchip PIC microcontrollers.
666
The Microchip PIC mid-range microcontrollers use a sequence known as Read-Modify-Write (RMW)
when changing an output state (1 or 0) on a pin. This can cause unexpected behavior under certain
circumstances.
When your program changes the state on a specific pin, for example RB0 in PORTB, the
microcontroller first READs all 8 bits of the PORTB register which represents the states of all 8 pins in
PORTB (RB7-RB0).
The microcontroller then stores this data in the MCU. The bit associated with RB that you’ve
commanded to MODIFY is changed, and then the microcontrollers WRITEs all 8 bits (RB7- RB0) back
to the PORTB register.
During the first reading of the PORT register, you will be reading the actual state of the physical pin.
The problem arises when an output pin is loaded in such a way that its logic state is affected by the
load. Instances of such loads are LEDs without current-limiting resistors or loads with high capacitance
or inductance.
For example, if a capacitor is attached between pin and ground, it will take a short while to charge
when the pin is set to 1. On the other hand, if the capacitor is discharged, it acts like a short circuit,
forcing the pin to '0' state, and, therefore, a read of the PORT register will return 0, even though we
wrote a 1 to it.
Great Cow BASIC resolves this issue using the LATx register when writing to ports, rather than using
PORTx registers. Writing to a LATx register is equivalent to writing to a PORTx register, but readings
from LATx registers return the data value held in the port latch, regardless of the state of the actual
pin. So, for reading use PORTx.
Note:
You can use the #option nolatch if problems occur with compiler redirection.
#Option Volatile
Syntax:
Introduction:
This will cause the compiler to set the bit without any glitches when copying a value from another
variable, but will increase code size slightly.
667
Example:
'Example command
#option volatile portb.0
do forever
portb.0 = !portb.0
loop
#Option ReserveHighProg
Syntax:
This option reserves program memory to be kept free at the top end of memory. This useful for
HEF/SAF or bootloaders.
The option provided a reservation for the memory region that is normally assumed to be available to
the compiler for the application code storage. In order to avoid any possible conflict (overlapping code
and data usage), it is important to reserve the devices pecific memory range by using the compiler
option (shown above) in the project configuration.
Using the #option ReserveHighProg [words] exposes the constant `ChipReserveHighProg in the user
program.
In the example below the region 0x1F80 to 0x1FFF (flash block for a PIC16F1509 microcontroller) has
been removed from the default space available for code storage using the compiler option.
Example:
'Example command
#option ReserveHighProg 128
668
Using Assembler
This is the Using Assembler section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Assembler Overview
Introduction:
You can use microcontroller assembler code within your Great Cow BASIC code.
You can put the assembler code inline in with your source code. The assembler code will be passed
through to the assembly file associated with your project.
Great Cow BASIC should recognise all of the commands in the microcontroller datasheet.
The commands should be in lower case, this is good practice, and have a space or tab in front of the
command.
Even if the mnemonics are not being formatted properly, gputils/MPASM should still be capable of
assembling the source code.
Example:
btfsc STATUS,Z
bsf PORTB,1
669
Macros
This is the Macros section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Macros Overview
Introduction:
You can use macros within your Great Cow BASIC code.
Macros are similar to subroutines. But during compilation, everything is inserted inline. This may
increase the code size slightly, but it also reduces stack usage.
Parameters are handled in a similar way to how constants are handled, so there is a lot more freedom
when passing things in to a macro. (Unlike subs or functions, where everything must be stored in a
variable.)
For example, for PulseOut one parameter is a pin, and the other is a time length like "500 ms". Neither
of those parameters could be stored in a variable, but passing them in as macro parameters is possible.
Demonstration Program:
'PulseOut Macro
macro Pulseout (Pin, Time)
Set Pin On
Wait Time
Set Pin Off
end macro
670
Example Macros
This is the Example Macros section of the Help file. Please refer the sub-sections for details using the
contents/folder view.
Introduction
The demonstration shows how a macro can be used to optimised code by compiling code inline.
When the measurement of a pulse width to sub-microsecond resolution is required for instance
measuring the high or low pulse width of an incoming analog signal a comparator can be combined
with a timer to provide the pulse width.
Microchip PIC has published a "Compiled Tips 'N Tricks Guide" that explains how to do certain tasks
with Microchip PIC 8-bit microcontrollers.
This guide provides the steps that need to be taken to perform the task of measuring a pulse width.
The guide provides guidance on measuring a pulse width using Timer 1 and the CCP module. This
guidance was used as the basis for the Great Cow BASIC port the shown below. The guidance was
generic and in this example polling the CCP flag bit was more convenient than using an interrupt.
In this demonstration shown below, a 16F1829 microcontroller operating at 32 Mhz uses the internal
oscillator. The demonstration code is based on a macro that uses Timer1 and CCP4. However, any of
the four CCP modules could be used, the 16F1829 microcontroller has four CCP module.
The timer resolution of this method uses a timer Prescaler of 1:8 and a microcontroller frequency of 32
MHz giving a pulse width resolution is 1ms. With the timer Prescaler of 1:2 and the microcontroller
frequency of 32MHz the resolution is 250 ns.
The accuracy is dependent upon the accuracy of the system clock, but oscilliscope measurements have
show an accuracy of +- 1us from 3us to 1000us.
• Using Great Cow BASIC a macro to ensure the generated assembler is inline to ensure the timing is
consistent and no sub routines are called.
• A 4x20 LDC module with an I2C Backpack was used to display the results. However, as an
alternative, a serial output
to a terminal program to view the data could be used
This demonstration could be improved by adding code to poll the TIMER1 overflow flag. If the timer
671
overflows, then either no pulse was detected or the pulse was longer than allowed by the
prescaler/OSC settings. In this case, return a value of zero for pulse width.
Usage:
PULSE_IN
Demonstration Program:
#Chip 16F1829, 32
#CONFIG MCLRE = OFF
'Set up LCD
#define LCD_IO 10
#define LCD_SPEED FAST
#define LCD_Backlight_On_State 1
#define LCD_Backlight_Off_State 0
'Note: This example can be improved by adding code to poll the 'TIMER1 overflow flag.
IF the timer overflows, then either no 'pulse was detected or the pulse was longer than
allowed by the 'prescaler/OSC settings. In this case, return a value of zero 'for pulse
width.
CLS
PRINT "Pulse Width Test"
DIM PULSE_WIDTH AS WORD
DIR PORTC.6 IN
'Setup timer
'Set timer1 using PS1_2 gives 250ns resolution
InitTimer1 OSC, PS1_8
wait 1 s
CLS
672
PULSE_IN 'Call the Macro to get positive pulse width.
Locate 0,0
PRINT Pulse_Width
PRINT " "
wait 1 s
Loop
Introduction
A constant such as a Pin name cannot be passed to a sub routine or a function. This is a constraint of
Great Cow BASIC.
A macro can be used to implement a method of passing a constant to reusable code section.
The example shown below implements a button press routine and takes an input port constant and
prints the result on an LCD display.
Note: A macro will use more program memory as the macro will be compiled as inline code.
Therefore, for every use of the macro will use additional program memory - the same amount of
673
program memory for each call to the macro.
Demonstration Program:
#chip 16F877a, 16
#define Button PORTC.1 ' Switch on PIN 14 via 10K pullup resistor
DIR Button In
wait 1 sec
'USART settings
#define USART_BAUD_RATE 9600
#define USART_TX_BLOCKING
Do While Button = On
Wait 10 ms
ButtonCount += 1
Loop
674
Example Programs
Flashing LEDs and an Interrupt
Explanation:
This code implements four flashing LEDs. This is based on the Microchip PIC Low Pin Count Demo
Board.
The example program will blink the four red lights in succession. Press the Push Button Switch, labeled
SW1, and the sequence of the lights will reverse. Rotate the potentiometer, labeled RP1, and the light
sequence will blink at a different rate.
This implements an interrupt for the switch press, reads the analog port and set the LEDs.
Demonstration program:
#chip 18F14K22, 32
#config OSC = IRC, MCLRE_OFF
675
ccount = 8
leddir = 0
SET PORTC = 15
WAIT 1 S
SET PORTC = 0
DO FOREVER
INTON
ADCreading = ReadAD10(AN0)
if ADCreading < minwait then ADCreading = minwait
'Set LEDs
Set PortC = ccount
wait ADCreading ms
if leddir = 0 then
rotate ccount left simple
'Restart LED position
if ccount = 16 then
ccount = 128
end if
end if
if leddir = 1 then
rotate ccount Right simple
'Restart LED position
if ccount = 128 then
ccount = 8
end if
end if
'Reset interrupt - this may be been reset so set to zero so interrupt can
operate.
intstate = 0
676
Loop
'Interrupt routine.
sub Setir
end sub
sub setLedDirection
case 0
leddir = 1
ccount = 8
case 1
leddir = 0
ccount = 1
end select
End Sub
When called, this subroutine will blink an LED for the number of times and duration as determined by
the input parameters.
677
' Flash_LED (numtimes, OnTime, (optional) OffTime)
' Where numtimes is from 1 - 255 and OnTime/OffTime is
' from 0 - 65535 ms. If OffTime is not entered, then
' OffTime = OnTime.
Sub Flash_LED (in numtimes, in OnTime as WORD, Optional OffTime as WORD = OnTime)
repeat numtimes
set LED on
wait OnTime ms
set LED OFF
wait OffTime ms
end repeat
End Sub
Demonstration program:
678
#chip 18F25K22, 16
#config OSC = INTIO67, MCLRE = OFF, XINST = OFF
#define LED PORTC.1 'Led on PIN 14 via 1K resistor
DIR LED OUT
wait 1 sec
Sub Flash_LED (in numtimes, in OnTime as WORD, optional OffTime as word = OnTime)
repeat numtimes
set LED on
wait OnTime ms
set LED OFF
wait OffTime ms
end repeat
End Sub
The PulseOut Command is a reliable method for generating pulses if accuracy is not critical, the
PulseOut command uses a calculation of the clock to speed for the timing .
If you need better accuracy and resolution then an alternative approach is required.
To generate pulses in the 100 us to 2500 us range with an accuracy of +- 1us over this range is practical
using the approach shown in this example.
This example code works on a midrange PIC16F690 operating at 8Mhz. However, it should work on
any Microchip PIC microcontroller, but may need some minor modifications.
Usage:
Pulse_Out_us ( word_value )
679
How It Works:
Timer1 is loaded with a preset value based upon the variable passed to the sub routine. The timer
(Timer1) is started and the pulse pin (the output pin) is set high. When Timer1 overflows the Timer1
interrupt flag bit (TMR1IF) is set. This causes the program to exit a polling loop and set the pulse Pin off.
Then, Timer1 is stopped and TMRIF flag is cleared and the sub routine exits.
This method supports delays between 5 us and 65535 us and uses Timer1.
Test Results:
Demonstration program:
;**************************************
; Code: Output an accurate pulse
; Author: William Roth 03/13/2015
;**************************************
#chip 16F690,8
CLS
680
; ---- USART settings
#define USART_BAUD_RATE 38400
#define USART_TX_BLOCKING
DIR PORTB.7 OUT
681
Explanation:
This demonstration code shows the set of commands supported by Great Cow BASIC.
Demonstration program:
;Chip Settings
#chip 16F877a,16
#include <glcd.h>
wait 1 s
GLCDCLS
GLCDPrint 0, 1, "Great Cow BASIC "
wait 1 s
GLCDCLS
rrun = 0
dim msg1 as string * 16
do forever
GLCDCLS
Box 18,30,28,40
Line 0,0,127,63
682
Line 0,63,127,0
wait 1 s
FilledBox 18,30,28,40
wait 1 s
GLCDCLS
GLCDDrawString 30,0,"ChipMhz@"
GLCDDrawString 78,0, str(ChipMhz)
Circle(10,10,10,1) 'upper left
Circle(117,10,10,1) 'upper right
Circle(63,31,10,1) 'center
Circle(63,31,20,1) 'center
Circle(10,53,10,1) 'lower left
Circle(117,53,10,1) 'lower right
wait 1 s
GLCDDrawString 30,0,"ChipMhz@"
GLCDDrawString 78,0, str(ChipMhz)
FilledCircle(10,10,10,1) 'upper left
FilledCircle(117,10,10,1) 'upper right
FilledCircle(63,31,10,1) 'center
FilledCircle(63,31,20,1) 'center
FilledCircle(10,53,10,1) 'lower left
FilledCircle(117,53,10,1) 'lower right
wait 1 s
GLCDCLS
GLCDDrawString 30,0,"ChipMhz@"
GLCDDrawString 78,0, str(ChipMhz)
Circle(10,0,10,1) 'upper left
Circle(117,0,10,1) 'upper right
Circle(63,31,10,1) 'center
Circle(63,31,20,1) 'center
Circle(10,63,10,1) 'lower left
Circle(117,63,10,1) 'lower right
wait 1 s
GLCDCLS
GLCDDrawString 0,10,"Hello" 'Print Hello
wait 1 s
GLCDDrawString 0,10, "ASCII #:" 'Print ASCII #:
Box 18,30,28,40 'Draw Box Around ASCII Character
for char = 0x30 to 0x39 'Print 0 through 9
GLCDDrawString 16, 20 , Str(char)+" "
GLCDdrawCHAR 20, 30, char
wait 250 ms
683
next
line 0,50,127,50 'Draw Line using line command
for xvar = 0 to 80 'Draw line using Pset command
pset xvar,63,on
next
FilledBox 18,30,28,40 'Draw Box Around ASCII Character '
wait 1 s
GLCDCLS
GLCDDrawString 0,10,"End "
wait 1 s
GLCDCLS
workingGLCDDrawChar:
dim gtext as string
gtext = "KS0108"
wait 1 s
GLCDCLS
loop
For more help, see Graphical LCD Demonstration, GLCDCLS, GLCDDrawChar, GLCDPrint,
GLCDReadByte, GLCDWriteByte, Pset
InfraRed Remote
Explanation:
Great Cow BASIC support interfacing with IR remote controls. The header file contains explanations,
684
for both hardware and software.
This has been tested on many different IR sensors, and different remote controls.
Demonstration program:
The example is expected to work with most any IR sensor running at a 38 kHz carrier frequency.
685
;This demo prints the device number and key number sent by
;a Sony compatible IR remote control unit to an LCD
;----- Constants
;----- Variables
;----- Program
do
readIR_Remote(device, button) ;wait for button press
locate 1,0
print "Button: " ;show button code
print button
686
See also SonyRemote.h.
SonyRemote.h
This include file will let you easily read and use the infrared signals from a Sony compatible television
remote control. In particular, the remote control transmits a pulse modulated signal, the sensor detects
this, and the subroutine in this header file decodes the signal, returning two numbers: one
representing the device (television, VCR, DVD, tuner, etc.), while the the other returns the key which
has been depressed (VOL+, MUTE, channel numbers 0 through 9, etc.).
This has been tested and confirmed with a fixed remote control purchased surplus for $2.00 from All
Electronics, as well as an universal remote control, set to Sony mode.
Moreover it has also been tested with a Panasonic IR sensor and a Vishay sensor, both purchased
surplus for about fifty cents.
Every combination performed well, and it is probably the; case that most any garden variety 38 kHz IR
sensor will work. The only tricky bit is making sure you get the pinout for your sensor correct; search
out the datasheet for whichever device you use.
It is essential to filter the power applied to the Vcc pin. Do this by connecting a 100 ohm resistor from
the +5V power supply to the Vcc pin, and bridge the pin to ground with a 4.7uF electrolytic capacitor.
There is only one constant required of the calling program. It indicates which port line the IR sensor is
connected to. For example,
readIR_Remote(IR_rem_dev, IR_rem_key)
The values returned are, respectively, the device number mentioned earlier and the key that is
currently pressed. Both are byte values.
Seventeen local bytes are consumed, and two bytes are used for the output parameters. That’s a grand
total of nineteen bytes required when invoking this subroutine.
Header File
687
sub readIR_Remote(out IR_rem_dev as byte, out IR_rem_key as byte)
dim IR_rem_count, IR_rem_i as byte
dim IR_rem_width(12) as byte ;pulse width array
do
IR_rem_count = 0 ;wait for start bit
do while IR_DATA_PIN = 0 ;measure width (active low)
wait 100 uS ;24 X 100 uS = 2.4 mS
IR_rem_count++
loop
loop while IR_rem_count < 20 ;less than this so wait
Great Cow BASIC can draw circles using the midpoint circle algorithm. The midpoint circle algorithm
688
determines the points needed for drawing a circle. The algorithm is a variant of Bresenham’s line
algorithm, and is thus sometimes known as Bresenham’s circle algorithm, although not actually
invented by Jack E. Bresenham.
The example program below show the midpoint circle algorithm within Great Cow BASIC.
#include <glcd.h>
;----- Constants
689
#define GLCD_CS1 PORTA.1 ;LCD pin 13
#define GLCD_RESET PORTA.2 ;LCD pin 14
#define GLCD_RW PORTA.3 ;LCD pin 15
#define GLCD_RS PORTA.4 ;LCD pin 16
#define GLCD_ENABLE PORTA.6 ;LCD pin 17
; Vee = pot low side ;LCD pin 18
; backlight anode ;LCD pin 19
; backlight cathode ;LCD pin 20
;----- Program
Do forever
GLCDDrawString 30,0,"ChipMhz@"
GLCDDrawString 78,0, str(ChipMhz)
Circle(10,10,10,0) ;upper left
Circle(117,10,10,0) ;upper right
Circle(63,31,10,0) ;center
Circle(63,31,20,0) ;center
Circle(10,53,10,0) ;lower left
Circle(117,53,10,0) ;lower right
GLCDDrawString 30,54,"PIC16F886"
loop
This program demonstrates how to read and write data from an EEPROM device using the serial
protocal called I2C.
This program uses the hardware I2C module within the microcontroller. If your microcontroller does
not have a hardware I2C module then please use the software I2C Great Cow BASIC ibrary.
2. Write and read a page of 64 bytes to and from the EEPROM, and
This program has an interrupt driven serial handler to capture and manage input from a serial
690
terminal.
Demonstration program:
' THIS CONFIG OF THE SERIAL PORT WORKS WITH max232 THEN TO PC
' USART settings
#define USART_BAUD_RATE 9600
Dir PORTc.6 Out
Dir PORTc.7 In
#define USART_DELAY 0 ms
#define USART_TX_BLOCKING
wait 500 ms
691
dim EepromAddress, syscounter as word
#define EEpromDevice 0xA0
do
HSerPrintCRLF 2
HSerPrint "Commence Array Write and Read"
'Populate the array
for tt = 1 to 64
outarray(tt) = tt
next
HSerPrintCRLF 2
HSerPrint "Commence Write and Read a single byte":HSerPrintCRLF
HSerPrint "Read value should be "
HSerPrint str(location):HSerPrintCRLF
HSerPrint "Read = "
'Use library to write and read from the I2C EEPROM
eeprom_wr_byte (EEpromDevice, location, location)
eeprom_rd_byte (EEpromDevice, location, bbyte )
HSerPrint bbyte
692
location++
HSerPrintCRLF 2
693
case 209
Hserprint "DS1307_RTC_Device"
case 249
Hserprint "FRAM_Device"
case else
Hserprint "Unknown_Device"
end select
HI2CSend ( 0 )
HSerPrintCRLF
end if
HI2CStop
next
HSerPrint "End of Device Search"
HSerPrintCRLF 2
end sub
694
exit sub
end select
end if
next
HSerPrintCRLF
HSerPrint "Done"
end Sub
This program demonstrates how to control and display using a LCD the code for the keypad.
This program can be adapted. This program uses the hardware I2C module within the microcontroller.
If your microcontroller does not have a hardware I2C module then please use the software I2C Great
Cow BASIC library; for most microcontrollers.
This program also has an interrupt driven I2C handler to manage the I2C from the Start event.
Demonstration program:
'Code for the keypad and LCD Microchip PIC microcontroller on the Microlab board v2
'microcontroller is responsible for:
' - Reading keypad
695
' - Displaying data on LCD
' - communication with main Microchip PIC microcontroller via I2C
' - providing 5 keypad lines to main Microchip PIC microcontroller (for
compatibility)
' - receiving remote control signals for button and keypad
'This code has support for two keypad layouts. This is one possible layout:
'0123
'4567
'89AB
'CDEF
'And this is the other possible layout:
'123A
'456E
'789D
'A0BC
'Select the keypad layout by uncommenting one of these lines:
'#define KEYPAD_KEYMAP KeypadMap0123
#define KEYPAD_KEYMAP KeypadMap123F
696
'Keypad ports connected to main Microchip PIC microcontroller
'These are disabled when KeyoutDisabled = true
#define KEYOUT_A PORTC.5
#define KEYOUT_B PORTC.2
#define KEYOUT_C PORTC.1
#define KEYOUT_D PORTC.0
#define KEYOUT_DA PORTB.1
'I2C ports
#define I2C_DATA PORTC.4
#define I2C_CLOCK PORTC.3
'RS232/USART settings
'To do if/when remote support needed
'Initialise
Dir KEYOUT_A Out
Dir KEYOUT_B Out
Dir KEYOUT_C Out
Dir KEYOUT_D Out
Dir KEYOUT_DA Out
'Key buffers
'255 indicates no key, other value gives currently pressed key
697
RemoteKey = 255
OutKey = 255
KeyoutDisabled = False 'False if KEYOUT lines used to send key
'Main loop
Do
Loop
698
13
12
3
6
9
11
2
5
8
0
1
4
7
10
End Table
Sub CheckPressedKeys
'Subroutine to:
' - Read keypad
' - Check remote keypress
' - Decide which key to output
'Read keypad
If RemoteKey <> 255 Then
OutKey = RemoteKey
Else
EnableKeypad
OutKey = KeypadData
End If
End Sub
Sub EnableKeypad
'Disable LCD so that keypad can be activated
Set LCD_RW Off 'Write mode, don't let LCD write
'Re-init keypad
InitKeypad
End Sub
Sub I2CHandler
'Handle I2C interrupt
'SSPIF doesn't trigger for stop condition, only start!
699
Do While HI2CHasData
HI2CReceive DataIn
'Sending code
If BufferSize = 0 Then
LastI2CWasRead = False
'Detect read address
If DataIn = 129 Then
LastI2CWasRead = True
HI2CSend OutKey
KeyoutDisabled = True
Dir KEYOUT_A In
Dir KEYOUT_B In
Dir KEYOUT_C In
Dir KEYOUT_D In
Dir KEYOUT_DA In
Exit Sub
End If
End If
End Sub
Sub SendKeys
700
KEYOUT_C = 1
Else
KEYOUT_C = 0
End If
If OutKey.3 Then
KEYOUT_D = 1
Else
KEYOUT_D = 0
End If
KEYOUT_DA = 1
Else
'If no key pressed, clear data available line to main Microchip PIC
microcontroller
KEYOUT_DA = 0
End If
End Sub
Sub ProcessI2C
If HI2CStopped Then
IntOff
CmdControl = I2CBuffer(1)
'Set backlight
If CmdControl.6 = On Then
Set BACKLIGHT On
Else
Set BACKLIGHT Off
End If
701
LCDDataBytes = CmdControl And 0x0F
If LCDDataBytes > 0 Then
For CurrSendByte = 1 To LCDDataBytes
LCDWriteByte I2CBuffer(LCDDataBytes + 1)
Next
End If
'LCDWriteByte I2CBuffer(2)
OldBufferSize = 0
End If
End Sub
This program demonstrates how to drive an RGB LED to create 4096 different colors. Each of the three
;elements (red, green and blue) responds to 16 different levels. A value of 0 means the element never
turns on, while a value of 15 means the element never shuts off. Values in between these two extremes
vary the pulse width.
The basic carrier frequency depends upon the microcontroller clock speed. For example, with an 8
MHz clock, the LED elements are modulated at about 260 Hz. The interrupts are generated by Timer 0.
With an 8 MHz clock they occur about every 256 uS. The interrupt routine consumes about 20 uS.
Do not forget the current limiting resistors to the LED elements. A value of around 470 ohms is typical,
but you may want to adjust the individual values, to balance the color response.
In this demonstration, three potentiometers are used to set the color levels using the slalom technique.
Demonstration program:
702
;----- Configuration
#chip 16F88, 8 ;PIC16F88 running at 8 MHz
#config mclr=off ;reset handled internally
#config osc=int ;use internal clock
;----- Constants
#define LED_R PortB.0 ;pin to red element
#define LED_G PortB.1 ;pin to green element
#define LED_B PortB.2 ;pin to blue element
;----- Variables
dim redValue, greenValue, blueValue, ticks as byte
;----- Program
dir PortA in ;three pots for inputs
dir PortB out ;the LED outputs
on interrupt Timer0Overflow call update
initTimer0 Osc, PS0_1/2
do
redValue = readAD(AN0)/16 ;red -- 0 to 15
greenValue = readAD(AN1)/16 ;green -- 0 to 15
blueValue = readAD(AN2)/16 ;blue -- 0 to 15
loop
703
Serial/RS232 Buffer Ring
Explanation:
This program program uses an interrupt event to capture the incoming byte value and place in the
buffer ring. When a byte is received the buffer ring is incremented to ensuer the next byte is handled
correctly.
Testing bkbhit will set to True when a byte has been received. Reading the function bgetc will return
the last byte received.
Demonstration program:
;Chip Settings
#chip 16F1937,32
#config LVP=OFF, BODEN=OFF, WDT=OFF, OSC=XT
' [change to your config] This is the config for a serial terminal
' turn on the RS232 and terminal port.
' Define the USART port
#define USART_BAUD_RATE 9600
#define USART_TX_BLOCKING
#define USART_BLOCKING
' [change to your config] Ensure these port addresses are correct
#define SerInPort PORTc.7
#define SerOutPort PORTc.6
'Set pin directions
Dir SerOutPort Out
Dir SerInPort In
'This assumes you are using an ANSI compatible terminal. Use PUTTY.EXE it is very
easy to use.
'Interrupt Handlers
On Interrupt UsartRX1Ready Call readUSART
704
#define bkbhit (next_in <> next_out)
;Variables
Dim buffer( BUFFER_SIZE - 1 ) 'we will use element 0 in the array as part of out
buffer
Dim next_in as byte: next_in = 0
Dim next_out as byte: next_out = 0
Dim syncbyte as Byte
Do
' Send some info to another device
Repeat 3
HSerSend syncbyte
end Repeat
HSerprint readad(an0)
do while bkbhit
' get three sync chars then display the next char in buffer
if bgetc = syncbyte and bgetc = syncbyte and bgetc = syncbyte then
HSerPrint "sync'ed '"
HSerPrint chr(bgetc)
HSerSend 10
HSerSend 13
end if
Loop
Loop
Sub readUSART
buffer(next_in) = HSerReceive
temppnt = next_in
next_in = ( next_in + 1 ) % BUFFER_SIZE
if ( next_in = next_out ) then ' buffer is full!!
next_in = temppnt
end if
End Sub
function bgetc
wait while !(bkbhit)
bgetc = buffer(next_out)
next_out=(next_out+1) % BUFFER_SIZE
end Function
Trigonometry Circle
Explanation:
705
Great Cow BASIC can draw circles on a Graphical LCD devcie using Great Cow BASIC library functions.
Demonstration program:
;----- Configuration
#include <GLCD.h>
#include <Trig2Places.h>
;----- Constants
706
#define GLCD_RESET PORTA.2 ;LCD pin 14
#define GLCD_RW PORTA.3 ;LCD pin 15
#define GLCD_RS PORTA.4 ;LCD pin 16
#define GLCD_ENABLE PORTA.6 ;LCD pin 17
; Vee = pot low side ;LCD pin 18
; backlight anode ;LCD pin 19
; backlight cathode ;LCD pin 20
;----- Variables
'None required.
;----- Program
GLCDCLS
707
Great Cow Graphical BASIC
This is the Great Cow Graphical BASIC section of the Help file. Please refer the sub-sections for details
using the contents/folder view.
Code Documentation
Documenting Great Cow BASIC is key for ease of use. This section is intended for developers only.
Documenting is the ability to read some extra information from comments in libraries.
Some comments that start with ''' have a special meaning, and will be displayed as tooltips or as
information to the user. These tooltips helps inexperienced users to use extra libraries.
1. Great Cow BASIC uses ; (a semicolon) to show comments that it has placed automatically, and ' to
indicate ones that the user has placed. When loading a program, it will not load any that start with
a ; (semi-colon). The use of comments do not impact the users but it worthy of note.
2. As for code documentation comments, Great Cow BASIC will load information about
subroutines/functions and any hardware settings that need to be set.
3. For subroutines, a line before the Sub or Function line that starts with ''' will be used as a tooltip
when the user hovers over the icon. A line that starts with '''@ will be interpreted differently,
depending on what comes after the @. '''@param ParamName Parameter Description will add a
description for the parameter. For a subroutine, this will show in the Icon Settings panel under the
parameter when the user has selected that icon.
4. For functions, it will show at the appropriate time in the Parameter Editor wizard. '''@return
Returned value applies to functions only. It will be displayed in the Parameter Editor wizard when
the user is asked to choose a function. An example of all this is given in srf04.h:
5. If a subroutine or command is used internally in the library, but Great Cow BASIC users should not
see it, it can be hidden by placing '''@hide before the Sub or Function line. Another example from
srf04.h:
'''@hide
Sub InitUSSensor
These should hopefully be pretty easy to add. It is also possible to add Hardware Settings. A particular
708
setting can be defined anywhere in the file, using this syntax:
These comments informs Great Cow BASIC when to show the setting. Normally, this is All, but
sometimes it can include a constant, a space and then a comma separated list of values. display name
is a friendly name for the setting to display. constant is the constant that must be set, and valuetype is
the type that will be accepted for that constant’s value. To allow for multiple value types, enter a list of
types with a | between them.
7. When the library is added the program, Great Cow BASIC will show a new device with the name of
the library file on the Hardware Settings window. The user can then set the relevant constants
without necessarily needing to see any code. Adding a Great Cow BASIC library to Great Cow BASIC
will not result in any changes to the library. Great Cow BASIC uses the information it reads to help
edit the user’s program, but then the user’s program is passed to the compiler along with the
unchanged library.
8. Hardware Settings are a bit more involved to add, but hopefully the bit of extra documentation for
709
subroutines will be straight forward.
710
Great Cow BASIC for Linux
This is the Great Cow BASIC for Linux section of the Help file. Please refer the sub-sections for details
using the contents/folder view.
Overview
Introduction: Great Cow BASIC can be used when using the Linux Operating System.
This instructions are not distribution specific, but are for Linux only (not Windows).
Instructions: Complete the following steps to compile and install Great Cow BASIC for Linux:
3. Unrar/unpack GCB@Syn.rar to a location of your choice within your home directory (eg. within
Downloads) with either a file manager or from a console.
4. From a console, change to the Great Cow BASIC Sources in the unpacked directory:
eg. cd ~/Downloads/GCB@Syn/GreatCowBasic/Sources/GCBASIC
5. Make sure that install.sh is set as executable (ie. chmod +x install.sh), and then execute:
./install.sh build
6. You will need root privileges for this step. You can switch user (su) to root, or optionally use sudo.
7. If you su’d to root, use exit to drop back to your normal user. Then, be sure to follow the
instructions given by the script for updating your path.
8. Confirm proper execution, and the version, of Great Cow BASIC by executing: gcbasic /version
Now you can create and compile Great Cow BASIC source files.
Programming microcontrollers:
To program your microcontroller with your Great Cow BASIC created hex file, you will need additional
programming and programmer software.
For Microchip PIC microcontroller programming, you might find what you need at:
711
http://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=pg164120
For Atmel AVR microcontroller programming, you will need avrdude. It should be available in your
distributions repository. If not, check here: http://www.nongnu.org/avrdude/
712
Great Cow BASIC for Apple macOS
This is the Great Cow BASIC for Apple macOS section of the Help file. Please refer the sub-sections for
details using the contents/folder view.
Great Cow BASIC can be used with the Apple macOS operating system.
1. download a macOS installer which will install a precompiled binary for Great Cow BASIC along
with support files and some optional components; or
2. download, compile and install Great Cow BASIC from the source files.
There are instructions below for both choices. If I was you, I would use the Great Cow BASIC Installer
and save valuable programming time :-)
1. Download the Great Cow BASIC - macOS Installer disk image (.dmg) file from
https://sourceforge.net/projects/gcbasic/files/GCBasic-macOS-Installer.dmg/download
2. Double click the .dmg file to mount it on your Desktop and a window will open which contains the
Installer.
3. Double click the Great Cow BASIC icon and follow the installer prompts.
Complete the following steps to compile and install Great Cow BASIC:
2. Download the Great Cow BASIC macOS Source Distribution from SourceForge at
https://sourceforge.net/projects/gcbasic/files/GreatCowBASIC-UNIX-v0_98_03.rar/download
3. Unfortunately Apple replaced the gcc compiler with the clang compiler and FreeBASIC needs the
real gcc due to a certain use of goto… so, you can compile your own version of gcc following the
instructions at: https://bitbucket.org/sol_prog/macos-gcc-binary or you can take the low road and
just download the pre-compiled binary version from https://bitbucket.org/sol_prog/macos-gcc-
binary/raw/38724f2d24aa5b7944d2227d8098b1d30ac5d7b5/gcc-8.1.tar.bz2
713
4. Open a Terminal window (Terminal can be found in Applications > Utilities).
5. Move gcc-8.1.tar.bz2 from your Downloads directory to your Home directory by typing the
following command in your Terminal window:
mv ~/Downloads/gcc-8.1.tar.bz2 ~/
6. Unpack the gcc-8.1.tar.bz2 compressed tar file by typing the following command into your Terminal
window:
7. Move the gcc-8.1 directory to the /usr/local/ directory by typing the following commands into your
Terminal window:
Note: You will be asked for your password to execute the above command.
8. Ensure that the Apple Developer Xcode app is installed. Xcode can be downloaded and installed
from the App Store for free.
9. Ensure that the Xcode command line tools are installed by typing the following command in your
Terminal window:
xcode-select --install.
10. Move the FreeBASIC compressed tar file from your Downloads directory to your home directory by
typing the following command in your Terminal window:
mv ~/Downloads/fbc-1.06-darwin-wip20160505.tar.bz2 ~/
11. Unpack the FreeBASIC compressed tar file by typing these commands in your Terminal window:
12. Move the Great Cow BASIC compressed tar file from your Downloads directory to your home
714
directory by typing the following command in your Terminal window:
mv ~/Downloads/GreatCowBASIC-UNIX-v0_98_03.rar ~/
13. Unpack the Great Cow BASIC compressed tar file by typing these commands in your Terminal
window:
unrar x GreatCowBASIC-UNIX-v0_98_03.rar
This will produce a new directory called GreatCowBasic. Note: If you do not currently have the unrar
program which can extract rar file archives you can download and install it for free from the App
Store.
14. Change to the GreatCowBasic/Sources directory by typing this command in your Terminal window:
cd ~/GreatCowBasic/Sources
15. Compile the Great Cow BASIC binary (gcbasic) by typing the following command into your
Terminal window:
sh DarwinBuild/build.sh
Note 1: If you did not install the various files with the same names as in the instructions above into
your Home directory, you will need to edit the build.sh script file and change the file paths and
filenames to the appropriate values.
Note 2: You may need to alter the library and include paths in the build.sh script depending on your
version of macOS (it is currently setup for the Xcode High Sierra 10.13 and Mohave 10.14 versions of
macOS).
16. Confirm the proper execution, and the version, of Great Cow BASIC by typing the following
command in the Terminal window:
gcbasic
Now you should be able create GCB source files with your favourite editor and compile those files with
the gcbasic compiler.
Programming microcontrollers
To program your microcontroller with your Great Cow BASIC-created hex file, you will need additional
715
hardware and software.
1. For Microchip PIC microcontroller programming, you might find what you need at:
https://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=pg164120 and the
macOS version of the pk2cmd v1.2 command line programming software.
2. For Atmel AVR microcontroller programming, you will need the avrdude programming software.
Check here: http://www.nongnu.org/avrdude/ for it.
Alternatively, if you use Virtual Machine software such as Parallels or VMWare Fusion to run Windows
programs, you can use Windows GUI programming software.
• For Microchip, the PICKit 2 and PICkit 3 standalone GUI software or even better the PICkitPlus
software (https://sourceforge.net/projects/pickit3plus/) for both the PICkit 2
(https://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=pg164120) and PICkit
3 (https://www.microchip.com/Developmenttools/ProductDetails/PG164130) which has fixed
various bugs in those programs and been updated to program the latest Microchip 8 bit
microcontrollers.
Help
Great Cow BASIC Help documentation is installed in the Documentation subdirectory in your
GreatCowBasic directory.
If at any time you encounter an issue and need help, you will find it over at the friendly Great Cow
BASIC discussion forums at https://sourceforge.net/p/gcbasic/discussion/
716
Great Cow BASIC installers:
The Windows Great Cow BASIC installer uses the InnoSoft installer with packaging completed using
R2Build.
The process uses a Gold build structure. The R2Build software creates four packages for Windows and
one package for the Linux distribution. The process is automated with automatic versioning and
configuration.
The macOS Great Cow BASIC installer uses the Packages installer
(http://s.sudre.free.fr/Software/Packages/about.html) with packaging completed using the Bourne shell
script pkg2dmg.sh to create a compressed disk image file containing the installer.
The DAT files are the Great Cow BASIC representation of the capabilities of a specific
microcontroller. The DAT is based upon a number of vendor sources and corrections/omissions added
by the Great Cow BASIC development team. The DAT file is exposed to the user program as a set of
registers and register bits that can used to configure the program in terms of the microcontroller
specifics.
Ste Description
p
1 Obtain the MPASM *.INC or the AVR *.XML files to be used. These files determine the scope of
registers and register bits.
2 For Microchip only. Place the source INC files in the ..DAT\incfiles\OrgFiles. Process the file using
'Preprocess.bat'. This is an AWK text processor - you will need AWK.EXE in the executing
folder. This preprocessing will examine all the INC files in the ..DAT\incfiles\OrgFiles folder. The
resulting files will be placed in the ..DAT\incfiles folder. The resulting files will have the 'BITS'
section sorted in port priority - this priority ensures the bits are assigned in the target DAT in the
same order every time.
717
Ste Description
p
3 Update the database of support microcontrollers. This database contains the microcontroller
configuration that Great Cow BASIC requires as the core information for the DAT files. The
database is called chipdata.csv or avr_chipdata.csv for Microchip and AVR respectively. - These
files are comma delimited. The first row of data specifies the field name - these field names
control the chip conversion program, see later notes. The database fields are controlled by the
Great Cow BASIC development team and the specification of the database may change between
releases to support new capabilities. Database fields will have the suffix of Variant such as
PWMTimerVariant and SMTClockSourceVariant and are microcontroller specific configuration
settings to support the various microcontroller settings. The values of these variants are
determined by the examination of the microcontroller datasheets as this information is NOT
specified in the source files. Variants are exposed in the user program with the prefix of Chip. If
the variant is called PWMTimerVariant a constant called ChipPWMTimerVariant will be exposed in the
user program.
4 Update the CriticalChanges.txt file, if required. The CriticalChanges.txt file contains changes to
the INC file during processing that are corrections or additions to the source files. The format of
each line is the filename , Append (new line) or Replace, find, replace. Each line is comma
delimited and spaces are NOT critical. Essentially, the processing with find a partial line and
replace or append the whole line. example: p16lf1615.inc,R,OSCFIE EQU H'0007',OSFIE
EQU H'0007' - so, when processing p16lf1615.inc find the line OSCFIE EQU H'0007' and
replace with OSFIE EQU H'0007'.
5 If required. This is not normally edited. Update the 18FDefaultASMConfig.txt to set the 18f
configuration defaults.
6 Maintain the conversion program. The conversion program may require maintenance. The
programs are written in FreeBASIC and therefore require compilation. An example of
maintenance is a new variant field is required. The source program will need to be updated to
support the new variant - simply edit the source, compile and publish. Another example is the
addition of a new Interrupt - follow the same process to edit, compile and publish.
7 Execute the program to convert the source files to the DAT files for Microchip or AVR. There are
two programs for each architecture. Executing the conversion program without a parameter will
process ALL the entries in the database (the csv file), passing a single parameter to the
conversion program will only convert the single microcontroller. The conversion program will
process as follows: a) Read the database for the chip specifics b) If a .DEV file or .INFO file is not
present a routine called GuessDefaultConfig is invoked. This method sets the bit(s). In all cases
the default mask is sometimes specified for a particular config option and that is used for
ASMConfig See the section below for the processing of a .DEV file. c) For all microcontrollers
read the CriticalChanges.txt file and process. d) For 18f microcontrollers read the
18FDefaultASMConfig.txt. This simply overwrites all options stated in 18FDefaultASMConfig.TXT
and marks this in the output DAT file. e) Create the output DAT file.
718
Where the default is selected from the Info_Type.
Prog = . An explanation of the parameter. The Prog value is measured in words. It is the same in the
device specific.dat files.
Microchip in the past have used words, but then they started using bytes on the website instead to
make their chips appear to have larger capacity.
An example: If a device has 8192 words, which is 8192 * 14 = 114688 bits, or 14336 bytes. It is an odd
measurement because dividing 14336 by 14/8 to see how many instructions you can use is extra maths
work within the compiler.
Great COW BASIC PROGram memory measurements in the compiler are in words.
719