Skip to content

Instantly share code, notes, and snippets.

@ajarmst
Created September 29, 2023 15:53
Show Gist options
  • Save ajarmst/f5de3541e705755cdb9fa0e5cf5f5d58 to your computer and use it in GitHub Desktop.
Save ajarmst/f5de3541e705755cdb9fa0e5cf5f5d58 to your computer and use it in GitHub Desktop.
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "pll.h"
#include "swled.h"
#include "segs.h"
#include "timer.h"
//Global Scope Variables
volatile counter = 0;
void main(void) {
//Local Scope Variables
unsigned int loopcount = 0; //Keep track of loops
//Startup code
PLL_To20MHz();
TMR_Init(eTMR_Prescale_128,0,0,eTMR_Pin_Toggle);
SWL_Init();
SEG_Init();
/******************************************************
* Set up J port (bits 0 and 1) as hardware interrupts
******************************************************/
//Set PJ0 and PJ1 as inputs
DDRJ &= ~(DDRJ_DDRJ1_MASK | DDRJ_DDRJ0_MASK);
//Set edges for PJ0 and PJ1
PPSJ |= PPSJ_PPSJ1_MASK; //rising edge, PRESS
PPSJ &= ~PPSJ_PPSJ0_MASK; //falling edge, RELEASE
//Enable Interrupts for PJ1 and PJ0
PIEJ |= PIEJ_PIEJ1_MASK | PIEJ_PIEJ0_MASK;
//Everything is set up, so enable interrupts globally
EnableInterrupts;
//Throw the current count to top segs, for startup value
SEG_16D(counter,0);
for(;;) {
//Let's just do a fast count on the bottom segs to show
//that the program is running normally
//note that even with this long sleep, my interrupts work
TMR_Sleep(1000);
SEG_16D(loopcount++,1);
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
/********************************************************************/
// Interrupt Service Routines
/********************************************************************/
interrupt VectorNumber_Vportj void Vportj_ISR(void)
{
if(PIFJ & PIFJ_PIFJ0_MASK)
{//PJ0 was detected an edge
PIFJ = PIFJ_PIFJ0_MASK; //clear flag;
if(++counter > 9999)
{
counter = 0;
}
}
if(PIFJ & PIFJ_PIFJ1_MASK)
{//PJ1 detected an edge
PIFJ = PIFJ_PIFJ1_MASK; //clear flag;
if(--counter < 0)
{
counter = 9999;
}
}
//Update top segs
SEG_16D(counter, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment