---------------------------------
#include <avr/io.h>
#include <util/delay.h>
#define CLR 0x01 //Clear Display 명령
#define HOME 0x02 //Return Home 명령
#define FNC 0x38 //Function Set 명령 0b00111000
//Data Length = 8bit. 행수 2행, 문자 폰트 5x8
#define CTL PORTL
#define DB PORTD
#define RXC 7
void uart0_init(void)
{
UCSR0A = 0x00;
UCSR0B = 0x18; //Enable transmitter
UCSR0C = 0X06; // No psrity, 1stop bit, 8bit data
UBRR0L = 0x67; //set baud rate lo 16Mhz baud rate 9600!!
UBRR0H = 0x00; //set baud rate hi
}
unsigned char USART_Receive(void)
{
/* Wait for data to be received */
while(!(UCSR0A &(1<<RXC)))
;
/* Get and return received data from buffer */
return UDR0;
}
void LCD_PortSetting(void)
{
DDRD = 0xFF; //data bus line
DDRL = 0xFF; //control line (0~2 제어핀 사용)
//D0: RS ,D1: R/W ,D2: EN
}
void IR_Write(unsigned char Data)
{
CTL &= 0xFC; //RS = 0, Write Enable
_delay_us(1); //Enable 까지 최소 대기 시간 40ns
CTL |= 0x04; //Enable 활성화
_delay_us(1); //
DB = Data; //Data
_delay_us(1);
CTL = 0b00000010; //Disable, Read Mode
}
void DR_Write(unsigned char Data)
{
CTL = 0x01; //RS = 1, Write Enable
_delay_us(1); //Enable 까지 최소 대기 시간 40ns
CTL |= 0x04; //Enable 활성화
_delay_us(1); //
DB = Data; //Data
_delay_us(1);
CTL = 0b00000010; //Disable, Read Mode
}
void LCD_Char(unsigned char ucChar)
{
_delay_ms(1); // 명령어 쓰기 전 일정 시간 지연, Busy Flage 확인 대용
DR_Write(ucChar);
}
void LCD_Position(unsigned char ucCol, const char ucRow)
{
IR_Write(0x80 | (ucRow + ucCol * 0x40));
}
void LCD_String(unsigned char ucLine,unsigned char ucRow, const char* ccString) // 문자열 출력 함수
{ // ucPosition 값에 따라 LCD_Position 값이 달라짐
LCD_Position(ucLine, ucRow);
while(*ccString != 0) // *ccString이 null을 가리킬때까지
{
LCD_Char(*ccString);
ccString++; // 문자열의 주소값이 자료형만큼 증가
if(*ccString <= 0x0F)
{
LCD_Position(1,0);
_delay_ms(2);
}
}
}
void LCD_drive()
{
_delay_ms(50);
IR_Write(FNC);
_delay_ms(5);
IR_Write(FNC);
_delay_us(100);
IR_Write(FNC);
_delay_ms(2);
IR_Write(0x0E); // DDD RAM내용 표시, 커서 ON, 커서 깜빡임 OFF
_delay_ms(2);
IR_Write(CLR); // Display Clear
_delay_ms(30);
IR_Write(0x06); // Entry Mode Set, 커서위치를 오른쪽으로 증가
_delay_ms(2);
}
void LCD_Usart()
{
int cnt=0;
LCD_Delete();
_delay_ms(2);
LCD_Position(0,0);
while(1)
{
while((UCSR0A&0x80)==0x80)
{
LCD_Char(UDR0);
cnt++;
if(cnt==16)
{
cnt=0;
LCD_Delete();
_delay_ms(2);
}
}
}
}
void LCD_Delete(void)
{
_delay_ms(1);
IR_Write(0x01);
}
int main(void)
{
uart0_init();
LCD_PortSetting();
LCD_drive();
_delay_ms(2);
DDRB = 0xFF;
PORTB = 0x00;
while(1)
{
LCD_Usart();
}
return 0;
}
---------------------------------------------------------------------------
#include <avr/io.h>
/////////// USART0 ///////////////////////////////////////////////
#define UDR0 (*((volatile unsigned char*)0xC6))
#define UCSR0A (*((volatile unsigned char*)0xC0))
#define UCSR0B (*((volatile unsigned char*)0xC1))
#define UCSR0C (*((volatile unsigned char*)0xC2))
#define UBRR0L (*((volatile unsigned char*)0xC4))
#define UBRR0H (*((volatile unsigned char*)0xC5))
#define TXEN 3
#define RXEN 4
#define USBS 3
#define UCSZ0 1
#define UDRE 5
#define RXC 7
#define FOSC 16000000 // Clock Speed
#define BAUD 9600 // Serial
#define MYUBRR FOSC/16l/BAUD-1
void USART_Init(unsigned int ubrr);
void USART_Transmit(unsigned char data);
unsigned char USART_Receive(void);
int main(void)
{
int i;
USART_Init(MYUBRR);
USART_Transmit("T");
DDRB = 0xFF;
PORTB = 0x00;
while(1)
{
switch(USART_Receive())
{
case '1' :
PORTB = 0xFF;
break;
case '2' :
PORTB = 0x00;
break;
case '3' :
PORTA = 0xFB;
break;
}
}
return 0;
}
void USART_Init(unsigned int ubrr)
{
/* Set baud rate */
UBRR0H = (unsigned char) (ubrr>>8);
UBRR0L = (unsigned char) ubrr;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN) | (1<<TXEN);
/* Set frame format : 8data, 1stop bit */
UCSR0C = 3<<UCSZ0;
}
void USART_Transmit(unsigned char data)
{
/* Wait for empty transmit buffer */
while(! (UCSR0A & (1<<UDRE)))
;
UDR0 = data;
}
unsigned char USART_Receive(void)
{
/* Wait for data to be received */
while(!(UCSR0A &(1<<RXC)))
;
/* Get and return received data from buffer */
return UDR0;
}
'Study > Embedded' 카테고리의 다른 글
아두이노 LCD (0) | 2017.06.01 |
---|---|
아두이노 기초 (0) | 2017.05.25 |
7/21 Atmega128 포트/ win32 api (0) | 2016.07.21 |
7/8 LCD제어2 (0) | 2016.07.08 |
7/5 LCD 제어 (0) | 2016.07.05 |