PicGIM example - EVENTS and TIMER

 

 Resources Version  Download Size  Date
 HT001_PG_EVENTS_and_TIMER  0.1 256KB   09/05/2015

 

RELATED TO THIS EXAMPLE :

DELAY module

EVENTS module

TIMER module

PINS name

DEFINES

MACRO

This howto is part of the kit PicGIM 0.5 library.


In this example we analyze how to configure PicGIM and how to write the right code using the TIMER interrupt

to control the blinking of a LED.

In this example we will use a PIC18F4620 with a 40MHz oscillator.

As always, the first thing to do is to configure the file "pgim_project_setup_public.h" to customize the project.

In this example we filled out the necessary fields in this way:

 

//P R O J E C T   D E T A I L S

#define PG_PROJECT_NAMEEVENTS_and_TIMER

#define PG_PROJECT_ORGANIZATIONgKript Howto

#define PG_PROJECT_AUTHORasyntote

#define PG_PROJECT_VERSION0.1

#define PG_PROJECT_STATEPG_DEBUG

#define PG_PROJECT_DATE04/05/2015

 

//H A R D W A R E   C O N F I G   M A I N   S Y S T E M

#define PG_CLOCK40.000

#define PG_USER_SUPPLY_VOLT5.00

#define PG_USER_SUPPLY_BATTERYPG_NO

#define PG_USE_INTERNAL_OSCPG_DISABLE

 

//P I C G I M   B E H A V I O U R

#define PG_VERBOSEPG_ENABLE

#define PG_SUGGESTIONPG_ENABLE

#define PG_PROJECT_INFO_SHOWPG_ENABLE

 

Before compile PicGIM we still have to set some configuration files.

The "pgim_module_setup_public.h" file defines which modules are included and compiled.

This example requires the presence of the TIMER and EVENTS module.

To operate the TIMER module needs the DELAY module.

If you do not enable the DELAY module you will get a dependency error in compilation.

So here are the necessary settings:

 

//S O F T W A R E   G E N E R A L

#define PGIM_ERRORPG_DISABLE

#define PGIM_CAL_DELAYPG_ENABLE

#define PGIM_FTOAPG_DISABLE

 

//H A R D W A R E   I N T E R N A L

#define PGIM_EVENTSPG_ENABLE

#define PGIM_AD_CONVERTERPG_DISABLE

#define PGIM_SPIPG_DISABLE

#define PGIM_EEPG_DISABLE

#define PGIM_TIMERPG_ENABLE

#define PGIM_PWMPG_DISABLE

#define PGIM_SERIALPG_DISABLE

 

//H A R D W A R E   E X T E R N A L

#define PGIM_LCD_HD44780PG_DISABLE

#define PGIM_BUZZERPG_DISABLE

#define PGIM_SENSORPG_DISABLE

 

//H A R D W A R E   G K R I P T

#define PGIM_ENCODERPG_DISABLE

 

Once you enabled the required modules we configure the HARDWARE.

TIMER and EVENTS are internal devices and are part of the category HARDWARE INTERNAL.

This means that I do not have to assign any pins to the devices.

 

TIMER and EVENTS module needs to be configured, because thay have special features

which change the parameters and operation.

We will use only the TIMER 0 of TIMER module.

These settings are made by editing the file "pgim_timer_setup_public.h".

Here are the settings required:

 

//T I M E R   E N A B L I N G

#define PGIM_TIMER_0PG_ENABLE

#define PGIM_TIMER_1PG_DISABLE

 

//P A R A M E T E R S

#define PG_TIMER_0_DELAY_TRIM320

#define PG_TIMER_1_DELAY_TRIM299

 

We will get only the interrupt from the TIMER 0.

These settings are made by editing the file "pgim_event_setup_public.h".

Here are the settings required:

 

//-------- Auto Interrupts Handler -----------------------------------------------------------

#define PG_EVENT_AUTO_HANDLERPG_ENABLE

 

//-------- External Interrupts ----------------------------------------------------------------

#define PG_EVENT_SET_INT0PG_DISABLE

#define PG_EVENT_SET_INT1PG_DISABLE

#define PG_EVENT_SET_INT2PG_DISABLE

#define PG_EVENT_SET_RB0PG_DISABLE

 

//-------- Internal Interrupts ----------------------------------------------------------------

#define PG_EVENT_SET_TMR0PG_ENABLE

#define PG_EVENT_SET_TMR1PG_DISABLE

