CC2430基础——串口测试实验

1、《main.c》

#define ENABLE_ALL_INTERRUPT() (IEN0 |= 0x80)
#define DISABLE_ALL_INTERRUPT() (IEN0 &= ~0x80)

#include"ioCC2430.h"

#define uchar unsigned char
#define uint unsigned int
#define BYTE unsigned char

extern void UARTxISRopen(void);   //开串口接收中断服务函数
extern void SET_UART0_ISR(void);  //串口中断接收设置函数 
extern void initUART(void);               //初始化函数

main( void )
{
     uchar temp = 0;
     
     SLEEP &= ~0x04;  //晶振   reset 2:Both oscillators powered up 
     while(!(SLEEP & 0x40));  //上电等待外晶振起振 XOSC is powered up and stable
     CLKCON &= ~0x47;  
//reset 6:Main clock oscillator select: 32 MHz crystal oscillator  

             //TICHSPD128分频,CLKSPD不分频

     SLEEP |= 0x04;  //set 2:Oscillator not selected by OSC bit powered down 

           //关闭不用的RC振荡器
                               //先让两个振荡器都起振;
等待晶体振荡器起振并稳定;
                               //通过CLKCON的OSC位把主时钟选择32M晶体振荡器;
                               //再让没有被CLKCON的OSC位选择的振荡器停止起振(关RC振荡器)

     
     initUART();  //初始化
     UARTxISRopen();  //开串口中断
     ENABLE_ALL_INTERRUPT(); //开全局中断
     while(1);
}

振荡器起振稳定——>设置主时钟——>初始化串口——>开串口中断——>开全局中断

//----------------------------------------------------------------------------

2、《uart.c》

#include"ioCC2430.h"
unsigned uarttemp;


#define ENABLE_ALL_INTERRUPT() (IEN0 |= 0x80)  //开全局中断
#define DISABLE_ALL_INTERRUPT() (IEN0 &= ~0x80) 
//关全局中断

#define IO_PER_LOC_UART0_AT_PORT0_PIN2345() do { PERCFG = (PERCFG&~0x01)|0x00; } while (0)
                             //reste 0:USART0 I/O location: Alternative 1 location
                             //选择USART0为通道1:P0.2345
#define uchar unsigned char

#define CRYSTAL 0x00
#define RC      0x01

#define HIGH_STOP                   0x02   //停止位为高电平
#define LOW_STOP                   
 0x00   //停止位为低电平

#define CLKSPD  (CLKCON & 0x07) //32KHZ and 32MHZ crystal oscilliator;32MHZ ticks

#define TRANSFER_MSB_FIRST          0x80 //最高位先发送
#define TRANSFER_MSB_LAST           0x00 //最高位后发送
//LSB(Least Significant Bit),意为最低有效位
//MSB(Most Significant Bit),意为最高有效位

#define UART_ENABLE_RECEIVE         0x40  //UART接收使能

 

#define UART_SETUP(uart, baudRate, options)        \
   do                        \
      {                                            \
        if((uart) == 0)      \ //if uart=0,chose USART0_UART/SPI
        {                                          \
           if(PERCFG & 0x01) \ //if U0CFG=1,chose location 2
           {                                       \
              P1SEL |= 0x30; \  //then P1.4/5 set as peripheral I/O 
           }                   //P1.4/5分别是RX,TX两条线,UART可由这两线组成,也可再加上RT,CT这两条
           else              \ //if U0CFG=0,chose location 1
           {                                
              P0SEL |= 0x0C; \ //then P0.2/3 set as peripheral I/O;同样为RX,TX两线   
           }                                       \
        }                                          \
        else                 \ //if uart=1,chose USART1_UART/SPI
        {                                          \
           if(PERCFG & 0x02) \ //if U1CFG=1,chose location 2
           {                                       \
              P1SEL |= 0xC0; \ //then P1.6/7 set as peripheral I/O                    
           } 
           else              \  //if U1CFG=0 ,chose location 1
           {                                       \
              P0SEL |= 0x30; \ //then P0.4/5 set as peripheral I/O                  
           }                                       \
        }                                          \
                                                 //以上进行USATR通道选择设置
                                                 // ##中间的uart根据上面的设置为0或1
      U##uart##GCR = BAUD_E((baudRate),CLKSPD);  \ //设置波特率指数值
              //U##uart##GCR:普通控制寄存器
      U##uart##BAUD = BAUD_M(baudRate);          \ //设置波特率尾数值
              //U##uart##BAUD:波特率控制寄存器
                                                 \
      U##uart##CSR |= 0x80;                  \                     //USART设为UART模式  
              //U##uart##CSR:控制及状态寄存器                                               
      U##uart##UCR |= ((options) | 0x80);      \ //chose UART stop bit level and set FLUSH bit
              //U##uart##UCR :UART控制寄存器
                                         \
      if((options) & TRANSFER_MSB_FIRST) \ //options=0x02:High stop bit
      { \  // 0x02/0x00   &   0x80    
         U##uart##GCR |= 0x20;           \ //MSB first
      }  \ //对于options=0x00这种情况,估计是以GCR寄存器默认的LSB first方式
      U##uart##CSR |= 0x40;              \ //Receiver enabled 接收使能
   } while(0)  // The macros in this section are available for both SPI and UART operation.
   
     
     
