Saturday, August 28, 2021

Port B Weak Pullup Resistors of PIC18F1220

 

Introduction

Port B of PIC18F1220 has an internal programmable weak pull up resistors. However it’s not individually select-able. These resistors have only one shared global switch locates in OPTION Register.

Port B Weak Pullup Resistors of PIC18F1220
Pin diagram of PIC18F1220

Additionally, this port is multiplexed with ADC input. To configure it to a solely digital I/O, the user must check the ADCON1 register.

Interfacing and Programming

Using a simulator to simulate the circuit, the overall prototyping is fast and effective. It reduces cost, time and physical hardware defect.

Hardware Interfacing

PIC18F1220 microcontroller accept digital input on lower nibble of Port B. Its higher nibble connects to LED representing the digital input on lower nibble.

Port B Weak Pullup Resistors of PIC18F1220
Schematic Diagram

Programming

At setting up section of C main program, programmer need to clear port I/O, disabling analog input function, setting up port direction, and turning on Port B weak pull up resistors.

C

PIC18F1220 configuration bits setting is very long that programmer doesn’t want to put inside C main file.

C

Click here to download this programming example.

Friday, August 27, 2021

Getting Started With PIC18F1220 in C

 

Introduction

PIC18F1220 is an 8-bit Harvard architecture microcontroller. Its instruction is 8-bit wide. Its essential features are list below.

Getting Started With PIC18F1220 in C

PIC18F1220/PIC18F1320 Summary

Only the features of PIC18F1220 we target.

Getting Started With PIC18F1220 in C

PIC18F1220 device feature


Getting Started With PIC18F1220 in C

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.

Getting Started With PIC18F1220 in C

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.

// PIC18F1220 Configuration Bit Settings
// 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.

Getting Started With PIC18F1220 in C

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.

Getting Started With PIC18F1220 in C

Circuit Diagram


Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)