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

📄 main.c

📁 美国模拟混合器件领导厂商Silabs 芯科实验室 收音机系列 源码
💻 C
字号:
//-----------------------------------------------------------------------------
// USB_MAIN.c
//-----------------------------------------------------------------------------
// Target: C8051F32x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f320.h>        
#include <stddef.h>       
#include <intrins.h>
#include "typedefs.h"

//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
					
sbit GPIO1    = P0^2;
sbit GPIO2    = P0^3;
sbit GPIO3    = P0^4;
sbit GP1      = P0^5;								
sbit GP2      = P0^6;		
sbit SDIO     = P0^7;
sbit GP3      = P1^0;		
sbit SCLK     = P1^1;
sbit SENB     = P1^2;
sbit RSTB     = P1^3;
sbit RCLK_MCU = P1^4;
sbit LED 	  = P1^5;



//-----------------------------------------------------------------------------
// Function prototypes
//-----------------------------------------------------------------------------

void Init_Device(void);
void Port_IO_Init(void);
void Suspend_Device(void);
void Timer_Init(void);
void SMBus_Init(void);

void wait_us(u16 us);

void test_autoseek(void);
void test_rds(void);
void _nop_(void);

//-----------------------------------------------------------------------------
// Externals
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
void main(void) 
{
    PCA0MD &= ~0x40;           // Disable Watchdog timer
    VDM0CN = 0x80;             // Enable VDD Monitor Circuit
    wait_us(100);              // Wait for VDD Monitor output to settle
    RSTSRC = 0x02;             // Enable VDD Monitor as Reset Source
    Init_Device();             // Initialize C8051F320


    test_autoseek();           // call a test function to exercise the si470x
//  test_rds();

}

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

//-----------------------------------------------------------------------------
// Init_Device
//-----------------------------------------------------------------------------
// - Enables all peripherals needed for the application
void Init_Device(void)
{
    Port_IO_Init();
	Timer_Init();
}
//-----------------------------------------------------------------------------
// Port_IO_Init
//-------------------------
// - Port Initialization
// - Configure the Crossbar and GPIO ports.
//-----------------------------------------------------------------------------
void Port_IO_Init(void)
{


	// P0.0  -  Skipped,     Open-Drain, Digital
	// P0.1  -  Skipped,     Open-Drain, Digital
	// P0.2  -  Skipped,     Open-Drain, Digital, GPIO1, default configuration: input
	// P0.3  -  Skipped,     Open-Drain, Digital, GPIO2, default configuration: input 
	// P0.4  -  Skipped,     Open-Drain, Digital, GPIO3, default configuration: input
	// P0.5  -  Skipped,     Open-drain, Digital
	// P0.6  -  Skipped,     Open-Drain, Digital
	// P0.7  -  Skipped,     Open-Drain, Digital, SDIO, default configuration: input

	// P1.0  -  Skipped,     Open-Drain, Digital, 
	// P1.1  -  Skipped,     Push-Pull,  Digital, SCLK
	// P1.2  -  Skipped,     Push-Pull,  Digital, SENB
	// P1.3  -  Skipped,     Push-Pull,  Digital, RSTB
	// P1.4  -  Skipped,     Open-Drain, Digital
	// P1.5  -  Unassigned,  Push-Pull,  Digital, LED
	// P1.6  -  Unassigned,  Open-Drain, Digital
	// P1.7  -  Unassigned,  Open-Drain, Digital

	P0MDIN    = 0xFF;	// All Port 0 and 1 pins are NOT analog
	P0MDOUT   = 0x20;   // Only P0.5 is output
	P0SKIP    = 0x7F;   
	SMB0CF 	  = 0xC1;	// Enable SMBus
	XBR0      = 0x00;	// SYSCLK output NOT routed to port pin

	P1MDIN    = 0xFF;	// All Port 0 and 1 pins are NOT
	P1MDOUT   = 0x3C;
	P1SKIP    = 0xFF;
	XBR1      = 0xC0;	// Crossbar enabled and pull-ups disabled

	GPIO1 = 1;			// GPIO1, GPIO2, GPIO3, SDIO
	GPIO2 = 1;			// configured as digital inputs
	GPIO3 = 1;
	SDIO  = 1;
	SCLK  = 1;
	LED   = 1;			// LED is on.




}

//-----------------------------------------------------------------------------
// Timer_Init
//-----------------------------------------------------------------------------
void Timer_Init(void)
{
	u8 i;

    CLKMUL    = 0x80;
    for (i = 0; i < 20; i++);    // Wait 5us for initialization
    CLKMUL    |= 0xC0;
    while ((CLKMUL & 0x20) == 0);

	CLKSEL = CLKSEL | 0x02;										// 4x clock multiplier/2 selected as system clock source	
	OSCICN = OSCICN | 0x83;										// SYSCLK derived from internal oscillator divided by 1.

	// Timer 0 operates in mode 1 (16-bit counter/timer).
	// Note: Timer 0 is used in the function wait_us. 
    TMOD      = 0x21;										

	// Counter/Timer 0 uses the system clock.
	CKCON     = 0x0C;					

    TCON      = 0x01;											// Disable Timer 0.
}
//-----------------------------------------------------------------------------
// delay routine for long delays
//
// Inputs:
//		Delay in milliseconds
//
//-----------------------------------------------------------------------------
void wait_ms(u16 ms)
{
	int i;
	
	for (i=0; i<ms; i++)
	{
		wait_us(1000);
	}		 		   
}

//-----------------------------------------------------------------------------
// Delay routine based on hardware timer
//
// Inputs:
//		Delay in microseconds
//
//-----------------------------------------------------------------------------
void wait_us(u16 us)
{
	u16 j;
	
	j = 65535u -  24*us;							
	TL0 = j;                        						// Load Timer 0 low byte
	TH0 = j >> 8;                   						// Load Timer 0 high byte
	TR0 = 1;                        						// Enable Timer 0
	TF0 = 0;												      // Clear Timer 0 Overflow flag
	while(TF0 != 1);                						// Wait for Timer 0 to overflow
	TR0 = 0;                        						// Disable Timer 0
	TF0 = 0;                        						// Clear Timer 0 Overflow Flag

}

//-----------------------------------------------------------------------------
// delay routine for small delays.  Assume each for loop takes
// at least 64nS, and divide parameter by just doing a shift.
// This routine primarily exists for documentation purposes.  
// On this 8051, all waits will be much longer than they need to be.
//
// Inputs:
//		Delay in nanoseconds
//
//-----------------------------------------------------------------------------
void wait_ns(u16 ns)
{
	u8 i;

	for(i = 1; i <= ns/32; i++)
		;
}

//-----------------------------------------------------------------------------
// Return when gpio is the correct state
//-----------------------------------------------------------------------------
void wait_gpio2(bit state)
{
	while (GPIO2 != state) // wait until GPIO2 == state
		_nop_();
	;
	_nop_();
}






⌨️ 快捷键说明

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