#define SET_MAIN_CLOCK_SOURCE(source) \  //source=CRYSTAL(0x00)/RC(0x01)
   do {                               \
      if(source)  //RC Oscillator
      {                    \
        CLKCON |= 0x40;  //set 7, 16MHZ HF RC Oscillator             
        while(!HIGH_FREQUENCY_RC_OSC_STABLE);  // wait for stable
        if(TICKSPD == 0)
        {             \
          CLKCON |= 0x08;  //set 3,16MHZ Timer ticks output          
        }                             \
        SLEEP |= 0x04;   //set 2,"oscillator not slected by OSC bit"  powered down               
      }                \//因为这里是用RC Oscillator,因此在CLKCON中的OSC位
                        //选择16M的RC;无论在SLEEP中有没有选择让它起振,
                        //通过OSC位选择的振荡器都会起振。最后SLEEP |= 0x04
                        //确定所用振荡器由CLKCON中的OSC位来选择,
                        //那这里是选择了16M的RC,因而CRYSTAL没有起振。
                        
      else  //CRYSTAL Oscillator
      {                          \
        SLEEP &= ~0x04;  //reset 2,both oscillator powered up;             
        while(!XOSC_STABLE);  //wait for stable 
        asm("NOP");                   \
          CLKCON &= ~0x47; //rest 6: 32M crystal oscillator;这里也应该是默认的16M ticks吧?            
        SLEEP |= 0x04;         \ //首先SLEEP选择了两个振荡器都起振
      }                        \        //然后通过CLKON的OSC位选择了32M的主时钟晶体振荡器
   }while (0)                     //最后SLEEP |= 0x04确定所用振荡器由CLKCON中
                                       //的OSC位来选择,那这里是选择了32M的CRYSTAL,因而

                                        //晶体振荡器起振,RC振荡器没有起振 
// BAUD_E along with BAUD_M decides the UART baud rate
// and the SPI master SCK clock frequency
// BAUD_E和BAUD_M共同决定了波特率,参见波特率计算公式 
// BAUD_E=Baud rate exponent value 指数值 BAUD_E[4:0]在U0/1GCR寄存器中
// BAUD_M=Baud rate mantissa value 尾数值 BAUD_M[7:0]在U0/1BAUD寄存器中
     
#define BAUD_E(baud, clkDivPow)  // The macros in this section simplify UART operation.
     (     \                           //此处为多级条件运算
    (baud==2400)   ?  6  +clkDivPow : \  //表达式1?表达式2:表达式3
    (baud==4800)   ?  7  +clkDivPow : \  //1真为2;1假为3
    (baud==9600)   ?  8  +clkDivPow : \
    (baud==14400)  ?  8  +clkDivPow : \
    (baud==19200)  ?  9  +clkDivPow : \
    (baud==28800)  ?  9  +clkDivPow : \
    (baud==38400)  ?  10 +clkDivPow : \
    (baud==57600)  ?  10 +clkDivPow : \
    (baud==76800)  ?  11 +clkDivPow : \
    (baud==115200) ?  11 +clkDivPow : \
    (baud==153600) ?  12 +clkDivPow : \
    (baud==230400) ?  12 +clkDivPow : \
    (baud==307200) ?  13 +clkDivPow : \
    0  )


       
