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

📄 c8051f120-i2c-simulate.c

📁 c8051f120端口模拟的I2C,点亮PCF8574驱动的发光二极管
💻 C
📖 第 1 页 / 共 2 页
字号:
}
}

void main (void) 
{
   
   WDTCN = 0xde;
   WDTCN = 0xad;           //关看门狗

   SFRPAGE = CONFIG_PAGE;

   SYSCLK_Init ();                     // Initialize system clock to 24.5MHz

   ExtCrystalOsc_Init ();              // Initialize for and switch to the
                                       // external crystal oscillator
                                       
   
   PORT_Init();                       // Initialize Port I/O

   // Turn off the LED before the test starts
   //LED = 0;
   //OUT1= 0;
   
   EA = 1;											       // enable global interrupts 

   
   SFRPAGE = LEGACY_PAGE;              // Sit in this SFRPAGE         	    
   
   LED = 0;
   
   OUT1 = 0;
   
   SCL = 1;
   SDA = 1;
     
   
while (1)
   {  
   LED = ~LED;
   Delay1ms(500);    
	
	 writeNbyte(0x42, 0x6F);
	 Delay1ms(500);
	 writeNbyte(0x42, 0xDF);
	
   }                                   // end of while(1)
}                                      // end of main()


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

//-----------------------------------------------------------------------------
// SYSCLK_Init ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine initializes the system clock to use the internal 24.5 MHz
// oscillator as its clock source.  Also enables missing clock detector reset.
//
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
      
   OSCICN = 0x83;                      // Configure internal oscillator for
                                       // its highest frequency (24.5 MHz),不分频

   RSTSRC = 0x04;                      // Enable missing clock detector如时钟丢失则启动复位,复位源寄存器
   
}

//-----------------------------------------------------------------------------
// ExtCrystalOsc_Init ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This routine initializes for and switches to the External Crystal
// Oscillator.
//
//-----------------------------------------------------------------------------
void ExtCrystalOsc_Init (void)
{
 
   // Set the appropriate XFCN bits for the crystal frequency
   //
   //   XFCN     Crystal (XOSCMD = 11x)
   //   000      f <= 32 kHz
   //   001      32 kHz < f <= 84 kHz
   //   010      84 kHz < f <= 225 kHz
   //   011      225 kHz < f <= 590 kHz
   //   100      590 kHz < f <= 1.5 MHz
   //   101      1.5 MHz < f <= 4 MHz
   //   110      4 MHz < f <= 10 MHz
   //   111      10 MHz < f <= 30 MHz
   #if (Crystal_Clock <= 32000)
      #define XFCN 0
   #elif (Crystal_Clock <= 84000L)
      #define XFCN 1
   #elif (Crystal_Clock <= 225000L)
      #define XFCN 2
   #elif (Crystal_Clock <= 590000L)
      #define XFCN 3
   #elif (Crystal_Clock <= 1500000L)
      #define XFCN 4
   #elif (Crystal_Clock <= 4000000L)
      #define XFCN 5
   #elif (Crystal_Clock <= 10000000L)
      #define XFCN 6
   #elif (Crystal_Clock <= 30000000L)
      #define XFCN 7
   #else
      #error "Defined Crystal Frequency outside allowable range!"
      #define XFCN 0
   #endif

   unsigned int i;

   // Step 1. Enable the external oscillator.
   OSCXCN = 0x60;                      // External Oscillator is an external
                                       // crystal (no divide by 2 stage)

   OSCXCN |= XFCN;


   // Step 2. Wait at least 1 ms.
   for (i = 13000; i > 0; i--);        // at 24.5 MHz, 1 ms = 24500 SYSCLKs
                                       // DJNZ = 2 SYSCLKs


   // Step 3. Poll for XTLVLD => ??
   while ((OSCXCN & 0x80) != 0x80);


   // Step 4. Switch the system clock to the external oscillator.
   CLKSEL = 0x01;                    // select external oscillator as SYSCLK禁止使用内部振荡器
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures the crossbar and ports pins.
// 
// To configure a pin as a digital input, the pin is configured as digital
// and open-drain and the port latch should be set to a '1'.  The weak-pullups
// are used to pull the pins high.  Pressing the switch pulls the pins low.
//
// To configure a pin as a digital output, the pin is configured as digital
// and push-pull.  
//
// Some ports pins do not have the option to be configured as analog or digital,
// so it not necessary to explicitly configure them as digital.
//
// An output pin can also be configured to be an open-drain output if system
// requires it.  For example, if the pin is an output on a multi-device bus,
// it will probably be configured as an open-drain output instead of a 
// push-pull output.  For the purposes of this example, the pin is configured
// as push-pull output because the pin in only connected to an LED.
//
// P0.0  -  TX0 (UART0), Push-Pull,  Digital
// P0.1  -  RX0 (UART0), Open-Drain, Digital
// P0.2  -  SCK  (SPI0), Push-Pull,  Digital
// P0.3  -  MISO (SPI0), Open-Drain, Digital
// P0.4  -  MOSI (SPI0), Push-Pull,  Digital
// P0.5  -  LCD_CS,      Push-Pull,  Digital
// P0.6     SMBus SDA    open-drain  digital        
// P0.7     SMBus SCL    open-drain  digital   
//
// P1.0   digital   Push-Pull     TX1
// P1.1   digital   open-drain    RX1
// P1.2   digital   open-drain    CEX0
// P1.3   digital   open-drain    CEX1
// P1.4   digital   open-drain    SCL作为LCD-SCLK
// P1.5   digital   open-drain    SDA作为LCD-SID
// P1.6   digital   push-pull     LED1
// P1.7   digital   open-drain    CS--作为LCD-CS
// P3.7   digital   open-drain    Switch 1
// P4.0   digital   open-drain    Input 1
// P4.1   digital   push-pull     Output 1
//
// P3.7   digital   open-drain    Switch 1
//
// all other port pins unused
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = CONFIG_PAGE;              // set SFR page before writing to
                                       // registers on this page
   
   XBR0    = 0x17;                     // Enable SMBus,SPI0, and UART0,CEX0,CEX1
   XBR1    = 0x00;										 // Enable
   XBR2    = 0x44;                     // Enable crossbar and weak pull-ups0,  UART1
   
   //P0MDOUT = 0x00;                     // All P0 pins open-drain output
   //P0 = 0xFF;
   P0MDOUT = 0x35; 
   
   P1MDIN |= 0xFF;                     // P1端口所有引脚均设置为digital,数字输入方式
   P1MDOUT = 0x41;                     // P1.6--LED P1.0-TX1 is push-pull,推挽输出方式
   																		 // P1.7--LCD_CS,P1.5--LCD_SID,P1.4---LCD_CLK与LCD--5V系统接口时,
   																		 // 需配置成漏极开路输出
   P3MDOUT = 0x00;                     // P3.7 is open-drain,漏极开路输入方式   																		 
   P3     |= 0x80;                     // Set P3.7 latch to '1'
   
   P4MDOUT = 0x02;                     // P4.1 is push-pull,推挽输出方式
   
   SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page
}

