STM32F10x 使用SysTick的延時(shí)函數(shù)
http://www.rayfile.com/files/66369fee-d80c-11df-ac1d-0015c55db73d/
關(guān)鍵代碼:
UART.h:
/******** 串口收發(fā) 環(huán)形緩沖區(qū)實(shí)現(xiàn) ********
* 版本.........: 1.0
* 作者.........: 陳利棟
* 目標(biāo).........: ATmega128
* 文件名.......: UART.h
* 編譯器.......: IAR for AVR V5.5
* 創(chuàng)建時(shí)間.....: 2010.10.15
* 最后修改.....: 2010.10.15
*****************************************/
#ifndef __UART_H__
#define __UART_H__
#includeavr.h>
#include "../main.h"/* F_CPU */
#define UART_BAUD 9600//波特率
#define UART_TXBUF_LENGTH 500
#define UART_RXBUF_LENGTH 500
#if UART_TXBUF_LENGTH <= 0xff
#define UART_TXBUF_HEAD_TAIL_TYPE unsigned char
#else
#define UART_TXBUF_HEAD_TAIL_TYPE unsigned int
#endif/* UART_TXBUF_LENGTH */
#if UART_RXBUF_LENGTH <= 0xff
#define UART_RXBUF_HEAD_TAIL_TYPE unsigned char
#else
#define UART_RXBUF_HEAD_TAIL_TYPE unsigned int
#endif/* UART_TXBUF_LENGTH */
typedefstruct
{
unsignedcharda ta[UART_TXBUF_LENGTH];
UART_TXBUF_HEAD_TAIL_TYPEhead;
UART_TXBUF_HEAD_TAIL_TYPEtail;
UART_TXBUF_HEAD_TAIL_TYPEnumber;
}UART_TXBUF;
typedefstruct
{
unsignedcharda ta[UART_RXBUF_LENGTH];
UART_RXBUF_HEAD_TAIL_TYPEhead;
UART_RXBUF_HEAD_TAIL_TYPEtail;
UART_RXBUF_HEAD_TAIL_TYPEnumber;
}UART_RXBUF;
externvolatileUART_TXBUFMy_UART_TxBuf;
externvolatileUART_RXBUFMy_UART_RxBuf;
externvoidUART_Init(void);
externvoidUART_TxByte(unsignedchar_da ta);
#endif/* __UART_H__ */
* 版本.........: 1.0
* 作者.........: 陳利棟
* 目標(biāo).........: ATmega128
* 文件名.......: UART.h
* 編譯器.......: IAR for AVR V5.5
* 創(chuàng)建時(shí)間.....: 2010.10.15
* 最后修改.....: 2010.10.15
*****************************************/
#ifndef __UART_H__
#define __UART_H__
#include
#include "../main.h"/* F_CPU */
#define UART_BAUD 9600//波特率
#define UART_TXBUF_LENGTH 500
#define UART_RXBUF_LENGTH 500
#if UART_TXBUF_LENGTH <= 0xff
#define UART_TXBUF_HEAD_TAIL_TYPE unsigned char
#else
#define UART_TXBUF_HEAD_TAIL_TYPE unsigned int
#endif/* UART_TXBUF_LENGTH */
#if UART_RXBUF_LENGTH <= 0xff
#define UART_RXBUF_HEAD_TAIL_TYPE unsigned char
#else
#define UART_RXBUF_HEAD_TAIL_TYPE unsigned int
#endif/* UART_TXBUF_LENGTH */
typedefstruct
{
unsignedcharda
UART_TXBUF_HEAD_TAIL_TYPEhead;
UART_TXBUF_HEAD_TAIL_TYPEtail;
UART_TXBUF_HEAD_TAIL_TYPEnumber;
}UART_TXBUF;
typedefstruct
{
unsignedcharda
UART_RXBUF_HEAD_TAIL_TYPEhead;
UART_RXBUF_HEAD_TAIL_TYPEtail;
UART_RXBUF_HEAD_TAIL_TYPEnumber;
}UART_RXBUF;
externvolatileUART_TXBUFMy_UART_TxBuf;
externvolatileUART_RXBUFMy_UART_RxBuf;
externvoidUART_Init(void);
externvoidUART_TxByte(unsignedchar_da
#endif/* __UART_H__ */
UART.c:
/******** 串口收發(fā) 環(huán)形緩沖區(qū)實(shí)現(xiàn) ********
* 版本.........: 1.0
* 作者.........: 陳利棟
* 目標(biāo).........: ATmega128
* 文件名.......: UART.c
* 編譯器.......: IAR for AVR V5.5
* 創(chuàng)建時(shí)間.....: 2010.10.15
* 最后修改.....: 2010.10.15
*****************************************/
#include "UART.h"
volatileUART_TXBUFMy_UART_TxBuf;
volatileUART_RXBUFMy_UART_RxBuf;
voidUART_Init(void)
{
UBRR0L=(F_CPU/16L/UART_BAUD-1);
UCSR0B_RXEN0=1;// 接收使能
UCSR0B_TXEN0=1;// 發(fā)送使能
UCSR0B_RXCIE0=1;// 接收結(jié)束中斷使能
UCSR0B_TXCIE0=1;// 發(fā)送結(jié)束中斷使能
// 緩沖區(qū)初始化
My_UART_TxBuf.head=0;
My_UART_TxBuf.tail=0;
My_UART_TxBuf.number=0;
My_UART_RxBuf.head=0;
My_UART_RxBuf.tail=0;
My_UART_RxBuf.number=0;
__enable_interrupt();// 使能總中斷
}
voidUART_TxByte(unsignedchar_da ta)
{
if(UCSR0A_UDRE0==1&&My_UART_TxBuf.number==0)// 數(shù)據(jù)寄存器空 且 緩沖區(qū)沒(méi)有數(shù)據(jù)
{
UDR0=_da ta;// 直接發(fā)送
}
else
{
if(My_UART_TxBuf.number<(UART_TXBUF_LENGTH-1))// 發(fā)送緩沖區(qū)未滿
{
My_UART_TxBuf.number++;
My_UART_TxBuf.da ta[My_UART_TxBuf.tail]=_da ta;
My_UART_TxBuf.tail++;
if(My_UART_TxBuf.tail==UART_TXBUF_LENGTH)
{
My_UART_TxBuf.tail=0;
}
}
else// 發(fā)送緩沖區(qū)已滿
{
// 發(fā)送緩沖區(qū)溢出
}
}
}
#pragma vector = USART0_TXC_vect
__interruptvoidUSART0_TXC(void)
{
if(UCSR0A_UDRE0==1&&My_UART_TxBuf.number>0)// 數(shù)據(jù)寄存器空 且 緩沖區(qū)有數(shù)據(jù)
{
UDR0=My_UART_TxBuf.da ta[My_UART_TxBuf.head];
My_UART_TxBuf.number--;
My_UART_TxBuf.head++;
if(My_UART_TxBuf.head==UART_TXBUF_LENGTH)
{
My_UART_TxBuf.head=0;
}
}
}
#pragma vector = USART0_RXC_vect
__interruptvoidUSART0_RXC(void)
{
staticunsignedchartemp=0;
temp=UDR0;
if(My_UART_RxBuf.number<(UART_RXBUF_LENGTH-1))// 接收緩沖區(qū)未滿
{
My_UART_RxBuf.da ta[My_UART_RxBuf.tail]=temp;
My_UART_RxBuf.number++;
My_UART_RxBuf.tail++;
if(My_UART_RxBuf.tail==UART_RXBUF_LENGTH)
{
My_UART_RxBuf.tail=0;
}
}
else
{
// 接收緩沖區(qū)溢出
}
}
* 版本.........: 1.0
* 作者.........: 陳利棟
* 目標(biāo).........: ATmega128
* 文件名.......: UART.c
* 編譯器.......: IAR for AVR V5.5
* 創(chuàng)建時(shí)間.....: 2010.10.15
* 最后修改.....: 2010.10.15
*****************************************/
#include "UART.h"
volatileUART_TXBUFMy_UART_TxBuf;
volatileUART_RXBUFMy_UART_RxBuf;
voidUART_Init(void)
{
UBRR0L=(F_CPU/16L/UART_BAUD-1);
UCSR0B_RXEN0=1;// 接收使能
UCSR0B_TXEN0=1;// 發(fā)送使能
UCSR0B_RXCIE0=1;// 接收結(jié)束中斷使能
UCSR0B_TXCIE0=1;// 發(fā)送結(jié)束中斷使能
// 緩沖區(qū)初始化
My_UART_TxBuf.head=0;
My_UART_TxBuf.tail=0;
My_UART_TxBuf.number=0;
My_UART_RxBuf.head=0;
My_UART_RxBuf.tail=0;
My_UART_RxBuf.number=0;
__enable_interrupt();// 使能總中斷
}
voidUART_TxByte(unsignedchar_da
{
if(UCSR0A_UDRE0==1&&My_UART_TxBuf.number==0)// 數(shù)據(jù)寄存器空 且 緩沖區(qū)沒(méi)有數(shù)據(jù)
{
UDR0=_da
}
else
{
if(My_UART_TxBuf.number<(UART_TXBUF_LENGTH-1))// 發(fā)送緩沖區(qū)未滿
{
My_UART_TxBuf.number++;
My_UART_TxBuf.da
My_UART_TxBuf.tail++;
if(My_UART_TxBuf.tail==UART_TXBUF_LENGTH)
{
My_UART_TxBuf.tail=0;
}
}
else// 發(fā)送緩沖區(qū)已滿
{
// 發(fā)送緩沖區(qū)溢出
}
}
}
#pragma vector = USART0_TXC_vect
__interruptvoidUSART0_TXC(void)
{
if(UCSR0A_UDRE0==1&&My_UART_TxBuf.number>0)// 數(shù)據(jù)寄存器空 且 緩沖區(qū)有數(shù)據(jù)
{
UDR0=My_UART_TxBuf.da
My_UART_TxBuf.number--;
My_UART_TxBuf.head++;
if(My_UART_TxBuf.head==UART_TXBUF_LENGTH)
{
My_UART_TxBuf.head=0;
}
}
}
#pragma vector = USART0_RXC_vect
__interruptvoidUSART0_RXC(void)
{
staticunsignedchartemp=0;
temp=UDR0;
if(My_UART_RxBuf.number<(UART_RXBUF_LENGTH-1))// 接收緩沖區(qū)未滿
{
My_UART_RxBuf.da
My_UART_RxBuf.number++;
My_UART_RxBuf.tail++;
if(My_UART_RxBuf.tail==UART_RXBUF_LENGTH)
{
My_UART_RxBuf.tail=0;
}
}
else
{
// 接收緩沖區(qū)溢出
}
}
評(píng)論