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

📄 main.c

📁 UART sample code for AT90CAN128
💻 C
字号:
/*	Sample program for Olimex AVR-H128 with ATMEGA-128 processor
 *	Send and receives back  characters on the uart.
 *	Compile with AVRStudio4
 */

#include <avr/io.h>
//#include "iom128.h"

#define	__AVR_ATMEGA128__	1
#define OSCSPEED	16000000		/* in Hz */

void PORT_Init()
{
	//PORTA = 0b00000000;		DDRA = 0b01000000;	//Relay set as output (Bit6 = 1)
	PORTB = 0b00000000;		DDRB = 0b00000000;
	PORTC = 0b00000000;		DDRC = 0b00000111;
	//PORTD = 0b11000000;		DDRD = 0b00001000;	
	PORTE = 0b00000000;		DDRE = 0b0000010;		//TX set as output (Bit1 = 1)
	PORTF = 0b00000000;		DDRF = 0b00000000;
}

void UART_Init(uint32_t Baud)
{
	unsigned int BaudRate = OSCSPEED / (16 * Baud) - 1;	/* as per pg. 173 of the user manual */

	//set BaudRate to registers UBRR1H and UBRR1L
	UBRR0H = (unsigned char) (BaudRate>>8);
	UBRR0L = (unsigned char) BaudRate;

	UCSR0B = UCSR0B | 0b00011000;	//enable Receiver and Transmitter (Bit3 = 1, Bit4 = 1)

	UCSR0C = UCSR0C | 0b10000110;	//Set frame format: disabled parity, 8 data (Bit1 = 1, Bit2 = 1), 1 stop bit (Bit3 = 0)
}

unsigned char UART_Receive()
{
	if (UCSR0A & 0b10000000)	//if there is unreaded data
		return UDR0;
	else						//no unreaded data
		return 0;
}

void UART_Transmit(unsigned char data)
{
	while (!(UCSR0A & 0b00100000));	//waiting until buffer is ready to receive

	UDR0 = data;
}

int main()
{
	static int uart_flag=0;
	static unsigned char Ch1,Ch2;
	PORT_Init();
	UART_Init(9600);
	while (1)
	{
		Ch1='0';
		UART_Transmit(Ch1);
		Ch2 = UART_Receive();
		if (Ch1==Ch2)
		{
			uart_flag=1;
		}
	}
	return 0;
}

⌨️ 快捷键说明

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