//-----------------------------------------------------------------------------
// Timer0_ms
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//   1) unsigned char ms - number of milliseconds to wait
//                        range is full range of character: 0 to 255
//
// Configure Timer0 to wait for <ms> milliseconds using SYSCLK as its time
// base.
//
/*
void Timer0_ms (uint ms)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = TIMER01_PAGE;

   TCON &= ~0x30;                      // Stop Timer0; Clear TF0
   TMOD &= ~0x0f;                      // 16-bit free run mode
   TMOD |=  0x01;

   CKCON |= 0x08;                      // Timer0 counts SYSCLKs

   while (ms) {
      TR0 = 0;                         // Stop Timer0
      TH0 = -(SYSCLK/1000 >> 8);       // Overflow in 1ms
      TL0 = -(SYSCLK/1000);
      TF0 = 0;                         // Clear overflow indicator
      TR0 = 1;                         // Start Timer0
      while (!TACK);                    // Wait for overflow
      //TF0 = 0;
      ms--;                            // Update ms counter
   }
    
    //TF0 = 0;  
   TR0 = 0;                            // Stop Timer0

   SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page detector
}


//-----------------------------------------------------------------------------
// Timer0_us
//-----------------------------------------------------------------------------
//
// Configure Timer0 to delay <us> microseconds before returning.
//
void Timer0_us (ulong us)
{
   ulong i;                         // millisecond counter

   TCON  &= ~0x30;                     // STOP Timer0 and clear overflow flag
   TMOD  &= ~0x0f;                     // configure Timer0 to 16-bit mode
   TMOD  |=  0x01;
   CKCON |=  0x08;                     // Timer0 counts SYSCLKs

   for (i = 0; i < us; i++) {          // count microseconds
      TR0 = 0;                         // STOP Timer0
      TH0 = (-SYSCLK/1000000) >> 8;    // set Timer0 to overflow in 1us
      TL0 = -SYSCLK/1000000;
      TR0 = 1;                         // START Timer0
      while (TF0 == 0);                // wait for overflow
      TF0 = 0;                         // clear overflow indicator
   }
   TR0 = 0;                            // Stop Timer0
}
*/
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------


⌨️ 快捷键说明

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