atmega128모듈과 적외선 센서를 연결을 하여 적외선센서의 값을 받아온다
atmega128에는 8개의 10bit analog/digital-convertor가 있는데 그중에 한개로 받아오고
디지털값을 비교하여 일정이상신호가 나오면 물건을 감지한걸로 해서
주차장의 문을 열어준다
 
문을 열어줌과 동시에 한개의 문자를 시리얼통신(UART0)을 이용하여 전송해주면
cpp파일에서 그 값을 받아와서 이벤트처리를 해준다
텍스트창에 출력을 해주고 그것을 파일로 저장해논다
 
후에 이 파일을 소켓프로그래밍을 이용하여 전송을 하고
홈페이지에서 PHP를 이용하여 그 값으로 여러 이벤트 처리를 할 것 이다 ^^*
 
 
cpp파일
 
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFSIZE 1
void err_quit(char *msg);
int main(int argc, char* argv[])
{
 // port open
 HANDLE hComm = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE,
    0,NULL,OPEN_EXISTING,0,NULL);
 if(hComm == INVALID_HANDLE_VALUE)
  err_quit("CreateFile()");
 // get the port setting value
 DCB dcb;
 if(!GetCommState(hComm, &dcb))
  err_quit("GetCommState()");
 // change the port setting value
 dcb.BaudRate = CBR_9600;
 dcb.fParity = FALSE;
 dcb.fNull = FALSE;
 dcb.ByteSize = 8;
 dcb.Parity = NOPARITY;
 dcb.StopBits = ONESTOPBIT;
 if(!SetCommState(hComm, &dcb))
  err_quit("SetCommState()");
 // set the read and write timeout value
 COMMTIMEOUTS timeouts;
 timeouts.ReadIntervalTimeout = 0;
 timeouts.ReadTotalTimeoutMultiplier = 0;
 timeouts.ReadTotalTimeoutConstant = 0;
 timeouts.WriteTotalTimeoutMultiplier = 0;
 timeouts.WriteTotalTimeoutConstant = 0;
 if(!SetCommTimeouts(hComm, &timeouts))
  err_quit("SetCommTimeouts()");
 // argument for data communications
 char buf[BUFSIZE+1];
 DWORD BytesRead;
 int retval;
 FILE *fd;
 // data communication with client
 while(1)
 {
  // receive data
  retval = ReadFile(hComm, buf, BUFSIZE, &BytesRead, NULL);
  if(retval==0)
   err_quit("ReadFile()");
  // print data
  buf[BytesRead] = '';
  // printf("received data %sn",buf);
  if(buf[0]=='o')
  {
   printf("Gate OPEN n");
   if((fd=fopen("gate.txt","w"))==NULL)
    err_quit("fopen()");
   fprintf(fd,"OPEN");
   fclose(fd);
  }
  else if(buf[0]=='c')
  {
   printf("Gate CLOSE n");
   if((fd=fopen("gate.txt","w"))==NULL)
    err_quit("fopen()");
   fprintf(fd,"CLOSE");
   fclose(fd);
  }
  // send data
  /*retval = WriteFile(hComm, buf, BUFSIZE, &BytesWritten, NULL);
  if(retval==0)
   err_quit("WriteFile()");*/
 }
 // close port
 CloseHandle(hComm);
 return 0;
}

void err_quit(char *msg)
{
 LPVOID lpMsgBuf;
 FormatMessage(
  FORMAT_MESSAGE_ALLOCATE_BUFFER|
  FORMAT_MESSAGE_FROM_SYSTEM,
  NULL, WSAGetLastError(),
  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  (LPTSTR)&lpMsgBuf, 0, NULL);
 MessageBox(NULL, (LPCTSTR)lpMsgBuf, msg, MB_ICONERROR);
 LocalFree(lpMsgBuf);
 exit(-1);
}
 
 
 
//////////////////////////////////////////////////////////////////////////
 
 
 
atmega128
 
 
#include <mega128.h>                                                  
#include <delay.h>
#include <stdio.h>
#define ADC_VREF_TYPE 0xE0
unsigned char read_adc(unsigned char adc_input)
{
        ADMUX=adc_input|ADC_VREF_TYPE;
        ADCSRA|=0x40;
        while((ADCSRA & 0x10)==0);
        ADCSRA|=0x10;
        return ADCH;
}
void putch(unsigned char input)
{
        unsigned char frame[1];
        frame[0]=input;
        while(!(UCSR0A & 0X20));
        UDR0=frame[0];
        UCSR0A |= 0x20;
}
void main(void)
{
        unsigned int result;
        int i,dir=1;
   
        DDRF=0x00;
        DDRE=0xff;
        DDRC=0xff;
        PORTE=0x00;
        PORTC=0x00;
   
        ADMUX=ADC_VREF_TYPE;
        ADCSRA=0x87;
   
        // USART0 initialization
        // Communication Parameters: 8 Data, 1 Stop, No Parity
        // USART0 Receiver: Off
        // USART0 Transmitter: On
        // USART0 Mode: Asynchronous
        // USART0 Baud rate: 9600
        UCSR0A=0x00;
        UCSR0B=0x08;
        UCSR0C=0x06;
        UBRR0H=0x00;
        UBRR0L=0x67;
        UDR0=0x00;
        for(i=0;i<150;i++)
        {
                PORTC=0x80;
                delay_us(1500);
                PORTC=0x00;
                delay_ms(5);               
        }
           
                
        while(1)
        {
                result = read_adc(0x01);
                if(result > 120)
                {
                        if(dir==1)
                        {
                                for(i=0;i<150;i++)
                                {
                                        PORTC=0x80;
                                        delay_us(700);
                                        PORTC=0x00;
                                        delay_ms(5);               
                                }
                                dir=0;
                                putch('o');
                        } 
                        else
                        {
                                for(i=0;i<150;i++)
                                {
                                        PORTC=0x80;
                                        delay_us(1500);
                                        PORTC=0x00;
                                        delay_ms(5);               
                                }
                                dir=1;
                                putch('c');
                        }
                }                
                delay_ms(1000);
        }     
}      
Posted by 용학도리
,