Swordfish (also known as SF) offers a truly structured and modular approach toward program development - very similar in some respects to programming with Visual Basic.
Swordfish produces extremely efficient and stable code. For more information or assistance, please visit the forum.
Free version limitations; 200 variables on compile, no program length restrictions. Download.
This tip is almost trivial, but it's a D'uh moment too. Just in case anyone else has missed the obvious. here's a quick tip.
I usually use Swordfish's hardware UART routines and I don't recommend Swordfish's software UART for input, where there are some "gotcha's." The software UART does work great for output.
A UART output is often handy for keeping an eye on program parameters and status. I usually use the hardware UART,...
A square wave generator can be handy for simulating sensors and providing an input to a micro-controller. This signal generator can be adjusted from 10 Hz - 10 kHz, with an adjustable duty cycle from 1 to 99 percent.
I recently purchased a couple of the Parallax RFID readers (#28140) that were on sale at Radio Shack. These are similar to the one Graham used to develop his code, so I wanted to see if the code would work. I'm happy to report that the code does work for this RFID reader with no changes, but some bumps along the way provide some useful troubleshooting tips.
To verify that the DS18B20 tutorial does indeed work as written, I wanted to test it out.
Equipment:
DS18B20 temperature probe from Sure Electronics (convenient package - a bare part would work just as well)
TAP-28 board, equipped with an 18F242 processor and 20 MHz crystal
(2) PICkit 2 - for convenience, 1 used for programming, 1 for UART tool
Procedure (with timeline):
1:00
Begin. Gather hardware together on top ...
A simple program that generates some LED patterns. I wrote this to help someone else out on another site, and thought it would be worth sharing here?
The patterns are (in order of sequence):
Left to right
Right to left
Center to outside
Outside to center
All flashing
The Result
Results are usually more interesting then how-you-got-there, so here they are:
(A simple circuit. 2N2222 NPN transistors could be used to switch banks of...
EM4100 readers are somewhat popular because they are cheap, widely available and easy to use. You can pick up an EM4100 RFID reader for ~$40 or less. The module used in this example may not work for every EM Reader out there - though it offers an approach to start with. If you do create a new module for a different device, then please share it here at digital-diy.com
This program was the result of a recent issue a digital-diy.com member had regarding SD Cards. It also provides a means to benchmark the speeds of different cards and software configurations. Please ensure you are using the latest version of SDFileSystem.bas (at time of writing: 4.1.4). If you are updating (or do not...
The LS20031 GPS Module supports up to 32 Channels and outputs at 5Hz. That's some serious specs and a will serve the needs of almost any GPS application!
Also, the LS20031 GPS receiver includes an embedded antenna and GPS receiver circuits. It is a low-cost unit which outputs an astounding amount of position information 5 times a second. The receiver is based on the proven technology found in LOCOSYS 32 channel GPS SMD type receivers that use MediaTek...
PORTBinterrupts are used for instantaneous response for changes to Pins 4, 5, 6, 7 on PORTB. Further more, the interrupt will even wake the PIC from a Sleep instruction, so your device can be in low power mode (the PIC can draw as low as 0.1u Amps while in Sleep mode), and be instantly awoken when there is a change on PORTB.
One precaution to be aware of is not to forget to read the contents of PORTB within the ISR (Interrupt Service Routine). This...
Context Saving and Restoring is vital for interrupts, as the program could get pulled from anywhere at any time. What this means is that the system variables/registers are backed up at the start of the Interrupt Handler, and then when the Interrupt is complete, they are all restored again.
This can be a little memory demanding, so you only want to back up what you need to. Consider this, your Interrupt uses a function that is also used...
Swordfish will also handle the priority of your Interrupt Service Routines (ISR's). 18F PIC's support twp different Interrupt priority routines. These are known as High and Low Interrupts.
If you require an interrupt that needs priority over everything else (including other interrupts), then you must let the compiler know by setting its priority, eg,
Const ipLow = 1, ipHigh = 2
Here's an extract from the 18F452 datasheet;
Timer2 is an 8-bit timer with a prescaler and a postscaler. It can be used as the PWM time base for the PWM mode of the CCP module(s). The TMR2 register is readable and writable and is cleared on any device Reset. The input clock (FOSC/4) has a prescale option of 1:1, 1:4 or 1:16, selected by control bits T2CKPS1:T2CKPS0 (T2CON).
The Timer2 module has an 8-bit period register, PR2. Timer2...
From the 18F452 datasheet:
The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and TMR1L) which are readable and writable. The TMR1 register pair (TMR1H:TMR1L) increments from 0000h to FFFFh and rolls over to 0000h. The TMR1 interrupt, if enabled, is generated on overflow which is latched in interrupt flag bit, TMR1IF (PIR1). This interrupt can be enabled/disabled by setting/clearing TMR1 interrupt enable bit,...
TMR0 is an 8 byte timer/counter that can be pre-scaled to change vary its incrementing cycle. It is quite different in regards to setting up compared to the 16F's, but here's an example.
Pre-scaling is important as we may want the Timer Register to increment faster/slower in different applications. The speed in which it does cycle is dependent on both the external crystal speed and the pre-scaling options, or the Prescaler and external clock...
Who ever said PIC's can't do more than one thing at the same time was right, that's because they have a single instruction pointer that executes one command at a time. But what if one function took 1mS, and another took 3mS (and so on). Instead of waiting forever servicing the one function/waiting for a condition, why not allocate a time interval for each task, eg
Perform Task 1 every 10mS
Perform Task 2 every 5mS
Perform Task 3 every 25mS
One...
The fastest a 16F PIC micro can run is 5 Million Instructions Per Second (MIPS), almost every 18F PIC micro can run at 10 MIPS, some more. Here's an extract from an 18F datasheet;
"The HSPLL mode makes use of the HS mode oscillator
for frequencies up to 10 MHz. A PLL then multiplies the
oscillator output frequency by 4 to produce an internal
clock frequency up to 40 MHz."
But how exactly to we enable this PLL feature so our...
Using the internal oscillator that is available on many PIC's decreases the external component count by 3 (removing the crystal and two capacitors). Most 18F PIC's that end with 0 have an internal oscillator, such as the 18F4520, or the 18F2520.
Most 18F PIC's with internal oscillators can run at 8Mhz, and with PLL enabled, that means its the equivalent of a 32Mhz crystal attached. PLL allows the PIC to execute a new command every oscillation...
Structures are a powerful addition to the Swordfish arsenal, and here's a quick example of how to use them;
Structure TTime Hours As Byte Minutes As Byte Seconds As Byte End Structure
The declaration above informs the compiler that TTime contains three members, all of which are byte types (Hours, Minutes and Seconds). We can now create a variable of Type TTime, in exactly the same way as you would...
Digital to Analogue Converting (DAC) is an extremely handy tool for some projects, and is always a good thing to keep on the back on your mind. Sadly the PIC 18F range of micro's do not have DAC, however, there are many DAC chips out there ready to interface with to get the job done. But why should you when there's a much cheaper and easy solution
Pulse...
Red Green Blue (RGB) LED's are a marvel to watch in action, and I have got to say, they are worth the effort to play around with. The programming side of things does take a little thought, primarily because you need to control 3 different Pulse Width Modulated (PWM) signals to control the brightness of each LED (Red Green and Blue).
RGB LED's have 4 pins, 3 of them are for a specific colour (either Red Green...
Pulse Width Modulation (PWM) is extremely handy for multiple purposes, one in particular is controlling high current devices such as DC motors and large LED arrays. PIC's have their limitations, namely 25mA per output, so if you want to drive something a lot larger, using PWM and a high power switching device such as a logic MOSFET will do the job perfectly.
Although there are many other uses (such as Inferred modulation and Ultrasonic), this...
Hall Effect sensors allow the detection of a magnetic field. They can be extremely small, and very accurate, and often used in applications that require some sort of speed/RPM feedback. For example, you wanted to make a digital speedometer for your car, and don't want to impede on the pre-existing circuit, you could use a Hall Effect sensor to determine how many revolutions/second your wheel is doing, and calculate your speed from there.
In this...
The MCP3001 is a high speed 10 Bit Analogue to Digital converter. It utilizes an SPI interface, and is very easy to interface with. The output of the MCP3001 is the "hard" part to take into account. Well that and the fact that I have made the following program accurate too 3 decimal places without the use of Floats.
The output of the MCP3001 Would look something like this;
0011 1110 0100 0010 0111 1100
The first...
There are 3 Dallas digital temperature sensors, so be sure to get the correct one. There is the 1820, 18S20 and 18B20. The 18S20 is the successor to the 1820, and does not drift over time. The 18B20 is further enhanced, and offers faster conversions. Be aware that the 1820 and 18S20 will not function correctly with this 18B20 example.
The basic wiring diagram;
Note the PIC's power supply/oscillator are...
Communication between you PIC micro and PC could not be easier then with your PICKit 2. Forget the hassles of wiring up your DB9 connectors and worrying about MAX232's or DS275's. don't even worry about connecting an LCD to get real time data from your program... This is the fast, effective and simple solution for real time debugging and user interfacing on your projects.
I have done an example earlier utilizing the DS275, it does the same job, but does not require any additional components. The MAX232 requires 4 external capacitors, but is much cheaper than the DS275. A great benefit is that the Pins that require capacitors are usually 3 pins apart, allowing easy installation of the capacitors - even on protoboards.
The DS275 is $3.75, where as the MAX232 is $0.93 (prices are from Core Electronics). The external...
The DS1307 is a great piece of kit, it provides real time date and clock values, and interfaces with the PIC micro via I2C. The values it holds for date/time are, Secs, Mins, Hours, Day, Date, Month and Year. I hear you asking, why use this when you could make a small program do the same with a PIC;
The DS1307 has leap year compensation until 2100
A 32.768Khz crystal is far more accurate than a 4Mhz+ crystal
In back-up mode, the DS1307 consumes...
This Analogue to Digital (ADC) PIC micro example is not only great for learning how to use ADC with Swordfish, but also to learn how a Function works. The Function will perform the required task/commands and then return with a Word variable called Result. you don't need to declare the variable "Result", as its the sole purpose of using a Function.
In this case, the Function is called "ADInAsVolt". It gets the ADC value on channel 0, and...
This tutorial/example covers another quick guide on how to use Analogue to Digital conversions with a PIC micro. It's achieved by interfacing with a 1K potentiometer to determine how fast the LED will blink. Some other aspects covered in this example include the use of the 18F4550 with the internal oscillator and basic function use.
Simulation video of circuit
Note...
Swordfish has many built in libraries, one being "LCD.bas". This library allows seemingly easy control of LCD modules, and only requires a few declarations.
Note the PIC's power supply/oscillator are not shownClick here to see this circuit in action!
Note that the contrast resistor pot can be anything from 4.7K to 10K, and that the unused data lines on...
User Libraries allow a structured modular approach to programming, and leave the end program much cleaner and easy to understand for others to follow. If you have never saved a User Library to the Swordfish "User Library" directory, perhaps this video tutorial will help;
LED's are great as indicators, and are very easy to interface with. PIC's can produce around 25mA on an output. This is much more than most logic outputs will deliver, and it helps to interface with many devices, especially simple LED's that only require 5mA-20mA usually.
Here's a simple program that will turn an LED on and off twice a second in Swordfish;
Device = 18F4520 Clock = 8 Config OSC = INTIO67 // Use the...
This tip is almost trivial, but it's a D'uh moment too. Just in case anyone else has missed the obvious. here's a quick tip.
I usually use Swordfish's hardware UART routines and I don't recommend Swordfish's software UART for input, where there...
When developing an embedded application using analog sensor inputs, testing at specific sensor levels can be difficult. For example, in my icemaker controller project, actions need to occur at certain water levels. Using the actual sensor...
I recently had use for the simple signal generatorI described before for testing out some piezo buzzers I got for next to nothing. I reloaded the code onto a TAP-28 board...
Header pins are ubiquitous on dev boards. Jumpers can be used to make connections during tests and code development but real-world applications need reliable connections. Connectors for single row headers have always been a problem for me,...
I had a couple of issues when using David Barkers module NMEA.bas on my Big GPS Clock project. In particular, variables were being corrupted making the program very unstable.
I'm not familiar...
I'm working on a control system that will have several relays, a bunch of LEDs and a number of switches. I was rapidly running out of port pins on a TAP-28 board to handle all the desired I/O. Possible solutions included adding an I2C port...
Recent Comments
Thanks Graham, I was afraid I was describing the obvious... I hat...
By Jon Chandler
I'm not sure how many times I have included a separate header for...
By Graham
ohararp brought up a good point on the Swordfish forum: To see th...
By Jon Chandler
I hope this hasn't been obvious to everyone but me :)
By Jon Chandler
I added a couple pictures showing the 10-turn pot I use in an enc...
By Jon Chandler