#include "USART.h"
void USART0_Init(void)
{
UBRR0H = (unsigned long)((OSC/(16.0*BAUD))-0.5)>>8;
UBRR0L = ((OSC/(16.0*BAUD))-0.5);
//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);
}
unsigned char USART0_RX(void)
{
while(0==((1<<RXC)&UCSR0A));
return UDR0;
}
void USART0_TX(unsigned char ucData)
{
while(((1<<UDRE) & UCSR0A)==0);
UDR0 = ucData;
}
void USART0_STR(const char *cString)
{
while(*cString!=0)//!=같지 않음
{
USART0_TX(*cString);
++cString;
}
}
void USART1_Init(void)
{
UBRR1H = (unsigned long)((OSC/(16.0*BAUD))-0.5)>>8;
UBRR1L = ((OSC/(16.0*BAUD))-0.5);
//UCSR1A = (0<<RXC)|(0<< TXC)|(0<< UDRE)|(0<< FE)|(0<< DOR) |(0<<UPE)|(0<< U2X)|(0<< MPCM);
UCSR1B = (0<<RXCIE)|(0<<TXCIE)|(0<<UDRIE)|(1<<RXEN)|(1<<TXEN)|(0<<UCSZ2)|(0<< RXB8)|(0<<TXB8);
UCSR1C = (0<<UMSEL1)|(0<<UMSEL0)|(1<<UPM1)|(1<<UPM0)|(1<<USBS)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UCPOL);
}
unsigned char USART1_RX(void)
{
while(((1<<RXC) & UCSR1A)==0);
return UDR1;
}
void USART1_TX(unsigned char ucData)
{
while(((1<<UDRE) & UCSR1A)==0);
UDR1=ucData;
}
void USART1_STR(const char *cString)
{
while(0!=(*cString))
{
USART1_TX(*cString);
++cString;
}
}
#ifndef __USART_H__
#define __USART_H__
#include "main.h"
#define OSC F_CPU
#define BAUD 115200
void USART0_Init(void);
unsigned char USART0_RX(void);
void USART0_Tx(unsigned char);
void USART0_STR(const char *cString);
#endif
#include "main.h"
#include "LCD.h"
#include "TC0.h"
#include "USART.h"
#include "ADC.h"
int main(void)
{
unsigned char ucData;
char caVal[] = "0000";
unsigned int uiVal;
LCD_Init();
USART1_Init();
ADC_Init();
while(1)
{
uiVal = ADC_RUN0();
caVal[0] = '0' + (uiVal/1000);
caVal[1] = '0' + ((uiVal%1000)/100);
caVal[2] = '0' + (((uiVal%1000)%100)/10);
caVal[3] = '0' + (uiVal%10);
LCD_STR(caVal);
USART1_STR(caVal);
LCD_Inst(LCD_HOME);
}
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 ADCSRA (*((volatile unsigned char*)0x7A))
#define ADCSRB (*((volatile unsigned char*)0x7B))
#define ADMUX (*((volatile unsigned char*)0x7C))
#define DIDR1 (*((volatile unsigned char*)0x7F))
#define DIDR0 (*((volatile unsigned char*)0x7E))
#define DIDR2 (*((volatile unsigned char*)0x7D))
#define ADCH (*((volatile unsigned char*)0x79))
#define ADCL (*((volatile unsigned char*)0x78))
#define ADC (*((volatile unsigned int*)0x78)) //ADC는 2바이트로 읽어 낸다. - 16비트로 보.
#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 REFS1 7
#define REFS0 6
#define ADLAR 5
#define MUX4 4
#define MUX3 3
#define MUX2 2
#define MUX1 1
#define MUX0 0
#define ACME 6
#define MUX5 3
#define ADTS2 2
#define ADTS1 1
#define ADTS0 0
#define ADEN 7
#define ADSC 6
#define ADATE 5
#define ADIF 4
#define ADIE 3
#define ADPS2 2
#define ADPS1 1
#define ADPS0 0
#define AIN1D 1
#define AIN0D 0
#define ADC7D 7
#define ADC6D 6
#define ADC5D 5
#define ADC4D 4
#define ADC3D 3
#define ADC2D 2
#define ADC1D 1
#define ADC0D 0
#define ADC15D 7
#define ADC14D 6
#define ADC13D 5
#define ADC12D 4
#define ADC11D 3
#define ADC10D 2
#define ADC9D 1
#define ADC8D 0
#define sei() __asm__ __volatile__ ("sei" ::) //ASM로 7번 비트만 1로 만들어라는 뜻
#define sleep() __asm__ __volatile__ ( "sleep" "\n\t" :: )
void Init(void);
void Port_Init(void);
void INT_Init(void);
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 "LCD.h"
#include "TC0.h"
#include "USART.h"
#include "ADC.h"
int main(void)
{
unsigned char ucData;
char caVal[] = "0000";
unsigned int uiVal;
LCD_Init();
USART1_Init();
ADC_Init();
while(1)
{
uiVal = ADC_RUN0();
caVal[0] = '0' + (uiVal/1000);
caVal[1] = '0' + ((uiVal%1000)/100);
caVal[2] = '0' + (((uiVal%1000)%100)/10);
caVal[3] = '0' + (uiVal%10);
LCD_STR(caVal);
USART1_STR(caVal);
LCD_Inst(LCD_HOME);
}
return 0;
}
TC0.h
#ifndef __TC0_H__
#define __TC0_H__
#include "main.h"
#define STOP 0
#define START 1
extern volatile unsigned int uiSec;
void TC0_Init(void);
void TC0_MSDelay(unsigned int uiDTime);
#endif