// pe // ------------------------------------- // program + const area : 0x4000 - 0xFFFF (paged) // data + bss area: 0x0800 - 0x2FFF // free ram: 0x3000 - 0x3FFF // stack: 0x2FFE // ------------------------------------- // library #include #include #include "pe.h" #define ADDR_PWM 0x00A0 #define ADDR_TIM 0x0040 // ------------------------------------ void main(void) { // port t, timer, input capture, output compare typedef struct{ char input_capture_output_compare_select; //0 char force_output_compare; //1 char output_compare_mask; //2 char output_compare_data; //3 short timer_count; //4 + 5 char timer_system_control; //6 char toggle_on_overflow; //7 char timer_control_1; //8 char timer_control_2; //9 char timer_control_3; //A char timer_control_4; //B char interrupt_enable; //C char timer_system_control_2; //D char main_interrupt_flag_1; //E char main_interrupt_flag_2; //F short input_capture_output_compare_chan0; //10 + 11 short input_capture_output_compare_chan1; //12 + 13 short input_capture_output_compare_chan2; //14 + 15 short input_capture_output_compare_chan3; //16 + 17 short input_capture_output_compare_chan4; //18 + 19 short input_capture_output_compare_chan5; //1A + 1B short input_capture_output_compare_chan6; //1C + 1D short input_capture_output_compare_chan7; //1E + 1F char pulse_accu_a_control; //20 char pulse_accu_a_flag; //21 char pulse_accu_count_3; //22 char pulse_accu_count_2; //23 char pulse_accu_count_1; //24 char pulse_accu_count_0; //25 char modulus_down_counter_control; //26 char modulus_down_counter_flag; //27 char input_control_pulse_accu; //28 char delay_counter_control; //29 char input_control_overwrite; //2A char input_control_system_control; //2B char dummy_2C; char dummy_2D; char dummy_2E; char dummy_2F; char pulse_accu_b_control; //30 char pulse_accu_b_flag; //31 char pulse_accu_holding_3; //32 char pulse_accu_holding_2; //33 char pulse_accu_holding_1; //34 char pulse_accu_holding_0; //35 short modulus_down_counter_count; //36 + 37 short input_capture_holding_0; //38 + 39 short input_capture_holding_1; //3A + 3B short input_capture_holding_2; //3C + 3D short input_capture_holding_3; //3E + 3F }TIMERmodule; typedef struct{ char enable; //0x00 char polarity; //0x01 char clock_select; //0x02 char presc_clk_sel; //0x03 char center_align; //0x04 char control; //0x05 char reserved_06; //0x07 char reserved_07; char scale_a; //0x08 char scale_b; //0x09 char counter_a; //0x0A char counter_b; //0x0B char counter0; //0x0C char counter1; //0x0D char counter2; //0x0E char counter3; //0x0F char counter4; //0x10 char counter5; //0x11 char counter6; //0x12 char counter7; //0x13 char period0; //0x14 char period1; //0x15 char period2; //0x16 char period3; //0x17 char period4; //0x18 char period5; //0x19 char period6; //0x1A char period7; //0x1B char duty0; //0x1C char duty1; //0x1D char duty2; //0x1F char duty3; //0x20 char duty4; //0x21 char duty5; //0x22 char duty6; //0x23 char duty7; //0x24 char shutd; //0x25 char reserved_37; //0x26 char reserved_38; //0x27 char reserved_39; //0x28 }PWMmodule; PWMmodule* pwm_ptr; TIMERmodule* timer_ptr; unsigned short capture_time = 0; unsigned short capture_time_old = 0; unsigned long int diff = 0; int overflow_count = 0; int i; // init serial communication for printf (8 MHz, no PLL) serial_asyn0_reg_ptr = (SerialAsynchronRegister*)SERIAL_ASYNC_0_BASE_ADR; serial_asyn0_reg_ptr->sci0bd = BAUD; serial_asyn0_reg_ptr->sci0cr1 = CONTROL1; serial_asyn0_reg_ptr->sci0cr2 = CONTROL2; // ---start of program printf("start\n\r"); pwm_ptr = (PWMmodule*)ADDR_PWM; //PWM Base Address pwm_ptr->polarity = 0x01; //chan0 = high->low pwm_ptr->clock_select = 0xFF; // clock sa, sb ; bus_clock = 8MHz pwm_ptr->presc_clk_sel = 0x07; // clock a = bus_clock / 128 (62500Hz) pwm_ptr->scale_a = 0xFF; //(122Hz->8,16ms) pwm_ptr->scale_b = 0xFF; //(122Hz->8,16ms) pwm_ptr->period0 = 0xF5; //8,16ms*245=2s pwm_ptr->period1 = 0xF5; //8,16ms*245=2s pwm_ptr->period3 = 0xF5; //8,16ms*245=2s pwm_ptr->duty0 = 0x7A; //2s/245*122=1s pwm_ptr->duty1 = 0x7A; //2s/245*122=1s pwm_ptr->duty3 = 0x7A; //2s/245*122=1s timer_ptr = (TIMERmodule*)ADDR_TIM; // TIME Base Address timer_ptr->input_capture_output_compare_select = 0x00; //Timerkanal 3 als Input timer_ptr->timer_system_control = 0x80; //timer enable timer_ptr->timer_system_control_2 = 0x87; //timer overflow enable, prescale-factor 128 timer_ptr->timer_control_3 = 0x00; //Capture disable timer_ptr->timer_control_4 = 0xC0; //Capture on any edge (rising or falling) timer_ptr->interrupt_enable = 0x08; //Interrupt 3 enable timer_ptr->main_interrupt_flag_1 = 0x08; // reset Interrupt 3 pwm_ptr->enable = 0x0F; //PWM Ausgange frei schalten for(i=0;i<20;i++) { if ((timer_ptr->main_interrupt_flag_1 & 0x08) == 0x08) { timer_ptr->main_interrupt_flag_1 = 0x08; // reset IR capture_time_old = timer_ptr->input_capture_output_compare_chan3; } for(;;) { // test overflow if ((timer_ptr->main_interrupt_flag_2 & 0x80) == 0x80) { timer_ptr->main_interrupt_flag_2 = 0x80; // reset overflow-IR overflow_count++; } // test capture-match if ((timer_ptr->main_interrupt_flag_1 & 0x08) == 0x08) { timer_ptr->main_interrupt_flag_1 = 0x08; // reset IR capture_time = timer_ptr->input_capture_output_compare_chan3; if (overflow_count == 0) { diff = (unsigned long int)capture_time - (unsigned long int)capture_time_old; } else { if (overflow_count == 1) { diff = ((unsigned long int)0x0000FFFF - (unsigned long int)capture_time_old) + (unsigned long int)capture_time; } else { if (overflow_count > 1) { diff = ((unsigned long int)0x0000FFFF - (unsigned long int)capture_time_old) + (unsigned long int)capture_time + (unsigned long int)(overflow_count -1) * (unsigned long int)0xFFFF; } } } printf("Differenz: %i Zeit_alt: %i Zeit_neu: %i Overflow: %i\n",(int)diff,(int)capture_time_old,(int)capture_time, overflow_count); capture_time_old = capture_time; overflow_count = 0; } } } // ----- end of main ----- }