void main(void) { // Initialize all tasks Speech_Init(); UART_Init(BAUD9600); Timer0_Init(500); Elecmotor_Init(); Button_Init(); Battery_Init(); Sensor_Init(); Alarm_Init(); Function_init(); Function_S_init(); // Add Tasks hSCH_Add_Task(Elecmotor_update, 1000, 1, 1); // 1ms/ticket hSCH_Add_Task(Button_update, 1500, 200, 1); // 100ms/ticket hSCH_Add_Task(Speech_update, 1500, 1000, 1); // 0.5s/ticket hSCH_Add_Task(Sensor_update, 2000, 1, 1); // 1ms/ticket hSCH_Add_Task(Alarm_update, 3000, 2000, 1); // 1s/ticket hSCH_Add_Task(Function_update, 1500, 4000, 1); // 2s/ticket hSCH_Add_Task(Function_s_update, 2000, 1, 1); // 1ms/ticket // Start the program. hSCH_Start(); while(1) { // Excute the program. hSCH_Dispatch_Tasks(); } }
// *************************** Capture image dimensions out of BMP********** int main(void){ TExaS_Init(SSI0_Real_Nokia5110_Scope); // set system clock to 80 MHz Random_Init(69); Output_Init(); ADC0_Init(); ST7735_InvertDisplay(0); ST7735_SetRotation(0); AI_Init(0x07); Player_Init(); player.potato=1; Timer0_Init(Master_Funk, 30); ST7735_DrawBitmap(53, 151, Bunker0, 18,5); // Delay100ms(1); // delay 5 sec at 80 MHz /* ST7735_FillScreen(0x0000); // set screen to black ST7735_SetCursor(1, 1); ST7735_OutString("GAME OVER"); ST7735_SetCursor(1, 2); ST7735_OutString("Nice try,"); ST7735_SetCursor(1, 3); ST7735_OutString("Earthling!"); ST7735_SetCursor(2, 4); LCD_OutDec(1234); */ while(1){ } }
int main(void) { unsigned char Mystring[20]; Serial_Init(); lcd_init(); print_string("\nSTART MAIN TIMER TEST"); Timer0_Init(); sei(); while(1) { if(Update_flag) { sprintf(Mystring,"%02d %02d %02d",(unsigned char)HRS_var,(unsigned char)MIN_var,(unsigned char)SEC_var); print_string("\n\r"); print_string(Mystring); lcd_gotoxy1(0); lcd_string(Mystring); Update_flag = 0; } } }
// *************************** Capture image dimensions out of BMP********** int main(void){ TExaS_Init(SSI0_Real_Nokia5110_Scope); // set system clock to 80 MHz Output_Init(); ADC0_Init(); DAC_Init(); SysTick_Init(); Menu_Init(); Sound_Init(); UART1_Init(); Button0_Init(); Timer0_Init(Play, 80000000/11025); // 11.025 kHz 80000000/11025 Timer1_Init(Master_Funk, 80000000/60); //Timer2_Init(SomeUART function, high speed); //for UART /* ST7735_FillScreen(0x0000); // set screen to black ST7735_SetCursor(1, 1); ST7735_OutString("GAME OVER"); ST7735_SetCursor(1, 2); ST7735_OutString("Nice try,"); ST7735_SetCursor(1, 3); ST7735_OutString("Earthling!"); ST7735_SetCursor(2, 4); LCD_OutDec(1234); */ while(1){ } }
void Init() { ReadConfigFromEEPROM(); //LEDs DDRC |= (1<<PC3); //Red LED DDRB |= (1<<PB5); //Green LED //check for service mode CheckServiceMode(); #ifndef NO_BINDING Bind_RX_Init(); #endif Red_LED_ON; Timer0_Init(); USART_Init(); RF22B_Init(); Servo_Init(); to_rx_mode(); sei(); }
void Init() { ReadConfigFromEEPROM(); //LEDs DDRC |= (1<<PC3); //Red LED DDRB |= (1<<PB5); //Green LED //check for service mode CheckServiceMode(); #ifndef NO_BINDING //check BIND button state DDRD &= ~(1<<PD3); PORTD |= (1<<PD3); _delay_ms(1); if (bit_is_clear(PIND,PD3)) { _delay_ms(1000); if (bit_is_clear(PIND,PD3)) bind = 1; } else bind = 0; if (bind) { RF_Header[0] = 'B'; RF_Header[1] = 'I'; RF_Header[2] = 'N'; RF_Header[3] = 'D'; } else { RF_Header[0] = SETTINGS.RF_HEADER[0]; RF_Header[1] = SETTINGS.RF_HEADER[1]; RF_Header[2] = SETTINGS.RF_HEADER[2]; RF_Header[3] = SETTINGS.RF_HEADER[3]; } #endif Red_LED_ON; Timer0_Init(); USART_Init(); RF22B_Init(); Servo_Init(); to_rx_mode(); sei(); }
void main() { EA=1; Timer0_Init(1); F_Set_Timer1(10); while(1) { KeyDriver(); } }
void rc_init(void) { Timer0_Init(); /* External INT0 */ MCUCR |= (1 << 1); // Falling edge generates interrupt MCUCR &= ~(1 << 0); GICR |= (1 << INT0); // Enable interrupt edge = 0; timer0_ovf = 0; state = RC_INIT; }
void main(void) /* 此main函数是单个数码管的自加程序 */ { NUM_LED_Init(); Timer0_Init(); while(1) { if(1 == flag) { flag = !flag; /* 清除标志位 */ NUM_ShowScan(); } } }
void main() { uint8 temp[20]; Timer0_Init(1); UART_Conf(9600); LCD1602_Init(); EEPROM_ReadString(0x40,16,temp); LCD1602_Show(0,0,temp,16); EEPROM_ReadString(0x80,16,temp); LCD1602_Show(0,1,temp,16); while(1) { UART_Driver(); } }
void main() { EA=1; P1&=0XEF; Timer0_Init(1); Timer1_Init(); INT0_Init(); while(1) { if(Flag_IR) { Flag_IR=0; LedBuff[0]=LedChar[IRBuff[0]/16]; LedBuff[1]=LedChar[IRBuff[0]%16];; LedBuff[4]=LedChar[IRBuff[2]/16];; LedBuff[5]=LedChar[IRBuff[2]%16];; } } }
void initializer() { // Calibrate the oscillator: OSCCAL_calibration(); // Initialize the USART USARTinit(); // Initialize timer0 to play a tune Timer0_Init(); // initialize piezo-element sbi(DDRB, 5); // set OC1A as output sbi(PORTB, 5); // set OC1A high // Display instructions on PC sendFString(TALKING_TO); sendFString(WHO_DEMO); sendFString(ENTER); sendFString(TEXT_FUR_ELISE); sendFString(ENTER); sendFString(TEXT_TURKEY_MARCH); sendFString(ENTER); sendFString(TEXT_MINUET); sendFString(ENTER); sendFString(TEXT_AULD_LANG_SYNE); sendFString(ENTER); sendFString(TEXT_SIRENE1); sendFString(ENTER); sendFString(TEXT_SIRENE2); sendFString(ENTER); sendFString(TEXT_WHISTLE); sendFString(ENTER); sendFString(VOLUME_UP); sendFString(THE_VOLUME); sendFString(ENTER); sendFString(VOLUME_DOWN); sendFString(THE_VOLUME); sendFString(ENTER); sendFString(STOP); }
int main(){ int i = 0; //Enable interrupt sei(); //Set direction to output DDRD = 0xFF; //Set portD0 to high PORTD = 0x01; //Initialization of Timer 0 Timer0_Init(); while(1) //Do nothing { i = i+1; } return 0; } //end main
//***************Timer3A_Handler**************** //Plays the Tetris Sound onces at the beginning of each round //Then it deactivates itself until the next round begins //Input: none //Output: none void Timer3A_Handler(void){ TIMER3_ICR_R = 0x01; TIMER3_CTL_R = 0x0000000; //disable TIMER0 // TIMER1_CTL_R = 0x0000000; //disable TIMER1 // TIMER2_CTL_R = 0x0000000; //disable TIMER2 for(int num =0; num < 133; num++){//133 if(TETRIS[num] == Z1) i=5;else i =150; //check if Z1 change the period to 5 Sound_Play2(TETRIS[num]); //play the note Delay10ms(i); //delay after each note usign the value of i } NVIC_ST_CTRL_R =0; //disable SysTick after the song has been played // TIMER0_CTL_R = 0x0000001; //enable TIMER0 // TIMER2_CTL_R = 0x0000001; //enable TIMER2 DisableInterrupts(); Timer0_Init(70000000); //1000000 Timer1_Init(7255); Timer2_Init(30000000); EnableInterrupts(); TIMER3_CTL_R = 0x0000000; //disable TIMER1A }
void InitApp(void) { /* TODO Initialize User Ports/Peripherals/Project here */ /* Setup analog functionality and port direction */ TRISD = 0b00000000; // PORTD bits 7:0 are all outputs (0) INTCON2bits.RBPU = 0; // enable PORTB internal pullups WPUBbits.WPUB0 = 1; // enable pull up on RB0 ANSELH = 0x00; // AN8-12 are digital inputs (AN12 on RB0) TRISBbits.TRISB0 = 1; // PORTB bit 0 (connected to switch) is input (1) /* Initialize peripherals */ Timer0_Init(); ADC_Init(); /* Configure the IPEN bit (1=on) in RCON to turn on/off int priorities */ /* Enable interrupts */ }
//debug code int main(void){ volatile unsigned long delay; PLL_Init(); // bus clock at 80 MHz SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF; // activate port F delay = SYSCTL_RCGC2_R; // allow time to finish activating GPIO_PORTF_DIR_R |= 0x0E; // make PF3-1 output (PF3-1 built-in LEDs) GPIO_PORTF_AFSEL_R &= ~0x0E; // disable alt funct on PF3-1 GPIO_PORTF_DEN_R |= 0x0E; // enable digital I/O on PF3-1 // configure PF3-1 as GPIO GPIO_PORTF_PCTL_R = (GPIO_PORTF_PCTL_R&0xFFFF000F)+0x00000000; GPIO_PORTF_AMSEL_R = 0; // disable analog functionality on PF LEDS = 0; // turn all LEDs off // Timer1_Init(&UserTask, 4000); // initialize timer2 (20,000 Hz) Timer0_Init(&UserTask, 5000000); // initialize timer1 (16 Hz) // Timer1_Init(&UserTask, 80000000);// initialize timer1 (1 Hz) // Timer1_Init(&UserTask, 0xFFFFFFFF); // initialize timer1 (slowest rate) EnableInterrupts(); while(1){ WaitForInterrupt(); } }
void main (void) { LED_Display = 1; // initialize // Init I/O TRISD = 0b00000000; // PORTD bits 7:0 are all outputs (0) TRISAbits.TRISA0 = 1; // TRISA0 input INTCON2bits.RBPU = 0; // enable PORTB internal pullups WPUBbits.WPUB0 = 1; // enable pull up on RB0 // ADCON1 is now set up in the InitADC() function. TRISBbits.TRISB0 = 1; // PORTB bit 0 (connected to switch) is input (1) // Init Timer0 Timer0_Init(); // now enables interrupt. // Init ADC ADC_Init(); // Set up switch interrupt on INT0 INTCON2bits.INTEDG0 = 0; // interrupt on falling edge of INT0 (switch pressed) INTCONbits.INT0IF = 0; // ensure flag is cleared INTCONbits.INT0IE = 1; // enable INT0 interrupt // NOTE: INT0 is ALWAYS a high priority interrupt // Set up global interrupts RCONbits.IPEN = 1; // Enable priority levels on interrupts INTCONbits.GIEL = 1; // Low priority interrupts allowed INTCONbits.GIEH = 1; // Interrupting enabled. while (1) { // we update the port pins in our "background" loop while the interrupts // handle the switch and timer. LATD = LED_Display; // output LED_Display value to PORTD LEDs } }
/* ------------------------------------------------------------------ */ int main (void) { uint8_t state = ST_TOP_MENU; uint8_t nextstate = ST_TOP_MENU; uint8_t key = KEY_NONE; func_p pStateFunc = states[state]; /* disable watchdog */ wdt_reset(); Wdt_clear_flag(); Wdt_change_enable(); Wdt_stop(); Clear_prescaler(); InitLED(); Led1On(); Timer0_Init(); SPEAKER_Init(); BUTTON_Init(); LCD_Init(); sei(); while (1) { key = BUTTON_GetKey(); nextstate = pStateFunc(key); if (nextstate != state) { pStateFunc = states[nextstate]; state = nextstate; } } /* end of while(1) */ return 0; }
//------------------------------------------------------------------------------------------- // MAIN Routine //------------------------------------------------------------------------------------------- void main (void) { bit restart = 0; unsigned int randnum = 0; WDTCN = 0xDE; // Disable the watchdog timer WDTCN = 0xAD; // Note: = "DEAD"! SYSCLK_INIT(); // Initialize the oscillator. PORT_INIT(); // Configure the Crossbar and GPIO. UART0_INIT(); // Initialize UART0. SFRPAGE = LEGACY_PAGE; IT0 = 1; // /INT0 is edge triggered, falling-edge. IT1 = 1; // INT1 falling-edge-triggered SFRPAGE = CONFIG_PAGE; EX0 = 1; // Enable Ext Int 0 only after everything is settled. EX1 = 1; // Enable External Interrupt 2 SFRPAGE = UART0_PAGE; // Direct output to UART0 printf("\033[2J"); // Erase screen and move cursor to the home posiiton. Timer0_Init(); //enable timer0 TR0 = 1; Timer2_Init(); TR2 = 1; //enable timer2 // printf("I am running :D \n\r"); while (1) playGame(); }
/* * Programa principal, configura el modulo ADC, el timer y luego espera las interrupciones */ int main(void) { // configurar el reloj para 40MHz SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_RCC_XTAL_16MHZ|SYSCTL_OSC_MAIN); // configurar un led SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x04); // configurar el adc0ss3 ADC0SS3_TimerTrigger_Init(); // configurar el timer con el tiempo de muestreo Timer0_Init(20000000); // habilitar las interrupciones globales IntMasterEnable(); while (1) { // hacer algo, esperar interrupciones /* PF1 ^= 0xff; SysCtlDelay(10000000); */ } }
// ************************************************************************************************* // @fn init_application // @brief Initialize the microcontroller. // @param none // @return none // ************************************************************************************************* void init_application(void) { volatile unsigned char *ptr; #ifndef EMU // --------------------------------------------------------------------- // Enable watchdog // Watchdog triggers after 16 seconds when not cleared #ifdef USE_WATCHDOG WDTCTL = WDTPW + WDTIS__512K + WDTSSEL__ACLK; #else WDTCTL = WDTPW + WDTHOLD; #endif // --------------------------------------------------------------------- // Configure PMM SetVCore(3); // Set global high power request enable PMMCTL0_H = 0xA5; PMMCTL0_L |= PMMHPMRE; PMMCTL0_H = 0x00; // --------------------------------------------------------------------- // Enable 32kHz ACLK P5SEL |= 0x03; // Select XIN, XOUT on P5.0 and P5.1 UCSCTL6 &= ~XT1OFF; // XT1 On, Highest drive strength UCSCTL6 |= XCAP_3; // Internal load cap UCSCTL3 = SELA__XT1CLK; // Select XT1 as FLL reference UCSCTL4 = SELA__XT1CLK | SELS__DCOCLKDIV | SELM__DCOCLKDIV; // --------------------------------------------------------------------- // Configure CPU clock for 12MHz _BIS_SR(SCG0); // Disable the FLL control loop UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx UCSCTL1 = DCORSEL_5; // Select suitable range UCSCTL2 = FLLD_1 + 0x16E; // Set DCO Multiplier _BIC_SR(SCG0); // Enable the FLL control loop // Worst-case settling time for the DCO when the DCO range bits have been // changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx // UG for optimization. // 32 x 32 x 8 MHz / 32,768 Hz = 250000 = MCLK cycles for DCO to settle __delay_cycles(250000); // Loop until XT1 & DCO stabilizes, use do-while to insure that // body is executed at least once do { UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG); SFRIFG1 &= ~OFIFG; // Clear fault flags } while ((SFRIFG1 & OFIFG)); // --------------------------------------------------------------------- // Configure port mapping // Disable all interrupts __disable_interrupt(); // Get write-access to port mapping registers: PMAPPWD = 0x02D52; // Allow reconfiguration during runtime: PMAPCTL = PMAPRECFG; // P2.7 = TA0CCR1A or TA1CCR0A output (buzzer output) ptr = &P2MAP0; *(ptr+7) = PM_TA1CCR0A; P2OUT &= ~BIT7; P2DIR |= BIT7; // P1.5 = SPI MISO input ptr = &P1MAP0; *(ptr+5) = PM_UCA0SOMI; // P1.6 = SPI MOSI output *(ptr+6) = PM_UCA0SIMO; // P1.7 = SPI CLK output *(ptr+7) = PM_UCA0CLK; // Disable write-access to port mapping registers: PMAPPWD = 0; // Re-enable all interrupts __enable_interrupt(); #endif // --------------------------------------------------------------------- // Configure ports // --------------------------------------------------------------------- // Reset radio core radio_reset(); radio_powerdown(); // --------------------------------------------------------------------- // Init acceleration sensor as_init(); // --------------------------------------------------------------------- // Init LCD lcd_init(); // --------------------------------------------------------------------- // Init buttons init_buttons(); // --------------------------------------------------------------------- // Configure Timer0 for use by the clock and delay functions Timer0_Init(); // --------------------------------------------------------------------- // Init pressure sensor ps_init(); }
//############################################################################ //Hauptprogramm int main (void) //############################################################################ { char altPhase = 0; int test = 0; unsigned int Blink,TestschubTimer; unsigned int Blink2,MittelstromTimer,DrehzahlMessTimer,MotorGestopptTimer; DDRC = 0x08; //PC3输出,控制红色led 端口C 数据方向寄存器 PORTC = 0x08; //PC3输出高电平,红色led亮 DDRD = 0x3A; //10111010A-B-C-配置为输出,TXD,LED_GRN(PD7)配置为输出,MITTEL/RXD/INTO为输入 PORTD = 0x00; DDRB = 0x0E; PORTB = 0x31; #if (MOTORADRESSE == 0) PORTB |= (ADR1 + ADR2); // Pullups für Adresswahl for(test=0;test<500;test++); if(PINB & ADR1) { if (PINB & ADR2) MotorAdresse = 1; else MotorAdresse = 2; } else { if (PINB & ADR2) MotorAdresse = 3; else MotorAdresse = 4; } HwVersion = 11; #else MotorAdresse = MOTORADRESSE; HwVersion = 10; #endif if(PIND & 0x80) {HwVersion = 12; IntRef = 0xc0;} //为1.2版本 DDRD = 0xBA; UART_Init(); Timer0_Init(); sei();//Globale Interrupts Einschalten // Am Blinken erkennt man die richtige Motoradresse /* for(test=0;test<5;test++) { if(test == MotorAdresse) PORTD |= GRUEN; Delay_ms(150); PORTD &= ~GRUEN; Delay_ms(250); } Delay_ms(500); */ // UART_Init(); // war doppelt PWM_Init(); InitIC2_Slave(0x50); InitPPM(); Blink = SetDelay(101); Blink2 = SetDelay(102); MinUpmPulse = SetDelay(103); MittelstromTimer = SetDelay(254); DrehzahlMessTimer = SetDelay(1005); TestschubTimer = SetDelay(1006); while(!CheckDelay(MinUpmPulse)) { if(SollwertErmittlung()) break; // 一旦发现有信号,立刻停止延时 } GRN_ON; PWM = 0; SetPWM(); SFIOR = 0x08; // Analog Comperator ein ADMUX = 1; MinUpmPulse = SetDelay(10); DebugOut.Analog[1] = 1; // 填写一些调试信号 PPM_Signal = 0; if(!SollwertErmittlung()) MotorTon(); //MotorTon(); PORTB = 0x31; // Pullups wieder einschalten // zum Test der Hardware; Motor dreht mit konstanter Drehzahl ohne Regelung if(TEST_MANUELL) Anwerfen(TEST_MANUELL); // kommt von dort nicht wieder while (1) { //ShowSense(); if(!TEST_SCHUB) PWM = SollwertErmittlung(); //I2C_TXBuffer = PWM; // Antwort über I2C-Bus if(MANUELL_PWM) PWM = MANUELL_PWM; // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if(Phase != altPhase) // es gab eine Kommutierung im Interrupt { MotorGestoppt = 0; ZeitFuerBerechnungen = 0; // direkt nach einer Kommutierung ist Zeit MinUpmPulse = SetDelay(250); // Timeout, falls ein Motor stehen bleibt altPhase = Phase; // 旧相位 = 新相位 } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if(!PWM) // Sollwert == 0 在开机后主控板静默,并没有发任何I2C信号过来 { MotorAnwerfen = 0; // kein Startversuch MotorAnwerfen means motor start ZeitFuerBerechnungen = 0; // 指使一些操作的优先级用 // nach 1,5 Sekunden den Motor als gestoppt betrachten if(CheckDelay(MotorGestopptTimer)) { DISABLE_SENSE_INT; MotorGestoppt = 1; STEUER_OFF; } } else { if(MotorGestoppt) MotorAnwerfen = 1; // Startversuch MotorGestopptTimer = SetDelay(1500); } if(MotorGestoppt && !TEST_SCHUB) PWM = 0; //TEST_SCHUB 1为测试模式,0为正常模式 SetPWM(); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if(!ZeitFuerBerechnungen++) //先!,后++ { if(MotorGestoppt) // 马达停止 { GRN_ON; FastADConvert(); } if(SIO_DEBUG) { DebugAusgaben(); // welche Werte sollen angezeigt werden? if(!UebertragungAbgeschlossen) SendUart(); else DatenUebertragung(); } // Berechnen des Mittleren Stroms zur (langsamen) Strombegrenzung if(CheckDelay(MittelstromTimer)) //254ms { MittelstromTimer = SetDelay(50); // alle 50ms if(Mittelstrom < Strom) Mittelstrom++;// Mittelwert des Stroms bilden 将mittelstorm平缓化 else if(Mittelstrom > Strom) Mittelstrom--; if(Strom > MAX_STROM) MaxPWM -= MaxPWM / 32; //为什么电流大于最大电流的时候,PWM最大值要减小? if((Mittelstrom > LIMIT_STROM))// Strom am Limit? 动态调整MaxPWM的值 { if(MaxPWM) MaxPWM--;// dann die Maximale PWM herunterfahren PORTC |= ROT; } else { if(MaxPWM < MAX_PWM) MaxPWM++; } } if(CheckDelay(DrehzahlMessTimer)) // Ist-Drehzahl bestimmen 1005ms { DrehzahlMessTimer = SetDelay(10); SIO_Drehzahl = CntKommutierungen;//(6 * CntKommutierungen) / (POLANZAHL / 2); CntKommutierungen = 0; // if(PPM_Timeout == 0) // keine PPM-Signale ZeitZumAdWandeln = 1; } #if TEST_SCHUB == 1 { if(CheckDelay(TestschubTimer)) { TestschubTimer = SetDelay(1500); switch(test) { case 0: PWM = 50; test++; break; case 1: PWM = 130; test++; break; case 2: PWM = 60; test++; break; case 3: PWM = 140; test++; break; case 4: PWM = 150; test = 0; break; default: test = 0; } } } #endif // Motor Stehen geblieben if((CheckDelay(MinUpmPulse) && SIO_Drehzahl == 0) || MotorAnwerfen) // SIO_Drehzahl 为当前转速值 前面为判断意外停机 { /* 下面三行代码给电机意外停转用*/ MotorGestoppt = 1; DISABLE_SENSE_INT; // 取消比较中断 MinUpmPulse = SetDelay(100); if(MotorAnwerfen) { PORTC &= ~ROT; //switch off red led Strom_max = 0; MotorAnwerfen = 0; if(Anwerfen(10)) // 以PMW = 10触发启动程序,期间要换相32次,成功返回1 { GRN_ON; //绿灯打开 MotorGestoppt = 0; // Phase--; // Phase = 1 PWM = 1; SetPWM(); SENSE_TOGGLE_INT; // ????? ENABLE_SENSE_INT; // ????? 使能比较器中断 MinUpmPulse = SetDelay(20); while(!CheckDelay(MinUpmPulse)); // kurz Synchronisieren PWM = 15; SetPWM(); MinUpmPulse = SetDelay(300); while(!CheckDelay(MinUpmPulse)) // kurz Durchstarten { if(Strom > LIMIT_STROM/2) { STEUER_OFF; // Abschalten wegen Kurzschluss RotBlink(10); MotorAnwerfen = 1; } } // Drehzahlmessung wieder aufsetzen DrehzahlMessTimer = SetDelay(50); altPhase = 7; } else if(SollwertErmittlung()) MotorAnwerfen = 1; } } } // ZeitFuerBerechnungen } // while(1) - Hauptschleife }
void main(void) { /*设置红外输出引脚为推挽输出*/ P1M1 = 0x00; P1M0 = 0x1C; /*PCA模块初始化*/ PCA_Init(); /*Timer定时器初始化*/ Timer0_Init(); /*串口初始化*/ Uart1_Init(); /*ADC的初始化*/ ADC_Init(); /*打开全局中断标识位*/ EA = 1; /*系统上电,管脚初始化*/ Signal_LED_PIN = 0; Check_Signal_PIN = 0; // #ifdef DEBUG // /*启动32HZ和38KHZ方波输出*/ // T38KHZ_Start(); // T32HZ_Start(); // Timer = 0; // // System_State = 1; // #endif /*输出初始化成功标识*/ printf("%s","INIT DONE \r\n"); while(1) { /*软件复位,不断电,自动下载*/ if(System_IAP_REST_PIN == 0) IAP_CONTR = 0x60; /***************************检查系统电极接触状态******************************/ if(ADC_Timer == 198) { ADC_Buffer = Get_ADC_Result(); printf("%d ",ADC_Buffer); /*清除系统状态*/ System_State = 0; if((ADC_Buffer > 140) && (ADC_Buffer <250)) { /*电极接触*/ System_State = 1; } if(System_State != System_State_Buffer) { if(System_State == 0) { T32HZ_Stop(); T38KHZ_Start(); } else if(System_State == 1) { T38KHZ_Stop(); T32HZ_Start(); } } System_State_Buffer = System_State; printf("%d ",System_State_Buffer); } else if(ADC_Timer == 200) { ADC_Timer = 0; } /*****************************************************************************/ /************************系统状态为充电电极未接触状态*************************/ if(System_State == 0) { /*****************************指示灯部分代码程序******************************/ /*未接触时,指示灯4S亮一次*/ if(Signal_LED_PIN_Timer < 50) { Signal_LED_PIN = 1; } else if(Signal_LED_PIN_Timer >= 3999) { Signal_LED_PIN_Timer = 0; Signal_LED_PIN = 0; } else { Signal_LED_PIN = 0; } /*****************************************************************************/ /****************************红外发射部分代码程序*****************************/ if((IR_Timer >= 0) && (IR_Timer < 50)) { /*全向红外发射序列*/ /*1*/ if((IR_Timer >= 0) && (IR_Timer < 3)) { LED_FF_PIN = 1; } else if(IR_Timer == 3) { LED_FF_PIN = 0; } /*0*/ else if(IR_Timer == 4) { LED_FF_PIN = 1; } else if((IR_Timer >= 5) && (IR_Timer < 7)) { LED_FF_PIN = 0; } /*0*/ else if(IR_Timer == 7) { LED_FF_PIN = 1; } else if((IR_Timer >= 8) && (IR_Timer < 10)) { LED_FF_PIN = 0; } /*1*/ else if((IR_Timer >= 10) && (IR_Timer < 13)) { LED_FF_PIN = 1; } else if(IR_Timer == 13) { LED_FF_PIN = 0; } /*0*/ else if(IR_Timer == 14) { LED_FF_PIN = 1; } else if((IR_Timer >= 15) && (IR_Timer < 17)) { LED_FF_PIN = 0; } /*1*/ else if((IR_Timer >= 17) && (IR_Timer < 20)) { LED_FF_PIN = 1; } else if(IR_Timer == 20) { LED_FF_PIN = 0; } /*0*/ else if(IR_Timer == 21) { LED_FF_PIN = 1; } else if((IR_Timer >= 22) && (IR_Timer < 24)) { LED_FF_PIN = 0; } /*1*/ else if((IR_Timer >= 24) && (IR_Timer < 27)) { LED_FF_PIN = 1; } else { LED_FF_PIN = 0; } } else if((IR_Timer >= 50) && (IR_Timer < 100)) { /*左右侧红外发送序列*/ /*L:0 R:0*/ if(IR_Timer == 50) { LED_L_PIN = 1; LED_R_PIN = 1; } else if((IR_Timer >= 51) && (IR_Timer < 53)) { LED_L_PIN = 0; LED_R_PIN = 0; } /*L:0 R:0*/ else if(IR_Timer == 53) { LED_L_PIN = 1; LED_R_PIN = 1; } else if((IR_Timer >= 54) && (IR_Timer < 56)) { LED_L_PIN = 0; LED_R_PIN = 0; } /*L:1 R:0*/ /*L:0 R:1*/ else if(IR_Timer == 56) { LED_L_PIN = 1; LED_R_PIN = 1; } else if((IR_Timer >= 57) && (IR_Timer < 59)) { LED_L_PIN = 1; LED_R_PIN = 0; } else if(IR_Timer == 59) { LED_L_PIN = 0; LED_R_PIN = 1; } else if(IR_Timer == 60) { LED_L_PIN = 1; LED_R_PIN = 1; } else if(IR_Timer == 61) { LED_L_PIN = 0; LED_R_PIN = 1; } else if(IR_Timer == 62) { LED_L_PIN = 0; LED_R_PIN = 0; } /*L:0 R:0*/ else if(IR_Timer == 63) { LED_L_PIN = 1; LED_R_PIN = 1; } else if((IR_Timer >= 64) && (IR_Timer < 66)) { LED_L_PIN = 0; LED_R_PIN = 0; } /*L:1 R:1*/ else if((IR_Timer >= 66) && (IR_Timer < 69)) { LED_L_PIN = 1; LED_R_PIN = 1; } else if(IR_Timer == 69) { LED_L_PIN = 0; LED_R_PIN = 0; } /*L:0 R:0*/ else if(IR_Timer == 70) { LED_L_PIN = 1; LED_R_PIN = 1; } else if((IR_Timer >= 71) && (IR_Timer < 73)) { LED_L_PIN = 0; LED_R_PIN = 0; } /*L:1 R:1*/ else if((IR_Timer >= 73) && (IR_Timer < 76)) { LED_L_PIN = 1; LED_R_PIN = 1; } else { LED_L_PIN = 0; LED_R_PIN = 0; } } else if((IR_Timer >= 100)) { IR_Timer = 0; LED_FF_PIN = 0; LED_L_PIN = 0; LED_R_PIN = 0; } /*****************************************************************************/ } /**************************统状态为充电电极接触状态***************************/ else if(System_State == 1) { /*****************************指示灯部分代码程序******************************/ /*接触时,指示灯常量*/ Signal_LED_PIN = 1; Signal_LED_PIN_Timer = 0; /*****************************************************************************/ /************************发射检测信号部分代码程序*****************************/ if(Check_Signal_Timer < 190) { Check_Signal_PIN = 1; } else if((Check_Signal_Timer >= 197) && (Check_Signal_Timer < 200 )) { Check_Signal_PIN = 0; } else if(Check_Signal_Timer >= 200) { Check_Signal_Timer = 0; Check_Signal_PIN = 1; } /*****************************************************************************/ } } }
void main (void) { LEDDirections Direction = LEFT2RIGHT; BOOL SwitchPressed = FALSE; LED_Display = 1; // initialize // Init I/O TRISD = 0b00000000; // PORTD bits 7:0 are all outputs (0) TRISAbits.TRISA0 = 1; // TRISA0 input INTCON2bits.RBPU = 0; // enable PORTB internal pullups WPUBbits.WPUB0 = 1; // enable pull up on RB0 // ADCON1 is now set up in the InitADC() function. TRISBbits.TRISB0 = 1; // PORTB bit 0 (connected to switch) is input (1) // Init Timer0 Timer0_Init(); // Init ADC ADC_Init(); while (1) { if (Direction == LEFT2RIGHT) { LED_Display <<= 1; // rotate display by 1 from 0 to 7 if (LED_Display == 0) LED_Display = 1; // rotated bit out, so set bit 0 } if (Direction == RIGHT2LEFT) { LED_Display >>= 1; // rotate display by 1 from 7 to 0 if (LED_Display == 0) LED_Display = 0x80; // rotated bit out, so set bit 7 } LATD = LED_Display; // output LED_Display value to PORTD LEDs do { // poll the switch while waiting for the timer to roll over. if (Switch_Pin == 1) { // look for switch released. SwitchPressed = FALSE; } else if (SwitchPressed == FALSE) // && (Switch_Pin == 0) due to if-else { // switch was just pressed SwitchPressed = TRUE; // change direction if (Direction == LEFT2RIGHT) Direction = RIGHT2LEFT; else Direction = LEFT2RIGHT; } } while (INTCONbits.TMR0IF == 0); // Timer expired INTCONbits.TMR0IF = 0; // Reset Timer flag // Take an ADC conversion and use it to set Timer0 TMR0H = ADC_Convert(); // MSB from ADC TMR0L = 0; // LSB = 0 }
void main() { INT8U i=0; INT8U search_temp = 0x06; // search_temp 可以增加到6 在search_temp小于3的时候 开始发送广播唤醒 CpuInit(); POWER_UP_RESET_CC1100(); halRfWriteRfSettings(); halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8); CC1101_Setwor(); G_IT_ON; // 开启单片机全局中断 Usart_printf(&g_module_id.Sn[0],1); Usart_printf(&g_module_id.Sn[1],1); Usart_printf(&g_gateway.Sn[0],1); Usart_printf(&g_gateway.Sn[1],1); Log_printf(" "); // 上电设置网关 // 只有外部中断没有打开,现在进行设置网关字节 地址和网管不能为全0xFFFF while( ( 0xFFFF == g_gateway.Sn_temp ) || ( 0xFFFF == g_module_id.Sn_temp ) ) { LED_D4 = ~LED_D4; delay(50000); if( 0x55 == g_rx_flag ) { g_rx_flag = 0x00; // 将网关数据写入 IapProgramByte(GATEWAY_ADDRESS,TxBuf[1]); IapProgramByte(GATEWAY_ADDRESS+1,TxBuf[2]); //g_gateway.Sn_temp = IapReadByte(GATEWAY_ADDRESS); // 将地址数据写入 IapProgramByte(MODEL_SN_ADDRESS,TxBuf[3]); IapProgramByte(MODEL_SN_ADDRESS+1,TxBuf[4]); //g_module_id.Sn_temp = IapReadByte(MODEL_SN_ADDRESS); Log_printf("GATEWAY OK\n"); Log_printf("MODEL_SN OK\n"); LED_D4 = 0; // 读取设置数据 IapReadModelSn(MODEL_SN_ADDRESS,&g_module_id); IapReadModelSn(GATEWAY_ADDRESS,&g_gateway); } } // g_module_rpl = IapReadByte(MODEL_RPL); // 默认模块id的路由等级1 模块ID 最高位为0 表示路由模块 g_module_id.Sn[0] |= ( (g_module_rpl<<4) & 0x7F ); g_pre_src = g_module_id.Sn_temp; Usart_printf(&g_module_id.Sn[0],1); Usart_printf(&g_module_id.Sn[1],1); Usart_printf(&g_gateway.Sn[0],1); Usart_printf(&g_gateway.Sn[1],1); Log_printf(" initialization ok "); // 地址网关设置完成 LED_D2 = ~LED_D2; // 读出搜索模式 首次上电为0xFF 则进行搜索 g_search = IapReadByte(SEARCH_MODE); if( 0xFF == g_search ) { SearchMode: while( search_temp-- != 0 ) { SearchData[2] = g_rid; // 网关地址 SearchData[4] = g_gateway.Sn[0]; SearchData[5] = g_gateway.Sn[1]; // 源地址(模块ID) SearchData[9] = g_module_id.Sn[0]; SearchData[10] = g_module_id.Sn[1]; // 目的地址(网关地址) SearchData[11] = g_gateway.Sn[0]; SearchData[12] = g_gateway.Sn[1]; for( i=0;i<13;i++) { SearchData[13] += SearchData[i]; } // 进行唤醒时,只需要把路由标识滤除即可 将路由标识高字节分出一位代表是模块还是基站 // 首先发送唤醒波,而后发送数据 进行路由搜索时,使用广播唤醒 if( search_temp < 3 ) CC1101_Wakeupcarry(WorCarry, 2,4); halRfSendPacket(SearchData, 14); g_rid++; // 发送完成后g_rid自增 //g_wor_flag = 0x55; timer = 0; Timer0_Init(10); TIMER0_ON; //g_search = 0x55; g_enter_rx = 0x55; // 存放校验和 SearchData[13] = 0x00; goto EnterRx; } g_search = 0x00; } // else // { // g_search = 0x00; // } while (1) { //Log_printf("Enter wor\n"); if( 0x55 == g_wor_flag ) { CC1101_Worwakeup(); // 将接收的数据存储到RxBuf数组中 EnterRx: while(g_enter_rx) halRfRxPacket(RfRecBuf); // 此处进行rf数据处理 if( 0x55 == g_rf_rx_flag ) { g_rf_rx_flag = 0x00; RfRouteManage(&rf_route_data); } if( 0xFF == g_search ) // 若没有搜索到路径,则跳转回搜索路径 goto SearchMode; } halSpiStrobe(CCxxx0_SWORRST); // 复位到 事件1 halSpiStrobe(CCxxx0_SWOR); // 启动WOR INT1_ON; // 开外部中断 PCON |= PD_ON; // 从掉电模式唤醒后,程序从这行开市 //Log_printf("Exit pd\n"); } }
void Init_Peripherals(void) { Init_Ports(); Timer0_Init(); }