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

📄 mcu_f320.c

📁 这是nrf24lu1的无线鼠标源代码,应用平台是keil c
💻 C
字号:
/* Copyright (c) 2007 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is confidential property of 
 * Nordic Semiconductor. The use, copying, transfer or disclosure 
 * of such information is prohibited except by express written
 * agreement with Nordic Semiconductor.
 */ 
 
/** @file
 *
 * Implementation of C8051F320 hardware functions. This file implements the
 * system_init() function, that sets up hardware such as internal clocks,
 * timers, and IO ports.
 *
 * @author Eirik Midttun
 * @author Runar Kjellaug
 * @author Lasse Olsen
 */
 
#include "fap.h"
#include "mcu_f320.h"

#define T2RELOAD 1000                // t2 reload time, in 祍

static void sysclk_init(void);
static void port_init(void);
static void t1_init(uint16_t value);
static void t2_init(uint16_t value);
static void t0_start(void);
static void t1_start(void);
static void t2_start(void);
static void t0_stop(void);
static void t1_stop(void);
static void t2_stop(void);
static void Delay100us(uint8_t count);

extern void hal_spi_init(uint8_t spi_speed);
extern void usb_init(void);

//-----------------------------------------------------------------------------
// Definitions
//-----------------------------------------------------------------------------

#define SYSCLK             12000000     // SYSCLK frequency in Hz

// USB clock selections (SFR CLKSEL)
#define USB_4X_CLOCK       0x00         // Select 4x clock multiplier, for USB
#define USB_INT_OSC_DIV_2  0x10         // Full Speed
#define USB_EXT_OSC        0x20
#define USB_EXT_OSC_DIV_2  0x30
#define USB_EXT_OSC_DIV_3  0x40
#define USB_EXT_OSC_DIV_4  0x50

// System clock selections (SFR CLKSEL)
#define SYS_INT_OSC        0x00         // Select to use internal oscillator
#define SYS_EXT_OSC        0x01         // Select to use an external oscillator
#define SYS_4X_DIV_2       0x02

//-----------------------------------------------------------------------------
// Global Variable Declarations
//-----------------------------------------------------------------------------

void system_init (void)
{
  uint16_t temp;

  PCA0MD &= ~0x40;              // Disable Watchdog timer

  REG0CN |= 0x80;               // Internal Voltage Regulator Disabled

  sysclk_init();                // initialize system clock
  
  port_init();                  // configure cross bar
 
  TCON |= 0x01;                 // Int1 level triggered  xxx same as ir0 = 1

  // Radio IRQ setup
  IT0 = 1;                      // /INT0, edge sens    
  IE0 = 0;                      // clear pending interrupt  
  EX0 = 1;                      // Extern interrupt0 (nRF) enabled  
 
   // PS2 IRQ setup
  IT1 = 1;                      // Int1 edge sens
  IE1 = 0;
  EX1 = 0;                      // PS2 IRQ
  PX1=1;                        // Int1 priority bit set (ps2)    
  
  hal_spi_init(0);              // SPI Init, 3-wire mode, CK/2 speed

  t2_init(FAP_RX_PERIOD);       // configure timer1, start FAP timer 
 
  //t1_init(35);                  // Baudrate gifven as 3*period -> 19200 bps  xxxxx

  temp=0xffff;                  // Waiting ...  
  while(--temp)                 // nRF radio is booting up
    ;                           // temp value can be tuned.
}

void sysclk_init (void)
{
#ifdef _USB_LOW_SPEED_
  
  OSCICN |= 0x03;                       // Configure internal oscillator for
                                        // its maximum frequency and enable
                                        // missing clock detector
  
  CLKSEL  = SYS_EXT_OSC;                // Select System clock
  CLKSEL |= USB_INT_OSC_DIV_2;          // Select USB clock
#else
  OSCICN |= 0x03;                       // Configure internal oscillator for
                                        // its maximum frequency and enable
                                        // missing clock detector
  
  CLKMUL  = 0x00;                       // Select internal oscillator as
                                        // input to clock multiplier
  
  CLKMUL |= 0x80;                       // Enable clock multiplier
  
  Delay100us(10);                       // Delay for clock multiplier to begin
  
  CLKMUL |= 0xC0;                       // Initialize the clock multiplier
  
  while(!(CLKMUL & 0x20))               // Wait for multiplier to lock
    ;
  CLKSEL  = SYS_INT_OSC;                // Select system clock
  CLKSEL |= USB_4X_CLOCK;               // Select USB clock
#endif  /* _USB_LOW_SPEED_ */
}

