⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 avr_uart.cc

📁 AVR 单片机程序设计用到的模拟器
💻 CC
字号:
/* $Id: AVR_UART.cc,v 1.5 2000/09/24 12:57:54 pure Exp $ */#include "AVR_UART.h"#include <iostream.h>#include <unistd.h>#include <sys/time.h>AVR_UART::AVR_UART(unsigned _mhz) : Device(0),rxthread(*this),txthread(*this){	mhz = _mhz;	ubrr = ucr = urxdr = utxdr = 0;	usr = RXC|TXC|UDRE;}AVR_UART::RXThread::RXThread(AVR_UART& _uart) : Thread(), uart(_uart){	arise();}AVR_UART::TXThread::TXThread(AVR_UART& _uart) : Thread(), uart(_uart){	arise();}AVR_UART::~AVR_UART(){}AVR_UART::RXThread::~RXThread(){}AVR_UART::TXThread::~TXThread(){}void AVR_UART::RXThread::action(){	while(1) {		cin >> uart.urxdr;		uart.usr |= RXC;		sem.wait();	}}void AVR_UART::TXThread::action(){	while(1) {		sem.wait();		cout << uart.utxdr;		cout.flush();		usleep(1000000 / (uart.mhz / ((uart.ubrr+1)*16*10)));		uart.usr |= UDRE;		do {			usleep(1000000 / (uart.mhz / ((uart.ubrr+1)*16*10)));			if (uart.usr & UDRE == 0) {				cout << uart.utxdr;				cout.flush();				uart.usr |= UDRE;			} else break; 		} while(1);		uart.usr |= TXC;	}}		Device* AVR_UART::cs(unsigned addr){	if ((addr >= UBRR) && (addr <= UDR)) {	    	return this;	}	return (Device*) 0;}unsigned AVR_UART::irq(){	if ((ucr & RXEN) && (ucr & RXCIE) && (usr & RXC)) 	    return 0x24;	if ((ucr & TXEN) && (ucr & UDREIE) && (usr & UDRE))	    return 0x26;	if ((ucr & TXEN) && (ucr & TXCIE) && (usr & TXC))	    return 0x28;	return Device::irq();}byte AVR_UART::readb(unsigned addr){	byte ret;	switch (addr) {	case UBRR:		return ubrr;	case UCR:		return ucr;	case USR:	        return usr;	case UDR:		ret = urxdr;		usr &= ~RXC;		rxthread.sem.signal();		return ret;	}	cerr << __FILE__ << ": reading from illegal address " << hex << addr << endl;	return 0;}void AVR_UART::writeb(unsigned addr, byte data){	switch (addr) {	case UBRR:		ubrr = data;		return;	case UCR:		if (data & CHR9) {					cerr << "UART simulation does not support 9-bit characters" << endl;			data &= ~CHR9;		}		ucr = data;		return;	case USR:		usr = data;		return;	case UDR:		utxdr = data;		usr &= ~(UDRE|TXC);		txthread.sem.signal();		return;	}	cerr << __FILE__ << ": writing to illegal address " << hex << addr << endl;}

⌨️ 快捷键说明

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