⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 f34x_uart0_interrupt.c

📁 8051f340实现cc1100软件SPI的正常数据收发
💻 C
字号:
//-----------------------------------------------------------------------------
// F34x_UART0_Interrupt.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program demonstrates how to configure the C8051F340 to write to and read
// from the UART interface. The program reads a word using the UART interrupts
// and outputs that word to the screen, with all characters in uppercase
//
// How To Test:
//
// 1) Ensure that jumpers are placed on J12 of the C8051F340 target board
//    that connect the P0.4 pin to the TX signal, and P0.5 to the RX signal.
// 2) Ensure that the serial cable is connected to the RS232 connector
//    on the target board.
// 3) Specify the target baudrate in the constant <BAUDRATE>.
// 4) Open Hyperterminal, or a similar program, and connect to the target
//    board's serial port.
// 5) Download and execute code on an 'F34x target board.
// 6) Type up to 64 characters into the Terminal and press Enter.  The MCU 
//    will then print back the characters that were typed
//
// Target:         C8051F34x
// Tool chain:     Keil C51 7.50 / Keil EVAL C51
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (SM)
//    -5 JUN 2007
//


//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include "include.h"

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK      12000000           // SYSCLK frequency in Hz
#define BAUDRATE        9600           // Baud rate of UART in bps
//#define unsigned int    uint;
/*cc1100*/
char paTable[8] = {0x17,0x1c,0x26,0x57,0x60,0x85,0xc8,0xc0};//cc1100对应功率值-20,-15,-10,-5,0,5,7,10
//char paTable[] = {0x97,0x6e,0x7f,0x97,0xa9,0xbb,0xfe,0xff,};//cc2500对应功率值-10,-8,-6,-4,-2,0,1
char paTableLen = 4;                                         //对应功率值,默认4则为0dbm发送

char txBuffer[14]={59,1,3,4,5,6,7,8,9,4,5,5,6,2};
char rxBuffer[5];
char delay=1;
unsigned char send_num=0;
unsigned char iii;
//函数声明
void  LCDinit(void);
void  TimerSet(void);
void  glit_light( unsigned int delay);
void delay1(unsigned int i);

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void SYSCLK_Init (void);
void UART0_Init (void);
void PORT_Init (void);
void Timer2_Init (int);

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------

#define UART_BUFFERSIZE 10
unsigned char UART_Buffer[UART_BUFFERSIZE];
unsigned char UART_Buffer_Size = 0;
unsigned char UART_Input_First = 0;
unsigned char UART_Output_First = 0;
unsigned char TX_Ready =1;
static char Byte;

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void delay1(unsigned int i)
{
  unsigned int j;
 for(j=0;j<i;j++)
 {
//  NOP();
 //_NOP();
 //_NOP();
 }


}
void main (void)
{

   PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer
                                       // enable)
   PORT_Init();                        // Initialize Port I/O
   SYSCLK_Init ();                     // Initialize Oscillator
   UART0_Init();

   EA = 1;

   TI_CC_SPISetup();                                 // spi设置初始化
    TI_CC_PowerupResetCCxxxx();                       // 复位RF 
    writeRFSettings();                                // 写RF配置信息
    
    //SPI验证用于测试无线模块是否连接(工作)正常
   // txBuffer[63]= TI_CC_SPIReadReg(TI_CCxxx0_IOCFG2); //如果不等于0x06为错误
   // txBuffer[63]= TI_CC_SPIReadReg(TI_CCxxx0_IOCFG0); //如果不等于0x06为错误   
    txBuffer[3]= TI_CC_SPIReadReg(0x08);   //如果不等于0x00为错误
    //输出功率设置    
    TI_CC_SPIWriteBurstReg(TI_CCxxx0_PATABLE, &paTable[paTableLen], 1);
                                                      //配置发送功率        
 delay1(50000);
//  TI_CC_GDO0_PxIES |= 0X02;       // Int on falling edge (end of pkt)
 // TI_CC_GDO0_PxIES &= ~TI_CC_GDO0_PIN;
  //address_bind=TI_CC_SPIReadReg(3);
 // P1OUT=0x02;//输入上拉
 // TI_CC_GDO0_PxIFG &= ~0X02;      // Clear flag
//  TI_CC_GDO0_PxIE |= 0X02;        // Enable int on end of packet
//  P1REN|=0X02;
   
//    _EINT();
  //   _BIS_SR(GIE);
    txBuffer[1] = 0x01;
    while(1)
    {    
     //  WDTCTL=WDT_ARST_1000;
      /* TI_CC_LED_PxOUT ^=TI_CC_LED1;
       send_num++;                                   //发送数据个数累加
       TI_CC_SPIStrobe(TI_CCxxx0_SIDLE);             //进入IDLE模式
       txBuffer[0] =6;                              // Packet length
       if(txBuffer[1] == 0x02)
       txBuffer[1] = 0x01;                           // Packet address
       else if(txBuffer[1] == 0x01)
       txBuffer[1] = 0x02;                           // Packet address
       txBuffer[3] = send_num;                       //写入当前发送数据个数
       RFSendPacket(txBuffer, 7);                   // 发送数据
              
      // TimerSet();                                   //设置TimerA定时器--定时大约一秒
    //   TI_CC_SPIStrobe(TI_CCxxx0_SPWD);             //进入IDLE模式
   //    WDTCTL=WDT_ARST_1000;
      // TI_CC_LED_PxOUT ^=TI_CC_LED1;
//       LPM3;  
   delay1(50000);  */
   // If the complete word has been entered via the terminal followed by
      // carriage return
	//	SBUF0 = 0XDD;
	//	TI0=1;
	/*		   while(RI0==0);
		   txBuffer[1] =SBUF0;
	        RI0=0;
    		   
    	    if(txBuffer[1] !='\0')  
		    {    
    		SBUF0=txBuffer[1] ;

			while(TI0==0);
			TI0=0;
			txBuffer[1] ='\0';
			}*/
	//	delay1(10000);
  ///    if(TX_Ready == 1 && UART_Buffer_Size != 0 && Byte == 13)
     // {
     //    TX_Ready = 0;                 // Set the flag to zero
     //    TI0 = 1;                      // Set transmit flag to 1
   //   }
    }    

}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure the Crossbar and GPIO ports.
//
// P0.4   digital   push-pull    UART TX
// P0.5   digital   open-drain   UART RX
//
//-----------------------------------------------------------------------------

