Skip to content

Instantly share code, notes, and snippets.

@jakabo27
Last active October 2, 2019 16:51
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 jakabo27/db81f9d3d30d49458e83ad20efeab90d to your computer and use it in GitHub Desktop.
Save jakabo27/db81f9d3d30d49458e83ad20efeab90d to your computer and use it in GitHub Desktop.
#include <plib.h>
#define maxRotaryNumber 9
#define maxLEDNum 8
//This is here to make timing calculations easier!
#pragma config FPBDIV = DIV_2
#define E 3031
#define F 2866
#define Fs 2703
#define Gb 2703
#define G 2551
//t2 = half notet4 = quarter note. t8 = eighth note. Etc
#define t4 400
#define t2 t4*2
#define t1 t2*2
#define t8 t4/2
#define t16 t8/2
#define t32 t16/2
int ledNumbers[9] = {0,1,3,7,15,31,63,127,255}; //1-5 are for Keypad, 6-8 are for rotary encoder
int currentLEDnum = 0;
//Have a connected to chip 6, b connected to 5, etc g connected to 0
int sevenSeg[14] = { 1, 79, 18, 6, 76, 36, 32, 15, 0, 12, 8,96,49,66};
int sevenSegSpot = 0;
static int passCodeLength = 8;
int PassCode[8] = {1,9,8,1,2,0,1,6};
static int maxUserCodeSpot = 8;
int userCodeSpot = 0;
int userCode[8] = {-1,-1,-1,-1,-1,-1,-1,-1};
int resetUserCode[8] = {-1,-1,-1,-1,-1,-1,-1,-1};
int count = 0;
char press = 0;
unsigned char mask[16]={0xEE,0xDE,0xBE,0x7E,
0xED,0xDD,0xBD,0x7D,
0xEB,0xDB,0xBB,0x7B,
0xE7,0xD7,0xB7,0x77};
unsigned char key[16] ={ 0x1,0x2,0x3,0xA,
0x4,0x5,0x6,0xB,
0x7,0x8,0x9,0xC,
0x0,0x0,0x0,0xD};
int alarmRunning = 1; //if the alarm is yelling at you currently
int disarmed = 1;
//ACTUAL CODE STARTS HERE ######################################################
//##############################################################################
//##############################################################################
// __ __ _____
// | \ | \ | \
// | $$ | $$ ______ __ __ \$$$$$ ______ ______ ____ ______ _______
// | $$__| $$ / \ | \ | \ | $$ | \ | \ \ / \ / \
// | $$ $$| $$$$$$\| $$ | $$ __ | $$ \$$$$$$\| $$$$$$\$$$$\| $$$$$$\| $$$$$$$
// | $$$$$$$$| $$ $$| $$ | $$ | \ | $$ / $$| $$ | $$ | $$| $$ $$ \$$ \
// | $$ | $$| $$$$$$$$| $$__/ $$ | $$__| $$| $$$$$$$| $$ | $$ | $$| $$$$$$$$ _\$$$$$$\
// | $$ | $$ \$$ \ \$$ $$ \$$ $$ \$$ $$| $$ | $$ | $$ \$$ \| $$
// \$$ \$$ \$$$$$$$ _\$$$$$$$ \$$$$$$ \$$$$$$$ \$$ \$$ \$$ \$$$$$$$ \$$$$$$$
// | \__| $$
// \$$ $$
// \$$$$$$
void __ISR(7) PIRSensorTripped(void){
//if the alarm isn't already going off...
if(alarmRunning == 0)
{
disarmed = 0;
alarmRunning = 1;
//Start the alarm buzzer
//T2CONbits.ON = T2CONbits.ON^1;//Toggle the buzzer hopefully
T2CONbits.ON = 1;
//Reset our arrays
ResetUserArray(); //User input array
currentLEDnum = 0; //The 8 digit indicator LEDs
OutputToLEDS(); //Should blank indicator LEDs
sevenSegSpot = 0; //Reset 7 seg to 0
OutputToSevenSeg(); //Update 7seg display
userCodeSpot = 0;
}
//If the alarm is already running don't worry
else
{
}
//Reset flag
}
void __ISR(8) PlayAlarm(void)
{
//lots of stuff deleted :)
}
void __ISR(11) RotaryEncoderCW(void){
//lots of stuff deleted :)
}
void __ISR(19) RotaryEncoderCCW(void){
//lots of stuff deleted :)
//Once it triggers for a 1, it needs to look for a 0 next
INTCONbits.INT4EP = (1 ^ B); // Uses difference function to flip polarity back and forth
IFS0bits.INT4IF = 0;//Reset flag
}
void __ISR(15) EncoderPushButton(void){
if(userCodeSpot > 4 || disarmed ==1)
{
//Save the encoder value to our user input array
userCode[userCodeSpot] = sevenSegSpot;
userCodeSpot++;
currentLEDnum++;
//If 8 digits have been entered, either turn it off or reset
if(currentLEDnum ==passCodeLength)
{
if(PasswordIsCorrect())
{
T2CONbits.ON = 0; //Turn off the buzzer
//Display C for Correct on the 7seg
sevenSegSpot = 0xC;
OutputToSevenSeg();
//Clear the LEDs
currentLEDnum = 0;
OutputToLEDS();
disarmed = 1;
}
else
currentLEDnum = 0;
userCodeSpot=0;
ResetUserArray();
}
OutputToLEDS();
}
IFS0bits.INT3IF = 0; //Reset flag
}
//Checks to see if the password matches user code
//Returns 0 if they are different, 1 if it is a match
int PasswordIsCorrect(void)
{
int f=0;
int passFail = 1;
for(f=0; f<passCodeLength-1; f++)
{
if(userCode[f]!=PassCode[f])
passFail = 0;
}
return passFail;
}
void ResetUserArray()
{
int f = 0;
for(f=0; f<maxUserCodeSpot-1; f++)
{
userCode[f] = resetUserCode[f];
}
}
void OutputToLEDS()
{
if(!disarmed)
{
//Pass data to SPI buffer
SPI1BUF = ledNumbers[currentLEDnum];
}
}
void OutputToSevenSeg()
{
//Pass data to SPI buffer
SPI2BUF = sevenSeg[sevenSegSpot];
}
main(){
//Globally enable interrupts
INTEnableSystemMultiVectoredInt();
//130 lines of setup code deleted
//##################### Speaker Setup ######################################
//##################### PIR Motion Sensor Setup ############################
//##################### Keypad Setup #######################################
//##################### Rotary Encoder Setup ###############################
//##################### Rotary PushButton Setup ############################
//##################### SPI 1 Setup ########################################
//##################### SPI 2 Setup ##########################################
//Display 0 on 7seg
sevenSegSpot = 0;
OutputToSevenSeg();
//Clear the LEDs
currentLEDnum = 0;
SPI1BUF = ledNumbers[currentLEDnum]; //Override myself damnit
while(1)
{
//lots of stuff deleted :)
//like... a lot
///yeah...... like a whole lot.
delay(250);
}
}
}
}
delay(int delayTime){
int i, j;
for(i = 0; i < delayTime; i++)
for(j = 0; j < 312; j++);
}
// ___..........__
// _,...._ _."'_,.++8n.n8898n.`"._ _....._
// .' `". _.'_.'" _.98n.68n. `"88n. `'. ," `.
// / . `. ,'. " -'" __.68`""'""=._`+8. `.' . `.
// . ` . `. ,d86+889" 8"""+898n, j8 9 ," . \
// : ' ., ,d"'" _..d88b..__ `"868' .' . ' :
// : . . _ ,n8""88":8"888."8. " ' :
// \ , ' , . .88" ,8P' ,d8. _ `"8n `+. `. . '
// `. .. . d89' " _..n689+^'8n88n.._ `+ . ` . , ' ,'
// `. . , ' 8' .d88+" j:""' `886n. b`. ' .' . ."
// ' , .j ,d'8. ` ."8.`. `. ':
// . .' n8 ,_ .f A 6. ,.. `8b, '. .'_
// .' _ ,88' :8"8 6'.d`i.`b. d8"8 688. ". `'
// ," .88 .d868 _ ,9:' `8.`8 "' ` _ 8+"" b `,
// _. d8P d' .d :88. .8'`j ;+. " n888b 8 . ,88. .
// ` :68' ,8 88 `. ' : l ` .' `" jb .` 688b. ',
// .' .688 6P 98 =+""`. ` ' ,-"`+"'+88b 'b. 8689 ` '
// ; .'"888 .8; ."+b. : `" ; .: "' ; ,n `8 q8, '88: \
// . . 898 8: : `.`--"8. d8`--' ' .d' ;8 898 '
// , 689 9: 8._ ,68 . . :89 ..n88+' 89 689,' ` .
// : ,88' 88 `+88n - . . . " _. 6: `868 ' '
// , ' .68h. 68 `" . . . . . . ,8' 8P' . .
// . '88 'q. _.f . . . ' .._,. .8" .889 ,
// .' `898 _8hnd8p' , . .. . . ._ `89,8P j"' _ `
// \ ` .88, `q9868' ,9 .. . . . 8n .8 d8' +' n8. , '
// ,' ,+"88n `"8 .8' . .. . . `8688P" 9' ,d868' . .
// . . `86b. " . . .. 68' _.698689; :
// . ' ,889_.n8. , ` . .___ ___. .n" `86n8b._ `8988'b .,6
// ' q8689'`68. . ` `:. `.__,' .:' , + +88 `"688n `q8 q8. 88
// , . ' " `+8 n . `:. .;' . ' . ,89 " `q, `8
// . . , . + c , `:.,:" , " d8' d8. :
// . ' 8n ` , . :: . ' " . .68h. .8'`8`. 6
// , 8b.__. , .n8688b., . .;:._ .___nn898868n. n868b "` 8
// `. `6889868n8898886888688898"' "+89n88898868868889' 688898b .8
// : q68 `""+8688898P ` " ' . ` ' ' `+688988P" d8+8P' `. .d8
// , 88b. `+88. ` ` ' .889"' ,.88' .,88
// : '988b "88b.._ ,_ . n8p' .d8"' ' 689
// '. "888n._, `"8"+88888n.8,88:`8 . _ .n88P' . ` ;88'
// :8. "q888. . "+888P" "+888n,8n8'" . . ,d986
// :.`8: `88986 `q8" , :688"
// ;. '8, "88b .d ' ,889'
// :.. `6n '8988 b.89p
// :. . '8. `88b 988'
// :. . 8b `q8. ' . ' .d89 '
// . . `8: `86n,. " . , , " ,98P ,
// .. . '6n. +86b. . . _,.n88' .
// . `"8b. 'q98n. , . _..n868688' .
// ' . . `"98. `8868. . _.n688868898p" d
// . . '88. "688. q89888688868" ,86
// mh '. . 88. `8898 " .889"' .988
//
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment