0% found this document useful (0 votes)
152 views15 pages

Main

This document contains the code for a lock system that includes functions for initializing devices, clearing variables, delaying, debouncing buttons, displaying codes on an LCD, spinning a motor, locking/unlocking the lock, erasing codes, and adding new codes. It defines global variables and constants used throughout. Key functions include initializing devices, clearing variables, delaying, reading button input with debouncing, displaying codes on the LCD, spinning the motor, locking/unlocking based on input codes, erasing all codes, and adding new codes to memory.

Uploaded by

api-241783239
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views15 pages

Main

This document contains the code for a lock system that includes functions for initializing devices, clearing variables, delaying, debouncing buttons, displaying codes on an LCD, spinning a motor, locking/unlocking the lock, erasing codes, and adding new codes. It defines global variables and constants used throughout. Key functions include initializing devices, clearing variables, delaying, reading button input with debouncing, displaying codes on the LCD, spinning the motor, locking/unlocking based on input codes, erasing all codes, and adding new codes to memory.

Uploaded by

api-241783239
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

#include <hidef.

h>
/* common defines and macros */
#include "derivative.h"
/* derivative-specific definitions */
#include "MCUinit.h"
// For MCU_init(), device initialization function.
#include <stdio.h>
// For sprintf().
#include "LCD.h"
// For LCDPutString().
#include "SCI.h"
// For SCIInit(), SCITransmitStr().
#include "device_headers.h" // Derivative-specific definitions.
#define MY_POT ATDDR1H
// Group 111, X = 90, PAT = 3, Range 1 0 to 75, Range 2 76 to 125, Range 3 126 255
/********************************************************
* Global Variables to be used throughout
*
********************************************************/
unsigned char vDirection, vPosition, input;
int vCycle = 0;
int vError = 0;
int vCodeCount = 0;
unsigned char vInput[6];
unsigned char vCodes[9];
unsigned int vCount,vNum,i;
/*********************************************************
* Function definitions
*
*********************************************************/
unsigned char hex_key_scan(void);
void MCU_init(void); /* Device initialization function declaration */
/***************************************************************
* Function Name: Clear()
*
* Author(s): Boris Barreto
* Input Parameter List: vNum, vCount, vInput[], i
*
* Output Parameter List:
*
* Clear() will clear vInput[] and reset the count and num to 0*
***************************************************************/
void Clear()
{
vCount = 6;
vNum = 0;
for (i = 0; i < 6; i++)
vInput[i] = 'J'; // Dummy Letter
}

/***************************************************************
* Function Name: delay_X_ms(int x)
*
* Author(s): Boris Barreto & Stephen Kwon
*
* Input Parameter List: i
*
* Output Parameter List:
*
* Simple Software delay loop
*
***************************************************************/
void delay_X_ms (int x)
{
int j;
for (i = 0; i < x; i++)
{
for (j = 0; j < x; j++)
{
}
}
}
/***************************************************************
* Function Name: keypad_debounce(unsigned int bounce_length) *
* Author(s): Boris Barreto
* Input Parameter List: bounce_length
*
* Output Parameter List: get_key , new_key
*
* keypad_debounce will make sure that only one button is
*
* returned for each keypad press.
*
***************************************************************/
unsigned char keypad_debounce(unsigned int bounce_length)
{
int count;
unsigned char get_key,new_key;
get_key = hex_key_scan();
count = bounce_length/2;
while ((count > 0) && (count < bounce_length))
{
delay_X_ms(10);
new_key = hex_key_scan();
if (new_key == get_key)
++count;
else
--count;
}
if (count == bounce_length)
return (get_key);
else
return (new_key);
}
/***************************************************************
* Function Name: Spin()
*

* Input Parameter List: vCycle, step()


*
* Output Parameter List:
*
* Spin will spin the motor for approximately one minute at
* approximately 90 rpm
*
***************************************************************/

void Spin(void)
{
vCycle = 0;
while(vCycle <= 3249) //3249
{
step();
delay_X_ms(37);//37
}
}
/***************************************************************
* Function Name: Lock()
*
* Author(s): Boris Barreto
* Input Parameter List: vDirection
*
* Output Parameter List:
*
* Lock() will display Lock to the LCD and then Spin the motor *
***************************************************************/
void Lock()
{
vDirection = 0;
LCDPutString(" T111 ML\n");
LCDPutString(" LOCK\n");
Spin();
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
}