void PORT_Init (void)
{
   P0MDIN&=~0x09;
   P0MDOUT |= 0xd4;                    // Enable UTX as push-pull output
   XBR0     = 0x01;                    // Enable UART on P0.4(TX) and P0.5(RX)
   XBR1     = 0x40;                    // Enable crossbar and weak pull-ups
}

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine initializes the system clock to use the internal oscillator
// at its maximum frequency.
// Also enables the Missing Clock Detector.
//-----------------------------------------------------------------------------

void SYSCLK_Init (void)
{
   OSCICN |= 0x03;                     // Configure internal oscillator for
                                       // its maximum frequency
   RSTSRC  = 0x04;                     // Enable missing clock detector
}

//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//-----------------------------------------------------------------------------

void UART0_Init (void)
{
   SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
                                       //        level of STOP bit is ignored
                                       //        RX enabled
                                       //        ninth bits are zeros
                                       //        clear RI0 and TI0 bits
   if (SYSCLK/BAUDRATE/2/256 < 1) {
      TH1 = -(SYSCLK/BAUDRATE/2);
      CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
      CKCON |=  0x08;
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01
      CKCON |=  0x01;
   } else if (SYSCLK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSCLK/BAUDRATE/2/12);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
   } else {
      TH1 = -(SYSCLK/BAUDRATE/2/48);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
      CKCON |=  0x02;
   }

   TL1 = TH1;                          // init Timer1
   TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
   TMOD |=  0x20;
   TR1 = 1;                            // START Timer1
   TX_Ready = 1;                       // Flag showing that UART can transmit
   IP |= 0x10;                         // Make UART high priority
   ES0 = 1;                            // Enable UART0 interrupts

}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// UART0_Interrupt
//-----------------------------------------------------------------------------
//
// This routine is invoked whenever a character is entered or displayed on the
// Hyperterminal.
//
//-----------------------------------------------------------------------------

void UART0_Interrupt (void) interrupt 4
{
   if (RI0 == 1)
   {
    //  if( UART_Buffer_Size == 0)  {      // If new word is entered
    //     UART_Input_First = 0;    }

      RI0 = 0;                           // Clear interrupt flag

      txBuffer[1] = SBUF0;                      // Read a character from UART

	   send_num++;                                   //发送数据个数累加
       TI_CC_SPIStrobe(TI_CCxxx0_SIDLE);             //进入IDLE模式
       txBuffer[0] =6;                              // Packet length
       if(txBuffer[1] == 0x02)
       txBuffer[1] = 0x01;                           // Packet address
       else if(txBuffer[1] == 0x01)
       txBuffer[1] = 0x02;                           // Packet address
       txBuffer[3] = send_num;                       //写入当前发送数据个数
       RFSendPacket(txBuffer, 7);                   // 发送数据
    /*  if (UART_Buffer_Size < UART_BUFFERSIZE)
      {
         UART_Buffer[UART_Input_First] = Byte; // Store in array

         UART_Buffer_Size++;             // Update array's size

         UART_Input_First++;             // Update counter
      }*/

   }

   if (TI0 == 1)                   // Check if transmit flag is set
   {
      TI0 = 0;                           // Clear interrupt flag

    /*  if (UART_Buffer_Size != 1)         // If buffer not empty
      {
         // If a new word is being output
         if ( UART_Buffer_Size == UART_Input_First ) {
              UART_Output_First = 0;  }

         // Store a character in the variable byte
         Byte = UART_Buffer[UART_Output_First];

         if ((Byte >= 0x61) && (Byte <= 0x7A)) { // If upper case letter
            Byte -= 32; }

         SBUF0 = Byte;                   // Transmit to Hyperterminal

         UART_Output_First++;            // Update counter

         UART_Buffer_Size--;             // Decrease array size

      }
      else
      {
         UART_Buffer_Size = 0;            // Set the array size to 0
         TX_Ready = 1;                    // Indicate transmission complete
      }*/
   }
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -