This project shows how to build a very simple yet very useful tool that every DIY enthusiast should have in his lab: a 100MHz+ frequency counter. The schematic is fairly simple and straightforward and uses a PIC16F628A microcontroller for measuring frequency and a high speed comparator for signal amplification and conditioning. Update on 3rd July 2020: Sorry for the bad video quality. It was recorded 12 years ago, when good video cameras weren't as accessible as today.
This is the first in a series of articles describing the construction of an interface board suitable for controlling 2 motors, 2 servos and providing a collection of binary inputs and outputs. The board provides limited onboard intelligence (only enough to protect the motors and servos from invalid operations) - it is designed to be controlled from an external computer through an RS232 connection.
Update The MPLAB X project file for this post is available here.
This article provides an introduction to the microcontroller that is at the heart of the project - the PIC 16F628A. All of the provided programs are in PIC assembly language, using C on a PIC (the 8 bit PIC processors at least) seems to be overkill - the resulting code looks a lot like assembly language but with a lot more boiler plate around it.
There are a number of things you are going to need for this to be more than an intellectual exercise ..
- You will need a way to convert the assembly language code into a binary file that can be loaded into the program memory on the PIC. The easiest solution is to download and install the MPLABX IDE which is available for Windows, Mac and Linux. You will need to download the appropriate installers for the MPLABX IDE and MPLAB XC8 Compiler from the previous link and install them in that order. If you prefer to use the command line and already have a preferred editor you might consider the gputils package (this can be installed via apt-get for Debian and Ubuntu).
- A simulator is handy to test your code before burning to a chip - it helps to iron out the more obvious bugs in a more friendly environment and reduce the chances of damaging any components in your circuit. MPLABX includes a simulator, you could also look at gpsim or PICsim which are available for both Linux and Windows. The gpsim project provides both a command line interface (similar to gdb) and a simple GUI. PICsim on the other hand provides a much more graphical interface as well as simulating some external hardware (switches, LED's, LCD displays, etc).
- A hardware programmer is required in order to burn the resulting code onto the chip itself. I'm using one known as the K150 (v2.1) - I'm not sure if this is available anymore (and if it is - from resellers for example - how long the supporting software will be available) as the main manufacturers site (kitsrus) seems to have expired. There are wide range of alternative programmers available (a google search should turn up something that suits your budget and needs).
- At least one PIC16F628A chip - preferably two in case of mistakes. In Australia these are available from Element14 at $AU 4.40 per unit, in the US you could source them from SparkFun for $US 3.18.
I highly recommend downloading a copy of the datasheet for the processor and keeping it open as a reference (it's large, 180 pages long, so printing it out may not be an option). I'm going to gloss over a lot of details and just concentrate on the basics - for more information please refer to the datasheet to get all the nitty gritty details.
This particular model of PIC has 2048 words of program memory (each instruction consumes a single word so your maximum program length is 2048 instructions), 224 bytes of RAM, 128 bytes of EEPROM for persistant storage and 16 IO pins. If you look at the pinout to the left you can see that most of the IO pins have multiple purposes - only one function is available at any given time so choosing to use a particular function will exclude that pin from any other purpose. This is a very important factor to consider when designing your project.
Memory layout on the PIC is very different to what you are probably used to if you have developed programs for desktop computers. Each memory location is considered as a register and is accessed by it's address. To further complicate the issue not all registers are available at any given time - memory is organised as four banks of 128 registers each and only one bank can be accessed at one time. The first 32 registers in each bank are special purpose registers that are used to control and configure the processor and IO pins, the last 16 registers in each bank are shared across all banks allowing for values that can be accessed regardless of which bank is currently selected. Not all register addresses are implemented either which leaves some gaps in the memory map that need to be worked around. The full memory map is shown in the image on the right. The entries labeled as General Purpose Register and the block of 16 bytes at the end of each page are free for use by your program.
The PIC processor is a RISC based system so it has a very simple instruction set - only 35 instructions in total. As well as the registers mentioned above there is a single working register (refered to as W) that is required for many operations. Operations that modify values have the option to store the result in this register or the file register being operated on.
Each instruction takes 4 clock cycles to execute with the exception of conditional and jump instructions which may take 8 clock cycles. This model of PIC can operate at up to 20MHz (approximately 5 MIPS) using an external oscillator. There is an onboard oscillator which operates at 4MHz (approximately 1 MIPS) and can be slowed to 48 KHz to save power or facilitate debugging and testing. All examples in this series will use the internal oscillator. Slither.io game.
The remainder of this article describes a circuit (and the software) required to flash an LED at a regular period (on for 250ms, off for 250ms). This project doesn't use any of the special periphials on the chip, timing is achieved purely though clock cycle counting and only a single output pin is used.
Electrically the PIC will operate with an input voltage from 3.0V to 5.5V and draws very little current (typically less than 2 mA @ 5V for the chip itself). It does require a steady DC power supply but a voltage regulation circuit is generally not necessary. As most of my projects are designed to be mobile (or at least portable) I tend to use a 6V battery pack (4 x AA NiCD batteries).
The circuit shown to the left is the simple LED flashing example we are going to use. The upper left part of the circuit is a simple voltage regulator which uses the forward voltage drop across a 1N4004 diode to ensure the input voltage is always less than 5.5V.
The LED itself is driven from the RB5 output through a 180 Ohm current limiting resistor. Outputs from a PIC can drive loads up to 25mA (so a single LED is fine) with a total of 200mA for all active outputs. For higher loads you would generally use the output from the PIC to drive a transistor which will bear the load rather than pulling it through the output pin itself.
For this project I've kept the code very simple, only the basic input and output functionality of the chip is being used. I have a simple
ORIGINAL: selbstdual These are variables. What do they mean, in which context did you use them?
A series of basic assembly language programs for the PIC16F628. Includes programs and diagrams. Program Memory Data Memory I/O CCP (PWM) USART Comparators Timers. 16F628a Simple Programs Rating: 5,0/5 5474reviews. Thats very interesting. LED Panel is always to one direction. This project uses a Microchip PIC microcontroller, a serial EEPROM and a thermistor to create a temperature recorder. The temperature is measured and stored at user. The following downloads.
Hmmm, no, they're commands (as specified in ICSP handbooks by Microchip) that you issue to the in-program part via Serial Clock-Serial Data (RB6-RB7). I was suggesting modifying the code of your programmer, so that it was sending one command instead of another. Don't know if it's easy to do or not, but it was a suggestion for you. As for buying a new one, of course you (Paulsbergman) are right. But I've to say that I did learn some things (and it was also fun) while programming the programmer! And had also much satisfaction when I could achieve a (draft) time for programming a 18F device which was 5-8 time faster than Microchip's one (don't remember exactly which one, it was a customer's one).
$35.00 you can get an assembled and tested programmer (the PICkit2). Why are you waisting your time trying to build something that will never perform as well as the Microchip product This is like 'Oh windows/linux/etc. Doesn't work as I want it to. What can I do?' Answer from someone: 'Reinstall it'. This method is amateurish and only useful as long as no additional costs are included.
My USB-Ports are used, and my parallel port's pin 12 is destroyed. I was suggesting modifying the code of your programmer, so that it was sending one command instead of another. This command does not exist. So I search for an equivalent. In which programming context is it to be used?
As for buying a new one, of course you (Paulsbergman) are right. Regarding my first quote, pb is wrong.
But I've to say that I did learn some things (and it was also fun) while programming the programmer! And had also much satisfaction when I could achieve a (draft) time for programming a 18F device which was 5-8 time faster than Microchip's one (don't remember exactly which one, it was a customer's one) That doesn't matter as long as it doesn't last for a minute for the 16F628A.
ORIGINAL: selbstdual I was suggesting modifying the code of your programmer, so that it was sending one command instead of another. This command does not exist.
So I search for an equivalent. In which programming context is it to be used? Hmmm, well, it's used every time you program a cell. So, a kind of inner loop, that code should repeat for every cell. I used the same syntax, i.e.
Execute Batch File After Tfs Build Manager more. Borrowed it from ICSP PDFs (if you take a loot at one of them, you 'll find that kind of command). I then created a table for my own use: // serie 12.16 C/F #define LOAD_CONFIGURATION 0 // 000000B, Invia al PIC il prossimo dato da scrivere in memoria programma. Al codice comando segue immediatamente il dato da memorizzare.
#define LOAD_CONFIGURATION_FOR_UNPROTECT 0x3f // 111111B, (v. Datasheet programming 16F627, DS39025c) #define UNPROTECT_CODE_1 1 // 000001B (v. Datasheet programming 16F874, DS39025c) #define LOAD_DATA_FOR_PROGRAM_MEMORY 0x2 // 000010B, Invia al PIC il prossimo dato da scrivere in memoria dati. Al codice comando segue immediatamente il dato da memorizzare. #define READ_DATA_FROM_PROGRAM_MEMORY 0x4 // 000100B, Legge dal PIC la locazione corrente dall’area programma. Appena riceve questo comando il PIC trasmette al programmatore il valore letto.
#define INCREMENT_ADDRESS 0x6 // 000110B, Incrementa il puntatore alla locazione corrente nella memoria dati/configurazione/programma. #define UNPROTECT_CODE_2 0x07 // 000111B (v. Datasheet programming 16F874, DS39025c) #define BEGIN_PROGRAMMING 0x8 // 001000B, Programma la locazione corrente. (cancella anche, su FLASH) #define BEGIN_PROGRAMMING_ONLY 0x18 // 011000B, Programma la locazione corrente. #define END_PROGRAMMING 0xE // 001110B, #define LOAD_DATA_FOR_DATA_MEMORY 0x3 // 000011B, Invia al PIC il prossimo valore da scrivere in memoria dati.
Al codice comando segue immediatamente il dato da memorizzare. #define READ_DATA_FROM_DATA_MEMORY 0x5 // 000101B, Legge dal PIC la locazione corrente dalla memoria dati.
16f628a Simple Programming Software
Appena riceve questo comando il PIC trasmette al programmatore il valore letto. #define BULK_ERASE_PROGRAM_MEMORY 0x9 // 001001B, Cancella l’intera memoria programma #define BULK_ERASE_DATA_MEMORY 0x11 // 001011B, Cancella l’intera memoria dati //#define?
16f628a Simple Programming Language
(unprotect code) 0x3f // 111111B // serie 18F #define CORE_INSTRUCTION 0x0 // 0000B, segue opcode #define SHIFTOUT_TABLAT 0x2 // 0010B, #define TBLRD 0x8 // 1000B, TBLRD #define TBLRD_POSTINC 0x9 // 1001B, TBLRD_POSTINC #define TBLRD_POSTDEC 0xa // 1010B, TBLRD_POSTDEC #define TBLRD_PREINC 0xb // 1011B, TBLRD_PREINC #define TBLWR 0xc // 1100B, TBLWR #define TBLWR_POSTINC2 0xd // 1101B, TBLWR_POSTINC_by_2 #define TBLWR_POSTDEC2 0xe // 1110B, TBLWR_POSTDEC_by_2 //#define TBLWR_POSTDEC2 0xe // 1110B, TBLWR_POSTINC_by_2 & START_PROGRAM!! Su 18F2550 et sim.???? #define TBLWR_PROGRAM 0xf // 1111B, TBLWR_PROGRAM So, into that code, you should find the LITERAL constant, or its hex or bin equivalent. To program the 16F628 PIC using FPP. It doesn't work with the 16F628A. What to change (hw and sw) to program this kind of device? These kind of parallel port programmers are flaky since they don't follow all the rules for the standard use of the port nor the PIC programming specification.