Demo entry 6676959

C++

   

Submitted by anonymous on Dec 01, 2017 at 16:18
Language: C++. Code size: 6.5 kB.

#include "LPC23xx.H"
#define Fpclk 12000000 //12MHz
 
#define IER_RBR        0x01
#define IER_THRE   0x02
#define IER_RLS        0x04
 
#define IIR_PEND   0x01
#define IIR_RLS        0x03
#define IIR_RDA        0x02
#define IIR_CTI        0x06
#define IIR_THRE   0x01
 
#define LSR_RDR        0x01
#define LSR_OE     0x02
#define LSR_PE     0x04
#define LSR_FE     0x08
#define LSR_BI     0x10
#define LSR_THRE   0x20
#define LSR_TEMT   0x40
#define LSR_RXFE   0x80
 
volatile unsigned int dataflag=0;
volatile unsigned char redata=0; //接收数据
volatile unsigned char trdata=0; //发送数据
volatile unsigned int addata=0;
volatile unsigned int final_data=0;
volatile unsigned char data1=0; //low eight
volatile unsigned char data2=0; //hight eight
 
volatile unsigned long UART0Status;
volatile unsigned char UART0TxEmpty = 1;
 
void __irq IRQ_Timer0 (void)
{   
    data1=final_data/256; //high eight
    data2=final_data%256; //low eight
    U0THR=data1;
    dataflag=1;
    T0IR = 0x01;                      //清除中断标志
    VICVectAddr = 0x00;           //通知VIC中断处理结束,更新VIC
}
 
 
 
 
void __irq IRQ_Uart0 (void)
{
    unsigned char IIRValue, LSRValue;
    unsigned char Dummy;
        unsigned char cdata;
    
    IIRValue = U0IIR;
     
    IIRValue >>= 1;         /* skip pending bit in IIR */
    IIRValue &= 0x07;         /* check bit 1~3, interrupt identification */
    if ( IIRValue == IIR_RLS )     /* Receive Line Status */
    {
            LSRValue = U0LSR;
            /* Receive Line Status */
            if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
            {
                    /* There are errors or break interrupt *///如果线上有错误
                    /* Read LSR will clear the interrupt */
                    UART0Status = LSRValue; //保留故障代码
                    Dummy = U0RBR;        /* Dummy read on RX to clear 
                                    interrupt, then bail out */ //取走无效数据
                    //IDISABLE;
                    VICVectAddr = 0;      /* Acknowledge Interrupt */
                    return;
            }
            if ( LSRValue & LSR_RDR )  /* Receive Data Ready */           
            {
                    /* If no error on RLS, normal ready, save into the data buffer. */
                    /* Note: read RBR will clear the interrupt */
                    cdata = U0RBR; //取走有效数据
                    //if(cdata!=redata)
                    {
                            redata=cdata;
 
                    }
            }
    }
    else if ( IIRValue == IIR_RDA )   /* Receive Data Available */
            {
            /* Receive Data Available *///接收数据可用
                    cdata = U0RBR; //取走有效数据
                    //if(cdata!=redata)
                    {
                            redata=cdata;
 
                    }
            }
            else if ( IIRValue == IIR_CTI )   /* Character timeout indicator */
                {
                    /* Character Time-out indicator */
                    UART0Status |= 0x100;     /* Bit 9 as the CTI error */
                }
                else if ( IIRValue == IIR_THRE )  /* THRE, transmit holding register empty */
                    {
                        if (dataflag==1)
                        {     
                        U0THR=data2;
                        dataflag=0;
                        }
                        /* THRE interrupt */
                        LSRValue = U0LSR;     /* Check status in the LSR to see if
                                            /// valid data in U0THR or not */
                        if ( LSRValue & LSR_THRE )
                        {
                                UART0TxEmpty = 1;
                        }
                        else
                        {
                                UART0TxEmpty = 0;
                        }
                    }
         
        VICVectAddr = 0x00;           //通知VIC中断处理结束,更新VIC
}
 
int main (void) 
{ 
        unsigned long baudrate=57600;//9600;
        unsigned long Fdiv;
      PINSEL10 = 0;      //禁止ETM
          /* 定时器0设定 */
     
      T0TC   = 0;                 //定时器0的计数器清零                                    
      T0PR   = 0;                 //时钟不预分频                                                    
      T0MCR  = 0x03;       //设置T0MR0匹配后复位T0TC,并产生中断标志                                  
      T0MR0  = 6000000 ; //TIM0的PCLK=CCLK/4=48/4=12M                    
                                            //设定为12000000,则每1s LED状态变换一次                       
      T0TCR  = 0x01;           //启动定时器                                                 
      /* 设置IRQ TIM0中断对应的是VIC中断通道的中断向量4*/
      VICIntSelect = 0x00;              //所有中断通道都设置为IRQ中断
      VICVectPriority4 = 00;            //设置中断向量4的优先级为0级    
      VICVectAddr4 = (unsigned int)IRQ_Timer0; //设置中断向量4的中断程序地址为TIM0中断程序               
      VICIntEnable = 1 << 0x04;         //使能中断向量4,即允许定时器0中断
     
     
      /*UART 设定 */
         
        PINSEL0 = 0x00000050;       /* RxD0 and TxD0 */
    U0LCR = 0x83;     /* 8 bits, no Parity, 1 Stop bit 允许访问除数锁存*/
    Fdiv = ( Fpclk / 16 ) / baudrate ; /*baud rate */
    U0DLM = Fdiv / 256; //除数高8位                     
    U0DLL = Fdiv % 256; //除数低8位
        U0LCR = 0x03;     /* 禁止访问除数锁存*/
    U0FCR = 0x07;     //FIFO使能,清空读写缓存
 
        /* 设置IRQ UART0中断对应的是VIC中断通道的中断向量6*/
        VICIntSelect = 0x00;              //所有中断通道都设置为IRQ中断
        VICVectPriority6 = 00;            //设置中断向量4的优先级为0级    
        VICVectAddr6 = (unsigned int)IRQ_Uart0;  //设置中断向量6的中断程序地址为UART0中断程序              
        VICIntEnable = (1 << 0x06)|(1 << 0x04);           //使能中断向量6,即允许串口0中断      
    
    U0IER = 0x07; /* Enable UART0 interrupt */
         
         
         /*AD 设定 */
 
     
      PCONP |= (1<<12);       // AD外设ON   
      PINSEL10 = 0;      //禁止ETM
     
      //硬件连接关系:AD0.0即P0.23接入热温电阻
      PINSEL1=0x00004000;      //P0.23功能设定为AD0.0
     
      while(1)
    {
        AD0CR = 0x01000001 | 0x00200300;       // A/D: 10位 AD时钟为PCLK/(3+1)=12/4=3MHz 启动AD
        do {
            addata = AD0GDR;                        // 读AD全局寄存器
        } while ((addata & 0x80000000) == 0);     //  判断AD是否转换完成
        AD0CR &= ~0x01000001;                  // 停止AD运行
        addata = (addata >> 6) & 0x03FF;             //获取AD值
    final_data=addata;
  }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).