The microcontroller code is posted below. The only files created/changed were main.c and definitions.h, so if you want to recreate the project then starting a new project and using these files should work great.
The final code for the project is posted below. The length of time that the lights is on is dictated by the line:
if (idleCount== INTEGER)
To change the time in your project, use the formula
(Integer / 200) = Number of minutes to wait before turning the lights off So, in our case we turn the lights off after one minute. If you wanted to wait five minutes, you would use the number 1000.
main.c
//Copyright Eric William McGinnis 2010 #include <hidef.h> /* for EnableInterrupts macro */ #include "derivative.h" /* include peripheral declaration */ #include <string.h> #include "definitions.h" //----------Serial Code, Borrowed From SECUROUTER----------- typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned long uint32; typedef signed char int8; typedef short int16; typedef long int int32; //sets up registers for tx //notice setup_Tx has no inputs or outputs so it is essentially a subroutine //called to made the code look neater. void setup_Tx(void); //sends one byte or one char void send_byte(uint8 b); //sends a string //calls send_byte void send_string(const uint8 * str); //-------------------END SERIAL CODE----------------------- //need a 16 bit integer to count to 300 int idleCount = 0; unsigned char leftWindowOpen = 0; unsigned char rightWindowOpen = 0; unsigned char doorOpen = 0; short analogDataBuffer = 0; unsigned char outputString[16]; unsigned char i; void checkDigitalInputs(void); void sendStringOverSerial(unsigned char status); void readVibrations(void); /***************************************************************************/ // call this to set up the clock speed when not in debugger /***************************************************************************/ void setup_Clock(void); void main(void); void interrupt VectorNumber_Vtpm1ovf TPM1_ovf(){ TPM1SC_TOF = 0; //disable the TPM TPM1SC_TOIE = 0; idleCount++; //after 65 seconds, the lights will turn off if (idleCount==200){ outputString[2] = '0'; PTAD_PTAD0 = 1; //turn off the light by sending a 1 to the driver chip idleCount = 0; } //enable the timer TPM1SC_TOIE = 1; } void interrupt VectorNumber_Vtpm2ovf TPM2_ovf(){ //disable the TPM TPM2SC_TOIE = 0; TPM2SC_TOF = 0; PTAD_PTAD1 = !PTAD_PTAD1; //enable the timer TPM2SC_TOIE = 1; } #pragma NO_EXIT void interrupt VectorNumber_Vreset _Startup(void) { SOPT = 0x22; __asm ldhx #$107F + 1 // Initialize Stack Pointer __asm txs __asm jmp main } #pragma NO_EXIT void main(void) { EnableInterrupts; /* enable interrupts */ /* include your code here */ setup_Clock(); //-----------TPM SET UP CODE--------------- //-------The TPM will be trigerred about once every second----- TPM1SC_CLKSA = 1; TPM1SC_CLKSB = 0; TPM1SC_PS0 = 1; TPM1SC_PS1 = 1; TPM1SC_PS2 = 1; TPM1SC_TOIE = 1; //TPM1MOD = 37500; TPM1MOD = 65000; //-----------TPM SET UP CODE END----------- setup_Tx(); //Port F will be used for digital inputs. Also, enable pulls ups on the port PTFDD = 0x00; PTFPE = 0xFF; //Port B will be used for analog inputs, configure as such: ATD1C_ATDPU = 1; ATD1C_RES8 = 1; ATD1C_PRS = 0x0E; ATD1SC_ATDCO = 1; ATD1PE = 0x01; ATD1SC_ATDCH0 = 0; //End analog configure code //Port B will be used to turn the lights on and off, configure for output //PTBDD = 0xFF; //PTBD = 0x00; outputString[0] = '/'; outputString[1] = ' '; outputString[3] = ' '; outputString[5] = ' '; outputString[7] = ' '; outputString[9] = ' '; outputString[11] = ' '; outputString[13] = '\n'; outputString[14] = '\r'; outputString[15] = '\0'; //temporary vibration variable outputString[12] = '1'; /* include your code here */ PTADD = 0xFF; PTAD = 0x00; TPM2SC = 0x08; //TPM clock = BUSCLK = 20MHz TPM2MOD = 168; //PWM frequency is equal to 56kHz //TPM1MOD = 355 //this will set it to 28kHz in conjunction with rest of code TPM2C0SC = 0x28; //When set the PWM will be set at active High TPM2C0V = 168; //Has to be half of TPMMOD so there is a 50% duty cycle TPM2SC_TOIE = 1; for(;;) { checkDigitalInputs(); readVibrations(); /* outputString[2] = '1'; outputString[4] = '0'; outputString[6] = '1'; outputString[8] = '0'; outputString[10] = '1'; outputString[12] = '0'; */ send_string(outputString); __RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */ /* please make sure that you never leave main */ } void checkDigitalInputs(void) { //check to see if the left window is open if (REEDWINDOWLEFT1==0){ outputString[4] = '0'; leftWindowOpen = 1; } else { outputString[4] = '1'; leftWindowOpen = 0; } //check to see if the right window is open if (REEDWINDOWRIGHT1 == 0) { outputString[6] = '0'; rightWindowOpen = 1; } else{ outputString[6] = '1'; rightWindowOpen = 0; } //check to see if the door is open if (REEDDOOR1 == 0){ outputString[8] = '0'; } else{ outputString[8] = '1'; outputString[2] = '1'; PTAD_PTAD0 = 0; //turn on the light by sending a 0 to the driver chip idleCount = 0; } if (PIRSENSOR){ outputString[10] = '1'; idleCount = 0; outputString[2] = '1'; PTAD_PTAD0 = 0; //turn on the light by sending a 0 to the driver chip } else { outputString[10] = '0'; } //if( !(REEDWINDOWLEFT1 + REEDWINDOWLEFT2 + REEDWINDOWRIGHT1 + REEDWINDOWRIGHT2) && PIRSENSOR && !(REEDDOOR1+REEDDOOR2)) { // } } //Set up a string that we will then send over serial using send_string void sendStringOverSerial(unsigned char status) { /*switch (status){ case 0: strcpy(outputString,"The room is empty, unless someone is taking a nap.\n\r"); break; case 1: strcpy(outputString,"The left window is open.\n\r"); break; case 2: strcpy(outputString,"The right window is open.\n\r"); break; case 3: strcpy(outputString,"The door is open.\n\r"); break; case 4: strcpy(outputString,"The table is shaking!\n\r"); break; case 5: strcpy(outputString,"Someone tripped the lasers!\n\r"); break; case 6: strcpy(outputString,"There is some general motion in the room.\n\r"); break; case 7: strcpy(outputString,"You tripped all the sensors! Someone is making a ruckus!\n\r"); break; } */ send_string(outputString); } void readVibrations(void) { //for(i=0;i<10;i++){ while(!(ATD1SC_CCF)){ //keep looping if this register is not set //if the register is not set, then analog data is not ready to be read } analogDataBuffer = ATD1RH; //} if (analogDataBuffer > 15){ outputString[12] = '1'; outputString[2] = '1'; PTAD_PTAD2 = 0; //turn on the VIBRATION DETECTED LED PTAD_PTAD0 = 0; //turn on the light by sending a 0 to the driver chip } else{ PTAD_PTAD2 = 1; //turn off the VIBRATION DETECTED LED outputString[12] = '0'; } } //----------SENDING DATA OVER SERIAL----------------- void setup_Tx(void) { /* Set baud rate register for SCI2 formula: SCI baud rate = BUSCLK/(16×BR) where BR refers to 13 writable bits of the SCIxBDH and DL registers */ //we want baud rate = 9600Hz = 20Mhz/(16xBR) //so BR SHOULD BE 130 or 0x82 //testing the frequency on the oscilloscope did not produce a signal of exactly 9600 baud //so BR had to be lowered to 0x7B SCI2BDH = 0x00; //set H register first SCI2BDL = 0x7B; //set L after H register /* Set SCI control register for SCI2 SCI x Control Register 1 options bit7=0 Normal operation - RxD1 and TxD1 use separate pins bit6=0 SCI clocks do not freeze while CPU is in wait mode bit5=0 Has no effect unless bit7 is set to 1 bit4=0 8-bit mode bit3=0 Idle-line wakeup method bit2=0 Idle character bit count starts after start bit bit1=0 Parity checking disabled bit0=0 Even parity but should not matter since parity checking is disabled SCI x Control Register 2 options bit7=0 Hardware interrupts for transmit data register empty (TDRE) disabled bit6=0 Hardware interrupts for transmission complete (TC) disabled bit5=0 Hardware interrupt for receive data register (RDRF) flag disabled bit4=0 Hardware interrupts from IDLE disabled bit3=1 Transmitter on bit2=0 Receiver off bit1=0 Normal SCI receiver operation, not in standby bit0=0 Do not sent break characters */ SCI2C1 = 0x00; SCI2C2 = 0x08; } void send_byte(uint8 b) { while(!(SCI2S1 & 0x80)); //check if transmit data register is empty, TDRE = 1 SCI2D = b; //while empty, write byte to data register } // Send a string out // Calls send_byte() to send each byte until end of string is reached void send_string(const uint8 * str) { __RESET_WATCHDOG(); for(;*str;str++) send_byte(*str); } /***************************************************************************/ // call this to set up the clock speed when not in debugger /***************************************************************************/ void setup_Clock(void) { ICGC1 = 0x38; ICGC2 = 0x78; ICGC1_OSCSTEN = 1; //have clock still running during stop3 mode } //----------SENDING DATE OVER SERIAL-----------------
definitions.h
//Copyright Eric William McGinnis, 2010 //define the reed switches #define REEDWINDOWLEFT1 PTFD_PTFD0 //#define REEDWINDOWLEFT2 PTFD_PTFD1 #define REEDWINDOWRIGHT1 PTFD_PTFD2 //#define REEDWINDOWRIGHT2 PTFD_PTFD3 #define REEDDOOR1 PTFD_PTFD4 //#define REEDDOOR2 PTFD_PTFD5 //define the PIR sensor #define PIRSENSOR PTFD_PTFD6 //define the piezo vibration sensors //define the IR LEDs #define IRSENSOR1 PTFD_PTFD1 #define IRSENSOR2 PTFD_PTFD3 #define IRSENSOR3 PTFD_PTFD5 #define IRSENSOR4 PTFD_PTFD7 //define event codes (to be used when calling setStringToSend) #define ROOMIDLE 0 #define LEFTWINDOWOPEN 1 #define RIGHTWINDOWOPEN 2 #define DOOROPEN 3 #define TABLESHAKE 4 #define IRTRIP 5 #define PIR 6 #define ALL 7