/** ******************************************************************************************** Function Name : process_adc() Description : Input : Void Output : Void Note : ********************************************************************************************** */ void process_adc(void) { U16 adc_value = 0; U8 index = 0; adc_value = adc_read(ADC0, CHANNEL_3); index = GET_SCALED_LED_MASK_BUF_INDEX(adc_value); if(index > sizeof(led_mask_buf)) index = sizeof(led_mask_buf) -1; turn_on_led(led_mask_buf[index]); }
void fault_interrupt(void) { ++ fault_counter; // Schalte nacheinander 5 LEDs ein for (uint8_t led = 1; led < 6; ++led) { for (volatile int i = 0; i < 100000; ++i) ; turn_on_led(led); } __asm("push {r0-r7}\n\t" // Lade Funktionsadresse, die nach Rückkehr aus Interrupt ausgeführt werden soll "ldr r0, =executed_after_fault_interrupt\n\t" "str r0, [sp, #20]\n\t" // lr "str r0, [sp, #24]\n\t" // PC "mov r0, #0x01000000\n\t" "str r0, [sp, #28]\n\t" // PSR "mov lr, #0xfffffff9\n\t" // Interrupt Return mit MSP Stack Pointer, da PSP korrupt ! "bx lr\n\t" ); }
void nmi_interrupt(void) { ++ nmi_counter; turn_on_led(1); while (1); }
int main(void) { u8 t; u8 len; u8 acc_flag = 0; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2 delay_init(168); //延时初始化 uart_init(115200); //串口初始化波特率为115200 LED_Init(); //初始化与LED连接的硬件接口 IIC_Init(); while(1) { if(USART_RX_STA&0x8000) { printf("\n"); len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度 for(t=0;t<len;t++) { USART_SendData(USART2, USART_RX_BUF[t]); //向串口2发送数据 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//等待发送结束 } string_to_value_new(); if(COMD_FLAG != 1) { if(COMMAND_BUF[0]-'l' == 0 && COMMAND_BUF[1]-'e' == 0 && COMMAND_BUF[2]-'d' == 0) { stop_timer(); stop_pwm(); acc_flag = 0; turn_off_led(0); if(FACTOR[0] != 3 && FACTOR[0] != 4 && FACTOR[0] != 5 && FACTOR[0] != 6) { printf("\n命令错误!请重新输入!参数为3|4|5|6"); } else { printf("\nLED控制: %d",FACTOR[0]); turn_on_led(FACTOR[0]); } } else if(COMMAND_BUF[0]-'p' == 0 && COMMAND_BUF[1]-'w' == 0 && COMMAND_BUF[2]-'m' == 0) { stop_timer(); acc_flag = 0; turn_off_led(0); if(FACTOR[0] - 100 > 0 || FACTOR[0] - 0 < 0) { printf("\n命令错误!请重新输入!参数范围在0到100"); } else { printf("\nPWM占空比:%d",FACTOR[0]); setPWMvalue(FACTOR[0]); } } else if(COMMAND_BUF[0]-'t' == 0 && COMMAND_BUF[1]-'i' == 0 && COMMAND_BUF[2]-'m' == 0) { stop_pwm(); acc_flag = 0; turn_off_led(0); if(FACTOR[0] > 10000 || FACTOR[0] < 20) { printf("\n命令错误!请重新输入!参数范围在20到10000"); } else { printf("\nTIMER定时时间:%d ",FACTOR[0]); send_str("ms"); set_TIM3_time(FACTOR[0]); } } else if(COMMAND_BUF[0]-'a' == 0 && COMMAND_BUF[1]-'c' == 0 && COMMAND_BUF[2]-'c' == 0) { acc_flag = 1; if(FACTOR[0] != 0 && FACTOR[0] != 1 && FACTOR[0] != 2) { printf("\n参数输入有误;默认输出Z轴!"); } } else { acc_flag = 0; turn_off_led(0); printf("\n命令错误!请重新输入!"); } } } if(acc_flag) { LSM303_ACC_Init(); LSM_ACC_Read(); ACC_Result_XYZ(); switch(FACTOR[0]) { case 0: printf("\nX轴:%f", ACC_XYZ[0]); break; case 1: printf("\nY轴:%f", ACC_XYZ[1]); break; case 2: printf("\nZ轴:%f.3", ACC_XYZ[2]); break; default:printf("\nZ轴:%f", ACC_XYZ[2]); } delay_ms(100); } }; }
/* Testet coreinterrupt_e Die Funktion generate_coreinterrupt wird danach getestet, ob ein Interrupt tatsächlich generiert und ausgeführt wird. Danach werden die Interrupts maskiert und is_coreinterrupt und clear_coreinterrupt werden getestet. Die Funktion ispending liefert 1, wenn ein generierter Interrupt auf Ausführung wartet, d.h. die Priorität ist nicht ausreichend oder alle Interrupts wurden maskiert. Die Funktion clearpending löscht das Pending-Flag des Interrupts, so dass ispending 0 liefert und der Interrupt daher nicht mehr ausgeführt wird. */ int main(void) { uint8_t led = 0; unsigned i; enable_gpio_clockcntrl(GPIO_PORTA_BIT/*switch*/|GPIO_PORTE_BIT/*led*/); config_input_gpio(GPIO_PORTA, GPIO_PIN0, GPIO_PULL_OFF); config_output_gpio(GPIO_PORTE, GPIO_PINS(15,8)); // Teste Interrupt Ausführung for (i = 0; i <= 16; ++i) { for (unsigned p = 0; p < sizeof(pos)/sizeof(pos[0]); ++p) { pos[p] = 0; } switch (i) { case coreinterrupt_NMI: case coreinterrupt_MPUFAULT: case coreinterrupt_BUSFAULT: case coreinterrupt_USAGEFAULT: case coreinterrupt_SVCALL: case coreinterrupt_DEBUGMONITOR: case coreinterrupt_PENDSV: case coreinterrupt_SYSTICK: if (generate_coreinterrupt((coreinterrupt_e)i) != 0) goto ONERR; if ( i == coreinterrupt_MPUFAULT || i == coreinterrupt_BUSFAULT || i == coreinterrupt_USAGEFAULT) { if (counter != 0) goto ONERR; // pending if (enable_coreinterrupt(i) != 0) goto ONERR; if (counter != 1 || pos[i] != 1) goto ONERR; // interrupt executed if (disable_coreinterrupt(i) != 0) goto ONERR; } else { if (counter != 1 || pos[i] != 1) goto ONERR; // interrupt executed if (EINVAL != enable_coreinterrupt(i)) goto ONERR; if (EINVAL != disable_coreinterrupt(i)) goto ONERR; } if (counter != 1) goto ONERR; // no second interrupt turn_on_led(led++); for (int i = 0; i < 50000; ++i) ; counter = 0; break; default: if (generate_coreinterrupt((coreinterrupt_e)i) != EINVAL) goto ONERR; if (counter != 0) goto ONERR; break; } } // Teste Interrupt Pending for (i = 0; i <= 16; ++i) { for (unsigned p = 0; p < sizeof(pos)/sizeof(pos[0]); ++p) { pos[p] = 0; } switch (i) { case coreinterrupt_MPUFAULT: case coreinterrupt_BUSFAULT: case coreinterrupt_USAGEFAULT: case coreinterrupt_SVCALL: case coreinterrupt_DEBUGMONITOR: case coreinterrupt_PENDSV: case coreinterrupt_SYSTICK: enable_coreinterrupt(i); disable_all_interrupt(); if (generate_coreinterrupt((coreinterrupt_e)i) != 0) goto ONERR; if (is_coreinterrupt((coreinterrupt_e)i) != 1) goto ONERR; for (unsigned p = 0; p < 16; ++p) { if (is_coreinterrupt((coreinterrupt_e)p) != (p==i)) goto ONERR; } if (clear_coreinterrupt((coreinterrupt_e)i) != 0) goto ONERR; if (is_coreinterrupt((coreinterrupt_e)i) != 0) goto ONERR; for (unsigned p = 0; p < 16; ++p) { if (is_coreinterrupt((coreinterrupt_e)p) != 0) goto ONERR; } enable_all_interrupt(); if (counter != 0) goto ONERR; disable_coreinterrupt(i); turn_on_led(led++); for (int i = 0; i < 50000; ++i) ; break; case coreinterrupt_NMI: // not maskable default: if (is_coreinterrupt((coreinterrupt_e)i) != 0) goto ONERR; if (clear_coreinterrupt((coreinterrupt_e)i) != EINVAL) goto ONERR; if (counter != 0) goto ONERR; break; } } // Teste Interrupt Priority for (i = 0; i <= 16; ++i) { switch (i) { case coreinterrupt_MPUFAULT: case coreinterrupt_BUSFAULT: case coreinterrupt_USAGEFAULT: case coreinterrupt_SVCALL: case coreinterrupt_DEBUGMONITOR: case coreinterrupt_PENDSV: case coreinterrupt_SYSTICK: enable_coreinterrupt(i); if (setpriority_coreinterrupt((coreinterrupt_e)i, 3) != 0) goto ONERR; if (getpriority_coreinterrupt((coreinterrupt_e)i) != 3) goto ONERR; setbasepriority_interrupt(3); if (generate_coreinterrupt((coreinterrupt_e)i) != 0) goto ONERR; if (is_coreinterrupt((coreinterrupt_e)i) != 1) goto ONERR; if (setpriority_coreinterrupt((coreinterrupt_e)i, 2) != 0) goto ONERR; if (getpriority_coreinterrupt((coreinterrupt_e)i) != 2) goto ONERR; if (is_coreinterrupt((coreinterrupt_e)i) != 0) goto ONERR; if (counter != 1 || pos[i] != 1) goto ONERR; // interrupt executed counter = 0; // reset if (setpriority_coreinterrupt((coreinterrupt_e)i, 0) != 0) goto ONERR; if (getpriority_coreinterrupt((coreinterrupt_e)i) != 0) goto ONERR; disable_coreinterrupt(i); turn_on_led(led++); for (int i = 0; i < 50000; ++i) ; break; case coreinterrupt_NMI: // not maskable with priority default: break; } } // Teste disable_coreinterrupt bei MPUFAULT, BUSFAULT, USAGEFAULT generate_coreinterrupt(coreinterrupt_MPUFAULT); generate_coreinterrupt(coreinterrupt_BUSFAULT); generate_coreinterrupt(coreinterrupt_USAGEFAULT); if (is_coreinterrupt(coreinterrupt_MPUFAULT) != 1) goto ONERR; if (is_coreinterrupt(coreinterrupt_BUSFAULT) != 1) goto ONERR; if (is_coreinterrupt(coreinterrupt_USAGEFAULT) != 1) goto ONERR; clear_coreinterrupt(coreinterrupt_MPUFAULT); clear_coreinterrupt(coreinterrupt_BUSFAULT); clear_coreinterrupt(coreinterrupt_USAGEFAULT); if (is_coreinterrupt(coreinterrupt_MPUFAULT) != 0) goto ONERR; if (is_coreinterrupt(coreinterrupt_BUSFAULT) != 0) goto ONERR; if (is_coreinterrupt(coreinterrupt_USAGEFAULT) != 0) goto ONERR; if (counter != 0) goto ONERR; // 2 grüne LEDs turn_on_led(3); write1_gpio(GPIO_PORTE, GPIO_PIN11|GPIO_PIN15); while (1) ; ONERR: // 2 rote LEDs enable_all_interrupt(); enable_fault_interrupt(); write1_gpio(GPIO_PORTE, GPIO_PIN9|GPIO_PIN13); while (1) ; }