#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
#define EnableIrq(bit) rINTMSK &= ~(bit)
static int whichUart=0;
static char PtStr[256];
void Isr_Init(void);
void HaltUndef(void);
void HaltSwi(void);
void HaltPabort(void);
void HaltDabort(void);
/////////////////////////////////////////////////////////////////////////////
//====================【延时】========================
void Delay1(U32 tt)
{
U32 i;
for(;tt>0;tt--)
{
for(i=0;i<10000;i++){}
}
}
//==============【等待Uart发送缓冲器清空】================
void Uart_TxEmpty(int ch)
{
if(ch==0)
while(!(rUTRSTAT0 & 0x4));
else if(ch==1)
while(!(rUTRSTAT1 & 0x4));
else if(ch==2)
while(!(rUTRSTAT2 & 0x4));
}
//=================【Uart初始化】=========================
void Uart_Init(int pclk,int baud)
{
if(pclk == 0)
pclk =PCLK;
rUFCON0 = 0x0;
rUFCON1 = 0x0;
rUFCON2 = 0x0;
rUMCON0 = 0x0;
rUMCON1 = 0x0;
//UART0
rULCON0 = 0x3;
rUCON0 = 0x245;
rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 );
//UART1
rULCON1 = 0x3;
rUCON1 = 0x245;
rUBRDIV1=( (int)(pclk/16./baud+0.5) -1 );
//UART2
rULCON2 = 0x3;
rUCON2 = 0x245;
rUBRDIV2=( (int)(pclk/16./baud+0.5) -1 );
Uart_TxEmpty(whichUart);
}
//====================【Uart选择】========================
void Uart_Select(int ch)
{
whichUart = ch;
}
//====================【发送字节】========================
void Uart_SendByte(int data)
{
if(whichUart==0)
{
if(data=='\n')
{
while(!(rUTRSTAT0 & 0x2));
Delay1(10);
WrUTXH0('\r');
}
while(!(rUTRSTAT0 & 0x2));
Delay1(10);
WrUTXH0(data);
}
else if(whichUart==1)
{
if(data=='\n')
{
while(!(rUTRSTAT1 & 0x2));
Delay1(10);
rUTXH1 = '\r';
}
while(!(rUTRSTAT1 & 0x2));
Delay1(10);
rUTXH1 = data;
}
else if(whichUart==2)
{
if(data=='\n')
{
while(!(rUTRSTAT2 & 0x2));
Delay1(10);
rUTXH2 = '\r';
}
while(!(rUTRSTAT2 & 0x2));
Delay1(10);
rUTXH2 = data;
}
}
//====================【发送字符串】========================
void Uart_SendString(char *PrintString)
{
while (*PrintString)
{
Uart_SendByte( *PrintString);
PrintString++;
}
}
//====================【接收字节】========================
char Uart_Getch(void)
{
if(whichUart==0)
{
while(!(rUTRSTAT0 & 0x1));
return RdURXH0();
}
else if(whichUart==1)
{
while(!(rUTRSTAT1 & 0x1));
return RdURXH1();
}
else if(whichUart==2)
{
while(!(rUTRSTAT2 & 0x1));
return RdURXH2();
}
}
//====================【接收字符串-待改】========================
void Uart_GetString(char *pt)
{
while((*pt=Uart_Getch())!=0xD)pt++;
*++pt=0;
}
/////////////////////////////////////////////////////////////////////////////
void Isr_Init(void)
{
pISR_UNDEF=(unsigned)HaltUndef;
pISR_SWI =(unsigned)HaltSwi;
pISR_PABORT=(unsigned)HaltPabort;
pISR_DABORT=(unsigned)HaltDabort;
rINTMOD=0x0; // All=IRQ mode
rINTMSK=BIT_ALLMSK; // All interrupt is masked.
}
void HaltUndef(void)
{
while(1);
}
void HaltSwi(void)
{
while(1);
}
void HaltPabort(void)
{
while(1);
}
void HaltDabort(void)
{
while(1);
}
/////////////////////////////////////////////////////////////////////
void Timer1_init(void){
rTCFG0=255;
rTCFG1=1<<4;
rTCNTB1=48828;
rTCMPB1=0x00;
rTCON=(1<<11)|(1<<9)|(0<<8);
rTCON=(1<<11)|(0<<9)|(1<<8);
Uart_SendString("Timer1_init\n");
}
void __irq Timer1_ISR(void){
int flag;
Uart_SendString("hello my world!wo de lao po liuyifei!\n");
//if(flag==0){rGPBDAT=rGPBDAT&0x00000000;flag=1;}
// else{rGPBDAT=rGPBDAT|0xffffffff;flag=0;}
rSRCPND|=BIT_TIMER1;
rINTPND|=BIT_TIMER1;
}
void Timer1INT_Iint(void){
if(rINTPND&BIT_TIMER1) rSRCPND|=BIT_TIMER1;
Uart_SendString("Timer1INT_Iint_1\n");
pISR_TIMER1=(U32)Timer1_ISR;
rINTMSK&=~(BIT_TIMER1);
Uart_SendString("Timer1INT_Iint_2\n");
}
///////////////////////////////////////////////////////////////
int Main(int argc, char **argv)
{
// rGPBCON=rGPBCON&0xffff00ff|0x00015400;
// rGPBDAT=0x00000000;
Uart_Init(0,115200);
Uart_Select(0);
Uart_SendString("Hello World!!!\n");
Isr_Init();
pISR_SWI=(_ISR_STARTADDRESS+0xf0);
Timer1_init();
Timer1INT_Iint();
Uart_SendString("Hello World!!!\n");
while(1)
{
Uart_GetString(PtStr);
Uart_SendString(PtStr);
}
}
我自己写得一个关于定时器中断的,理论是定时1S,通过串口通信发一串字符串到电脑,实际就是中断进不去,不能往电脑发送字符串,串口通亚段程序可以用的,可以帮忙分析一下吗