set=1 clear=0
수신 버퍼안에 읽지않은 데이터가 있으면7번Bit(RXCn)는 1이 된다.
0: 수신데이터X
1: 수신데이터O
----------------------------------------------------------------------------------------------------------------------
ADC:아날로그 디지털 컴버트-아날로그를 디지털화(녹음)
DAC:디지털 아날로그 컴버트-디지털을 아날로그화(재생)
#include "LCD.h"
void LCD_Init()
{
DDRC = (1<<RS)|(1<<RW)|(1<<EN);
DDRA = 0XFF;
CTL = (0<<RS)|(0<<RW)|(0<<EN);
BUS = 0X00;
LCD_Inst(LCD_FUNC);
LCD_Inst(LCD_DSP);
LCD_Inst(LCD_ENT);
LCD_Inst(LCD_CUR);
LCD_Inst(LCD_CLR);
LCD_Inst(LCD_HOME);
}
void LCD_Inst(unsigned char ucInst)
{
volatile unsigned int uiCnt;
CTL = CTL&(~(1<<RS));
CTL = CTL&(~(1<<RW));
CTL = CTL&(~(1<<EN));
BUS = ucInst;
for(uiCnt = 0; 30000>uiCnt; ++uiCnt);
for(uiCnt = 0; 30000>uiCnt; ++uiCnt);
CTL = CTL|(1<<EN);
for(uiCnt = 0; 30000>uiCnt; ++uiCnt);
CTL = CTL&(~(1<<EN));
for(uiCnt = 0; 30000>uiCnt; ++uiCnt);
for(uiCnt = 0; 30000>uiCnt; ++uiCnt);
}
void LCD_Data(unsigned char ucData)
{
volatile unsigned int uiCnt;
CTL = CTL|(1<<RS);
CTL = CTL&(~(1<<RW));
CTL = CTL&(~(1<<EN));
BUS = ucData;
for(uiCnt = 0; 3000>uiCnt; ++uiCnt);
for(uiCnt = 0; 3000>uiCnt; ++uiCnt);
CTL = CTL|(1<<EN);
for(uiCnt = 0; 3000>uiCnt; ++uiCnt);
CTL = CTL&(~(1<<EN));
for(uiCnt = 0; 3000>uiCnt; ++uiCnt);
for(uiCnt = 0; 3000>uiCnt; ++uiCnt);
}
void LCD_STR(const char *cString)
{
while(0!=*cString)
{
LCD_Data(*cString);
++cString;
}
}
#ifndef __LCD_H__
#define __LCD_H__
#include "main.h"
#define RS 4
#define RW 5
#define EN 6
#define BUS PORTA
#define CTL PORTC
#define LCD_CLR 0x01
#define LCD_HOME 0x02
#define LCD_ENT 0x06 //S:0Shift OFF, I/D:1 Increase Mode
#define LCD_DSP 0x0F //D:1 Display On C:1 Cursor on B:1 Blink On
#define LCD_CUR 0x14//S/C:0 Shift Cursor OFF R/L:1
#define LCD_FUNC 0x38 //DL:1 Data length 8bit N:1 2Line F:0 Font 5X8
void LCD_Init();
void LCD_Inst(unsigned char ucInst);
void LCD_Data(unsigned char ucData);
void LCD_STR(const char *cString);
#endif
#include "main.h"
#include "LCD.h"
#include "TC0.h"
#include "USART.h"
int main(void)
{
char caTime[] = "00:00:00";
unsigned int uiTime;
LCD_Init();
TC0_Init();
sei();
LCD_STR(caTime);
uiTime = uiSec = 0; // 대입 연산자가 2개일 경우 오른쪽부터 된다. 0에서 uiSec 에서 uiTime
while(1)
{
caTime[7]='0'+((uiSec%60)%10); // '3'
caTime[6]='0'+((uiSec%60)/10); // '3'
caTime[4]='0'+((uiSec/60)%60)%10; // '3'
caTime[3]='0'+((uiSec/60)%60)/10; // '3'
caTime[1]='0'+((uiSec/60)/60)%10; // '3'
caTime[0]='0'+((uiSec/60)/60)/10; // '3'
LCD_Inst(LCD_HOME); //앞으로 돌아오게한다.
LCD_STR(caTime); //글자를 찍는다.
while(uiTime == uiSec);
uiTime = uiSec;
//TC0_MSDelay(1000); //오차가 생기면 1000을 조금씩 줄인다.
//++uiSec;
//LCD_Data('A'); - A가 계속 찍힌다.
//TC0_MSDelay(1000);
}
return 0;
}
#ifndef __MAIN_H__
#define __MAIN_H__
#define PORTA (*((volatile unsigned char*)0x22))
#define DDRA (*((volatile unsigned char*)0x21))
#define PINA (*((volatile unsigned char*)0x20))
#define PORTB (*((volatile unsigned char*)0x25))
#define DDRB (*((volatile unsigned char*)0x24))
#define PINB (*((volatile unsigned char*)0x23))
#define PORTC (*((volatile unsigned char*)0x28))
#define DDRC (*((volatile unsigned char*)0x27))
#define PINC (*((volatile unsigned char*)0x26))
#define EICRA (*((volatile unsigned char*)0x69))
#define EICRB (*((volatile unsigned char*)0x6A))
#define EIMSK (*((volatile unsigned char*)0x3D))
#define SREG (*((volatile unsigned char*)0x5F))
#define TCCR0A (*((volatile unsigned char*)0x44))
#define TCCR0B (*((volatile unsigned char*)0x45))
#define TCNT0 (*((volatile unsigned char*)0x46))
#define OCR0A (*((volatile unsigned char*)0x47))
#define OCR0B (*((volatile unsigned char*)0x48))
#define TIMSK0 (*((volatile unsigned char*)0x6E))
#define TIFR0 (*((volatile unsigned char*)0x35))
#define UCSR0A (*((volatile unsigned char*)0xC0))
#define UCSR0B (*((volatile unsigned char*)0xC1))
#define UCSR0C (*((volatile unsigned char*)0xC2))
#define UBRR0L (*((volatile unsigned char*)0xC5))
#define UBRR0H (*((volatile unsigned char*)0xC4))
#define UDR0 (*((volatile unsigned char*)0xC6))
#define UDR1 (*((volatile unsigned char*)0xCE))
#define UDR2 (*((volatile unsigned char*)0xC6))
#define UDR3 (*((volatile unsigned char*)0x136))
#define UCSR1A (*((volatile unsigned char*)0xC8))
#define UCSR1B (*((volatile unsigned char*)0xC9))
#define UCSR1C (*((volatile unsigned char*)0xCA))
#define UBRR1L (*((volatile unsigned char*)0xCC))
#define UBRR1H (*((volatile unsigned char*)0xCD))
#define UCSR2A (*((volatile unsigned char*)0xD0))
#define UCSR2B (*((volatile unsigned char*)0xD1))
#define UCSR2C (*((volatile unsigned char*)0xD2))
#define UBRR2L (*((volatile unsigned char*)0xD4))
#define UBRR2H (*((volatile unsigned char*)0xD5))
#define UCSR3A (*((volatile unsigned char*)0x130))
#define UCSR3B (*((volatile unsigned char*)0x131))
#define UCSR3C (*((volatile unsigned char*)0x132))
#define UBRR3L (*((volatile unsigned char*)0x134))
#define UBRR3H (*((volatile unsigned char*)0x135))
#define INT7 7
#define INT6 6
#define INT5 5
#define INT4 4
#define INT3 3
#define INT2 2
#define INT1 1
#define INT0 0
#define ISC7 6
#define ISC6 4
#define ISC5 2
#define ISC4 0
#define ISC3 6
#define ISC2 4
#define ISC1 2
#define ISC0 0
#define RXC 7
#define TXC 6
#define UDRE 5
#define FE 4
#define DOR 3
#define UPE 2
#define U2X 1
#define MPCM 0
#define RXCIE 7
#define TXCIE 6
#define UDRIE 5
#define RXEN 4
#define TXEN 3
#define UCSZ2 2
#define RXB8 1
#define TXB8 0
#define UMSEL1 7
#define UMSEL0 6
#define UPM1 5
#define UPM0 4
#define USBS 3
#define UCSZ1 2
#define UCSZ0 1
#define UCPOL 0
#define COM0A1 7
#define COM0A0 6
#define COM0B1 5
#define COM0B0 4
#define WGM01 1
#define WGM00 0
#define FOC0A 7
#define FOC0B 6
#define WGM02 3
#define CS02 2
#define CS01 1
#define CS00 0
#define OCIE0B 2
#define OCIE0A 1
#define TOIE0 0
#define sei() __asm__ __volatile__ ("sei" ::) //ASM로 7번 비트만 1로 만들어라는 뜻
#define sleep() __asm__ __volatile__ ( "sleep" "\n\t" :: )
void __vector_1 (void) __attribute__((signal, used, externally_visible));
void __vector_2 (void) __attribute__((signal, used, externally_visible));
void __vector_3 (void) __attribute__((signal, used, externally_visible));
void __vector_21 (void) __attribute__((signal, used, externally_visible));
#endif
#include "TC0.h"
static volatile unsigned int uiMSec; //static을 쓰면 이 파일 내부에서만 사용할수있다.
volatile unsigned int uiSec;
volatile unsigned int uiSState;
TC0_Init()
{
TCCR0A = (0<<COM0A1)|(0<<COM0A0)|(0<<COM0B1)|(0<<COM0B0)|(0<<WGM01)|(0<<WGM00);
TCCR0B = (0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(0<<CS02)|(0<<CS01)|(0<<CS00);
//TCNT0 = 0X00;
OCR0A = 250;
//OCR0B = 0X00;
TIMSK0 = (0<<OCIE0B)|(1<<OCIE0A)|(0<<TOIE0);
//TIFR0 = 0X00;
EICRA = (3 << ISC1)|(3 << ISC0)|(3 << ISC2); // ISC0 == INT0
EIMSK = (1 << INT1)|(1 << INT0)|(1 << INT2); //EIMSK의 비트 1값들어감 임베디드에서 자주쓰는것 EIMSK = 1 동일
uiSState = STOP;
}
void __vector_1(void)
{
volatile unsigned int uiCnt;
if(STOP == uiSState)
{
TCCR0B = (0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(0<<CS02)|(1<<CS01)|(1<<CS00);
uiSState = START;
}
else
{
TCCR0B = (0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(0<<CS02)|(0<<CS01)|(0<<CS00);
uiSState = STOP;
}
for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
}
void __vector_2(void)
{
volatile unsigned int uiCnt;
TCCR0B = (0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(0<<CS02)|(0<<CS01)|(0<<CS00);
uiSec = 0;
uiSState = STOP;
for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
}
void __vector_3(void)
{
volatile unsigned int uiCnt;
uiSec = uiSec+60;
for(uiCnt = 0; 30000>uiCnt ; ++uiCnt);
}
void __vector_21 (void)
{
++uiMSec; //0.001초가 지나고 있다.
if(1000<=uiMSec)
{
uiMSec = 0;
++uiSec ;
}
}
TC0_MSDelay(unsigned int uiDTime )
{
if(uiDTime > 1000)
{
uiDTime = 1000;
}
uiMSec = 0;
while(uiMSec < uiDTime);
}
#ifndef __TC0_H__
#include "main.h"
#define __TC0_H__
#define STOP 0
#define START 1
extern volatile unsigned int uiSec;
void TC0_Init();
void TC0_MSDelay(unsigned int uiDTime);
#endif
USART.c
#include "USART.h"
void USART0_Init()
{
UBRR0H = ((OSC/(16*BAUD))-1)>>8;
UBRR0L = ((OSC/(16*BAUD))-1);
UCSR0A = (0<<RXC)|(0<<TXC)|(0<<UDRE)|(0<<FE)|(0<<DOR)|(0<<UPE)|(0<<U2X)|(0<<MPCM);
UCSR0B = (0<<RXCIE)|(0<<TXCIE)|(0<<UDRIE)|(1<<RXEN)|(1<<TXEN)|(0<<UCSZ2)|(0<< RXB8)|(0<<TXB8);
UCSR0C = (0<<UMSEL1)|(0<<UMSEL0)|(1<<UPM1)|(1<<UPM0)|(1<<USBS)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UCPOL);
}
USART.h
#ifndef __USART_H__
#define __USART_H__
#include "main.h"
#define OSC F_CPU
#define BAUD 115200
#endif