nxuart.c

来自「class to work with UART port on ATmega12」· C语言 代码 · 共 65 行

C
65
字号
#include "nxUART.h"

#ifdef NXUART_USE_V20090211

unsigned char * pUART0RecBuf;
unsigned int UART0RecCount;
unsigned int UART0RecIndex;
bool * pUART0DataReady;

//extern bool isReceive, isCreated, isRecStarted;
//extern unsigned int temp_baud;

#ifdef __ATmega128__
#pragma vector = USART0_RXC_vect
__interrupt void USART0RXC_ISR(void)
{
		unsigned char udr0 = UDR0;
            //isReceive = true;
		*(pUART0RecBuf + (UART0RecIndex++)) = udr0;
		if (UART0RecIndex >= UART0RecCount)
		{
			UCSR0B &= ~((1<<RXCIE0) | (1<<RXEN0));	
			*pUART0DataReady = true;
		}
}
#endif


nxUARTPort::nxUARTPort(unsigned char UARTNum, unsigned int UARTSpeed)
{
unsigned long int tmp_baud = int(float(CPU_FREQ) / float(16.0 * UARTSpeed)) - 1;
      //temp_baud = tmp_baud;
      DataReady = false;
#ifdef __ATmega128__
	switch (UARTNum)
	{
		case 0:
		{
			UBRR0H = (tmp_baud >> 8) & 0xFF;
			UBRR0L = tmp_baud & 0xFF;
			UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
                  PortNum = 0;
                  //isCreated = true;
			break;
	  }
	}
#endif
}

void nxUARTPort::Init()
{
	
}

void nxUARTPort::TransmitData(unsigned char * pBuf, unsigned int Count)
{
#ifdef __ATmega128__
	switch (PortNum)
	{
		case 0:
		{
			UCSR0B |= (1<<TXEN0);
			for (unsigned char i = 0; i < Count; ++i)
			{
				while(!(UCSR0A & (1 << UDRE0))) //铈桎噱

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?