/************************************************************* //! 函数名:void main (void) //! 函数说明:主程序 *************************************************************/ void main (void) { unsigned char i,max_data_chn; struct movement_info *car_cmd; PCA0MD &= ~0x40; //关闭看门狗 Oscillator_Init(); //时钟晶振初始化 // ADC_PortInit(); Timer1_Init(); //定时器1初始化 //Timer3_Init (); SMBus_Init (); //SMBus初始化(IIC控制器) PWM_Init(); //PWM初始化 servo_init(); //舵机控制初始化 //Watchdog_Init(); EA = 1; // 开中断 do { Flag = 0; while (!rec_flag) ; //检测接收控制数据的标志 rec_flag = 0; car_cmd = (struct movement_info *)Data; //若接收到数据,则从全局区取出 car_movement_control(car_cmd); //根据控制信息执行车体控制代码 }while(1); }
// Initialization function for device, // Call Init_Device() from your main program void Init_Device(void) { Reset_Sources_Init(); Timer_Init(); UART_Init(); SMBus_Init(); ADC_Init(); DAC_Init(); EMI_Init(); Voltage_Reference_Init(); Port_IO_Init(); Oscillator_Init(); Interrupts_Init(); }
//----------------------------------------------------------------------------- // MAIN Routine //----------------------------------------------------------------------------- // // Main routine performs all configuration tasks, then loops forever sending // and receiving SMBus data to the slave <SLAVE_ADDR>. // void main (void) { unsigned char odoslat[4] = {0x00,0x00,0x12,0xFF}; volatile unsigned char dat; // Test counter unsigned char i; // Dummy variable counters PCA0MD &= ~0x40; // WDTE = 0 (watchdog timer enable bit) OSCICN |= 0x07; // Set internal oscillator to highest // setting of 24500000 // If slave is holding SDA low because of an improper SMBus reset or error while(!SDA) { // Provide clock pulses to allow the slave to advance out // of its current state. This will allow it to release SDA. XBR1 = 0x40; // Enable Crossbar SCL = 0; // Drive the clock low for(i = 0; i < 255; i++); // Hold the clock low SCL = 1; // Release the clock while(!SCL); // Wait for open-drain // clock output to rise for(i = 0; i < 10; i++); // Hold the clock high XBR1 = 0x00; // Disable Crossbar } Port_Init (); // Initialize Crossbar and GPIO Timer1_Init (); // Configure Timer1 for use as SMBus // clock source Timer3_Init (); // Configure Timer3 for use with SMBus // low timeout detect SMBus_Init (); // Configure and enable SMBus EIE1 |= 0x01; // Enable the SMBus interrupt LED = 0; EA = 1; // Global interrupt enable // TEST CODE------------------------------------------------------------------- dat = 0; // Output data counter NUM_ERRORS = 0; // Error counter while (1) { // SMBus Write Sequence if (dat < 4){ SMB_DATA_OUT = odoslat[dat]; // Define next outgoing byte TARGET = SLAVE_ADDR; // Target the F3xx/Si8250 Slave for next // SMBus transfer SMB_Write(); // Initiate SMBus write } // SMBus Read Sequence // TARGET = SLAVE_ADDR; // Target the F3xx/Si8250 Slave for next // SMBus transfer //SMB_Read(); // Check transfer data /*if(SMB_DATA_IN != SMB_DATA_OUT) // Received data match transmit data? { NUM_ERRORS++; // Increment error counter if no match }*/ // Indicate that an error has occurred (LED no longer lit) if (NUM_ERRORS > 0) { LED = 0; } else { LED = ~LED; } // Run to here to view the SMB_DATA_IN and SMB_DATA_OUT variables dat++; T0_Wait_ms (1); // Wait 1 ms until the next cycle } // END TEST CODE--------------------------------------------------------------- }