void configure_ext_int(void) { //----------------------channel 2----------------------------------------- extint_chan_get_config_defaults(&eic_conf_2); eic_conf_2.gpio_pin = OW_IN_PIN_INT; eic_conf_2.gpio_pin_mux = OW_IN_PIN_MUX; eic_conf_2.gpio_pin_pull = EXTINT_PULL_NONE; eic_conf_2.detection_criteria = EXTINT_DETECT_RISING; eic_conf_2.filter_input_signal = true; extint_chan_set_config( OW_IN_INT_CHANNEL, &eic_conf_2); // Register and enable the callback function extint_register_callback(extint_user_callback_2, OW_IN_INT_CHANNEL,EXTINT_CALLBACK_TYPE_DETECT); extint_chan_enable_callback(OW_IN_INT_CHANNEL,EXTINT_CALLBACK_TYPE_DETECT); }
void configure_tc0(void) { struct tc_config config_tc; tc_get_config_defaults(&config_tc); config_tc.counter_size = TC_COUNTER_SIZE_16BIT; config_tc.wave_generation = TC_WAVE_GENERATION_MATCH_FREQ; config_tc.counter_16_bit.compare_capture_channel[0] = 350; config_tc.clock_prescaler=TC_CLOCK_PRESCALER_DIV8; config_tc.clock_source=GCLK_GENERATOR_0; config_tc.reload_action=TC_RELOAD_ACTION_RESYNC; tc_init(&tc_instance_tc0, TC0, &config_tc); tc_enable(&tc_instance_tc0); tc_stop_counter(&tc_instance_tc0); }
inline void my_delay_us(uint32_t usec) { volatile uint32_t ctr_reg_shdw ; ctr_reg_shdw = SysTick->CTRL; // Clear count flag by reading reg SysTick->LOAD = (usec) * (7880000 / 1000000); SysTick->VAL = 0; SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; // 0x5 // wait for flag, do not need interrupts here while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)); }
void extint_user_callback_2(void) { if(reset_flag==0) { reset_flag=1; tc_set_count_value(&tc_instance_tc0, 0); tc_start_counter(&tc_instance_tc0); port_pin_set_output_level(LED_0_PIN, 0); } else { tc_set_count_value(&tc_instance_tc0, 0); reset_flag=0; } }
void tc0_callback(struct tc_module *const module_inst) { volatile unsigned char tmp=0; if(port_pin_get_input_level(PIN_PA04)!=0) { while((OW_check_in_level())!=0); if(reset_flag==1) { reset_flag=2; SendPresense(); tmp=0; tmp=get_byte(); if (tmp==0xf0) { search_rom(); mute_on=0; reset_flag=0; } else { // comeend "read UID" if(tmp==0x33) { //send UID for(unsigned char i=0;i<8;i++) send_byte(ownuid[i]); } // match rom if(tmp==0x55) { // wait for own UID+ command+address // potential problem: if less than 10 bytes received program hangs here for(unsigned char i=0;i<10;i++) in_buffer[i]=get_byte(); // if own UID if((in_buffer[0]==ownuid[0])&&(in_buffer[1]==ownuid[1])&&(in_buffer[2]==ownuid[2]) &&(in_buffer[3]==ownuid[3])&&(in_buffer[4]==ownuid[4])&&(in_buffer[5]==ownuid[5]) &&(in_buffer[6]==ownuid[6])&&(in_buffer[7]==ownuid[7])) { // if command 0xA0 (reading) if(in_buffer[8]==0xa0) { // my_delay_us(3); // what address to read from? if(in_buffer[9]==0) { // count crc registers[0]=0; registers[0] = OWI_ComputeCRC8(registers[1], registers[0]); // send value and crc send_byte(registers[1]); send_byte(registers[0]); } ….. } // if command 0xA1 (write) if(in_buffer[8]==0xa1) { write_flag=1; in_buffer[10]=get_byte(); //my_delay_us(3); // what address to write to if((in_buffer[9]==0)||(in_buffer[9]==1)||(in_buffer[9]==2)) { //nothing to do, as they are read only } if(in_buffer[9]==3) { registers[4]=in_buffer[10]; } …. } }// end of if my_uid }//end of if(tmp==0x55) } }//end of if(reset_flag==1) } //end of if((PORTD.IN&0x01)==0) else reset_flag=0; }
Source: https://habr.com/ru/post/275377/
All Articles