BASIC4MCU | 질문게시판 | atmega128 소스 코드 질문
페이지 정보
작성자 만세에 작성일2018-12-14 17:17 조회6,028회 댓글0건첨부파일
본문
이는 avr을 이용한 코드입니다. 이를 CodeVisionAVR용 코드로 바꾸고 싶습니다. 어떻게 해야 할까요. 결과값은 다음과 같이 나오도록 하고 싶은데 가능한가요..? 사진은 첨부파일에 첨부 하였습니다!
시리얼 통신을 통해 하이퍼 터미널에 나타내고 싶습니다!!
/* Baseball서버(win) */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<winsock2.h>
#include <windows.h>#pragma comment (lib,"ws2_32.lib")
#define BUFSIZE 1024
void errorhandling(char *message);int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET hServSock;
SOCKET hClntSock;
SOCKADDR_IN servAddr;
SOCKADDR_IN clntAddr;
char message[BUFSIZE];
int ClntSize;
int len;char szPort[15];
HANDLE m_handle = NULL;
DCB dcb;
OVERLAPPED osWrite, osRead;
char ch;
char ch1[3]={0,};
char recvBaseball[2]={0,};
int cnt=0;
DWORD checkCnt = 0;
/* int i;
for(i=0;i<3;i++)
{
printf("보낼 숫자 : ");
scanf("%d",&ch[i]);
printf("%d", ch[i]);
}*/
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
errorhandling("WSAStartup() error!");puts("스타트업");
hServSock=socket(PF_INET,SOCK_STREAM,0);
if(hServSock==INVALID_SOCKET)
errorhandling("socket() error");
puts("소켓생성");
memset(&servAddr,0,sizeof(servAddr));
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
servAddr.sin_port=htons(7000);if(bind(hServSock,(SOCKADDR*)&servAddr,sizeof(servAddr))==SOCKET_ERROR)
errorhandling("bind() error");puts("bind");
if(listen(hServSock,5)==SOCKET_ERROR)
errorhandling("listen() error");
puts("listen");
ClntSize=sizeof(clntAddr);
hClntSock=accept(hServSock,(SOCKADDR*)&clntAddr,&ClntSize);
if(hClntSock==INVALID_SOCKET)
errorhandling("accept() error");printf("서버 on");
wsprintf(szPort, "COM%d", 1);
m_handle = CreateFile( szPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);if(m_handle == INVALID_HANDLE_VALUE)
{
printf("핸들값이 올바르지 않습니다.\n");
return 1;
}dcb.DCBlength = sizeof(DCB);
GetCommState(m_handle, &dcb);
dcb.BaudRate = 19200;
dcb.ByteSize = 8;
dcb.Parity = 0;
dcb.StopBits = 0;SetCommState(m_handle, &dcb);
osWrite.Offset = osRead.Offset = 0;
osWrite.OffsetHigh = osRead.OffsetHigh = 0;
osWrite.hEvent = osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);while(1)
{
static int check;
int i;
len=recv(hClntSock, (char*)&ch,1, 0);
if( len == 1)
{
printf("받은 값 확인 : %d\n",ch);
//WriteFile(m_handle, (CONST void far *)&ch, sizeof(ch), NULL, &osWrite); //보낸다
//printf("Send to DK128 : %d\n", ch);
}
else
continue;
check+=len;
if(check == 3)
{
while(1)
{
for( i = 0; i<3; i++)
{
ch = 0;
ReadFile(m_handle, (char*)&ch, 1, &checkCnt, &osRead);
printf("입력 : %d \n",ch);
send(hClntSock,(char*)&ch,1,0);
}
memset(recvBaseball, 0 , sizeof(recvBaseball));
len = recv(hClntSock,recvBaseball,2,0);
printf(" recv Byte( %d )\n",len );
printf(" 스트라이크 : %d \n",recvBaseball[0]);
printf(" 볼 : %d \n",recvBaseball[1]);
if( recvBaseball[0] == 3)
{
puts("축하합니다. ~~");
exit(0);
}
}}
}
//send(hClntSock,ch,sizeof(ch),0);
CloseHandle(m_handle);
closesocket(hClntSock);
WSACleanup();
return 0;
}
void errorhandling(char *message)
{
fputs(message,stderr);
fputc('\n',stderr);
exit(1);
}=======================================================================================================
/* Baseball서버(avr) */
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#define CPU_CLOCK 16000000
#define TICKS_PER_SEC 1000
#define PRE 64#define BAUD_RATE 19200
#define BAUD_RATE_L (CPU_CLOCK/(16l*BAUD_RATE))-1
#define BAUD_RATE_H ((CPU_CLOCK/(16l*BAUD_RATE))-1) >> 8#define BS 8
#define CR 13// 비트 값 디파인
#define BITMASK0 0XFE
#define BITMASK1 0XFD
#define BITMASK2 0XFB
#define BITMASK3 0XF7
#define BITMASK4 0XEF6#define BITMASK5 0XdF
#define BITMASK6 0XbF
#define BITMASK7 0X7F// 길이 정의
#define MENU_SIZE 5
typedef unsigned int un_int;
typedef unsigned int un_char;// 전역 변수들
volatile unsigned int g_time;
// 비트 저장을 위한 전열 배열
volatile unsigned char Rotation[]={0XFE, 0XFD, 0XFB, 0XF7,0XEF,0XdF,0XbF,0X7F};
void init_port(void);
void uart_send_byte(unsigned char byte);
void uart_send_string(unsigned char *str, unsigned char len);
void uart_send_arr(unsigned char **str, unsigned int cnt);
unsigned char uart_recv_byte(void);
void lineDwon(void);void led_1(void);
void led_2(void);void setTCCR0(void);
void initTCNT0(void);
void setTIMSK(void);
void sleep(unsigned int elapsed_time);
int check( unsigned char *pBuf, unsigned char ch, unsigned int cnt );SIGNAL(SIG_OVERFLOW0)
{
g_time++;
TCNT0 = 256 - (CPU_CLOCK/TICKS_PER_SEC/64);
}
void init_port(void)
{
DDRF = 0xFF; // LED 열기
PORTF = 0XFF; // 포트에 데이터 쓰기.
DDRE = 0XFF; // FND 열기
PORTE = 0XFF; // 포트에 데이터 쓰기.
DDRC = 0x00;
}void uart_send_byte(unsigned char byte) //1 byte 전송 함수
{
while(!(UCSR1A & (1 << UDRE1))); //전송 buffer가 비워질 때까지 대기UDR1=byte;
}
void uart_send_string(unsigned char *str, unsigned char len) //문자열 전송 함수
{
int i;
for(i=0 ; i<len ; i++)
{
if(! (*(str+i)))
{
break;
}uart_send_byte(*(str+i));
}
uart_send_byte('\n');
uart_send_byte('\r');
}
void uart_send_arr(unsigned char **str, unsigned int cnt) //문자열 전송 함수
{//uart_send_byte( *(&(str[i])+j) );
int i,j;
for(i=0 ; i<cnt ; i++)
{
for(j=0; str[i][j] != '\0'; j++)
{
uart_send_byte( str[i][j]);
}
if(i == cnt-1) break;
uart_send_byte('\n');
uart_send_byte('\r'); }}
unsigned char uart_recv_byte(void)
{
while(! (UCSR1A & (1 << RXC1)));return UDR1;
}
void led_1(void)
{
unsigned char rate= 0xff;
volatile unsigned int i;
for( i=0; i<9; i++)
{
PORTF = ( rate << i );
sleep(200);}
for( i=7; i>0; --i)
{
PORTF = ( rate << i );
sleep(300);}
PORTF = 0XFF;
}
void led_2(void)
{
unsigned char rate= 0xff;
unsigned int i;
for( i=0; i<9; i++)
{
PORTF = ( rate << i );
sleep(100);}
for( i=8; i>0; i--)
{
PORTF = ( rate >> i );
sleep(100);}
PORTF = 0XFF;
}
void Dodugi(void)
{//PORTC = 0X00;
volatile unsigned char random= 0;
volatile unsigned char button = 0;
volatile unsigned char cnt = 1;
//volatile unsigned char check = 0;
PORTE = (cnt << 4) | cnt;while(1)
{
random = rand()%8;
PORTF = Rotation[random];
sleep(700);button = PINC;
if( button == Rotation[random])
{
cnt++;
lineDwon();
uart_send_byte(cnt);
PORTE = (cnt << 4) | cnt;
if(cnt == 9)
cnt = 0;
}
}
PORTF = 0XFF;
}void vaseball(void)
{
// 비트 저장을 위한 전열 배열
// volatile unsigned char Rotation[]={0XFE, 0XFD, 0XFB, 0XF7,0XEF,0XdF,0XbF,0X7F};
volatile unsigned int i;
unsigned int cnt = 0;
unsigned char button ;
unsigned char state = 0xFF;
unsigned char chTemp[3]={0,};
unsigned int Bool = 1;while(1)
{
button = PINC; // 입력 대기
if(button < 0xFF)
{
sleep(500); // 잠시 대기한다.
state = ~(state ^ button);
for(i=0; i<8; i++)
{
if( Rotation[i] == button )
{
uart_send_byte(i);
break;
}}
PORTF = state;
cnt++;}
if(cnt == 3)
{
sleep(1000);
cnt=0;
}
//state = ~(state ^ button); // 버튼값을 연산하여 눌러진 모든 값을 LED에 찍는다.
// LED 출력한다.}
//uart_send_byte('e');}
int check( unsigned char *pBuf, unsigned char ch, unsigned int cnt )
{ unsigned int i;
for(i = 0; i<=cnt ; i++) // 버퍼에 저장된 값과 현재 값을 비교
if( pBuf[i] == ch ) // 비교했을 때 같다면 처음부터 시작
return -1;
return 0;
}
void lineDwon(void)
{
uart_send_byte('\n');
uart_send_byte('\r');
}
int main(void)
{
un_char *p_szMenu[MENU_SIZE]={"= 메뉴를 선택하세요 =",
" 1.LED 가지고 놀기" ,
" 2.FND 가지고 놀기" ,
" 3.두저지게임" ,
" 메뉴 선택 : "};
UBRR1L=(unsigned char)BAUD_RATE_L; //baud rate 설정
UBRR1H=(unsigned char)BAUD_RATE_H; //no parity, 1 stop bit, 8bit 설정
UCSR1C=(0 << UPM1) | (0 << UPM0) | (0 << USBS) |(1 << UCSZ1) | (1 << UCSZ0) ;
UCSR1B=(1 << TXEN) | (1 << RXEN) | (0 << UCSZ2); //rx/tx interrupt 설정, 8bit 설정
init_port();
setTCCR0();
initTCNT0();
setTIMSK();
sei();
// uart_send_arr(p_szMenu, MENU_SIZE);
vaseball();
return 0;
}void setTCCR0(void)
{
TCCR0 = ( 1 << CS02) | ( 0 << CS01) | ( 0 << CS00);
}
void initTCNT0(void)
{
TCNT0 = 256 - (CPU_CLOCK/TICKS_PER_SEC/64);
}
void setTIMSK(void)
{
TIMSK = ( 0 << OCIE0) | ( 1 << TOIE0);
}
void sleep(unsigned int time)
{
g_time = 0;
while( time > g_time );
}
댓글 0
조회수 6,028등록된 댓글이 없습니다.