#define BAUD_M(baud) (      \
    (baud==2400)   ?  59  : \
    (baud==4800)   ?  59  : \
    (baud==9600)   ?  59  : \
    (baud==14400)  ?  216 : \
    (baud==19200)  ?  59  : \
    (baud==28800)  ?  216 : \
    (baud==38400)  ?  59  : \
    (baud==57600)  ?  216 : \
    (baud==76800)  ?  59  : \
    (baud==115200) ?  216 : \
    (baud==153600) ?  59  : \
    (baud==230400) ?  216 : \
    (baud==307200) ?  59  : \
  0)
//以上宏定义的函数:UART_SETUP(uart, baudRate, options) 设置波特率
//                  SET_MAIN_CLOCK_SOURCE(source)   设置主时钟
//                  BAUD_E(baud, clkDivPow)    设置波特率的指数值
//                  BAUD_M(baud)    设置波特率的尾数值

       
       
#define XOSC_STABLE (SLEEP & 0x40)  //XOSC is powered up and stable
#define TICKSPD ((CLKCON & 0x38) >> 3)
#define HIGH_FREQUENCY_RC_OSC_STABLE    (SLEEP & 0x20) 
//HF RCOSC is powered up and stable

void ChangUartBaund(uchar temp);
void initUART(void);
void SET_UART0_ISR(void);


//*****************************************************************************
//串口初始化函数
//串口参数设置为57600,8,None,1,None
//打开串口,允许接收
//*****************************************************************************

void initUART(void)
{
   IO_PER_LOC_UART0_AT_PORT0_PIN2345(); //reste 0:USART0 I/O location: Alternative 1 location
   SET_MAIN_CLOCK_SOURCE(CRYSTAL); //主时钟时钟源设置函数(设置成晶体振荡器)
   UART_SETUP(0, 9600, HIGH_STOP); //波特率设置函数(将波特率设置成9600)
   U0CSR = 0xC5; //U0CSR(USART0控制和状态)
      //1100 0101 ;UART mode; receiver enable; received byte ready; USART busy in transmit 
      // or receive mode;
}

//------------------------------------------------------------------------
//describe:chang baund
//return:
//in:
//------------------------------------------------------------------------
void ChangUartBaund(uchar temp)
{
switch(temp)
{
case '0':UART_SETUP(0, 2400, HIGH_STOP);break;
case '1':UART_SETUP(0, 4800, HIGH_STOP);break;
case '2':UART_SETUP(0, 9600, HIGH_STOP);break;
case '3':UART_SETUP(0, 14400, HIGH_STOP);break;
case '4':UART_SETUP(0, 19200, HIGH_STOP);break;
case '5':UART_SETUP(0, 28800, HIGH_STOP);break;
case '6':UART_SETUP(0, 38400, HIGH_STOP);break;
case '7':UART_SETUP(0, 57600, HIGH_STOP);break;
case '8':UART_SETUP(0, 76800, HIGH_STOP);break;
case '9':UART_SETUP(0, 115200, HIGH_STOP);break;
default:  UART_SETUP(0, 9600, HIGH_STOP);break;
}
}
//example 9600;
//------------------------------------------------------------------------
//describe:open the uart isr(中断服务程序)
//return:
//in:
//本例串口接收程序是运用中断来完成的,所以首先要设置ISR
//------------------------------------------------------------------------

void SET_UART0_ISR(void) //串口接收设置函数
{
   U0CSR |= 0xd0;  //1101 0000;UART mode; receiver enable; Byte received with incorrect stop bit level 
   U0UCR |= 0x02; //UART stop bit level: High stop bit
}


void UARTxISRopen(void) //开串口接收中断函数
{
  U0CSR |= 0x40;  //receiver enable; 
  IEN0 |= 0X04;  //开串口接收中断 'URX0IE = 1',
}


#pragma vector = URX0_VECTOR
 __interrupt void URX0_ISR(void)
 {
    
    uarttemp = U0DBUF; //串口调试软件发送的数据给uarttemp

    U0DBUF = uarttemp; //把接收到的数据返送串口软件显示
}

 

//---------------------------------------------------------------------------------

转载于:https://www.cnblogs.com/LakeFollow/archive/2012/07/09/2582901.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值