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

📄 serial.c

📁 用avr单片机接收GPS的数据的c语言程序
💻 C
字号:
//ICC-AVR application builder : 2006-5-8 20:10:07
// Target : M16
// Crystal: 8Mhz

#include <iom32v.h>
#include <macros.h>
#include <serial.h>

#define  max 1024

/* Static Variables */
int         count;
char  		rcv_buf[max];
int         UART_wp;//write data to receive buffer
int         read_pt;//read pointer: read data from receive buffer


//UART0 initialize
// desired baud rate: 4800
// actual: baud rate:4800 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{

 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = BIT(URSEL) | 0x06;
 UBRRL = 0XCF; //set baud rate lo
 UBRRH = 0X00; //set baud rate hi
 UCSRB = 0x90;//10010000
 UART_wp = 0;
 read_pt = 0;
}

#pragma interrupt_handler uart0_rx_isr:14
void uart0_rx_isr(void)
{
     //uart has received a character in UDR
	if (count<max)
	/* read the received data */
	{
	    rcv_buf[UART_wp++]=UDR; /* store received data in buffer */
	    count++;
	}
	 
	/* calculate buffer index */
	if (UART_wp==max)
	   UART_wp = 0;   // full return to the start
}

/*********************************************************************
*                   read data from buffer
description: 1 close interrupt 
             2 whether there are data to be read
             3 read data, and count--
             4 return the data
			 5 rp point to next character
**********************************************************************/
char Read_buf(void)
{
    char temp;
	while ( !count);//if no data ,wait for new data
	count--;
	temp=rcv_buf[read_pt++];
	if (read_pt==1024)
	read_pt=0;
	return ( temp);
}




⌨️ 快捷键说明

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