Вы находитесь на странице: 1из 4

#include <avr/io.

h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
#define F_CPU 1000000UL
#define USART_BAUDRATE 4800UL
#define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
//define max buffer size
#define BUF_SIZE 20
//type definition of buffer structure
#define LCDsCrib PORTB
#define DataDir_LCDsCrib DDRB
#define LCDsControl PORTD
#define DataDir_LCDsControl DDRD
#define LightSwitch 5
#define ReadWrite 7
#define BiPolarMood 2
void Check_IF_LCD_isBusy(void);
void Peek_A_Boo(void);
void Send_A_Command(unsigned char command);
void Send_A_Character(unsigned char character);
void Send_A_String(char *StringOfCharacters);
typedef struct{
//Array of chars
uint8_t buffer[BUF_SIZE];
//array element index
uint8_t index;
}u8buf;
//declare buffer
u8buf buf;
uint8_t byte_recibido;
//initialize buffer
void BufferInit(u8buf *buf)
{
//set index to start of buffer
buf->index=0;
}
//write to buffer routine
uint8_t BufferWrite(u8buf *buf, uint8_t u8data)
{
if (buf->index<BUF_SIZE)
{
buf->buffer[buf->index] = u8data;
//increment buffer index
buf->index++;
return 0;
}
else return 1;
}
uint8_t BufferRead(u8buf *buf, volatile uint8_t *u8data)
{
if(buf->index>0)
{
buf->index--;
*u8data=buf->buffer[buf->index];
return 0;
}
else return 1;
}
void USART0Init(void)
{
// Set baud rate
UBRR0H = (uint8_t)(UBRR_VALUE>>8);
UBRR0L = (uint8_t)UBRR_VALUE;
// Set frame format to 8 data bits, no parity, 1 stop bit
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
//enable reception and RC complete interrupt
UCSR0B |= (1<<RXEN0)|(1<<RXCIE0);
}
//RX Complete interrupt service routine
ISR(USART_RX_vect)
{
uint8_t u8temp;
u8temp=UDR0;
//check if period char or end of buffer
if ((BufferWrite(&buf, u8temp)==1)||(u8temp=='.'))
{
//disable reception and RX Complete interrupt
UCSR0B &= ~((1<<RXEN0)|(1<<RXCIE0));
//enable transmission and UDR0 empty interrupt
UCSR0B |= (1<<TXEN0)|(1<<UDRIE0);
}
}
//UDR0 Empty interrupt service routine
ISR(USART_UDRE_vect)
{
//if index is not at start of buffer
if (BufferRead(&buf, &UDR0)==1)
{
//start over
//reset buffer
BufferInit(&buf);
//disable transmission and UDR0 empty interrupt
UCSR0B &= ~((1<<TXEN0)|(1<<UDRIE0));
//enable reception and RC complete interrupt
UCSR0B |= (1<<RXEN0)|(1<<RXCIE0);
}
}
int main (void)
{
DataDir_LCDsControl |= 1<<LightSwitch | 1<<ReadWrite | 1<<BiPolarMood;
_delay_ms(15);
Send_A_Command(0x01); //Clear Screen 0x01 = 00000001
_delay_ms(2);
Send_A_Command(0x38);
_delay_us(50);
Send_A_Command(0b00001110);
_delay_us(50);
for (int i = 0; i != 5; i++)
{
Send_A_Command(0b00010100);
}
//Init buffer
BufferInit(&buf);
//set sleep mode
set_sleep_mode(SLEEP_MODE_IDLE);
//Initialize USART0
USART0Init();
//enable global interrupts
sei();
while(1)
{
byte_recibido = BufferRead(&buf,&UDR0);
Send_A_Command(byte_recibido);
//put MCU to sleep
sleep_mode();
}
}
void Check_IF_LCD_isBusy()
{
DataDir_LCDsCrib = 0;
LCDsControl |= 1<<ReadWrite;
LCDsControl &= ~1<<BiPolarMood;
while (LCDsCrib >= 0x80)
{
Peek_A_Boo();
}
DataDir_LCDsCrib = 0xFF; //0xFF means 0b11111111
}
void Peek_A_Boo()
{
LCDsControl |= 1<<LightSwitch;
asm volatile ("nop");
asm volatile ("nop");
LCDsControl &= ~1<<LightSwitch;
}
void Send_A_Command(unsigned char command)
{
Check_IF_LCD_isBusy();
LCDsCrib = command;
LCDsControl &= ~ ((1<<ReadWrite)|(1<<BiPolarMood));
Peek_A_Boo();
LCDsCrib = 0;
}
void Send_A_Character(unsigned char character)
{
Check_IF_LCD_isBusy();
LCDsCrib = character;
LCDsControl &= ~ (1<<ReadWrite);
LCDsControl |= 1<<BiPolarMood;
Peek_A_Boo();
LCDsCrib = 0;
}
void Send_A_String(char *StringOfCharacters)
{
while(*StringOfCharacters > 0)
{
Send_A_Character(*StringOfCharacters++);
}
}