void port_init (void)
{
  P0MDIN = 0xff;                        // Port 0 set as digital IO
  P1MDIN = 0xff;                        // Port 1 set as digital IO
  P2MDIN = 0xff;                        // Port 2 set as digital IO

  P0MDOUT = 0x8D;                       // P0.0(SCK), P0.2(MOSI), P0.3(CSN), P0.4(CE) Outputs, P0.5(IRQ) rest inputs
  P1MDOUT = 0x63;                       // P1.0(LED1), P1.1(LED2), P1.2(LED3), P1.3(LED4) Outputs, rest inputs
  P2MDOUT = 0x00;                       // P2.1(SW1), P2.2(SW2), P2.3(SW3), P2[7..4] Rotary Switch, Inputs

  P0 = 0x7A;                            // P0.3(CSN), P0.5(IRQ) High(pullups), rest low
  P1 = 0x7c;                            // P1[3..0] LEDS OFF
  P2 = 0xff;                            // P2[3..1] low(ext.pullups), P2[7..4] pullups

  XBR0 = 0x00;                          // No periferi routed to crossbar
  XBR1 |= 0x40;                         // Enable Crossbar
  IT01CF = 0x65;                        // Int0 assigned to P0.5 and Int1 assigned to P0.6
                                        // Both active low 
}

void t0_init(bool start)
{
  TMOD |= 0x02;                         // T0:Mode2, 8-bit reload timer.
  TH0 = 244;                            // Init T0 reload value 12MHz/48 * 12 equ 48祍..
  TL0 = 244;                            // Init T0 start value..  
  if(start)
    t0_start();
}

void t1_set_period(uint16_t value)       // value specifies period in us
{
  TH1 = 0xff - (value/4);                
}
 
void fap_modify_timer_period()
{
  TMR2 = ~ FAP_TIMER_MODIFY_PERIOD;
  TF2H = 0;
  TF2L = 0;           
}  
        
void t1_init(uint16_t value)
{
  TMOD |= 0x20;                         // T1:Mode2, 8-bit reload timer.
  
  TH1 = ~(value);                       // Timeout will equal value/3     
  TL1 = ~(value);               

  t1_start();                           // Start T1 and enable irq
}

void t2_init(uint16_t value)
{                                     
  TMR2RL = ~value;                  // T2 reload time =  CK/12 *(TM2RL)
  TF2H = 0;
  TF2L = 0;
  TMR2 = ~value;                    // clear timer2 counter
    
  t2_start();  
}

static void t0_start(void)
{
  TR0 = 1;
  ET0 = 1;
}

static void t1_start(void)
{
  TR1 = 1;                              // Start timer1...
  ET1 = 1;
}

static void t2_start(void)                     // Start Timer2
{
  TR2 = 1;                              // T2 enable
  ET2 = 1;                              // T2 interrupt enable
}

static void t0_stop(void)
{
  TR0 = 0;                              // Stop Timer0...
  ET0 = 0;
}

void t1_stop(void)
{
  TR1 = 0;                              // Stop Timer1...
  ET1 = 0;
}

void t2_stop(void)                      // Stop Timer2...
{
  TR2 = 0;
  ET2 = 0;                              // disable Timer2 interrupt
}

static void Delay100us(uint8_t cnt)
{
  uint8_t delay;
  while(cnt-- > 0)
  {
    for(delay=0;delay<117;delay++);     // 100祍 for C8051F320-TB
  }
}

⌨️ 快捷键说明

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