Code Examples

Examples of PIC BASIC code, mainly written for the Basic Stamp, since PIC BASIC and PIC BASIC PRO are Stamp compatible, this code is of use for learning the theory and concepts of the PIC BASIC language.PIC BASIC users are invited to submit their commented PIC BASIC code for inclusion on this web page. send your code to code@picbasic.co.uk, or FTP to www.picbasic.co.uk/incoming and advise us by email of the upload. Please ensure that it is commented and let us know if you want your email address published to allow PICBASIC users to contact you.The files on this page are free for you to download, there is no technical support provided specifically for these files, please don't email us if you have a problem with the routines. Most code includes the original authors email address.

SONY RC5 Remote Control Encode and Decode. Article By Les Johnson with Source Code.
Download Source code:
asm_rx.asm
sony_rx.bas
Pseudo Multitasking This Page was submitted by, Timothy Box a member of the PIC BASIC mailing list.
The above link will take you to a page that will give you an explanation of the interrupt timer routine IDT I wrote. It cover's why and how I wrote it, along with instructions on using it in your PBP program.
You can also Download the Source code in .Zip Format.

SERIN.ZIP This file was submitted by, Jonathan Orrego a member of the PIC BASIC mailing list, The object is to show how easy it can be to connect your PIC to a PC to exchange and display Serial data.
  • The ZIP archive consists of the following: Serin.exe This sample program shows how to enable communications using VB and a 16F84 PIC. Its simple enough to keep beginners out of trouble. Jonathan says that a future release will show others functions.
  • You will need: * PicBasic compiler *
  • A pic (Jonathan tested only with the 16F84, but he thinks it will be ok for all PIC's)
  • The Serin.bas example in order to see it and compile it with the PicBasic compiler
  • A free comm in your pc in order to connect to the PIC
  • VB if you want to see the code, if not just run the exe

Submission to the PIC BASIC mailing List regarding Analogue to Digital I/O1. Can a pin previously set as an analog input be redefined (while the prog is running) as a digital?You can do this using TRISB.2 = 0 for example for "input" to "digital" not A/D. If you are talking about A/D over to digital then I am doing this currently in a program and it works fine. For some reason though I have not been able to go the other direction .....digital back over to A/D. 2. A code submission by Bill FindlayThis code routine demonstrates switching of an input from analog - digital - analog, as input only. Code was written for, and tested on, 16F877. It works, but probably could do with some refinement. For analog i/p I used a 2K multiturn precision linear pot. Also used same as digital switch by varying voltage i/p above and below port threshold values. When in analog mode, analog values between 0 and 1023 are displayed, and digital port reading displays constant value, 0. When in digital mode, analog value is pure garbage, and digital port reading switches when port digital switching threshold value is reached. Digital o/p would have its own set of problems. Might be possible if used in similar fashion to open-collector mode, but would require care in external hardware implementation to avoid voltage/current feedback loops etc
Here is another ADCIN routine that was submitted to the PIC BASIC mailing List
On the same subject:I, am using the ADC in the pic16f877. The data sheet says they are 10bit, I assume this means 0 - 1024 with 0 - 5V on the pin. All I can get is 0 - 255. What am I missing? - Shaun WoodsHere is some PIC Basic Pro code for 10-bit (0 - 1023) A/D reading with 16F877. With best regards, Dragan Kujovic
From the PIC BASIC mailing List 'Picbasic Pro program to read DS1820 1-wire temperature sensor and display temperature on LCD' and an alternative
FREQOUT !I have just started using pic basic pro and I am trying to write a program to run through the frequencies of musical notes. The program that I have included uses the lookup command to select each frequency and a counter to indicate which frequency to use. the first table has 85 entries and the second has the remaining entries. the if...else statements select the table to be used.
Garmin GPS to PIC and LCD display, code from a submission to the PIC BASIC mailing List three files 1, 2 & 3
INTERRUPTSFrom a submission to the PIC BASIC mailing List Hi all, I understand that many have problems with interrupts, and all they want is a timekeeper of some sort. Therefore i want to post this working code. Clock.bas and Int.bas It's a small program that writes a clock on a 4-line display, using interrupt on timer1. It also shows how it possible to make blinking lines. The file INT.BAS contains all the necessary code to give you a working 24 hour clock that runs in the background. Simply include this file at the top of your program and use the variables i_tic,i_sec,i_minu and i_hour, these will update automatically. It's written for 4MHz but is easy to change for other freqs. IngvarHere's that clock program again, now using timer0 instead. Could be useful for those PICs without Timer1. I must confess that i haven't checked it for accuracy for more than about 1 hour, but it seemed to be accurate within one second (i checked it against my wristwatch). IngvarAn example of implementing interrupts form another list member: Here is my 1302 program I used Quick Basic to set the clock if you need that Let me know I think I used an f84 chip for this job
Message sent to the PIC BASIC mailing ListHas any one wrote a routine in PicBasic to scan a 4X4 keyboard matrix and displayed the results on an LCD display?The ReplyHere is one that sends DTMF tones. It is 3x4 but you can modify it.
Hi all, Just a quick question on LCD custom graphics.Can anyone send me a simple one line example of how to do custom graphics on a 16x2 lcd using PBP and the lcdout command.If nobody helps you on this, keep nagging me, and i'll get to it. i've been doing it with quickbasic, and just need to adjust my code for picbasic. i've attached a small executable program to "paint" and convert the results to the correct hex/dec format. if it gets lost with the list, just email me: lightspeed@sbinet.comAnother solution from the PIC BASIC mailing List in assembler
Question posed on the PIC BASIC mailing list Is using an LCD in 8 bit mode faster or more code efficient than using it in 4 bit mode?Glendon Turner Wrote: Yes less time is taken in 8 bit mode. If you check some data sheets, in 8 bit mode, the entire 8 bits is sent in one data period. (cant remember the actual time .. 160uS I think), but in 4 bit mode, the data needs to be sent twice, so the actual time taken is longer. It is not actually double the time though. When complete (8 bits) data is received from the PIC, the LCD then processes it and displays it. During this time, the LCD is unavailable to the PIC. This overhead is common to 4 or 8 bit modes. But to answer your question, 8 bit mode is faster. But only about 160uS > faster. (less than 0.2 milliseconds)
A really useful snippet from the PIC BASIC mailing list LCDDEFS.BASHow to use "lcddefs.bas". First place the file in your PBP directory (where all the other .bas files are) then when you write a program, at the top of the file place the following:- include "lcddefs.bas".Right, say you want to clear the lcd screen and write "myname" this is how you would do it:-lcdout cmd,clss, "myname" lcdout followed by cmd which means a CoMmanD comes next, and then CLss means clear screen, then your text. cmd, can be followed by a few common commands they are:-cmd,clss = clear lcd screencmd,uline = underline cursorcmd,blink = blinking cursorthere are more commands to move the cursor to different positions, as follows. say you want to clear the lcd, move the cursor to the bottom line of the lcd and write "myname" (I use a 16 character by two line lcd display, top line is A and bottom line is B) this is how you would do it:-lcdout cmd,clss, cmd,movtob1, "myname"cmd,movtoa1 = move to top line first positioncmd,movtoa2 = move to top line second positionand so on,positions = a1 to a16 same for bottom line:-cmd,movtob1 = move to bottom line position 1 cmd,movtob2 = move to bottom line position 2 and so on, positions = b1 to b16
From the PIC BASIC mailing list LCDTHERM a BS2 program to read Dallas DS1620 and display on a LCD (4-bit interface)
From the PIC BASIC mailing list Here is my code for a fuel quantity gauge I built. I originally did this table lookup in assembly, but recently transcribed it to PBP for better readability. The fuel quantities in the table are in lbs. x 10. Also I call two routines to do 32bit multiplies and divides. These are rip-offs of the routines listed in Microchip's application guide. If anyone would like to see them let me know. Shawn KellerThanks for the program Shawn it will definitely help my learning curve. I would also like to see the subroutines for the multiplies and divides. No problem, here they are. I attached the files 32bit_Math.zip (use as INCLUDE's) since they are so small. Don't be alarmed by the assembly, just use them as they are. You must also include the following variable definitions
Todd Murphy wrote to the PIC BASIC mailing list FIRST OF ALL THANKS TO THOSE WHO HELP WITH MAX7219 SETUP INFO. BELOW IS THE CODE THAT I HAVE SO FAR TO DISPLAY SPEED READINGS (TARGET AND PATROL, 3 DIGITS EACH) FROM A RADAR THAT OUTPUTS 16 BYTES OF DATA AT 9600 BAUD, REPEATED EVERY 43mS. THE CODE DOES WHAT I WANT AT LEAST IN DISPLAYING THE SPEEDS, THE PROBLEM I SEEM TO HAVE IS I DON'T WANT TO SHOW THE LEADING ZEROS NOR DO I WANT TO SHOW ALL ZEROS WHEN THERE ARE NO SPEEDS. CAN ANYONE HELP ME IN SUPPRESSING THE LEADING ZEROS? ALSO, ANY ADVICE IN STREAMLINING MY CODE WOULD BE GREATLY APPRECIATED! THANKS, TODD MURPHY
From the PIC BASIC mailing list Negative BiasThis circuit is so simple it hurts. If you can afford to tie-up one of the PWM modules on the F877 then you can incorporate this circuit quite easily and inexpensively. Although RS232 and ICM7660 have been mentioned for powering the negative bias, this solution is nice for not adding another chip or where a RS232 chip is not incorporated. The PWM module can be set to a "fixed value" and forgotten about, or incorporated into your code for "user viewing adjustment". Alternatively, if the application is used in a wide temperature environment, one can use a thermistor/cap on another I/O to automatically compensate/correct the view angle. Wide temperature changes affect the LCD view angle. Also, you could program one of the 8-pin PICs or a 7555 to "independently" drive this circuit as well. It is important you use the low forward drop diodes in the circuit. The 1N34's are cheapDriver code
From the PIC BASIC mailing list I know I said that I had a deadline and that I'd get to writing the VB serial routine in a few days... but I couldn't sleep last night and decided to "tinker" with it a bit. I tinkered a lot... it's done, it's attached. The file "RS232.BS2" was written for a BASIC Stamp-2 but is easy to modify for the PIC. If you write your own program, have it do a "WAIT" until it detects "##", then have it store the number that follows. When it is finished doing whatever you want it to do with the number, have it send a reply. Everything is defaulted to 9600 BAUD, 8 bits, 1 stop bit. The other four files are the VB-4 program. It ran just fine under VB-6, so it should work on VB-5 just as well. Attached is RS232.ZIP which is the five files compressed using PKZIP. Just uncompress them into a sub-directory and have a blast. The VB program sends out a string consisting of "##" followed by a number. The number is determined by which button you press on the control panel. If you send out the number 0, I.E. "##0", the Stamp will perform a "soft reset". This means it will jump to the top of the program, set variables to 0, etc. I sprinkled it with comments... but it seems straight forward to me... even at 3:45am. Just have fun and let me know what happens. Jerry
From the PIC BASIC mailing list There has been a lot of questions about Infra-Red communications, both on the Piclist and Here on the PicBasic List and the replies have been rather thin. I myself have asked several questions about the subject without any reply. So here is my Code for sending and receiving Infra-Red Data without using any Encoder or Decoder chips (Just a PIC!). The transmitter uses assembly language but the way it is used is transparent to the Program. I hope you like it and ANY comments (both positive as well as negative) are most welcome. Regards Les les@top204.freeserve.co.uk
Routines for driving unipolar 4 phase stepper motor by Geoff FoxSome information about SERVOS and the puleout command
Dialer.bs2 DTMF dialer for upto 15 pre stored digitsFreeADC.bs2 Free running ADC to serial outputGPSmon.bs2 Displays latitude and longitude from a GPS (NMEA output) on a 16*2 lines 'LCD display.Keyboard.bs2 A keypad reading routineKeybtst.bs2 Routine to connect to a PC AT KeyboardMonitor.bs2 A room monitor (alarm?)Morse.bs2 Morse decoderMorse2.bs2 Morse sender (4 pre stored messages)Panservo.bs2 servo controller routinePantilit.bs2 another servo controller routinePrectemp.bs2 temperature reading with a Dallas DS1620Sertest.bs2 serial test routineStepctrl.bs2 stepper motor control