[an error occurred while processing this directive]
Вот мой модуль UART.
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено _Bill 25 апреля 2006 г. 14:20
В ответ на: тренеруюсь отправлено винтик 25 апреля 2006 г. 14:04


#define BAUD 9600 // Default baud rate value

#include "uart.h"

/* Constant definitions */

#define TX_BUSY (1<<0) // Tx busy flag
#define CMND_RQ (1<<1) // Command request
#define INFO_RQ (1<<2) // Device info request

//
// Variables
//
static char UARTflags; // Various flags
static char TxCount; // Bytes to transmit counter
static char *TxPtr; // A pointer to buffer
//static char RxState; // Reception state
//
// ** InitUART -- the routine initiates the UART module
//
void InitUART(void)
{
unsigned br;

UART_DDR |= DIR_BIT; // Initiate direction bit
UART_PORT &= ~DIR_BIT; // set reception mode
UARTflags = 0; // Reset flags

br = BaudRate;
if (br == 0xFFFF) // Baud rate is not set
br = BaudRate = BAUD; // set default value
br = FOSC/16/br - 1;
UBRRL = br & 0xFF; // Set the Baud Rate register
UBRRH = br >> 8; //
UCSRC = (1< UCSRB |= RXEN_BIT | TXEN_BIT; // Enable UART transiver
UCSRA |= RXC_BIT; // Reset pending interrupt request
UCSRB |= RXCIE_BIT; // Enable UART receiver interrupt
}
//
// ** TxData -- the function initiates UART data transmission.
// Returns 1 in case of success. Otherwise returns 0.
//
char TxData(char *buf, char cnt)
{
char _cnt;

if (UARTflags & TX_BUSY) // Previous transmission is not completed yet
return 0; //
UART_PORT |= DIR_BIT; // Switch to transmission mode
TxPtr = buf; // Set a buffer pointer
TxCount = cnt; // and counter
_cnt = 20;
do {
SleepMCU(255);
}
while (--_cnt);
UARTflags |= TX_BUSY; // Set transmission is not completed flag
UCSRB |= UDRIE_BIT; // Enable UART buffer empty interrupt
return 1; // OK!
}
//
// ** ReceiveByte -- the function gets one byte from UART
//
char ReceiveByte(void)
{
while (!(IntFlags & UART_BIT)); // Wait for a data
IntFlags &= ~UART_BIT; // Reset rhe flag
return RxByte;
}

//
// ** TxByte -- the function sends one byte to UART
//
void TxByte(char _byte)
{
while (!(UCSRA & UDRE_BIT)); // Wait for the transmitter buffer is empty
UDR = _byte; // Transmit the data
}

//
// **** UART interrupt handlers ***
//
// UART reciever handler
//
#pragma vector = USART_RXC_vect
__interrupt void RX_Int(void)
{
RxByte = UDR; // Get the byte
if (RxByte == '?') // Info request is received
{ //
UARTflags |= INFO_RQ; // Set the info request flag
UARTflags &= ~CMND_RQ; // Sure the only one flag is set
return; // Wait for device address
} //
if (RxByte == '!') // Command is received
{ //
UARTflags |= CMND_RQ; // Set the comand request flag
UARTflags &= ~INFO_RQ; // Sure the only one flag is set
return; // Wait for device address
} //
if ((UARTflags & CMND_RQ) && RxByte == Address)
HostLink = lnCmnd;
else if ((UARTflags & INFO_RQ) && (RxByte==0 || RxByte==Address))
HostLink = lnInfo;
else {
HostLink = 0;
return;
}
IntFlags |= UART_BIT; // Set the flag if action is needed
}
//
// UART transmitter handlers
//
#pragma vector = USART_TXC_vect
__interrupt void TXC_Int(void)
{
UART_PORT &= ~DIR_BIT; // Return to reception mode
UCSRB &= ~TXCIE_BIT; // Disable UART transmitter completed interrupt
UARTflags &= ~TX_BUSY; // Reset the flag
}

#pragma vector = USART_UDRE_vect
__interrupt void UDRE_Int(void)
{
UDR = *TxPtr++; // Transmit byte
if (!--TxCount) // Adjust counter and if it's zero
{ //
UCSRB |= TXCIE_BIT; // enable UART transmitter completed interrupt
UCSRB &= ~UDRIE_BIT; // and disable UART buffer empty interrupt
}
}


Составить ответ  |||  Конференция  |||  Архив

Ответы


Отправка ответа

Имя (обязательно): 
Пароль: 
E-mail: 
NoIX ключ Запомнить

Тема (обязательно):
Сообщение:

Ссылка на URL: 
Название ссылки: 

URL изображения: 


Rambler's Top100 Рейтинг@Mail.ru
Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание

E-mail: info@telesys.ru