Con questo articolo si mostra come si può imparare a programmare facilmente un microcontrollore Microchip, il PIC16F18855 contenuto all'interno della MPLAB Xpress Evaluation Board della Microchip (DM164140), dal costo davvero contenuto.
Al momento in cui scrivo questo articolo il costo della Board, che include anche altri utili componenti, è di circa 11 euro.

La programmazione viene fatta con il linguaggio C e il compilatore XC8 della Microchip. L'ambiente di sviluppo che usiamo è MPLAB Xpress IDE, in cloud.
E' necessario creare un account gratuito nel sito della Microchip per poter salvare i propri progetti, legandoli così al proprio account. Lo si può fare cliccando su myMicrochip Login eppoi <<New to myMicrochip? Register for an account>> .
Una volta registrati, possiamo iniziare.
Parliamo prima dell'Hardware della Board.
La MPLAB Xpress Evaluation Board può essere alimentata con un cavo USB dal proprio computer. Al primo collegamento della Board in un PC Windows o Linux viene configurato il PC e la memoria Flash contenuta nel MCU (Microcontrollore o Microcontroller Unit) viene vista come disco dal PC. Pertanto programmare il microcontrollore equivale a trascinare dentro quel disco il file con estensione .hex che conterrà il codice macchina dell'eseguibile e prodotto dalla compilazione dell'ambiente di sviluppo. La stessa cosa in ambiente MAC, ma non sono in grado di provarla direttamente, non disponendo di tale hardware. Nel mio caso è visto come disco E: con etichetta XPRESS e dimensioni 128KB.
Nel seguente datasheet dal sito ufficiale si può vedere lo schematico della Board, composto da due pagine. La prima pagina è relativa all'hardware del programmatore, con interfaccia USB verso il computer. In esso è presente il PIC18LF25K50 che gestisce la comunicazione via USB e i pin che servono a programmare il PIC16F18855.
Il processo di programmazione del PIC16F18855 è dunque "trasparente". Non ce ne dobbiamo occupare, perché lo ha pensato e fatto funzionare la Microchip.
La seconda pagina del datasheet è più interessante. Si vede il PIC16F18855. I Pin RA0, RA1, RA2, RA3 sono collegati ai quattro diodi SMD chiamati D2, D3, D4, D5 attraverso resistenze da 1kΩ.
Nella scheda è presente anche un potenziometro R15, collegato a RA4 impostato come Input, utile per le letture "analogiche", è presente un "tasto" indicato come S2 collegato a RA5, impostato come Input. Sono inoltre presenti degli header o pin header con uscita femmina, per le connessioni con i dispositivi della Mikroelettronica con MikroBus e con il mondo esterno sensori ed attuatori.
Come si vede dallo schema elettrico non c'è un oscillatore al quarzo esterno e viene usato quello integrato all'interno del PIC.
Lo scopo di questo primo esempio è di fare lampeggiare il solo led D3 con intervallo di mezzo secondo.
L'inserimento di questo piccolo programma sovrascrive il programma fornito nativamente da Microchip.
Non si vuole usare MCC Code Configurator.
Si usa MPLAB Xpress IDE in cloud, creando un nuovo progetto.
New Project --> Microchip Embedded ---> Standalone Project
Family: Mid Range 8-bit MCUs (PIC 10/12/16)
Device: PIC16F18855
eppoi si sceglie il nome significativo al progetto: PIC16F18855_Blink_D3
Si comincia inserendo il programma principale main.c:
Ecco il codice:
/*
* File: main.c
* Author: mariani.fausto
*
* Created on 10/16/2017 4:06:08 PM UTC
* "Created in MPLAB Xpress"
*/
#include "config.h" // custom edited config file.
#include <xc.h>
#define _XTAL_FREQ 32000000
void main(void) {
SYSTEM_Initialize();
TRISA = 0b00000000; // we set RA1 as Output
PORTA = 0x00;
while(1) {
LATAbits.LATA1 = 1;
__delay_ms(500);
LATAbits.LATA1 = 0;
__delay_ms(500);
}
return;
}
Per completezza si riporta anche il codice di configurazione config.h che conviene prendere da uno dei tanti esempi preesistenti.
/*
Copyright (c) 2013 - 2015 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*/
// Configuration bits: selected in the GUI
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits->Oscillator not enabled
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits->HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1
#pragma config CLKOUTEN = OFF // Clock Out Enable bit->CLKOUT function is disabled; i/o or oscillator function on OSC2
#pragma config CSWEN = ON // Clock Switch Enable bit->Writing to NOSC and NDIV is allowed
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit->FSCM timer enabled
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit->MCLR pin is Master Clear function
#pragma config PWRTE = OFF // Power-up Timer Enable bit->PWRT disabled
#pragma config LPBOREN = OFF // Low-Power BOR enable bit->ULPBOR disabled
#pragma config BOREN = ON // Brown-out reset enable bits->Brown-out Reset Enabled, SBOREN bit is ignored
#pragma config BORV = LO // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices
#pragma config ZCD = ON // Zero-cross detect disable->Zero-cross detect circuit is disabled at POR.
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control->The PPSLOCK bit can be cleared and set only once in software
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit->Stack Overflow or Underflow will cause a reset
// CONFIG3
#pragma config WDTCPS = WDTCPS_31 // WDT Period Select bits->Divider ratio 1:65536; software control of WDTPS
#pragma config WDTE = OFF // WDT operating mode->WDT Disabled, SWDTEN is ignored
#pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits->window always open (100%); software control; keyed access not required
#pragma config WDTCCS = SC // WDT input clock selector->Software Control
// CONFIG4
#pragma config WRT = OFF // UserNVM self-write protection bits->Write protection off
#pragma config SCANE = available // Scanner Enable bit->Scanner module is available for use
#pragma config LVP = ON // Low Voltage Programming Enable bit->Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit->UserNVM code protection disabled
#pragma config CPD = OFF // DataNVM code protection bit->DataNVM code protection disabled
#include <xc.h>
void OSCILLATOR_Initialize(void)
{
// NOSC HFINTOSC; NDIV 1;
OSCCON1 = 0x60;
// CSWHOLD may proceed; SOSCPWR Low power;
OSCCON3 = 0x00;
// MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
OSCEN = 0x00;
// HFFRQ0 32_MHz;
OSCFRQ = 0x06;
// HFTUN 0;
OSCTUNE = 0x00;
// Set the secondary oscillator
}
void PIN_MANAGER_Initialize(void)
{
LATB = 0x0;
WPUE = 0x8;
LATA = 0x1;
LATC = 0x0;
WPUA = 0xFF;
WPUB = 0xFF;
WPUC = 0xFF;
ANSELA = 0xFF;
ANSELB = 0xFF;
ANSELC = 0xFF;
TRISB = 0xFF;
TRISC = 0xFF;
TRISA = 0xFE;
// interrupts-on-change are globally disabled
PIE0bits.IOCIE = 0;
}
void SYSTEM_Initialize(void)
{
OSCILLATOR_Initialize();
PIN_MANAGER_Initialize();
}
/**
End of File
*/