Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ###################################################################
- ** Filename : main.c
- ** Project : semaforo
- ** Processor : MKL25Z128VLK4
- ** Version : Driver 01.01
- ** Compiler : GNU C Compiler
- ** Date/Time : 2017-03-29, 15:21, # CodeGen: 0
- ** Abstract :
- ** Main module.
- ** This module contains user's application code.
- ** Settings :
- ** Contents :
- ** No public methods
- **
- ** ###################################################################*/
- /*!
- ** @file main.c
- ** @version 01.01
- ** @brief
- ** Main module.
- ** This module contains user's application code.
- */
- /*!
- ** @addtogroup main_module main module documentation
- ** @{
- */
- /* MODULE main */
- /* Including needed modules to compile this module/procedure */
- #include "Cpu.h"
- #include "Events.h"
- #include "car_stop.h"
- #include "BitIoLdd1.h"
- #include "car_att.h"
- #include "BitIoLdd2.h"
- #include "car_go.h"
- #include "BitIoLdd3.h"
- #include "ped_stop.h"
- #include "BitIoLdd4.h"
- #include "ped_go.h"
- #include "BitIoLdd5.h"
- #include "light_sens.h"
- #include "AdcLdd1.h"
- #include "ped_button.h"
- #include "ExtIntLdd1.h"
- #include "TI1.h"
- #include "TimerIntLdd1.h"
- #include "TU1.h"
- /* Including shared modules, which are used for whole project */
- #include "PE_Types.h"
- #include "PE_Error.h"
- #include "PE_Const.h"
- #include "IO_Map.h"
- /* User includes (#include below this line is not maintained by Processor Expert) */
- #include "semaf.h"
- /*lint -save -e970 Disable MISRA rule (6.3) checking. */
- // Flags de controle
- volatile int st = 0; // Indicador de estado
- volatile int night = 0; // Indicador de período noturno, 0 - dia 1 - noite
- volatile int button = 0; // Indicador de botão pressionado
- int semafCar; // Indicador de estado do semáforo de carros
- int semafPed; // Indicador de estado do semáforo de pedestres
- uint16_t light_sens; // Valor do sensor LDR
- // Contadores
- volatile unsigned int count_1 = 0;
- volatile unsigned int count_2 = 0;
- int main(void)
- /*lint -restore Enable MISRA rule (6.3) checking. */
- {
- /* Write your local variable definition here */
- const unsigned int sens_threshold = 45000;
- const unsigned int maxCount_1 = 2000; // 2 s
- const unsigned int maxCount_2 = 1000; // 1,2 s
- const unsigned int maxCount_3 = 250; // 250 ms
- int low_light = 0; // Indicador de luminosidade, 0 - alta, 1 - baixa
- /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
- PE_low_level_init();
- /*** End of Processor Expert internal initialization. ***/
- /* Write your code here */
- for(;;){
- while(st == 0){ // Início - semáforo para carros: verde, pedestres: vermelho, período: dia
- light_sens = light_sens_value(); // Coleta o valor do sensor LDR
- if(light_sens > sens_threshold){ // Verificação do valor de tensão do sensor LDR
- if(low_light == 0){
- low_light = 1;
- count_2 = 0;
- }
- }
- if(count_2 > maxCount_1 && low_light == 1){ // Segunda verificação do LDR, para evitar falhas
- if(light_sens > sens_threshold){
- night = 1;
- count_2 = 0;
- }
- low_light = 0;
- }
- if(semafCar != 3){ // Farol verde (carros)
- semafCar = 3;
- semafCar_st(3);
- }
- if(semafPed != 1){ // Farol vermelho (pedestres)
- semafPed = 1;
- semafPed_st(1);
- }
- if(button == 1 && night == 0){ // Próximo estado
- count_1 = 0;
- st = 1;
- button = 0;
- }
- if(night == 1){ // Estado de noite
- st = 5;
- }
- }
- while(st == 1){ // Intervalo de transição para amarelo
- if(count_1 > maxCount_2){
- count_1 = 0;
- st = 2;
- }
- }
- while(st == 2){ // Farol amarelo (carros)
- if(semafCar != 2){
- semafCar = 2;
- semafCar_st(2);
- }
- if(count_1 > maxCount_2){
- count_1 = 0;
- st = 3;
- }
- }
- while(st == 3){ // Semáforo para carros: vermelho, pedestres: verde, período: dia
- if(semafCar != 1){
- semafCar = 1;
- semafCar_st(1);
- }
- if(semafPed != 2){
- semafPed = 2;
- semafPed_st(2);
- }
- if(count_1 > maxCount_1){
- count_1 = 0;
- st = 4;
- }
- }
- while(st == 4){ // Transição para estado 0
- if(semafPed != 3){
- semafPed = 3;
- semafPed_st(1);
- }
- if(count_2 >= maxCount_3){ // Farol vermelho de pedestre, piscando
- ped_stop_NegVal();
- count_2 = 0;
- }
- if(count_1 > maxCount_2){
- count_1 = 0;
- st = 0;
- }
- }
- while(st == 5){ // Estado noturno
- light_sens = light_sens_value(); // Verificação do valor de tensão do sensor LDR
- if(light_sens <= sens_threshold){
- if(low_light == 1){
- count_1 = 0;
- low_light = 0;
- }
- }
- if(count_1 > maxCount_1 && low_light == 0){ // Segunda verificação do LDR, para evitar falhas
- if(light_sens <= sens_threshold){
- night = 0;
- count_1 = 0;
- }
- low_light = 1;
- }
- if(night == 0){ // Retorno ao estado inicial
- st = 0;
- break;
- }
- if(semafCar != 4){ // Farol amarelo (carros)
- semafCar = 4;
- semafCar_st(2);
- }
- if(semafPed != 4){ // Farol de pedestres desligado
- semafPed = 4;
- semafPed_st(3);
- }
- if(count_2 >= maxCount_3){ // Farol amarelo de carros, piscando
- car_att_NegVal();
- count_2 = 0;
- }
- }
- }
- /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
- /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
- #ifdef PEX_RTOS_START
- PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
- #endif
- /*** End of RTOS startup code. ***/
- /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
- for(;;){}
- /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
- } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
- /* END main */
- /*!
- ** @}
- */
- /*
- ** ###################################################################
- **
- ** This file was created by Processor Expert 10.3 [05.09]
- ** for the Freescale Kinetis series of microcontrollers.
- **
- ** ###################################################################
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement