#include 90s8535.h
#include stdio.h
#include lcd.h
#include string.h
#define MAX_IN_LENGTH 16
#define MAX_OUT_LENGTH 32
#define timer_control 0x04
#define ADC_control 0xC5 //ADEN=1, ADSC=0, ADFR=0, ADIF=0,
//ADIE=0 (No interrupts)
//Prescale = 101: 4MHZ/32 = 125KHz
#define OPEN 1
#define CLOSED 0
char door_state = CLOSED;
char i=0, j=0;
char new_key[16];
char output[16];
char textout[MAX_OUT_LENGTH];
char lcd_buffer[17];
char update=1;
char hold;
int people;
char debounce;
int door;
int voltage; //input voltage
int voltage_temp;
char light_inten;
float temp;
#define reload1 65400
void init(void);
void clear_input(void);
//**********************************************************
//this interrupt is triggered when a character is received into the serial port input buffer
interrupt[UART_RXC] void keyboard_interrupt(void) {
//we only want to read the whole input, which we know to be 16 bytes long
//so we loop until we have a full buffer
new_key[i] = UDR;
if(UDR == 0)
new_key[i] = 0;
i++;
//once we have a full buffer, we try to match the input to a known command
if(i==16){
new_key[16] = 0;
i=0;
//test to see if everything is working normally
if(!strncmpf(new_key, "status", 6)){
strcpyf(textout, "System working.");
strcpyf(output, "sys up");
clear_input();
}
//turn on and off LEDs on the Development board
else if((!strncmpf(new_key, "led", 3)) && (new_key[3] > 47) && (new_key[3] &60; 54)){
PORTB = PORTB ^ (0x01 leftshift (new_key[3] - 48));
strcpyf(output, "LedDone");
strcpyf(textout, "LedDone");
clear_input();
}
//Turn on the Red Led on the SitePlayer board
else if(!strncmpf(new_key, "redledon", 8)){
strcpyf(output, "RedLED1");
strcpyf(output, "Red Led is On");
printf("%c",0x90);
printf("%c%c",0x14,0xff);
printf("%c",0x00);
clear_input();
}
//Turn on the Red Led off the SitePlayer board
else if(!strncmpf(new_key, "redledoff", 9)){
strcpyf(output, "RedLED0");
strcpyf(textout, "Red Led is Off");
printf("%c",0x90);
printf("%c%c",0x14,0xff);
printf("%c",0x01);
clear_input();
}
else if(!strncmpf(new_key, "greenledon", 10)){
strcpyf(output, "GrnLED1");
strcpyf(textout, "Green Led is On");
printf("%c",0x90);
printf("%c%c",0x15,0xff);
printf("%c",0x00);
clear_input();
}
else if(!strncmpf(new_key, "greenledoff", 11)){
strcpyf(output, "GrnLED0");
strcpyf(textout, "Green Led is Off");
printf("%c",0x90);
printf("%c%c",0x15,0xff);
printf("%c",0x01);
clear_input();
}
//Retrieve the current temperature reading
else if(!strncmpf(new_key, "gettemp", 7)){
sprintf(output, "Temp: %-i",(int)temp);
sprintf(textout, "Temperature: %-i degF",(int)temp);
clear_input();
}
//Check to see how many times the IR beam has been tripped
else if(!strncmpf(new_key, "door", 4)){
sprintf(output, "Ppl: %-i",people);
sprintf(textout, "People counter = %-i",people);
clear_input();
}
//Check to see if it is dark in the room
else if(!strncmpf(new_key, "lights", 6)){
if (light_inten > 75) {
sprintf(output, "LghtsOn");
sprintf(textout, "Lights are On");
} else {
sprintf(output, "LghtsOff");
sprintf(textout, "Lights are Off");
} clear_input();
}
//Turn Fan on, for use with the thermometer
else if(!strncmpf(new_key, "fanon", 5)){
strcpyf(output, "Fan On");
strcpyf(textout, "Fan is now On.");
PORTB = PORTB | 0x40;
clear_input();
}
else if(!strncmpf(new_key, "fanoff", 6)){
strcpyf(output, "Fan Off");
strcpyf(textout, "Fan is now Off.");
PORTB = PORTB & 0xBF;
clear_input();
}
else if(new_key[0] == 0) {}
//if nothing else, bad syntax
else{
strcpyf(output, "badcmd!");
strcpyf(textout, "Syntax Error");
clear_input();
}
//Send result to the SitePlayer, one byte at a time, to be safe
if(update){
for(i=0;i &60; MAX_OUT_LENGTH;i++){
printf("%c",0x80);
printf("%c",(MAX_IN_LENGTH+i));
printf("%c",textout[i]);
}
i=0;
//Update the command displayed on the local LCD
lcd_clear();
lcd_gotoxy(0,0);
sprintf(lcd_buffer,"%s",output);
lcd_puts(lcd_buffer);
update = 0;
}
}
}
interrupt[TIM1_OVF] void quartersec(void) {
//Blink a light to know we're alive
PORTB = (PORTB ^ 0x80);
TCNT1 = reload1;
//Constantly request latest value of "Input" variable on the SitePlayer
printf("%c",0xCf);
printf("%c",0x00);
//examine door setting
ADMUX = 1;
ADCSR = ADC_control;
while ((ADCSR & 0x40) == 0x40) {}
ADMUX = 1;
ADCSR = ADC_control;
while ((ADCSR & 0x40) == 0x40) {}
door = ADCL;
hold = ADCH;
if (door_state == OPEN) {
if (door &60; 2) { // door is still open...
if (debounce++ == 30) {
people++;
PORTB = (PORTB ^ 0x01);
}
}
else {
door_state = CLOSED;
debounce = 0;
}
} else if (door &60; 2) door_state=OPEN; // door was last closed, check if it's open now
//always measure temp
ADMUX = 0;
ADCSR = ADC_control;
while ((ADCSR & 0x40) == 0x40) {}
ADMUX = 0;
ADCSR = ADC_control;
while ((ADCSR & 0x40) == 0x40) {}
voltage_temp = ADCL;
voltage = ADCH;
voltage = voltage_temp | voltage shift left 8;
// print out voltage
temp = voltage / 10.0;
//always measure light intensity
ADMUX = 5;
ADCSR = ADC_control;
while ((ADCSR & 0x40) == 0x40) {}
ADMUX = 5;
ADCSR = ADC_control;
while ((ADCSR & 0x40) == 0x40) {}
light_inten = ADCL;
hold = ADCH;
}
//This function will blank out the Input variable on the Siteplayer, so we don't keep
//running the same command repeatedly
void clear_input(void){
update = 1;
for(i=0;i &60; MAX_IN_LENGTH;i++){
printf("%c",0x80);
printf("%c",i);
printf("%c",0);
}
i = 0;
}
void init(void)
{
DDRB = 0xff; //setup LEDs
PORTB = 0xff;
DDRD = 0xff; //setup Serial port
TIMSK = 0x04;
TCCR1B = 0x05;
TCNT1 = reload1;
//more serial port setup
UCR = 0x90 + 0x08 ;
UBRR = 25 ;
//LCD on port C
#asm
.equ __lcd_port=0x15
#endasm
lcd_init(16);
lcd_clear();
lcd_gotoxy(0,0);
sprintf(lcd_buffer,"init");
lcd_puts(lcd_buffer);
ADMUX = 1;
ADCSR = ADC_control;
// disable Analog comparator
ACSR = 0x80;
people = 0;
// set IP on SitePlayer
printf("%c",0x93);
printf("%c%c",0xE6,0xFF);
printf("%c%c%c%c",128,84,235,244);
#asm
sei
#endasm
}
void main(void)
{
init();
while(1) {}
}
|