Académique Documents
Professionnel Documents
Culture Documents
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()
*
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()
*
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()
*
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
*
*
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(" 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");
}
}
}