Introduction
PIC18F1220 is an 8-bit Harvard architecture microcontroller. Its instruction is 8-bit wide. Its essential features are list below.
PIC18F1220/PIC18F1320 Summary |
Only the features of PIC18F1220 we target.
PIC18F1220 device feature |
A used PIC18F1220 in 18-pin DIP package |
Memory
It has 4KBytes in program (Flash) memory. Due to its 16-bit wide instruction length, maximum number of instruction line is 2048 lines (16×2048). Its scratch pad Static RAM (SRAM) is 256 bytes. Non-volatile auxiliary storage EEPROM is 256bytes, keeping data persistent even the controller is power off.
Program counter is 21-bit in length (address up to 2Mbyte program memory). Reset vector locates at 0x0000. Unlike Mid-range PICMicro PIC18F1220 reset vectors locate at 0x0008 and 0x0018.
Digital I/O Port
This 18-pin in DIP package device has up to 16 digital I/O, regardless of other multiplexing functions.
Pin Diagram of PIC18F1220 in DIP package |
Disabling reset pin, it can be configured as a digital input pin. User can configure RA6 and RA7 as digital I/O as long as it’s driven from internal RC oscillator. Port A and Port B are 8-bit wide.
There are three external interrupt sources relate to digital I/O. Each I/O source/sink current up to 25mA/25mA.
Analog Inputs
Analog to Digital Converter (ADC) inside this device is 10-bit in resolution. It has 7 input channel that multiplexed with digital I/O pins. These analog input pins range from RA0 to RA6. ADC must be properly configured in software.
Analog Outputs
It has an analog output – Pulse Width Modulation (PWM) inside its ECCP module. Requirements for PWM usage is its timer. PWM is useful for driving analog output device – electric motor, light dimming, DC/DC converter, etc.
Timers
Timer is an essential part of microcontroller. PIC18F1220 has up to 4 timers,
- Timer0 – 8-bit
- Timer1 – 16-bit
- Timer2 – 16-bit
- Timer3 – 16-bit
They are useful in creating precise timing delay, pulse counting, pulse measurement, waveform generation, and PWM output.
Communications
Enhanced Addressable USART is commonly communicate to host PC, and other USART devices – GSM module, bluetooth, etc. In PIC18F1220 it has many configurations.
There’s a pair of Serial Peripheral Interface (SPI) and Inter-integrated Circuit (I2C) in its SSP module.
Others
It has other features that we will not list their details here. They are,
- Reset and its programmable brown out reset
- Low voltage detect
- Start up delay
Device operates up to 40MHz using its High Speed Phase Lock Loop (PLL).
Programming and Development Tools
Popular programming tools are high level programming/compiler. Embedded C is a first choice for most of small microcontroller nowadays.
C Compilers
There are some well known C compilers for this device:
- Mikroelectronika MikroC
- Custom Computer Service (CCS) PICC
- MPLABX XC8
MPLABX XC8 developed by Microchip Technology. It has a free version with no code optimization and technical support. However its free version is suitable for students and hobbyists at no cost.
Development Tools
This low pin counts device is easily prototype on breadboard. However it will require more times and many types of error.
Using a simulator could save prototyping time and spending on components. It’s safe without burning components even it’s wrong wiring.
I prefer Proteus VSM for a decade now. It’s a paid software but I use it b sharing with my development team.
Programming with MPLABX XC8
MPLABX Integrated Development Environment (IDE) is a powerful software. Programmer can write codes and debug in software and hardware. Currently MPLABX version 5.40 is the latest version.
XC8 is a compiler that plugged into this software targeting 8-bit PIC micro-controllers. XC8 2.1 is currently the latest version.
Getting Started with Coding
Blinking LED on microcontroller port is an easiest starting up with embedded C programming. This introductory programming toggles Port B of PIC18F1220 on for 0.5 second and off for 1 second.
I don’t show project setting up in MPLABX XC8 due to repetition. To get started with this tool chain click here.
Source code files are divided into two files.
* Start coding PIC18F1220 in XC8
*/
#include <xc.h>
#include "p18f1220Config.h"
#define _XTAL_FREQ 20000000
void main(void){
/*Clear Port B*/
LATB=0x00;
/*Port B output*/
TRISB=0x00;
while(1){
LATB=0x00;
__delay_ms(500);
LATB=0xFF;
__delay_ms(1000);
}
}
It needs another file “p18f1220Config.h” in the same project.
// CONFIG1H
#pragma config OSC = HS // Oscillator Selection bits (HS Oscillator)
#pragma config FSCM = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode enabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bit (Brown-out Reset enabled)
// BORV = No Setting
// CONFIG2H
#pragma config WDT = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled, RA5 input pin disabled)
// CONFIG4L
#pragma config STVR = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Low-Voltage ICSP Enable bit (Low-Voltage ICSP enabled)
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (00200-0007FFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (000800-000FFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot Block (000000-0001FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (00200-0007FFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (000800-000FFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0001FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (00200-0007FFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (000800-000FFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0001FFh) not protected from table reads executed in other blocks)
MPLABX IDE version is v1.51 while XC8 compiler in used is v2.00. I prefer these old versions because the are light weight.
MPLABX IDE and XC8 Compiler screen shot |
Schematic Diagram
Circuit diagram drawn in Proteus. It can be used for on board wiring, and also in simulation.
Circuit Diagram |
No comments:
Post a Comment