안녕하세요 L입니다.
지난시간의 직렬통신에 대한 실습문제 2가지를 풀어보겠습니다.
소스를 간결하게 하기위해 함수는 헤더파일에 포함되어 있습니다.
1. PC 하이퍼터미널에서 0~9를 전송하면 LED에 이진수로 출력하세요.
========================================================
#include <L.h>
int main(void)
{
unsigned char ch;
init_TC0();
UBR_UCSR();
init_LED();
while(1)
{
ch = USART_Receive();
PORTF = (ch-48)^0xFF;
uart_send_byte(ch);
sleep(10);
}
return 1;
}
========================================================
2. PC 하이퍼터미널에서 1을 입력하면 첫번째 LED ON,
2를 입력하면 두번째 LED ON, 3을 입력하면 세번째 LED ON 식으로
출력되도록 만드세요.
========================================================
#include <L.h>
int main(void)
{
unsigned char ch;
init_TC0();
UBR_UCSR();
init_LED();
while(1)
{
ch = USART_Receive();
if((ch-48)==0)
{
PORTF = 0xFF;
}
else
{
PORTF = ~(1<<ch-49);
}
uart_send_byte(ch);
sleep(10);
}
return 1;
}
========================================================
결과 : 동영상 참고.
< 헤더 파일 함수포함 >
========================================================
#define PINF *((volatile unsigned char *)0x20)
#define DDRF *((volatile unsigned char *)0x61)
#define PORTF *((volatile unsigned char *)0x62)
#define TCCR0 *((volatile unsigned char *)0x53)
#define TCNT0 *((volatile unsigned char *)0x52)
#define TIMSK *((volatile unsigned char *)0x57)
#define SREG *((volatile unsigned char *)0x5F)
#define UBRR1L *((volatile unsigned char *)0x99)
#define UBRR1H *((volatile unsigned char *)0x98)
#define UCSR1A *((volatile unsigned char *)0x9B)
#define UCSR1B *((volatile unsigned char *)0x9A)
#define UCSR1C *((volatile unsigned char *)0x9D)
#define UDR1 *((volatile unsigned char *)0x9C)
#define UDRE 5
#define UPM1 5
#define UPM0 4
#define USBS 3
#define UCSZ1 2
#define UCSZ0 1
#define TXEN 3
#define UCSZ2 2
#define RXEN 4
#define RXC 7
#define TOIE0 0
#define CPU_CLOCK 16000000 // MCU의속도
#define TICKS_PER_SEC 1000
#define PRESCALER 64
/* 직렬통신 */
#define BAUD_RATE 4800 // 통신시이용할속도
#define BAUD_RATE_L (CPU_CLOCK / (16l * BAUD_RATE)) - 1
#define BAUD_RATE_H ((CPU_CLOCK / (16l * BAUD_RATE)) -1) >> 8
// 통신속도의결과값을입력하기위해상하위비트로구분
// 16l 은16 + L 이며, 연산시값이너무커져overflow가발생하므로
// 32비트연산을위해16에Long을의미하는l을붙인다.
//========================================================
/* 통신설정 */
void UBR_UCSR(void);
void UBR_UCSR(void)
{
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);
// rx/tx interrupt 설정, 8bit 설정
UCSR1B = (1 << TXEN) | (1 << RXEN) | ( 0 << UCSZ2);
}
//========================================================
/* 1byte 전송함수 */
void uart_send_byte(unsigned char byte);
void uart_send_byte(unsigned char byte)
{
while (!(UCSR1A & (1 << UDRE))); // 전송버퍼가빌때까지기다린다
UDR1 = byte; // 문자1개를전송한다.
}
//========================================================
/* 문자열전송함수선언 */
void uart_send_string(unsigned char *str, unsigned char len);
void uart_send_string(unsigned char *str, unsigned char len)
{
for(unsigned int i = 0 ; i < len ; i++) // 문자단위로전체문자열전송
{
uart_send_byte(*(str+i)); // 문자1개를직렬포트로전송
}
uart_send_byte('\r');
uart_send_byte('\n');
}
//========================================================
/* 양방향통신 */
unsigned char USART_Receive( void );
unsigned char USART_Receive( void )
{
while(!(UCSR1A & (1<<RXC)));
return (UDR1);
}
//========================================================
/* LED 초기화 */
void init_LED(void)
{
DDRF = 0xFF;
PORTF = 0xFF;
}
//========================================================
/* delay */
volatile unsigned int g_elapsed_time;
void sleep(unsigned int elapsed_time)
{
g_elapsed_time = 0;
for(;g_elapsed_time<elapsed_time;);
}
void __vector_16 (void) __attribute__ ((signal, used, externally_visible));
void __vector_16 (void)
{
TCNT0 = 6;
++g_elapsed_time;
}
//========================================================
/* Timer/counter Control regi초기화 */
void init_TC0(void)
{
TCCR0 = 0x04;
TCNT0 = 6;
TIMSK = TIMSK | (1<<TOIE0);
/*전체인터럽트활성화*/
SREG = SREG | (1<<7);
/*전체인터럽트활성화*/
}
========================================================
궁금하신 점은 댓글로 남겨 주세요 !
오늘도 좋은 하루 되세요
USART (0) | 2016.09.30 |
---|---|
아트메가128 (DK-128 / 직렬 통신 / 양방향 직렬 통신 ) [20120605] (2) | 2016.09.30 |
아트메가128 ( PC기반 직렬통신 기초 ) [20120531] (0) | 2016.09.30 |
아트메가128 (EEPROM제어) [20120530] (0) | 2016.09.30 |
아트메가128 ( 타이머카운터 / rand활용 / 별로 엑스그리기 ) [20120524] (0) | 2016.09.29 |