#include <90s8515.h> #include #include #include "send.c" // the number of bytes of data the camera returns #define PIC_SIZE 20680 // the baud rate should be 57,600 #define BAUD_RATE 7 #define UCR_SETTING 0x18 #define SNAPSHOT_BUTTON PIND.2 #define SNAPSHOT_LED PORTD.4 #define CAPTURE_BUTTON PIND.3 #define CAPTURE_LED PORTD.5 // defines for the state of continuous capture mode #define OFF 0 #define ON 1 #define SWITCH_ON 2 #define SWITCH_OFF 3 unsigned char capture = OFF; unsigned char picture[PIC_SIZE]; unsigned char count=0; void programLoop(); // check if the capture button has been pressed every 30 ms // and set the capture variable accordingly interrupt [TIM1_COMPA] void cmpA(void){ if(CAPTURE_BUTTON == 0){ if(capture == ON) capture = SWITCH_OFF; else if(capture == OFF) capture = SWITCH_ON; } else { if(capture == SWITCH_ON){ capture = ON; CAPTURE_LED = 0; } else if(capture == SWITCH_OFF){ capture = OFF; CAPTURE_LED = 1; } } count++; } // if an error is encountered, blink some LED's void error(){ SNAPSHOT_LED = 1; CAPTURE_LED = 0; while(1){ SNAPSHOT_LED = !SNAPSHOT_LED; CAPTURE_LED = !CAPTURE_LED; delay_ms(1000); } } // sends a one character command to the camera void send_command(unsigned char cmd){ putchar(0x02); // STX putchar(cmd); // command putchar(0x00); // 0x00 putchar(0x03); // ETX } unsigned char getchar_timeout(){ #asm cli #endasm count=0; #asm sei #endasm while(!USR.7){ if(count >= 200){ CAPTURE_LED=1; SNAPSHOT_LED=1; programLoop(); return 0; } } return getchar(); } // gets an expected response from the camera void get_response(unsigned char rsp){ if(getchar_timeout() != 0x06) error(); // ACK if(getchar_timeout() != 0x02) error(); // STX if(getchar_timeout() != rsp) error(); // response if(getchar_timeout() != 0x00) error(); // 0x00 if(getchar_timeout() != 0x03) error(); // ETX } // gets the image data from the camera and buffers // it into memory unsigned char get_picture(){ unsigned int i; if(getchar_timeout() != 0x06) error(); // ACK if(getchar_timeout() != 0x02) error(); // STX if(getchar_timeout() != 'u') error(); // response if(getchar_timeout() != 164) error(); // N1 = 164 if(getchar_timeout() != 2) error(); // N2 = 2 if(getchar_timeout() != 124) error(); // N3 = 124 if(getchar_timeout() != 16) error(); // N4 = 16 for(i = 0; i < PIC_SIZE; i++){ picture[i] = getchar_timeout(); } if(getchar_timeout() != 0x03) error(); // ETX return 1; } // sends the picture array to the SCSI chip void send_picture(){ int i; for(i = 0; i < PIC_SIZE; i++){ send(picture[i]); } } void programLoop(){ while(1){ ram_check(); // if the snapshot button is pressed or continuous capture is on // then take a picture if((SNAPSHOT_BUTTON == 0) || (capture == ON)){ SNAPSHOT_LED = 0; // turn on the LED send_command('A'); // tell the camera to set the index to 0 get_response('a'); delay_ms(10); send_command('G'); // take the snap shot get_response('g'); delay_ms(500); send_command('A'); // reset the index to 0 get_response('a'); delay_ms(10); send_command('U'); // upload the picture at index 0 get_picture(); send_picture(); // send the picture to the SCSI chip SNAPSHOT_LED = 1; // turn off the LED } } } void main(void){ // set up the UART UCR = UCR_SETTING; UBRR = BAUD_RATE; // set up the two LED's DDRD.4 = 1; DDRD.5 = 1; SNAPSHOT_LED = 1; CAPTURE_LED = 1; // set up Timer1 TIMSK = 0x40; // enable the timer1 compare A interrupt TCCR1B = 0b00001010; // set to reset on match and prescale to 8 TCNT1 = 0; OCR1A = 30000; // interrupt every 30 ms (.030 s x 8 Mhz / 8 = 30000) // enable interrupts #asm sei #endasm // initialize the chip to chip send routine send_init(); programLoop(); }