마이크로프로세서 - Serial Port와 모터 동작.hwp
본 자료는 4페이지 의 미리보기를 제공합니다. 이미지를 클릭하여 주세요.
닫기
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
해당 자료는 4페이지 까지만 미리보기를 제공합니다.
4페이지 이후부터 다운로드 후 확인할 수 있습니다.

목차

1. Introduction
2. Goal
3. Strategy
4. Process
5. Conclusion

본문내용

d=whatever, 8 data bits, no parity, 1 stop bit.
dcb.BaudRate = baudRate;
dcb.fBinary = TRUE;
dcb.fParity = FALSE;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
fSucess = SetCommState(hCom, &dcb);
if (!fSucess) {
return (-3);
}
hCom1 = hCom;
return (0);
}
int closeserial() {
CloseHandle(hCom1);
return(0);
}
// sends a byte to the serial port. returns 0 on success and -1 on failure.
// 기존에 존재하던 sendword function을 1 byte만 전송하도록 수정하였다.
int sendword(unsigned char c){
unsigned long numBytes = 1;
// PurgeComm(hCom1, PURGE_RXCLEAR);
// FlushFileBuffers(hCom1);
// printf("Entering sendByte with parameter = %c\n", c);
//(void *) &c
if (!WriteFile(hCom1, (void *) &c,
1, &numBytes, NULL) || numBytes != 1) {
return -1;
}
// FlushFileBuffers(hCom1);
return 0;
}
/* sets the timeout for reading a byte
returns:
-6 on failure to set timeout
0 on success
(timeout is specified in milliseconds)
*/
int setreadtimeout(int initialTimeout){
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutMultiplier = initialTimeout;
timeouts.ReadTotalTimeoutConstant = 0;
if (!SetCommTimeouts(hCom1, &timeouts))
return (-6);
return 0;
}
// read a byte from serial port.
// returns 0 on success, -1 on failure or timeout.
// 기존에 존재하던 readword function을 1 byte만 읽어들이게 수정하였다.
int readword(unsigned char *ptr){
unsigned long numread=0;
ReadFile(hCom1, ptr, 1, &numread, NULL);
if (numread != 2)
return -1;
return 0;
}
void delay(clock_t sleep)
{
clock_t cur = clock(), el;
for(;;){ // 무한루프를 돌린다.
el = clock();
if((el - cur) > sleep)
break ;
}
} // Delay주는 함수
int main(void){
int prompt, quit;
unsigned char rot, read_rot, temp;
while(1){
quit=0;
printf("1. ClockWise\n2. CounterClockWise\n3. Quit\nPrompt: ");
scanf("%d",&prompt);
fflush(stdin);
switch(prompt){
case 1:
printf("... Send Command 'CW' to AVR ...\n... Waiting ...\n");
openserial(1,2);
rot='r';
sendword(rot);
readword(&read_rot);
if(rot!=read_rot){
printf("Fail To Work!");
break;
}
else{
printf("... Recive Sign 'CWOK' from AVR ...\n... OK\n");
delay(2000);
rot='s';
sendword(rot);
readword(&read_rot);
if(rot!=read_rot) printf("Fail to Stop!\n\n");
else printf("Moter is Stopped\n\n");
closeserial();
break;
}
case 2:
printf("... Send Command 'CCW' to AVR ...\n... Waiting ...\n");
openserial(1,2);
rot='l';
sendword(rot);
readword(&read_rot);
if(rot!=read_rot){
printf("Fail To Work!");
break;
}
else{
printf("... Recive Sign 'CCWOK' from AVR ...\n... OK\n");
delay(2000);
rot='s';
sendword(rot);
readword(&read_rot);
if(rot!=read_rot) printf("Fail to Stop!\n\n");
else printf("Moter is Stopped\n\n");
closeserial();
break;
}
case 3:
quit=1;
break;
default:
printf("Wrong Selection! Please Reselect!! \n\n");
break;
}
if(quit==1) {
return 0;
break;
}
}
}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
5. Conclusion
아래와 같은 결과를 얻을 수 있다.
55를 입력하고 7번의 Guess로 맞출 수 있었다.
이번 lab을 통하여 여러 가지를 배울 수 있었다. C를 배웠을 때 가장 기본적이고 중요한 부분이었던 if문과 for문 등을 어셈블리어로는 어떻게 구현하나 궁금했었는데, 이번 lab으로 약간은 해결된 느낌이다.
  • 가격2,300
  • 페이지수12페이지
  • 등록일2008.02.27
  • 저작시기2006.3
  • 파일형식한글(hwp)
  • 자료번호#452390
본 자료는 최근 2주간 다운받은 회원이 없습니다.
청소해
다운로드 장바구니