//----------------------------------------------------------------------------- // F12x_INIT_1.c //----------------------------------------------------------------------------- // Copyright 2002 Cygnal Integrated Products, Inc. // // AUTH: FB // DATE: 19 SEP 02 // // This file contains example initialization routines for the C8051F12x series // of devices. // // This program uses the the 24.5 MHz internal oscillator multiplied by two // for an effective SYSCLK of 49 MHz. This program also initializes and uses // UART1 at bits per second. // // // Target: C8051F12x // Tool chain: KEIL C51 6.03 / KEIL EVAL C51 // //----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include // SFR declarations #include // printf() and getchar() //----------------------------------------------------------------------------- // 16-bit SFR Definitions for 'F12x //----------------------------------------------------------------------------- sfr16 DP = 0x82; // data pointer sfr16 ADC0 = 0xbe; // ADC0 data sfr16 ADC0GT = 0xc4; // ADC0 greater than window sfr16 ADC0LT = 0xc6; // ADC0 less than window sfr16 RCAP2 = 0xca; // Timer2 capture/reload sfr16 RCAP3 = 0xca; // Timer3 capture/reload sfr16 RCAP4 = 0xca; // Timer4 capture/reload sfr16 TMR2 = 0xcc; // Timer2 sfr16 TMR3 = 0xcc; // Timer3 sfr16 TMR4 = 0xcc; // Timer4 sfr16 DAC0 = 0xd2; // DAC0 data sfr16 DAC1 = 0xd2; // DAC1 data sfr16 PCA0CP5 = 0xe1; // PCA0 Module 5 capture sfr16 PCA0CP2 = 0xe9; // PCA0 Module 2 capture sfr16 PCA0CP3 = 0xeb; // PCA0 Module 3 capture sfr16 PCA0CP4 = 0xed; // PCA0 Module 4 capture sfr16 PCA0 = 0xf9; // PCA0 counter sfr16 PCA0CP0 = 0xfb; // PCA0 Module 0 capture sfr16 PCA0CP1 = 0xfd; // PCA0 Module 1 capture //----------------------------------------------------------------------------- // Global CONSTANTS //----------------------------------------------------------------------------- #define TRUE 1 #define FALSE 0 #define INTCLK 24500000 // Internal oscillator frequency in Hz #define SYSCLK 49000000 // Output of PLL derived from (INTCLK*2) #define BAUDRATE 115200 // Baud rate of UART in bps sbit LED = P1^6; // LED='1' means ON sbit SW2 = P3^7; // SW2='0' means switch pressed //----------------------------------------------------------------------------- // Function PROTOTYPES //----------------------------------------------------------------------------- void main(void); void SYSCLK_Init(void); void PORT_Init(void); void UART1_Init (void); //----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- void main (void) { WDTCN = 0xde; // disable watchdog timer WDTCN = 0xad; PORT_Init (); // initialize crossbar and GPIO SYSCLK_Init (); // initialize oscillator UART1_Init (); // initialize UART1 SFRPAGE = UART1_PAGE; // Direct printf output to UART1 printf("Hello\n"); // Print a string while(1); } //----------------------------------------------------------------------------- // Initialization Routines //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // SYSCLK_Init //----------------------------------------------------------------------------- // // This routine initializes the system clock to use the internal oscillator // at 24.5 MHz multiplied by two using the PLL. // void SYSCLK_Init (void) { int i; // software timer char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page SFRPAGE = CONFIG_PAGE; // set SFR page OSCICN = 0x83; // set internal oscillator to run // at its maximum frequency CLKSEL = 0x00; // Select the internal osc. as // the SYSCLK source //Turn on the PLL and increase the system clock by a factor of M/N = 2 SFRPAGE = CONFIG_PAGE; PLL0CN = 0x00; // Set internal osc. as PLL source SFRPAGE = LEGACY_PAGE; FLSCL = 0x10; // Set FLASH read time for 50MHz clk // or less SFRPAGE = CONFIG_PAGE; PLL0CN |= 0x01; // Enable Power to PLL PLL0DIV = 0x01; // Set Pre-divide value to N (N = 1) PLL0FLT = 0x01; // Set the PLL filter register for // a reference clock from 19 - 30 MHz // and an output clock from 45 - 80 MHz PLL0MUL = 0x02; // Multiply SYSCLK by M (M = 2) for (i=0; i < 256; i++) ; // Wait at least 5us PLL0CN |= 0x02; // Enable the PLL while(!(PLL0CN & 0x10)); // Wait until PLL frequency is locked CLKSEL = 0x02; // Select PLL as SYSCLK source SFRPAGE = SFRPAGE_SAVE; // Restore SFR page } //----------------------------------------------------------------------------- // PORT_Init //----------------------------------------------------------------------------- // // This routine configures the crossbar and GPIO ports. // void PORT_Init (void) { char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page SFRPAGE = CONFIG_PAGE; // set SFR page XBR0 = 0x00; XBR1 = 0x00; XBR2 = 0x44; // Enable crossbar and weak pull-up // Enable UART1 P0MDOUT |= 0x01; // Set TX1 pin to push-pull P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull SFRPAGE = SFRPAGE_SAVE; // Restore SFR page } //----------------------------------------------------------------------------- // UART1_Init //----------------------------------------------------------------------------- // // Configure the UART1 using Timer1, for and 8-N-1. // void UART1_Init (void) { char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page SFRPAGE = UART1_PAGE; SCON1 = 0x10; // SCON1: mode 0, 8-bit UART, enable RX SFRPAGE = TIMER01_PAGE; TMOD &= ~0xF0; TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload if (SYSCLK/BAUDRATE/2/256 < 1) { TH1 = -(SYSCLK/BAUDRATE/2); CKCON |= 0x10; // T1M = 1; SCA1:0 = xx } else if (SYSCLK/BAUDRATE/2/256 < 4) { TH1 = -(SYSCLK/BAUDRATE/2/4); CKCON &= ~0x13; // Clear all T1 related bits CKCON |= 0x01; // T1M = 0; SCA1:0 = 01 } else if (SYSCLK/BAUDRATE/2/256 < 12) { TH1 = -(SYSCLK/BAUDRATE/2/12); CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00 } else { TH1 = -(SYSCLK/BAUDRATE/2/48); CKCON &= ~0x13; // Clear all T1 related bits CKCON |= 0x02; // T1M = 0; SCA1:0 = 10 } TL1 = TH1; // initialize Timer1 TR1 = 1; // start Timer1 SFRPAGE = UART1_PAGE; TI1 = 1; // Indicate TX1 ready SFRPAGE = SFRPAGE_SAVE; // Restore SFR page }