BASIC-52 FOR THE MICROPAC 515C I N T R O D U C T I O N This manual documents EMAC's adaptation of BASIC- 52. All information here supersedes its equivalent in the MCS BASIC-52 USER'S MANUAL (c) Intel Corporation, 1989. This manual is intended to be used with MCS BASIC-52 USER'S MANUAL. Take note that in BASIC-52, you can't jump out of FOR/NEXT, DO/WHILE, or DO/UNTIL loops. Otherwise you will overload the control stack. I M P O R T A N T D I F F E R E N C E S I N T H I S B A S I C The baud rate defaults to 9600 baud N,8,1 and is driven by TIMER1 instead of TIMER2. The IE function affects register IEN0. The IP function affects register IP0. The RCAP2 function allows you to read or write a 16 bit value to the special function registers CRCL and CRCH (CRCL corresponds to the lower 8 bits and CRCH to the upper 8 bits). These registers are used to reload TIMER2 when reload is enabled (see TIMER2 in the hardware manual). Note that the C515C's registers CRCL and CRCH correspond to the 8052's registers RCAP2L and RCAP2H, respectively. UO and UI allow for multiple input and output devices. Pulse width modulation is implemented using the C515C's compare outputs CC1, CC2 and CC3. N E W C O M M A N D S LOAD This retreives a file that was previously SAVEd (see SAVE). Note that LOAD will write over or merge with any program that is already in memory, so if this is undesirable type NEW first. This requires 128k of RAM. NVRUN This allows you to run a program without erasing the declared variables. This is useful for testing a program that you intend to run out of nonvolatile RAM and in which the variables are to be retained when power is off. Some tips for writing programs with nonvolatile variables: 1) You must make sure all string, dimensioned and other variables are allocated in the same same order each time the program is run. This can be done by allocating string space first (with STRING) then all dimensioned variables (with DIM) and then by reading all the simple variables into one dummy variable. For example, X=BB : X=CC : X=DD . This guarantees that the variables will be allocated in the same place since the interpreter allocates variable space depending on the order that it encounters the variables. 2) Do not use the CLEAR statement (CLEAR S and I are allowed) and do not use the STRING statement anywhere but once at the beginning of the program since it affects where all the variables are stored. 3) For extra data integrity have a shutdown subroutine in your program which is called when the computer needs to be shut off. This procedure should put the program in an idle state so that it won't be in the middle of writing to a variable when the microprocessor stops. SAVE If you have 128K of RAM on board, this command allows you to save one program for later retreival, even after a NEW or a reset. If you have the 128K RAMDISK option the program is maintained even after power is removed. TARGET This invokes the target application menu. See Target Applications. N E W S T A T E M E N T S ADCIN() This function returns the digital representation of the analog signal on analog to digital (A/D) channel . If the value for is in the range of 0 to 7, the value returned by the function is in 8 bit resolution and in the range 0 to 255. If the value for is in the range of 20 to 27, the value returned by the function is in 10 bit resolution and in the range of 0 to 1023 (with a slightly slower conversion rate). The following program shows the values in hex of the 8 A/D channels. 1010 ? " CHANNEL" 1020 ? " 0 1 2 3 4 5 6 7" 1030 FOR A1=0 TO 7 1040 PH0. ADCIN(A1), 1050 NEXT A1 1070 FOR A2=1 TO 20:NEXT A2 : REM WAIT 1080 ? CR, 1090 GOTO 1030 DACOUT()= This function writes to the optional 8 bit digital to analog convertor channel . The range on is 0 to 255 and can be 0, 1, 2 or 3 which correspond to channels A, B, C and D, respectively. The following example repeatedly sends an ascending ramp signal to all four DAC channels. 10 FOR A1=0 TO 3 20 FOR X=0 TO 255 30 DACOUT(A1)=X 40 NEXT X 50 NEXT A1 60 GOTO 10 HPWM ,, This enables or disables pulse width modulation (PWM) on the compare capture output selected by . The value for can be 1 to 3 selecting output on CC1 to CC3 (on digital I/O HDR 2) respectively. If is non-zero the respective output is enabled and if it is zero it is disabled. The value of can be 0 to 65535 and it is the value that TIMER2 must match to cause the respective output to go high. The output will go low again when TIMER2 rolls over from 65535 to the reload value. When this statement is executed TIMER2 is put in auto reload mode and defaults with a clock of 1/12 the crystal frequency. The period of the outputs can be changed by changing the value of RCAP2 (this is the name for BASIC's combination of TIMER2's reload registers CRCH and CRCL). The value of 0 gives the longest period and 65535 gives the shortest. Remember that the value of for a given output must be greater than RCAP2 or there will be no output change. Following example outputs from CC1 a duty cycle that starts at around 100% and goes down to 0%. 10 RCAP2=0 15 HPWM 1,1,1 20 FOR X=0 TO 65536 STEP 20 30 HPWM 1,1,X 40 NEXT X 50 HPWM 1,0,0 RTC() This function allows you to access the optional hardware Real Time Clock (RTC). This is unrelated to the CLOCK0, CLOCK1 and TIME statements. The value for can be 0 to 8 which selects one of nine registers used to acces the RTC. The registers and their ranges are as follows: register description range 0 hundredths of seconds 0-99 1 seconds 0-59 2 minutes 0-59 3 hours 0-23 if 24H mode, 1-12 otherwise 4 day of week 1-7 5 day of month 1-31 6 month 1-12 7 year 0-99 8 mode 0=AM, 1=PM, 2=24H To write to the RTC, write to any of registers 0 through 7 in any order but write to register 8 last. Writing to register 8 will cause the registers to be written to the RTC. To read the RTC, read register 8 first. This will copy the current time, date, and mode to the registers (the registers are only updated once each time register 8 is read). The following program demonstrates reading and writing to the RTC. The time 23:59:58.00 and date 12/31/99 is written to the RTC and then it shows the changing date and time on the terminal. 10 RTC(0)=0 : REM HUNDREDTHS 20 RTC(1)=58 : REM SECONDS 30 RTC(2)=59 : REM MINUTES 40 RTC(3)=23 : REM HOURS 50 RTC(4)=1 : REM DAY OF WEEK 60 RTC(5)=31 : REM DAY OF MONTH 70 RTC(6)=12 : REM MONTH 80 RTC(7)=99 : REM YEAR 90 RTC(8)=2 : REM SELECT 24H MODE AND WRITE 100 REM STORED DATA TO RTC 130 FOR X=8 TO 0 STEP -1 140 PRINT RTC(X), 150 NEXT X 160 PRINT 170 GOTO 130 EEPROM() This function allows reading and writing of the serial EEPROM. This device has 63 words (16 bit words) of non-volatile storage. The value written to it should be in the range of 0 to 65535. An of 1 to 63 will select one of the 63 words to write or read. Reading with of 0 will return the status of the EEPROM. If the value returned is 0, the data has been corrupted since the last write operation, and if it is 1 then the data is okay. WARNING: The serial EEPROM is limited to 10,000 write cycles, so it should only be used to store data that seldom changes. The following program writes to the EEPROM and reads it back along with the EEPROM status. 10 FOR X=1 TO 63 20 EEPROM(X)=(63-X)+(X*256) 30 NEXT X 40 FOR X=63 TO 1 STEP -1 50 PH1. EEPROM(X) 60 NEXT X 70 PRINT "status =",EEPROM(0) SFR() This function allows reading and writing of the special function registers (SFRs). The value read from or written to it and the value for must be in the range of 0 to FF hex. See the hardware manual for addresses of specific SFRs. Note that when reading an SFR port such as P4 the value returned is the value read from the port inputs, not necessarily the value last written to it. In order to toggle individual bits without affecting the others, it will be necessary to keep a variable that indicates the value last written to the port. 10 SFR(0E8H)=255 : REM SET UP P4 FOR ALL INPUTS 20 PH0. SFR(0E8H) , : REM SHOW THE HEX VALUE OF THE INPUT ON P4 30 GOTO 20 UI x The input source for the INPUT and GET statements can be redirected from the default of COM0 to other devices. The commands and the selected devices follow: UI 0 COM0 UI 1 COM1 (optional) UI 2 COM2 (optional) UI 3 keypad (optional) UI 4 data buffer 0 UI 5 data buffer 1 The keypad matrix (UI 3) returns the following ASCII characters when the corresponding X and Y coordinate is intersected (see the hardware manual for the definition of keypad X and Y): X1 X2 X3 X4 Y1 1 2 3 A Y2 4 5 6 B Y3 7 8 9 C Y4 (BS) 0 (CR) D Y5 W X Y Z In the above diagram, BS reperesents backspace and CR represents a carriage return. The keys can be redefined using UO 3. See the statement description of UO x. UI 4 and UI 5 are used to allow redirection of I/O to user defined I/O devices. These are not normally used before UO4 or UO5 buffers have been filled with data and ended with a CHR(0). See UO x description for details. Note that breaking out of a program by pressing control-C is only allowed on UI 0 so if a different input device is used you may not be able to break out of the program. UO x The output destination for PRINT and related statements can be redirected from the default of COM0 to other devices. The commands and the selected devices are as follow: UO 0 COM0 UO 1 COM1 (optional) UO 2 COM2 (optional) UO 3 keypad definition buffer UO 4 data buffer 0 UO 5 data buffer 1 UO 6 LCD (optional) Note that the PRINT statement automatically sends a carriage return (CR) and line feed (LF) unless you end the statement with a comma. This is significant with UO 6 since CR and LF perform screen positioning. It is also significant with UO 3, UO 4 and UO 5 if you want to send a single character to one of the buffers. To send a single character, use the statement "? CHR()," (note that the statement ends with a comma) . UO 4 redirects output to a 100 byte data buffer. Sending 0 to the buffer (this can be done with the statement "? CHR(0),") will store 0 at the current buffer position and reset the pointer to the beginning of the buffer. If the buffer is full, all subsequent characters written will be stored in the last location. When the program has finished sending all the characters to the buffer it should send 0 to mark the end of the buffer and reset the pointer to the beginning so it is ready to be read. UO 5 works the same way only it uses a different 100 byte buffer. UO 4 and 5 are designed to be used in conjunction with UI 4 and 5 to allow redirection of PRINT to user defined output devices and/or allow redirection of user defined input devices to an INPUT statement. For instance, to redirect data from a PRINT statement to digital output port 1, select one of the buffers, such as UO 5 and PRINT the desired data and "? CHR(0)," to mark the end. In the output subroutine select UI 5 and GET characters and send them out port 1 until CHR(0) is encountered. UO 3 is a 20 byte buffer which allows you to change the default keypad definitions. First Send 0 to the buffer to reset the pointer, then send 20 characters. For example: 10 UO 3 : REM SELECT KEYPAD DEFINITION BUFFER 20 PRINT CHR(0),"ABCDEFGHIJKLMNOPQRS",CR, This will define the keypad as follows (see the hardware manual for the definition of keypad X and Y): X1 X2 X3 X4 Y1 A B C D Y2 E F G H Y3 I J K L Y4 M N O P Y5 Q R S (RETURN) This definition allows the keypad to be used with the INPUT statement since one of the keys is defined to be the return key. UO 6 redirects all output to the optional LCD display. Most characters will be displayed the same on the LCD as they would to a terminal, but some characters may differ, so some experimentation may be necessary. Some characters have been defined to perform specific functions on 20 X 2 displays. They are as follows: 08h (backspace) move cursor 1 space back 0ah (line feed) move cursor to other line (i.e. from 1 to 2, or 2 to 1). 0dh (carriage return) move cursor to beginning of line. 1ah clear display and move cursor to top left corner. 1bh the next byte received after this will be written to register 0 of the LCD display. 1eh (home) move cursor to upper left corner. XBY() This function allows you to read or write to the external data memory. The value for can be -65535 to 65535. When writing to memory remember that since BASIC uses parts of address range 32768 to 65535, writing to these addresses can cause BASIC to crash. In boards with 128K RAM remember that SAVE stores your program starting at XBY address -1 and depending on the size of your SAVEd program, up to -65535. TARGET APPLICATIONS When you want to make a program run stand-alone, you have two options. If you have the 128k battery backed RAM, you can autostart a program which was SAVEd, or you may generate a HEX file of your program and take it to an EPROM programmer to burn it in. To make a target application, first load your program and test it. If it is running correctly, invoke the target application menu by using the command TARGET After this, the following main menu will appear: 1. Burn EPROM 2. Generate HEX file 3. Program options 4. Autostart Enter option->4 Pressing the escape key at this point will return you to BASIC. Pressing "4" will cause the following response: Press a key to change status, or escape for no change. Autostart is off Pressing any key, except escape, will cause the autostart feature to toggle on and off. Pressing escape will leave the autostart mode in the current selected state and return to the target menu. If Autostart is on, any program that was SAVEd will automatically load and run on power-up or during reset. NOTE: Battery-backed RAM is more easily corrupted than EPROM or flash, therefore autostart should not be used in critical applications and should not be used in remote locations (where much travel would be required to correct a failure). 1. Burn EPROM 2. Generate HEX file 3. Program options 4. Autostart Enter option->3 Pressing "3" will cause the following response: Option=2 This allows you to select the properties of the target application that you are making (whether you are generating a hex file or burning an EPROM). Currently there is only option 2 and 6. Option 6 prevents your variables from getting cleared on reset or power-up and is intended to be used in systems that have nonvolatile RAM (see command NVRUN). The default option 2 resets all variables on power-up and reset. Press enter twice at this point if the desired option is being displayed, or press 2 or 6 and then any other key to and the new option will be selected and the main menu will appear. Pressing the escape key at this point will return you to BASIC. Pressing "2" will cause the following response: Press a key when ready At this time you should prepare your terminal emulator to receive ASCII text into a file. Once it is ready, press the space key and you will see a HEX file begin to dump to the screen. (The whole process could take several minutes depending on the size of your program). When characters are no longer being printed to the screen you should tell the terminal emulator that this is the end of the file (if necessary). Then press a key and you will return to the main menu again. Pressing the escape key at this point will return you to BASIC. Following is an example of part of a HEX file dump: Press a key when ready :100000000203A820312DC0D0024003C0D0202E1002 :1000100002400B024013FFFFFFFFFFC0D0021FE9A9 :1000200002193AC0D0201F1C024023C0D002402B2E . .(and more) You may now take the file to an EPROM programmer and burn it into a 27C256 (32K by 8) EPROM. EMAC's EPROM Programmer Board is currently not supported by the Micropac 515C BASIC.