?? main.c
字號:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: main.c
** Last modified Date: 2004-09-16
** Last Version: 1.0
** Descriptions: The main() function example template
**
**------------------------------------------------------------------------------------------------------
** Created by: Chenmingji
** Created date: 2004-09-16
** Version: 1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by: Chenxibing
** Modified date: 2005-03-02
** Version: V1.0.0
** Descriptions: 從上位機接收字符串,然后送回上位機顯示。
**
********************************************************************************************************/
#include "config.h"
/*
*********************************************************************************************************
** 函數名稱 :DelayNS()
** 函數功能 :長軟件延時。
** 入口參數 :dly 延時參數,值越大,延時越久
** 出口參數 :無
*********************************************************************************************************
*/
void DelayNS (uint32 dly)
{
uint32 i;
for ( ; dly>0; dly--)
for (i=0; i<50000; i++);
}
#define UART_BPS 115200 /* 串口通訊波特率 */
/*
*********************************************************************************************************
** 函數名稱 :UART1_Init()
** 函數功能 :串口初始化,設置為8位數據位,1位停止位,無奇偶校驗,波特率115200。
** 入口參數 :無
** 出口參數 :無
*********************************************************************************************************
*/
void UART1_Init (void)
{
uint16 Fdiv;
U1LCR = 0x83; /* DLAB=1,允許設置波特率 */
Fdiv = (Fpclk / 16) / UART_BPS; /* 設置波特率 */
U1DLM = Fdiv / 256;
U1DLL = Fdiv % 256;
U1LCR = 0x03;
}
/*
*********************************************************************************************************
** 函數名稱 :UART1_GetByte()
** 函數功能 :從串口接收1字節數據,使用查詢方式接收。
** 入口參數 :無
** 出口參數 :接收到的數據
*********************************************************************************************************
*/
uint8 UART1_GetByte (void)
{
uint8 rcv_dat;
while ((U1LSR & 0x01) == 0);
rcv_dat = U1RBR;
return (rcv_dat);
}
/*
*********************************************************************************************************
** 函數名稱 :UART1_GetStr()
** 函數功能 :從串口接收
** 入口參數 : s 指向接收數據數組的指針
** n 接收的個數
** 出口參數 : 無
*********************************************************************************************************
*/
void UART1_GetStr (uint8 *s, uint32 n)
{
for ( ; n>0; n--)
{
*s++ = UART1_GetByte();
}
}
/*
*********************************************************************************************************
** 函數名稱 :UART1_SendByte()
** 函數功能 :向串口發送字節數據,并等待發送完畢,查詢方式。
** 入口參數 :dat 要發送的數據
** 出口參數 :無
*********************************************************************************************************
*/
void UART1_SendByte (uint8 dat)
{
U1THR = dat;
while ((U1LSR & 0x40) == 0); /* 等待數據發送完畢 */
}
/*
*********************************************************************************************************
** 函數名稱 :UART0_SendStr()
** 函數功能 :向串口發送一字符串
** 入口參數 :str 要發送的字符串的指針
** 出口參數 :無
*********************************************************************************************************
*/
void UART1_SendStr (uint8 const *str)
{
while (1)
{
if (*str == '\0') break; /* 遇到結束符,退出 */
UART1_SendByte(*str++); /* 發送數據 */
}
}
/*
*********************************************************************************************************
** 函數名稱 :main()
** 函數功能 :從串口UART1接收字符串"Hello EasyARM2131!",并發送回上位機顯示。
** 調試說明 :需要PC串口顯示終端軟件如EasyARM.exe。
*********************************************************************************************************
*/
int main (void)
{
uint8 snd[32];
//PINSEL0 = 0x00550000; /* 設置I/O連接到UART1 */
PINSEL0 = (PINSEL0 & (~(0x0F<<16))) | (0x05<<16); // 設置I/O連接到UART1
UART1_Init(); /* 串口初始化 */
UART1_GetStr(snd,18); /* 從串口接收字符串 */
DelayNS(10);
UART1_SendStr(snd); /* 向串口發送字符串 */
DelayNS(10);
while (1);
return 0;
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -