?? uart0_main_receive1.c
字號(hào):
// *********************************
// LPC2000 SYSTEM WITHOUT OS
// FILE: Uart0_main_receive1
// MODIFIED: ZY 2007-5-10 10:34
// *********************************
//串口將接受的字符發(fā)送給PC,8個(gè)BYTE以內(nèi)
#include <stdio.h>
#include "lpc22xx.h"
#include "init_c.h"
#include "vic.h"
#include "eint.h"
#include "main.h"
#include "config.h"
//extern short adc_data[];
volatile unsigned char renew;//1,有新數(shù)據(jù);0,無(wú)新數(shù)據(jù)
unsigned char r;
// #define a F_OSC]
unsigned char irqTime = 0;//進(jìn)入中斷次數(shù),用于調(diào)試
#define UART_BPS 9600; //波特率
unsigned char SEND_STRING[] = "Hello World!\n";
unsigned char RECIEVE_CHAR = 'a';
unsigned char RECIEVE_STRING[16];//string received that causes the RDA
unsigned char receiveLenth = 0;
unsigned char RECIEVE_STRINGEXTRA[8] = "abcdefgh";//string received that causes the DTI
void DelayNS(int dly);
void UART0_SendByte(unsigned char data);
/****************************************************************************
* 名稱:IRQ_Time0()
* 功能:接收數(shù)據(jù)中斷處理函數(shù)
* 入口參數(shù):無(wú)
* 出口參數(shù):無(wú)
****************************************************************************/
void /*__irq*/ UART0_Receive(void)
{
unsigned char u0iir;
int irqState;
int i;
irqTime++;
/*******
這段程序針對(duì)的是發(fā)送字符串
*******/
/*
if( (U0IIR & 0x0f) == 0x04 )//是否接受到新數(shù)據(jù)
{
renew = 1;
for( i = 0;i < 8;i ++)
{
RECIEVE_STRING[i] = U0RBR;
}
}
irqState = U0IIR;
*/
i = 0;
while (((u0iir = U0IIR) & 0x01) == 0)// Still have request
{
switch (u0iir & 0x0e)
{
case 0x04: // RDA
renew = 1;
for( i = 0;i < 8;i ++)
{
RECIEVE_STRING[i] = U0RBR;
}
break;
case 0x0C: // CTI
renew = 1;
while ((U0LSR & 0x01) != 0)
{
RECIEVE_STRING[i] = U0RBR;
i++;
}
//receiveLenth = i;
break;
}
}
receiveLenth = i;
/*******
這段程序針對(duì)的是發(fā)送單個(gè)字符
*******/
/*
if( (U0IIR & 0x0f) ==0x04 )//是否接受到新數(shù)據(jù)
{
renew = 1;
}
RECIEVE_CHAR = U0RBR;
irqState = U0IIR;
*/
//規(guī)范化的中斷處理函數(shù)
}
/****************************************************************************
* 名稱:DelayNS()
* 功能:長(zhǎng)軟件延時(shí)
* 入口參數(shù):dly 延時(shí)參數(shù),值越大,延時(shí)越久
* 出口參數(shù):無(wú)
****************************************************************************/
void DelayNS(int dly)
{ int i;
for(; dly>0; dly--)
for(i=0; i<5000; i++);
}
/****************************************************************************
* 名稱:UART0_Ini()
* 功能:初始化串口0。設(shè)置為8位數(shù)據(jù)位,1位停止位,無(wú)奇偶校驗(yàn),波特率為9600
* 入口參數(shù):無(wú)
* 出口參數(shù):無(wú)
****************************************************************************/
void UART0_Ini(void)
{
//串口設(shè)置
U0LCR = 0x83; // DLAB = 1
U0DLM = (F_PCLK / 16 / 9600) >> 8; // Set baud rate
U0DLL = (F_PCLK / 16 / 9600) & 0xff; // N, 8, 1
U0LCR = 0x03;
//U0FCR = 0X01; //使能FIFO中斷,并設(shè)置觸發(fā)字節(jié)為1
U0FCR = 0X81; //使能FIFO中斷,并設(shè)置觸發(fā)字節(jié)為8
U0IER = 0X01; //允許RBR中斷
//中斷設(shè)置
VICIntSelect = 0x00;//所有中斷通道設(shè)置為IRQ中斷
VICVectCntl0 = 0x26;//定時(shí)器0中斷通道分配最高優(yōu)先級(jí)(向量控制器0)
VICVectAddr0 = (unsigned int)UART0_Receive;//中斷入口地址
VICIntEnable = 0x00000040 ;//使能UART0中斷
}
/****************************************************************************
* 名稱:UART0_SendByte()
* 功能:向串口發(fā)送字節(jié)數(shù)據(jù),并等待發(fā)送完畢。
* 入口參數(shù):data 要發(fā)送的數(shù)據(jù)
* 出口參數(shù):無(wú)
****************************************************************************/
void UART0_SendByte(unsigned char data)
{
while( (U0LSR & 0x20)==0 ); // 等待數(shù)據(jù)發(fā)送完畢
U0THR = data;
}
/****************************************************************************
* 名稱:UART0_SendByte()
* 功能:將數(shù)據(jù)發(fā)還給主機(jī)
* 入口參數(shù):無(wú)
* 出口參數(shù):無(wú)
****************************************************************************/
void UART0_SendBack(void)
{
int i;
for(i = 0;i < receiveLenth;i ++)
{
UART0_SendByte(RECIEVE_STRING[i]);
while( (U0LSR & 0x20)==0 ); // 等待數(shù)據(jù)發(fā)送完畢
}
}
/****************************************************************************
* 名稱:UART0_SendStr()
* 功能:向串口發(fā)送一字符串
* 入口參數(shù):srt 要發(fā)送的字符串的指針
* 出口參數(shù):無(wú)
****************************************************************************/
void UART0_SendStr(unsigned char *str)
{ while(1)
{ if( *str == '\0' ) break;
UART0_SendByte(*str++); // 發(fā)送數(shù)據(jù)
}
}
int main(void)
{
renew = 0;
r = 'n';
target_init();
//init_serial_0();
UART0_Ini();
while(1)
{
//renew = 1;
if(renew == 1)//接收到
{
UART0_SendBack();
//UART0_SendByte(RECIEVE_CHAR);
renew = 0;
}
//renew = 0;
DelayNS(1000);
}
return(0);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -