Created
March 15, 2012 22:44
-
-
Save eggie5/2047431 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <hidef.h> | |
#include <MC9S12C128.h> | |
#include <stdio.h> | |
#include <termio.h> | |
#define SL 16 //sequence length | |
//word is unsigned int | |
void delay(word d); | |
byte getkey (); | |
void block_LEDS (byte scan_code); | |
byte getChar(); | |
void putChar(byte output); | |
void handle_serial_port(); | |
word delayTime=2000/SL; //125 ms | |
void init_serial() { | |
SCIBD = 52; | |
SCICR2 = 0x0C; | |
} | |
void init_sensors() { | |
byte line[80]; | |
ATDCTL2 = 0x80; | |
ATDCTL3 = 0x20; | |
ATDCTL4 = 0x80; | |
ATDDIEN = 0xFF; | |
DDRAD = 0x00; | |
sprintf(line, "\n\nPot1 Photo1 Pot2 Photo2\n\n"); | |
printf("%s", line); | |
SCIDRL = 0x0D; //newline | |
sprintf(line, "---- ------ ----- ------\n\n"); | |
printf("%s", line); | |
SCIDRL = 0x0D; //newline | |
} | |
void sample_sensors() | |
{ | |
byte data1,data2; | |
float po1, po2, phot1, phot2; | |
byte line[80]; | |
int time=150; | |
ATDCTL5=0x14; | |
// delay(time); | |
data2 = ATDDR3H; | |
po1 = (float)data2/255*5.0; | |
data1 = ATDDR2H; | |
phot1 = (float)data1/255*5.0; | |
data1 = ATDDR1H; | |
po2 = (float)data1/255*5.0; | |
data1 = ATDDR0H; | |
phot2 = (float)data1/255*5.0; | |
sprintf(line, "%-8.2f%-8.2f%-14.2f%-12.2f\r\n", po1, phot1, po2, phot2); | |
printf("%s", line); | |
SCIDRL = 0x20;//space | |
SCIDRL = 0x0D;//newline | |
} | |
byte getChar() | |
{ | |
char input; | |
while( SCISR1_RDRF == 0 ) {}; | |
input = (char) SCIDRL; | |
return input; | |
} | |
void putChar(byte output) | |
{ | |
while( SCISR1_TDRE == 0 ) {}; | |
SCIDRL = (byte) output; | |
} | |
byte getkey () { | |
int i; | |
byte scan_code = 0xff; //off code | |
PTP = 0x0E ; //0111 | |
for (i = 0; i<4; i++) { | |
if (PTT_PTT0 == 0) { | |
scan_code = i*4+0; | |
break; | |
} | |
else if (PTT_PTT1 == 0) { | |
scan_code = i*4+1; | |
break; | |
} | |
else if (PTT_PTT2 == 0) { | |
scan_code = i*4+2; | |
break; | |
} | |
else if (PTT_PTT3 == 0) { | |
scan_code = i*4+3; | |
break; | |
} | |
PTP = PTP<<1; //shift PTP to check the next row PTP *2 | |
PTP = PTP+1; //add1 1 to PTP | |
} | |
return scan_code; | |
} | |
void block_LEDS (byte scan_code) { | |
PORTA = scan_code; | |
while (getkey()==scan_code) { //block LEDs until key release | |
}; | |
} | |
void delay(word d) | |
{ | |
int constant=350; | |
int i; | |
while(d>0) | |
{ | |
i = constant; | |
while(i>0) | |
i--; | |
d--; | |
} | |
} | |
void readButtons() | |
{ | |
word delta=500/SL; //32.12 ms | |
if(PTM_PTM4 == 0) | |
{ | |
delay(10); | |
while(PTM_PTM4==0) { | |
; | |
} | |
delay(10); | |
if(delayTime>=delta) | |
delayTime-=delta; | |
else | |
delayTime=0; | |
} | |
else if(PTM_PTM5==0) | |
{ | |
delay(10); | |
while (PTM_PTM5==0) | |
{ | |
; | |
} | |
delay(10); | |
delayTime+=delta; | |
} | |
} | |
void handle_serial_port() | |
{ | |
char output; | |
char input; | |
if(SCISR1_RDRF == 0) { | |
return; | |
} | |
input=getChar(); | |
if(input>='a' && input <= 'z') | |
{ | |
output=input-32; //make it upcase | |
} | |
else if( input =='\r' || input =='\n' ) | |
{ | |
putChar('\r'); | |
output='\n'; | |
} | |
else | |
{ | |
output=input; | |
} | |
putChar(output); | |
} | |
void main(void) { | |
byte patterns[]= {0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF,0x7F,0x1F,0x0F,0x07,0x03,0x01,0x00}; | |
int i; | |
byte scan_code; | |
DDRA = 0xFF; | |
PERT = 0x0F; //pull up resistor? | |
PORTA = 0xff; | |
DDRP = 0x0f; //pull up resistor? | |
init_serial(); | |
init_sensors(); | |
while(1) | |
{ | |
for(i=0; i<SL; i++) | |
{ | |
PORTA=patterns[i]; | |
delay(delayTime); | |
readButtons(); | |
scan_code = getkey (); | |
if (scan_code != 0xff) //if keyboard press | |
block_LEDS(scan_code); | |
//serial port code | |
handle_serial_port(); | |
//read_sensors_write_xterm(); | |
sample_sensors(); | |
}//end for | |
}//end while | |
EnableInterrupts; | |
for(;;) { | |
_FEED_COP(); /* feeds the dog */ | |
} /* loop forever */ | |
/* please make sure that you never leave main */ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment