Jungletronics
Published in

Jungletronics

Templates for Easy PIC18 Coding

How to use the template in mikroC PRO for PIC.

Fig 1. Video showing how to program PIC18 in no time!
keyword: configtimer
Description: Config Complete Timer and MCU Template (REV1.0)
#define TRUE 1
// Timer $ Port ? byte * PRESCALE ps MODE md EDGE edge (high/low or low/high)
void ConfigMCU(); // Prototype
void ConfigTimer();
void ConfigMCU()
{
#ifdef P18F45K22
ANSEL? = 0; // (PIC18F45K22) R* is analog
#else
ADCON1 |= 0X0F; // (PIC18F45220) Determine if anal/digital pins
#endif
TRIS? = 0; // LED is attached to PORT?.R* BYTE
PORT? = 0;
}
void ConfigTimer()
{
#ifdef P18F45K22 // (PIC18F45K22)
T$CON = 0B00000000; // Timer$ off, Prescale 1:ps , Modemd-BITS, edge
#else // (PIC18F45220)
T$CON = 0B00000000; // Timer$ off, Prescale 1:ps , Mode md-BITS, edge
#endif
TMR$H = 0X; // Initial values for accumulator registers
TMR$L = 0X;
BYTE.TMR$IF = 0; // Flag cleared
BYTE.TMR$ON = 1; // Timer$ on, everything is configured, right?
}
void main() {
ConfigMCU();
ConfigTimer();
while (TRUE) // Do forever
{
if(BYTE.TMR$IF == 1)
{
PORT?.RD* = ~LAT?.RD*; // toggle LED
TMR$H = 0X; // Recharge default values
TMR$L = 0X;
BYTE.TMR$IF = 0; // Clear Timer$'s overflow Flag
}
}
}
keyword: calcmen
Description: Calculation Memory for a Timer - comments (REV 1.1)
/* ---------------------Calculation Memory ------------------------
Formulae: (picGenios and easyPIC v7 boards)
Machine Cycle = FOSC/4 -> 8MHz/4 -> 2MHz -> 0.5us
**************************
Formulae: @1s = 1.000.000us
OverflowTime = P_MacCycle * Prescale * (Mode_8_16bits - InitialCounter)
So, for períod 1 second we have:
1.000.000us = 0.5us * ps * (65536 - ? ) //solve equation by choosing prescale (ps)
1.000.000us = 0.5us * ps * (65536 - ? )
1.000.000us / (0.5us * ps) = 65536 - X
15625 = 65536 - X
X = 65536 - 15625
X = 49911 (InitialCounter - inside two registers TMR0L e TMR0H)
Transform the number in HEX: 00 00
****************
TMR0L = 0X 00;
TMR0H = 0X 00;
****************
Tip: What is the max period for Timer0 (PIC184520 @8MHz)?
OverflowTime = 0.5us * 256 * (65536 - 0)
OverflowTime =8.388.608us ~ 8.4 segundos
*****************
Confirmation:
If 8.4s takes 65536 counts then
?s will yelds ? counts; that brings 0,?s
*****************
Good Practices: Before turn on any peripherals, please configure it first;);
in other words, in configuration mode init TM0 at later time possible;)
-----------------------------------------------------------------*/
keyword: ptemplate
Description:header comment for a project
/*
* Project name:
|<prj_name> (<abstract>)
* Copyright:
(c) <owner, date>
* Revision History:
<versionNo> -
<change_description>
* Status:
<XX% completed.>
* Description:
<project_description>
* Test configuration:
MCU: %DEVICE%
Dev.Board: x
Oscillator: %DEVICE_CLOCK%
Ext. Modules: x
SW: %COMPILER%
* NOTES:
<all that matters>

Date: %DATE%
*/

References and Credits

Datasheets:

PIC18F2420/2520/4420/4520 https://ww1.microchip.com/downloads/en/DeviceDoc/39631E.pdf

Fig 2. The new easyPIC v8 ( https://www.mikroe.com/easypic)
keyword: configcr
Description: Configure Control Registers for a peripherals ( Rev1.0)
void configcr???|() // Specific CONTROL REGISTER(s) configuration(s) for ??? peripheral
{
???CON = 0B00000000; // SFR
}
keyword: configgi
Description: Configure Global Interruption fuction (REV 1.0)
void configIndividualPrf???|() // ???'s Interruptions Peripheral configurations (IF,IP & IE)
{ // See datasheet page ?
BYTE|.???IF = 0; // Overflow flag; please init zero
BYTE.???IP = 1; // Interruption priority vector: HIGH
BYTE.???IE = 1; // ???'s interruption enabled
}
keyword: configip
Description: Configure Individual Peripheral Interruption vectors IF, IP IE fuction (Fire Protector Enginners) (REV 1.0)
void configIndividualPrf<???|>() // ???'s Interruptions Peripheral configurations (IF,IP & IE)
{ // See datasheet page ?
BYTE.???IF = 0; // Overflow flag; please init zero
BYTE.???IP = 1; // Interruption priority vector: HIGH
BYTE.???IE = 1; // ???'s interruption enabled
}
keyword: configisrh
Description: Configure ISR HIGH priority for a peripherals ( Rev1.0)
void INTERRUPTION_HIGH() iv 0x0008 ics ICS_AUTO {

if (BYTE.???|IF == 1 )
{
BYTE.???IF == 0;
}
}keyword: confiisrlDescription: Configure ISR LOW priority for a peripherals ( Rev1.0)void INTERRUPTION_LOW() iv 0x0018 ics ICS_AUTO {

if (BYTE.???|IF == 1 )
{
BYTE.???IF == 0;
}
}}keyword: confiisrhDescription: Configure ISR HIGH priority for a peripherals ( Rev1.0)
void INTERRUPTION_HIGH() iv 0x0008 ics ICS_AUTO {

if (BYTE.???|IF == 1 )
{
BYTE.???IF == 0;
}
}

--

--

J of Jungle + 3 Plats Arduino/RPi/Pic = J3

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
J3

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!