//------------------------------------------------------------------------------------ // BlinkyF330.c //------------------------------------------------------------------------------------ // // This program flashes the green LED // // Target: C8051F330 // // Tool chain: SDCC 'c' // // Change Log: // // 2009-02-03 mk, none yet // //------------------------------------------------------------------------------------ // Includes //------------------------------------------------------------------------------------ #include // SFR declarations //------------------------------------------------------------------------------------ // Global CONSTANTS //------------------------------------------------------------------------------------ sbit at 0x93 LED; // p1.3 F330 green LED: '1' = OFF; '0' = ON //------------------------------------------------------------------------------------ // Function PROTOTYPES //------------------------------------------------------------------------------------ char _sdcc_external_startup(void); void OSCILLATOR_Init(void); void PORT_Init_330(void); //----------------------------------------------------------------------------- // Global Variables //----------------------------------------------------------------------------- unsigned long mainCount = 0; //------------------------------------------------------------------------------------ // MAIN Routine //------------------------------------------------------------------------------------ void delay_ms( unsigned long ms ) { unsigned long i; while ( ms-- ) { for ( i=0; i<250; i++ ) { // jumbo mumbo } } } void main (void) { //OSCILLATOR_Init (); // Initialize system clock to 24.5 Mhz P1MDOUT = 0x08; // P1.3 is push-pull XBR1 = 0x40; // Enable Crossbar; pull-ups enabled //PORT_Init_330(); while (1) { // spin forever mainCount++; LED = !LED; // change state of LED delay_ms(500); } } //----------------------------------------------------------------------------- // OSCILLATOR_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This function initializes the system clock to use the internal 24.5/8 MHz // oscillator as its clock source. Also enables missing clock detector reset. // //----------------------------------------------------------------------------- void OSCILLATOR_Init (void) { OSCICN = 0x80; // Configure internal oscillator for lowest frequency RSTSRC = 0x04; // Enable missing clock detector } //----------------------------------------------------------------------------- // PORT_Init //----------------------------------------------------------------------------- // // Return Value : None // Parameters : None // // This function configures the crossbar and GPIO ports. // // // P0.4 digital push-pull UART TX // P0.5 digital open-drain UART RX // P0.6 digital open-drain /INT0 // P1.3 digital push-pull LED (and CEX0 depending on Blink_State) // P1.6 analog Potentiometer (ADC input) // //----------------------------------------------------------------------------- void PORT_Init_330 (void) { P1MDIN = 0xBF; // P1.6 is analog, rest of P1 digital P0MDOUT = 0x10; // P0.4 is push-pull P1MDOUT = 0x08; // P1.3 is push-pull P0SKIP = 0xCF; // Skip all of P0 except for UART0 pins P1SKIP = 0x47; // Skip P1.6 and push CEX0 to P1.3 P0 |= 0x60; // Set port latch for pins RX and /INT0 // to configure as inputs XBR0 = 0x01; // Enable UART XBR1 = 0x40; // Enable Crossbar; pull-ups enabled } //----------------------------------------------------------------------------- // SDCC special - stop the Watchdog timer prior to initialization // // !!! to work properly this requires prototype define (see above) //----------------------------------------------------------------------------- char _sdcc_external_startup(void) { // disable watchdog on F020 //VDM0CN = 0xde; //VDM0CN = 0xad; // disable watchdog on F330 PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer enable) return 0; // do not skip variable initialization }