/***************************************************************
* Function Name: DisplayCodes()
*

* Author(s): Boris Barreto & Stephen Kwon


*
* Input Parameter List: vTemp, vCodes[],
*
* Output Parameter List:
*
* DisplayCodes() will display the unlock codes one at a time. *
* There will be a delay between the codes to make sure they *
* are readable
*
***************************************************************/
void DisplayCodes()
{
int vTempCount;
unsigned char vTemp[8];
if (vCodes[0] == 'J')
{
LCDPutString(" T111 MD\n");
LCDPutString("N1-\n");
}
else if (vCodes[1] == 'J')
{
vTempCount = (vCodes[0] - 48);
sprintf(vTemp,"N1-%d\n",vTempCount);
LCDPutString(" T111 MD\n");
LCDPutString(vTemp);
}
else if (vCodes[2] == 'J')
{
vTempCount = (((vCodes[0] - 48) * 10) + vCodes[1] - 48);
sprintf(vTemp,"N1-%d\n",vTempCount);
LCDPutString(" T111 MD\n");
LCDPutString(vTemp);
}
else
{
vTempCount = (((vCodes[0] - 48) * 100) + ((vCodes[1] - 48) * 10) + vCodes[2] - 48);
sprintf(vTemp,"N1-%d\n",vTempCount);
LCDPutString(" T111 MD\n");
LCDPutString(vTemp);
}
delay_X_ms(500);
if (vCodes[3] == 'J')
{
LCDPutString(" T111 MD\n");
LCDPutString("N2-\n");
}
else if (vCodes[4] == 'J')
{
vTempCount = (vCodes[3] - 48);
sprintf(vTemp,"N1-%d\n",vTempCount);
LCDPutString(" T111 MD\n");

LCDPutString(vTemp);
}
else if (vCodes[5] == 'J')
{
vTempCount = (((vCodes[3] - 48) * 10) + vCodes[4] - 48);
sprintf(vTemp,"N1-%d\n",vTempCount);
LCDPutString(" T111 MD\n");
LCDPutString(vTemp);
}
else
{
vTempCount = (((vCodes[3] - 48) * 100) + ((vCodes[4] - 48) * 10) + vCodes[5] - 48);
sprintf(vTemp,"N2-%d\n",vTempCount);
LCDPutString(" T111 MD\n");
LCDPutString(vTemp);
}
delay_X_ms(500);
if (vCodes[6] == 'J')
{
LCDPutString(" T111 MD\n");
LCDPutString("N3-\n");
}
else if (vCodes[7] == 'J')
{
vTempCount = (vCodes[6] - 48);
sprintf(vTemp,"N1-%d\n",vTempCount);
LCDPutString(" T111 MD\n");
LCDPutString(vTemp);
}
else if (vCodes[8] == 'J')
{
vTempCount = (((vCodes[6] - 48) * 10) + vCodes[7] - 48);
sprintf(vTemp,"N1-%d\n",vTempCount);
LCDPutString(" T111 MD\n");
LCDPutString(vTemp);
}
else
{
vTempCount = (((vCodes[6] - 48) * 100) + ((vCodes[7] - 48) * 10) + vCodes[8] - 48);
sprintf(vTemp,"N3-%d\n",vTempCount);
LCDPutString(" T111 MD\n");
LCDPutString(vTemp);
}
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
}
/***************************************************************
* Function Name: Erase()
*

* Author(s): Boris Barreto


* Input Parameter List: i, vCodeCount, vCodes[]
*
* Output Parameter List:
*
* Erase() will delete the codes currently in the vCodes[] *
***************************************************************/