#define PG_EVENT_SET_TMR2PG_DISABLE

#define PG_EVENT_SET_ADPG_DISABLE

#define PG_EVENT_SET_USARTRC        PG_DISABLE

#define PG_EVENT_SET_USARTTX        PG_DISABLE

#define PG_EVENT_SET_SSPPG_DISABLE

#define PG_EVENT_SET_CCP1PG_DISABLE

#define PG_EVENT_SET_CCP2PG_DISABLE

#define PG_EVENT_SET_OSCFPG_DISABLE

#define PG_EVENT_SET_CMPG_DISABLE

#define PG_EVENT_SET_EEPG_DISABLE

#define PG_EVENT_SET_BCLPG_DISABLE

 

 

Now PicGIM is properly configured for the project to be developed.

 These are the warnings that PicGIM returns during compilation.

If all goes well they will be like these :

 

Warning [2105] PicGIM:  Core >  Version "0.5-0"  : Milestone "0.5"  : State "rel"

Warning [2105] PicGIM:  Message >  ~~~  Check periodically on www.gkript.org if you are using the latest PicGIM released!

Warning [2105] -

Warning [2105] -

Warning [2105] PicGIM:  Project: [ TIMER_to_EVENTS   V 0.1  ] by [ gKript Howto  ]  -  Author: [ asyntote  ]

Warning [2105] -

Warning [2105] -

Warning [2105] PicGIM:  Core >  Set 18F4620 as current mcu

Warning [2105] PicGIM:  Core >  Using EXTERNAL oscillator ( 20.000  [MHz] )

Warning [2105] PicGIM:  Core >  ~~~  All fuses are set as you want.

Warning [2105] PicGIM:  Core >  ~~~  TRADITIONAL code activated but EXTENDED Instructions Set is available with this MCU. Keep on mind!

Warning [2105] PicGIM:  Core >  Set MCU POWER SUPPLY to 5.00  [V]

Warning [2105] PicGIM:  Core >  Make sure that the 5.00  voltage is enough for the clock frequency chosen.

Warning [2105] PicGIM:  DELAY module >  Loaded

Warning [2105] PicGIM:  IRQ module >  INTERRUPTS handle module loaded

Warning [2105] PicGIM:  Note >  ~~~  See the documentation to know how initialize interrupts you want to manage.

Warning [2105] PicGIM:  TIMER 0 module >  ~~~  Max time available is 4.000  * 65535.000  * 256  / 20.000  [us]

Warning [2105] PicGIM:  TIMER 0 module >  Loaded

 

 

 


 

Here is the example code

 

//Only in the file main.c it is always necessary to include the header file picgim_main.h.

#include "picgim_main.h"

 

//A PIN definition ( LATBbits.LATB3 = L_B3 ) for a LED 

#define MY_LED          L_B3

 

//A TRIS definition for the PIN ( TRISBbits.TRISB3 = T_B3 )

#define MY_LED_TRIS     T_B3

 

 

//This is the function that I want to be invoked at each EVENT TIMER.

void    led_blink( void ) {

 

// This macro takes care to invert the status of a PIN. 

    pg_pin_toggle( MY_LED );        

}

 

void main( void ) {

//It is compulsory to initialize PicGIM with this function.

pg_initialize();

 

//This macro sets a TRIS as required by the user. It is not compulsory to use these macros.

    pg_pin_mode( MY_LED_TRIS , PG_OUT );

 

//It enables global interruption.

    pg_event_set( PG_EVENT_GLOBAL , PG_ENABLE );

 

//It enables the interrupts from the internal peripherals.

    pg_event_set( PG_EVENT_PERIPHERAL , PG_ENABLE );

 

//It connects the "led_blink" event TIMER 0.

    pg_event_attach( PG_EVENT_TMR0 , led_blink );

 

//Enabling the event from TIMER 0.

    pg_event_set( PG_EVENT_TMR0 , PG_ENABLE );

 

//You set the frequency of the timer 0 to 2 Hz.

//Each 2Hz we will get an interrupt.

    pg_timer_set_freq( PG_TIMER_0 , 2 , PG_HZ );

 

//Starting the TIMER 0.

//After this line will begin the interrupts.

    pg_timer_start( PG_TIMER_0 );

 

//We enter into an infinite loop.

PG_HALT;

}

 

//At this point you can compile the project and verify that the LED will blink 2 times per second.