/*********************************************************************
*
* Konstantin Avdashchenko Lucid dreaming aid.
*
********************************************************************/
/** I N C L U D E S **********************************************************/
#include
#include
//#include
#include "delay.h"
/** C O N F I G U R A T I O N ************************************************/
#define REDon GPIO5 = 0; GPIO4 = 1;
#define LEDoff GPIO5 = 0; GPIO4 = 0;
#define IRon GPIO5 = 1; GPIO4 = 0;
#define XTAL 4000000
#define BRATE 9600
#define TxData GPIO0 /* Map TxData to pin */
/** V A R I A B L E S ********************************************************/
unsigned short num[30];
unsigned short *ptr = &num[0];
unsigned char i,tmp,tmp1,tmp2,Pass,ni;
//unsigned char valu[];
/** P R I V A T E P R O T O T Y P E S ***************************************/
void Judge (void);
void Flash (void);
void Baud (void);
void Check (void);
void Fill (void);
/** D E C L A R A T I O N S **************************************************/
#define SCALER 10000000
#define ITIME 4*SCALER/XTAL /* Instruction cycle time */
#if BRATE > 1200
#define DLY 3 /* cycles per null loop */
#define TX_OHEAD 13 /* overhead cycles per loop */
#else
#define DLY 9 /* cycles per null loop */
#define TX_OHEAD 14
#endif
#define RX_OHEAD 12 /* receiver overhead per loop */
#define DELAY(ohead) (((SCALER/BRATE)-(ohead*ITIME))/(DLY*ITIME))
/******************************************************************************
* Function: unsigned int sendu (unsigned char chan)
*
* PreCondition: None
*
* Input: byte to send
*
* Output: uart structured byte to the rx pin of pickit2
*
* Side Effects: None
*
* Overview: sends data to uart reciever
*
* Note: None
*****************************************************************************/
void
sendu (unsigned short byte)
{
GPIO1 = 1;
DelayUs(100);
GPIO1 = 0;
i=0;
GPIO0 = 0;
Baud();
while (i!= 8){
if((byte>>i) & 1) GPIO0 = 1;
if(!((byte>>i) & 1)) GPIO0 = 0;
Baud();
i++;
}
GPIO0 = 1;
Baud();
Baud();
GPIO1 = 1;
DelayUs(100);
GPIO1 = 0;
}
/******************************************************************************
* Function: void Fill(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Fills array for checker.
*
* Note: None
*****************************************************************************/
void Fill(){
*ptr = ADRESL;
if (ptr == &num[30]){
Check();
ptr = (&num[0] -1);
}
ptr++;
}
/******************************************************************************
* Function: void Check(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Checks wether eyes are moving.
*
* Note: None
*****************************************************************************/
void Check(){
unsigned char diff, count = 0;
for (i=0;i<29;i++){
tmp1 = num[i];
tmp2 = num[i+1];
if (tmp1 >= tmp2) diff = tmp1 -tmp2;;
if (tmp2 >= tmp1) diff = tmp2 -tmp1;;
if (diff >= 2)count++;
}
if (count >= 2) Flash();
}
/******************************************************************************
* Function: void Flash(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Flashes the LED
*
* Note: None
*****************************************************************************/
void Flash(void){
i=0;
while(i <>
REDon
DelayMs(50);
LEDoff
DelayMs(30);
i++;
}
sendu(0x69);
}
/******************************************************************************
* Function: unsigned int readadc (unsigned char chan)
*
* PreCondition: None
*
* Input: char of channel to read from
*
* Output: 10 bit value of read adc
*
* Side Effects: None
*
* Overview: reads Analog signals
*
* Note: None
*****************************************************************************/
unsigned int
readadc (unsigned char chan)
{
IRon
unsigned short value;
ADCON0 = ((chan <<>
ADON = 1; // AD module enabled
DelayUs(5); // wait for conversion
GODONE = 1; // Conversion start
while(GODONE); // wait to finish
value = ADRESL; // takes the high 8 bits of 10 to reduce noise
ADON = 0; // disable module
return value;
}
/******************************************************************************
* Function: void main(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Main program entry point.
*
* Note: None
*****************************************************************************/
void main(void)
{
OSCCON = 0x60;
ANSEL = 0x34;
TRISIO = 0x04;
GPIO = 0x00;
CMCON0 = 0x07;
ADCON0 = 0x09;
// GPIO = 0x20;
tmp = 0;
while(tmp<5){
REDon
DelayMs(50);
LEDoff
DelayMs(50);
tmp++;
}
GPIO0 = 1;
// T0IE = 1;
// TMR0 = 0xE1;
// GIE = 1;
// num = 10;
/* serial sending works
unsigned short pass;
while(1){
IRon
pass = readadc(2);
LEDoff
sendu(pass);
Flash();
}
*/
//
tmp= 0;
Pass= 0;
tmp = readadc(2);
DelayMs(10);
while(1){
Pass = readadc(2);
Fill();
sendu(Pass);
DelayMs(1);
}
// continue; // let interrupt do its job
}//end main
/******************************************************************************
* Function: unsigned int Baud (unsigned long baud)
*
* PreCondition: None
*
* Input: baud rate to delay for
*
* Output: None
*
* Side Effects: None
*
* Overview: delays before sending next bit
*
* Note: None
*****************************************************************************/
void Baud (void)
{
DelayUs(140);
}
No comments:
Post a Comment