void Erase()
{
LCDPutString(" T111 ME\n");
LCDPutString(" ERASED\n");
for (i = 0; i < 9; i++)
vCodes[i] = 'J';
vCodeCount = 0;
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
}
/***************************************************************
* Function Name: Unlock()
*
* Author(s): Boris Barreto & Stephen Kwon
*
* Input Parameter List: vInput[], vCodes[], vDirection
*
* Output Parameter List:
*
* Unlock will check the given input with the vCodes[] one at a*
* time and then spin the motor for sixty seconds if the input *
* is correct. Otherwise, it will Display an error and Clear() *
***************************************************************/
void Unlock()
{
int j,p;
j = 0;
p = 0;
while (p != 1)
{
input = keypad_debounce(50);
if (input == '#')
{
if ((vInput[5] == vCodes[0] && vInput[4] == vCodes[1] && vInput[3] == vCodes[2]) ||
(vInput[5] == vCodes[3] && vInput[4] == vCodes[4] && vInput[3] == vCodes[5]) ||
(vInput[5] == vCodes[6] && vInput[4] == vCodes[7] && vInput[3] == vCodes[8]))
{
vDirection = 1;
LCDPutString(" T111 MU\n");
LCDPutString(" OK\n");
Spin();
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
p = 1;

}
else
{
LCDPutString(" T111 MU\n");
LCDPutString("NOTVALID\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
p = 1;
}
}
else if (input == '*' || (input >= 'A' && input <= 'D'))
{
LCDPutString(" T111 MS\n");
LCDPutString(" USERERR\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
p = 1;
}
else if (input >= '0' && input <= '9')
{
vCount = vCount - 1;
vInput[vCount] = input;
vNum = vNum + 1;
LCDPutString(" T111 MS\n");
LCDPutString(" INPY\n");
delay_X_ms(10);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
++j;
}
if (j == 3)
{
LCDPutString(" T111 MS\n");
LCDPutString(" USERERR\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
p = 1;
}
}
}
/***************************************************************
* Function Name: OpenProg()
*
* Author(s): Boris Barreto
* Input Parameter List: vInput[], vNum, vCount, input

*
*

* Output Parameter List:


*
* OpenProg() is interesting. It will enter this function if *
* the input is ##n. Once it is entered, it will display the *
* input code until the # is pressed again (max of 3 n).
*
*
*
* The code will be constantly checking if an invalid button is*
* pressed.
*
*
*
* After the # is pressed, the prog will check if the codes are*
* full. If so, it will display so, and Clear(). Otherwise it *
* will add the code.
*
***************************************************************/
void OpenProg()
{
unsigned char vTemp[8];
int vTempCount,j,p;
j = 0;
p = 0;
if ((vInput[3] >= '0' && vInput[3] <= '9') &&
(vInput[2] == 'J')
&&
(vInput[1] == 'J'))
{
vTempCount = (vInput[3] - 48);
sprintf(vTemp," %d\n",vTempCount);
LCDPutString(" T111 MP\n");
LCDPutString(vTemp);
j = 1;
}
else
{
j = 0;
LCDPutString(" T111 MP\n");
LCDPutString(" ERR\n");
vCount = 4;
vNum = 2;
vInput[3] = 'J';
vInput[2] = 'J';
vInput[1] = 'J';
}
while (p != 1)
{
input = keypad_debounce(50);
if (j < 3 && (input == '*' || (input >= 'A' && input <= 'D')))
{
LCDPutString(" T111 MP\n");
LCDPutString(" ERR\n");
j = 0;
vCount = 4;
vNum = 2;

vInput[3] = 'J';
vInput[2] = 'J';
vInput[1] = 'J';
}
else if (j < 3)
{
if (j == 0)
{
if (input >= '0' && input <= '9')
{
vCount = vCount - 1;
vInput[vCount] = input;
vNum = vNum + 1;
vTempCount = vInput[vCount] - 48;
sprintf(vTemp," %d\n",vTempCount);
LCDPutString(" T111 MP\n");
LCDPutString(vTemp);
++j;
}
else if (input == '#')
{
LCDPutString(" T111 MP\n");
LCDPutString(" ADDED\n");
vCodes[vCodeCount++] = vInput[3];
vCodes[vCodeCount++] = vInput[2];
vCodes[vCodeCount++] = vInput[1];
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
p = 1;
Clear();
}
}
else if (j == 1)
{
if( input == '#')
{
LCDPutString(" T111 MP\n");
LCDPutString(" ADDED\n");
vCodes[vCodeCount++] = vInput[3];
vCodes[vCodeCount++] = vInput[2];
vCodes[vCodeCount++] = vInput[1];
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
p = 1;
Clear();
}
else if (input >= '0' && input <= '9')
{
vCount = vCount - 1;

vInput[vCount] = input;
vNum = vNum + 1;
vTempCount = (((vInput[vCount + 1] - 48) * 10) + (vInput[vCount] - 48));
sprintf(vTemp," %d\n",vTempCount);
LCDPutString(" T111 MP\n");
LCDPutString(vTemp);
++j;
}
}
else if (j == 2)
{
if( input == '#')
{
LCDPutString(" T111 MP\n");
LCDPutString(" ADDED\n");
vCodes[vCodeCount++] = vInput[3];
vCodes[vCodeCount++] = vInput[2];
vCodes[vCodeCount++] = vInput[1];
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
p = 1;
Clear();
}
else if (input >= '0' && input <= '9')
{
vCount = vCount - 1;
vInput[vCount] = input;
vNum = vNum + 1;
vTempCount = (((vInput[vCount + 2] - 48) * 100) + ((vInput[vCount + 1] - 48) * 10) + (vInput[vCount] - 48));
sprintf(vTemp," %d\n",vTempCount);
LCDPutString(" T111 MP\n");
LCDPutString(vTemp);
++j;
}
}
}
if (j >= 3 && input == '#')
{
if (vCodes[8] == 'J')
{
LCDPutString(" T111 MP\n");
LCDPutString(" ADDED\n");
vCodes[vCodeCount++] = vInput[3];
vCodes[vCodeCount++] = vInput[2];
vCodes[vCodeCount++] = vInput[1];
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
p = 1;
Clear();

}
else
{
LCDPutString(" T111 MP\n");
LCDPutString(" FULL\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
p = 1;
Clear();
}
}
}
}

/***************************************************************
* Function Name: CheckInput()
*
* Author(s): Boris Barreto & Stephen Kwon
*
* Input Parameter List: vNum, vCount, vInput[]
*
* Output Parameter List:
*
* This function is the function which will tell the program *
* which function to run. The program will check the input for *
* any of the key inputs. If they are recognized, the program *
* will run the correct function.
*
*
*
* If an incorrect pattern is discovered at any point, it will *
* immediately reset the code.
*
***************************************************************/
void CheckInput()
{
if (vNum == 1)
{

if (vInput[5] == '*')
{
Lock();
}
else if (vInput[5] >= '0' && vInput[5] <= '9')
Unlock();
}
else if (vNum == 2)
{
if (((vInput[5] <= '9' && vInput[5] >= '0') || (vInput[5] >= 'A' && vInput[5] <= 'D')) && (vInput[4] == '#' || vInput[4] ==
'*'))
{
LCDPutString(" T111 MS\n");
LCDPutString(" USERERR\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
}
else if (vInput[5] == '#' && ((vInput[4] <= '9' && vInput[4] >= '0') || (vInput[4] >= 'A' && vInput[4] <= 'D')))
{
LCDPutString(" T111 MS\n");
LCDPutString(" USERERR\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
}
}
else if (vNum == 3)
{
if (vInput[5] == '#' && vInput[4] == '#' && vInput[3] == '#')
DisplayCodes();
else if (vInput[5] == '#' && vInput[4] == '*' && vInput[3] == '#')
Erase();
else if (vInput[5] == '#' && vInput[4] == '#' && (vInput[3] == '*' || (vInput[3] >= '0' && vInput[3] <= '9')))
OpenProg();
else if (vInput[5] == '#' && vInput[4] == '*' && (vInput[3] == '*' || (vInput[3] >= '0' && vInput[3] <= '9')
|| (vInput[3] >= 'A' && vInput[3] <= 'D')))
{
LCDPutString(" T111 MS\n");
LCDPutString(" USERERR\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
}
else if (((vInput[5] >= '0' && vInput[5] <= '9') || (vInput[5] >= 'A' && vInput[5] <= 'D')) &&
((vInput[4] >= '0' && vInput[4] <= '9') || (vInput[4] >= 'A' && vInput[4] <= 'D')) &&
(vInput[3] == '*' || vInput[3] == '#'))
{

LCDPutString(" T111 MS\n");


LCDPutString(" USERERR\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
}
}
else if (vNum == 4)
{
if (((vInput[5] >= '0' && vInput[5] <= '9') || (vInput[5] >= 'A' && vInput[5] <= 'D')) &&
((vInput[4] >= '0' && vInput[4] <= '9') || (vInput[4] >= 'A' && vInput[4] <= 'D')) &&
((vInput[3] >= '0' && vInput[3] <= '9') || (vInput[3] >= 'A' && vInput[3] <= 'D')) && vInput[2] == '#')
Unlock();
else if (((vInput[5] >= '0' && vInput[5] <= '9') || (vInput[5] >= 'A' && vInput[5] <= 'D')) &&
((vInput[4] >= '0' && vInput[4] <= '9') || (vInput[4] >= 'A' && vInput[4] <= 'D')) &&
((vInput[3] >= '0' && vInput[3] <= '9') || (vInput[3] >= 'A' && vInput[3] <= 'D')) &&
(vInput[2] == '*' || (vInput[2] >= '0' && vInput[3] <= '9') || (vInput[2] >= 'A' && vInput[3] <= 'D')))
{
LCDPutString(" T111 MS\n");
LCDPutString(" USERERR\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
Clear();
}
else if ((vInput[5] == '#') && (vInput[4] == '#') &&
((vInput[3] >= '0' && vInput[3] <= '9') || (vInput[3] >= 'A' && vInput[3] <= 'D')) &&
(vInput[2] == '*' || vInput[2] == '#' || (vInput[2] >= '0' && vInput[2] <= '9')))
OpenProg();
}
else if (vNum == 5)
{
OpenProg();
}
return;
}
/***************************************************************
* Function Name: Debug()
*
* Author(s): Boris Barreto
* Input Parameter List: vDirection
*
* Output Parameter List: SLK_LED(1-4)
*
* Debug will be activated on PB1 pressed. The function will *
* check the POT value and decide which direction to spin the *
* motor
*
***************************************************************/
void Debug(void)
{
LCDPutString(" T111 MD\n");

LCDPutString(" DEBUG\n");
PTT_PTT7 =1;
while(SLK_PB1 == 1) //Wait for button
{
delay_X_ms(100);
//Reverse
while(MY_POT < 76 && SLK_PB1 == 1 )
{
vDirection = 1;
step();
delay_X_ms(37);
}
//Stop
while(MY_POT >= 76 && MY_POT <= 125 && SLK_PB1 == 1)
{
vDirection = 2;
step();
}
//Bounce back for switch
delay_X_ms(100);
//Forward
while(MY_POT > 126 && SLK_PB1 == 1)
{
vDirection = 0;
step();
delay_X_ms(37);
}
}
SLK_LED1 = SLK_LED2 = SLK_LED3 = SLK_LED4 = OFF;
LCDPutString(" T111 MS\n");
//power on mode
LCDPutString(" POR\n");
PTT_PTT7 =0;
}
/***************************************************************
* Function Name: main()
*
* Author(s): Boris Barreto
* Input Parameter List:
*
* Output Parameter List:
*
* The main program will initialize all variables, and then *
* the program will enter a neverending loop. This loop will *
* read the keypad, and when a valid value is pressed, the *
* value will be placed in the input variable. The program will*
* then enter CheckInput() to see if a valid command is found *
***************************************************************/
void main(void)
{
MCU_init(); /* call Device Initialization */
DisableInterrupts;
IO_ASM();
//Initialize ports used

INIT_SLK_LEDs;
// Initialize for use of Student Learning Kit project board Light-Emitting Diodes via port A.
INIT_SLK_POT;
// Initialize A2D to use Student Learning Kit project board potentiometer.
LCDInit();
// Initialize for use of Student Learning Kit project board liquid crystal display
vCount = 6;
vNum = 0;
for (i = 0; i < 9; i++)
vCodes[i] = 'J';
for (i = 0; i < 6; i++)
vInput[i] = 'J';
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
EnableInterrupts;
for(;;)
{
input = keypad_debounce(50);
if ((input == '*') || (input == '#') || (input >= '0' && input <= '9'))
{
vCount = vCount - 1;
vInput[vCount] = input;
vNum = vNum + 1;
LCDPutString(" T111 MS\n");
LCDPutString(" INPY\n");
delay_X_ms(10);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
CheckInput();
}
if(vNum == 6)
{
Clear();
LCDPutString(" T111 MS\n");
LCDPutString(" CLRINP\n");
delay_X_ms(500);
LCDPutString(" T111 MS\n");
LCDPutString(" POR\n");
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy