Skip to content

Instantly share code, notes, and snippets.

Created February 26, 2017 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/619b77bc725c5eb0d65c57749ccfd50a to your computer and use it in GitHub Desktop.
Save anonymous/619b77bc725c5eb0d65c57749ccfd50a to your computer and use it in GitHub Desktop.
//******************************************************************************
// notes
//******************************************************************************
/*
PIC16F1618 (chip in use)
project description here
*/
//******************************************************************************
// includes
//******************************************************************************
#include <xc.h>
#include <stdbool.h>
#include <stdint.h>
//******************************************************************************
// defines
//******************************************************************************
//my pin macros
#define _PIN_SET(pt,pn,m) LAT##pt##pn = m
#define _PIN_TOGGLE(pt,pn) LAT##pt##pn = ~LAT##pt##pn
#define _PIN_VALUE(pt,pn) R##pt##pn
#define _PIN_IO(pt,pn,m) TRIS##pt##pn = m
#define _PIN_PU(pt,pn,m) WPU##pt##pn = m
#define _PIN_DRIVE(pt,pn,d) HID##pt##pn = d
#define _PIN_DRAIN(pt,pn,d) OD##pt##pn = d
//instead of using ANS##pt##pn = m; , use byte method on ANSELx register
//as not all pins analog - this will result in set/clear of a ANSELx bit
//(if bit not implemented, no harm done and will keep PIN_INIT always happy)
#define _PIN_MODE(pt,pn,m) do{ if( m ) ANSEL##pt |= 1<<pn; \
else ANSEL##pt &= ~(1<<pn); } while(0)
#define _PIN_INIT(pt,pn,m,io,pu,v) _PIN_SET(pt,pn,v); _PIN_PU(pt,pn,pu); \
_PIN_MODE(pt,pn,m); _PIN_IO(pt,pn,io)
//need to expand pp by calling above macros
#define PIN_SET(pp,m) _PIN_SET(pp,m)
#define PIN_TOGGLE(pp) _PIN_TOGGLE(pp)
#define PIN_VALUE(pp) _PIN_VALUE(pp)
#define IO_IN 1
#define IO_OUT 0
#define PIN_IO(pp,m) _PIN_IO(pp,m)
#define PU_ON 1
#define PU_OFF 0
#define PIN_PU(pp,m) _PIN_PU(pp,m)
#define DRIVE_HIGH 1
#define DRIVE_NORMAL 0
#define PIN_DRIVE(pp,d) _PIN_DRIVE(pp,d)
#define DRAIN_OPEN 1
#define DRAIN_NORMAL 0
#define PIN_DRAIN(pp,d) _PIN_DRAIN(pp,d)
#define MODE_ANA 1
#define MODE_DIG 0
#define PIN_MODE(pp,m) _PIN_MODE(pp,m)
#define PIN_INIT(pp,m,io,pu,v) _PIN_INIT(pp,m,io,pu,v)
//clock freq
#define _XTAL_FREQ 4000000 //4,000,000
//pin defines
#define LED_A A,0
#define LED_B A,1
#define LED_C A,2
#define LED_D A,3
//******************************************************************************
// global variables
//******************************************************************************
//******************************************************************************
// pause ms milliseconds (cpu keeps running)
//******************************************************************************
void pause(uint16_t ms)
{
for( ; ms; ms-- ) __delay_ms(1);
}
//******************************************************************************
// oscillator init
//******************************************************************************
void osc_init()
{
//default is 500khz MF
OSCCONbits.IRCF = 0b1101; //4MHz
}
//******************************************************************************
// pins init
//******************************************************************************
void pins_init()
{
//setup pins (can optimize to single register writes if ever needed)
PIN_INIT( LED_A, MODE_DIG, IO_OUT, PU_OFF, 0 );
PIN_INIT( LED_B, MODE_DIG, IO_OUT, PU_OFF, 0 );
PIN_INIT( LED_C, MODE_DIG, IO_OUT, PU_OFF, 0 );
PIN_INIT( LED_D, MODE_DIG, IO_OUT, PU_OFF, 0 );
}
//******************************************************************************
// main
//******************************************************************************
void main(void)
{
osc_init();
pins_init();
for(;;){
PIN_TOGGLE( LED_A );
PIN_TOGGLE( LED_B );
PIN_TOGGLE( LED_C );
PIN_TOGGLE( LED_D );
pause(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment