int main (void) { En_RC32M(); //Enable LowLevel & HighLevel Interrupts PMIC_CTRL |= PMIC_HILVLEN_bm | PMIC_LOLVLEN_bm |PMIC_MEDLVLEN_bm; PORT_init(); TimerD0_init(); TimerC0_init(); TimerE1_init(); USARTE0_init(); ADCA_init(); //wdt_enable(); // Globally enable interrupts sei(); //Address[0]=Address[0] + RobotID ; //// NRF Initialize NRF_init () ; while(1) { asm("wdr"); // BUZZER //adc = adc +(adc_get_unsigned_result(&ADCA,ADC_CH0)-adc)*0.01; adc = adc_get_unsigned_result(&ADCA,ADC_CH0); if (adc<=2250)//10 volt Buzzer_PORT.OUTSET = Buzzer_PIN_bm; else if(shoot_alarm_flg && (charge_count>=30000)) Buzzer_PORT.OUTSET = Buzzer_PIN_bm; else Buzzer_PORT.OUTCLR = Buzzer_PIN_bm; //SHOOT PORTC_OUTCLR=KCK_SH_PIN_bm; if((KCK_Ch_Limit_PORT.IN & KCK_Ch_Limit_PIN_bm)>>KCK_Ch_Limit_PIN_bp) { full_charge=1; tc_disable_cc_channels(&TCC0,TC_CCDEN); charge_count=0; } else { if((flg || flg_sw)==0) { tc_enable_cc_channels(&TCC0,TC_CCDEN); } } if (charge_flg)//full_charge { if (Robot_D[RobotID].KCK ) { if( KCK_Sens || (Robot_D[RobotID].KCK%2)) { flg = 1; } } } if (KCK_DSH_SW)//bazi vaghta begir nagir dare { flg_sw = 1; } if (free_wheel >= 500 ) { NRF_init(); } if(KCK_Sens) LED_Green_PORT.OUTSET = LED_Green_PIN_bm; else LED_Green_PORT.OUTCLR = LED_Green_PIN_bm; // //motor test //switch(flag2sec) //{ case 200: //// M.Setpoint=1000; //Robot_D[RobotID].M0b = 0xE8;//low //Robot_D[RobotID].M0a = 0X03;//high //break; // //case 400: ////M.Setpoint=2000; //Robot_D[RobotID].M0b = 0xD0;//low //Robot_D[RobotID].M0a = 0X07;//high //break; // //case 600: ////M.Setpoint=500; //Robot_D[RobotID].M0b = 0xF4;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 800: ////M.Setpoint=4000; //Robot_D[RobotID].M0b = 0xA0;//low //Robot_D[RobotID].M0a = 0X0F;//high //break; // //case 1000: ////M.Setpoint=1000; //Robot_D[RobotID].M0b = 0xE8;//low //Robot_D[RobotID].M0a = 0X03;//high //break; // //case 1200: ////M.Setpoint=500; //Robot_D[RobotID].M0b = 0xF4;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 1400: ////M.Setpoint=-500; //Robot_D[RobotID].M0b = 0x0C;//low //Robot_D[RobotID].M0a = 0XFE;//high //break; // //case 1600: ////M.Setpoint=400; //Robot_D[RobotID].M0b = 0x90;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 1800: ////M.Setpoint=350; //Robot_D[RobotID].M0b = 0x5E;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 2000: ////M.Setpoint=340; //Robot_D[RobotID].M0b = 0x54;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 2200: ////M.Setpoint=330; //Robot_D[RobotID].M0b = 0x4A;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 2600: ////M.Setpoint=100; //Robot_D[RobotID].M0b = 0x64;//low //Robot_D[RobotID].M0a = 0X00;//high //break; // //case 2800: ////M.Setpoint=50; //Robot_D[RobotID].M0b = 0x32;//low //Robot_D[RobotID].M0a = 0X00;//high //break; // //case 3000: ////M.Setpoint=1000; //Robot_D[RobotID].M0b = 0xE8;//low //Robot_D[RobotID].M0a = 0X03;//high //break; // //case 3200: ////M.Setpoint=-50; //Robot_D[RobotID].M0b = 0xCE;//low //Robot_D[RobotID].M0a = 0XFF;//high //flag2sec=0; //break; // //} //Robot_D[RobotID].M0b = 0xD0;//0X18;//-1000//01;//low37121 //Robot_D[RobotID].M0a = 0x07;//0XFC;//high //Robot_D[RobotID].M1b = 0XE8;//2000//ghalat17325 //Robot_D[RobotID].M1a = 0X03; //Robot_D[RobotID].M2b = 0XDC;//1000//low13703 //Robot_D[RobotID].M2a = 0X05;//high //Robot_D[RobotID].M3b = 0xF4;//3000//32;//ghalat30258 //Robot_D[RobotID].M3a = 0X01;//76; ////SEND TEST DATA TO FT232 //char str1[20]; //uint8_t count1 = sprintf(str1,"%d\r",adc); // //for (uint8_t i=0;i<count1;i++) //{ //usart_putchar(&USARTE0,str1[i]); // //} //usart_putchar(&USARTE0,'a'); }
void avr_init(void) { //!! Set-up F_CPU: BEGIN //PS. Default FREQ wo settings 2Mhz from internal factory calibrated RC //Config32MHzClock_Internal(); // INTERNAL RC 32Mhz factory calibrated with accuracy of 1% at 3V and 25°C Config32MHzClock_External_XTAL_8Mhz_x_PLL4(); // EXT XTAL 8Mhz to SYSCLOCK 32Mhz with PLLx4 //Config32MHzClock_External_XTAL_16Mhz_x_PLL2(); // EXT XTAL 16Mhz to SYSCLOCK 32Mhz with PLLx2 //!! Set-up F_CPU: END // make clkout on PORTC:7 PORTCFG.CLKEVOUT = PORTCFG_CLKOUT_PC7_gc; PORTC.DIR = (1<<7); // clkout //TCC0 not used here /* // configure timer/counter0 TCC0.CTRLA = 0x7; // clk/1024 TCC0.CNT = 0; // reset count */ // configure PORTA:6 as output to LED && All other pins of PORTA as input // Classic style /*PORTA.DIR |= (1<<LED); // PORTA.6 is out PORTA.DIR &= ~(1<<SW1); // PORTA.7 is in*/ // XMEGA style PORTA.DIRSET = 1<<LED; // PORTA.6 is out - DIRSET устанавливает битовую маску на <1> PORTA.DIRCLR = 1<<SW1; // PORTA.7 is in - DIRCLR устанавливает битовую маску на <0> PORTA.PIN6CTRL |= PORT_OPC_TOTEM_gc; // Totem-pole type (default - PUSH-PULL analog for STM32) // additional cfg for SW1 input pin for pull-up: PORTA.PIN7CTRL |= PORT_OPC_PULLUP_gc; // Totempole w/ Pull-up on Input PULL-UP //PORTA.PIN7CTRL |= PORT_OPC_PULLDOWN_gc; // Totempole w/ Pull-down on Input PULL-DOWN //PORTA.PIN7CTRL |= PORT_OPC_TOTEM_gc; // Totempole - FLOATING - DEFAULT //USARTE0 init USARTE0_init(); //!!Redirect STDOUT (printf etc..) stdout = &USARTE0_stdout; // Template to redirect STDOUT&STDIN /* stdout = stdin = &uart1_str; */ //USARTE0_WriteString("Hello from XMEGA USARTE0!\r\n"); // Classic style to waste RAM Print Out //USARTE0_WriteString_P(PSTR("\r\nXMEGA USARTE0 RX IRQ example..\r\n")); // for Save RAM style Print Out //!! TCE0 setup for 1Hz IRQ TCE0_init(); //Micros timer init TCD0_init_micros(); //!! Enable LO LVL IRQ && Round robin mechanism for LOW LEVEL IRQ //PMIC.CTRL = PMIC_HILVLEN_bm; // enable high level interrupts PMIC.CTRL = PMIC_RREN_bm | PMIC_LOLVLEN_bm; //!! Set-up Virtual port0 to PORTA virtual_port_init(); sei(); // enable interrupts }
void avr_init(void) { //!! Set-up F_CPU: BEGIN //PS. Default FREQ wo settings 2Mhz from internal factory calibrated RC //Config32MHzClock_Internal(); // INTERNAL RC 32Mhz factory calibrated with accuracy of 1% at 3V and 25°C Config32MHzClock_External_XTAL_8Mhz_x_PLL4(); // EXT XTAL 8Mhz to SYSCLOCK 32Mhz with PLLx4 //Config32MHzClock_External_XTAL_16Mhz_x_PLL2(); // EXT XTAL 16Mhz to SYSCLOCK 32Mhz with PLLx2 //!! Set-up F_CPU: END // make clkout on PORTC:7 PORTCFG.CLKEVOUT = PORTCFG_CLKOUT_PC7_gc; PORTC.DIR = (1<<7); // clkout //TCC0 not used here /* // configure timer/counter0 TCC0.CTRLA = 0x7; // clk/1024 TCC0.CNT = 0; // reset count */ // configure PORTA:6 as output to LED && All other pins of PORTA as input // Classic style /*PORTA.DIR |= (1<<LED); // PORTA.6 is out PORTA.DIR &= ~(1<<SW1); // PORTA.7 is in*/ // XMEGA style PORTA.DIRSET = 1<<LED; // PORTA.6 is out - DIRSET устанавливает битовую маску на <1> PORTA.DIRCLR = 1<<SW1; // PORTA.7 is in - DIRCLR устанавливает битовую маску на <0> // additional cfg for SW1 input pin for pull-up: PORTA.PIN7CTRL |= PORT_OPC_PULLUP_gc; // Totempole w/ Pull-up on Input PULL-UP //PORTA.PIN7CTRL |= PORT_OPC_PULLDOWN_gc; // Totempole w/ Pull-down on Input PULL-DOWN //PORTA.PIN7CTRL |= PORT_OPC_TOTEM_gc; // Totempole - FLOATING - DEFAULT //USARTE0 init USARTE0_init(); //!!Redirect STDOUT (printf etc..) stdout = &USARTE0_stdout; // Template to redirect STDOUT&STDIN /* stdout = stdin = &uart1_str; */ //USARTE0_WriteString("Hello from XMEGA USARTE0!\r\n"); // Classic style to waste RAM Print Out //USARTE0_WriteString_P(PSTR("\r\nXMEGA USARTE0 RX IRQ example..\r\n")); // for Save RAM style Print Out //!! TCE0 setup for 1Hz IRQ TCE0_init(); //Micros timer init TCD0_init_micros(); //!!********************TCC0 as FREQ OUT (freq generator) //PC0 define (OCA) as Output PORTC.DIRSET = 0x01; //Set Timerprescaler TCC0.CTRLA = TC_CLKSEL_DIV4_gc; // Select the output frequency and output enable Compare A TCC0.CTRLB = TC_WGMODE_FRQ_gc | TC0_CCAEN_bm; // Set comparison value of CCA //TCC0.CCA = 0xffff; // ~ 61Hz T=250ns*65536 //TCC0.CCA = 40000-1; // 100Hz T=250ns*40000 //TCC0.CCA = 4000-1; // 1kHz T=250ns*4000 //TCC0.CCA = 800-1; // 5kHz T=250ns*800 //TCC0.CCA = 400-1; // 10kHz T=250ns*400 //TCC0.CCA = 80-1; // 50kHz T=250ns*80 //TCC0.CCA = 40-1; // 100kHz T=250ns*40 //TCC0.CCA = 8-1; // 500kHz T=250ns*8 //TCC0.CCA = 0x3; // 1Mhz T=250ns*4 //TCC0.CCA = 0x1; // 2Mhz T=250ns*2 TCC0.CCA = 0x0; // 4Mhz i.e T=250ns*1 //!!PS Formula for FREQ OUT with SYSCLOCK = 32Mhz && TCC0 prescaler 4 // Step INC 250ns /* f = system clock / (2 * prescaler * (CCA + 1)) In our example, the frequency is then as follows: fmin = 32MHz / (2 * 4 * (65535 + 1) = 4Mhz / 65536 = ca. 61 Hz fmax = 32MHz / (2 * 4 * (0 + 1) = 4Mhz / 1 = 4Mhz */ //!! Enable LO LVL IRQ && Round robin mechanism for LOW LEVEL IRQ //PMIC.CTRL = PMIC_HILVLEN_bm; // enable high level interrupts PMIC.CTRL = PMIC_RREN_bm | PMIC_LOLVLEN_bm; sei(); // enable interrupts }