Advertisement
ace_ventura

main.c

Mar 29th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.45 KB | None | 0 0
  1. /* ###################################################################
  2. **     Filename    : main.c
  3. **     Project     : semaforo
  4. **     Processor   : MKL25Z128VLK4
  5. **     Version     : Driver 01.01
  6. **     Compiler    : GNU C Compiler
  7. **     Date/Time   : 2017-03-29, 15:21, # CodeGen: 0
  8. **     Abstract    :
  9. **         Main module.
  10. **         This module contains user's application code.
  11. **     Settings    :
  12. **     Contents    :
  13. **         No public methods
  14. **
  15. ** ###################################################################*/
  16. /*!
  17. ** @file main.c
  18. ** @version 01.01
  19. ** @brief
  20. **         Main module.
  21. **         This module contains user's application code.
  22. */        
  23. /*!
  24. **  @addtogroup main_module main module documentation
  25. **  @{
  26. */        
  27. /* MODULE main */
  28.  
  29.  
  30. /* Including needed modules to compile this module/procedure */
  31. #include "Cpu.h"
  32. #include "Events.h"
  33. #include "car_stop.h"
  34. #include "BitIoLdd1.h"
  35. #include "car_att.h"
  36. #include "BitIoLdd2.h"
  37. #include "car_go.h"
  38. #include "BitIoLdd3.h"
  39. #include "ped_stop.h"
  40. #include "BitIoLdd4.h"
  41. #include "ped_go.h"
  42. #include "BitIoLdd5.h"
  43. #include "light_sens.h"
  44. #include "AdcLdd1.h"
  45. #include "ped_button.h"
  46. #include "ExtIntLdd1.h"
  47. #include "TI1.h"
  48. #include "TimerIntLdd1.h"
  49. #include "TU1.h"
  50. /* Including shared modules, which are used for whole project */
  51. #include "PE_Types.h"
  52. #include "PE_Error.h"
  53. #include "PE_Const.h"
  54. #include "IO_Map.h"
  55.  
  56. /* User includes (#include below this line is not maintained by Processor Expert) */
  57. #include "semaf.h"
  58. /*lint -save  -e970 Disable MISRA rule (6.3) checking. */
  59.  
  60. // Flags de controle
  61. volatile int st = 0; // Indicador de estado
  62. volatile int night = 0; // Indicador de período noturno, 0 - dia   1 - noite
  63. volatile int button = 0; // Indicador de botão pressionado
  64. int semafCar; // Indicador de estado do semáforo de carros
  65. int semafPed; // Indicador de estado do semáforo de pedestres
  66. uint16_t light_sens; // Valor do sensor LDR
  67.  
  68. // Contadores
  69. volatile unsigned int count_1 = 0;
  70. volatile unsigned int count_2 = 0;
  71.  
  72. int main(void)
  73. /*lint -restore Enable MISRA rule (6.3) checking. */
  74. {
  75.   /* Write your local variable definition here */
  76.     const unsigned int sens_threshold = 45000;
  77.     const unsigned int maxCount_1 = 2000; // 2 s
  78.     const unsigned int maxCount_2 = 1000; // 1,2 s
  79.     const unsigned int maxCount_3 = 250; // 250 ms
  80.     int low_light = 0; // Indicador de luminosidade, 0 - alta, 1 - baixa
  81.  
  82.   /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  83.   PE_low_level_init();
  84.   /*** End of Processor Expert internal initialization.                    ***/
  85.  
  86.   /* Write your code here */
  87.   for(;;){
  88.      
  89.       while(st == 0){ // Início - semáforo para carros: verde, pedestres: vermelho, período: dia
  90.           light_sens = light_sens_value(); // Coleta o valor do sensor LDR
  91.          
  92.           if(light_sens > sens_threshold){ // Verificação do valor de tensão do sensor LDR
  93.               if(low_light == 0){
  94.                   low_light = 1;
  95.                   count_2 = 0;
  96.               }
  97.           }
  98.          
  99.           if(count_2 > maxCount_1 && low_light == 1){ // Segunda verificação do LDR, para evitar falhas
  100.               if(light_sens > sens_threshold){
  101.                   night = 1;
  102.                   count_2 = 0;
  103.               }
  104.              
  105.               low_light = 0;
  106.           }
  107.          
  108.           if(semafCar != 3){ // Farol verde (carros)
  109.               semafCar = 3;
  110.               semafCar_st(3);
  111.           }
  112.          
  113.           if(semafPed != 1){ // Farol vermelho (pedestres)
  114.               semafPed = 1;
  115.               semafPed_st(1);
  116.           }
  117.          
  118.           if(button == 1 && night == 0){ // Próximo estado
  119.               count_1 = 0;
  120.               st = 1;
  121.               button = 0;
  122.           }
  123.          
  124.           if(night == 1){ // Estado de noite
  125.               st = 5;
  126.           }
  127.       }
  128.      
  129.       while(st == 1){ // Intervalo de transição para amarelo
  130.           if(count_1 > maxCount_2){
  131.               count_1 = 0;
  132.               st = 2;
  133.           }
  134.       }
  135.      
  136.       while(st == 2){ // Farol amarelo (carros)
  137.           if(semafCar != 2){
  138.               semafCar = 2;
  139.               semafCar_st(2);
  140.           }
  141.          
  142.           if(count_1 > maxCount_2){
  143.                   count_1 = 0;
  144.                   st = 3;
  145.               }
  146.       }
  147.      
  148.       while(st == 3){ // Semáforo para carros: vermelho, pedestres: verde, período: dia
  149.           if(semafCar != 1){
  150.               semafCar = 1;
  151.               semafCar_st(1);
  152.           }
  153.          
  154.           if(semafPed != 2){
  155.               semafPed = 2;
  156.               semafPed_st(2);
  157.           }
  158.          
  159.           if(count_1 > maxCount_1){
  160.               count_1 = 0;
  161.               st = 4;
  162.           }
  163.       }
  164.      
  165.       while(st == 4){ // Transição para estado 0
  166.           if(semafPed != 3){
  167.               semafPed = 3;
  168.               semafPed_st(1);
  169.           }
  170.          
  171.           if(count_2 >= maxCount_3){ // Farol vermelho de pedestre, piscando
  172.               ped_stop_NegVal();
  173.               count_2 = 0;
  174.           }
  175.          
  176.           if(count_1 > maxCount_2){
  177.               count_1 = 0;
  178.               st = 0;
  179.           }
  180.       }
  181.      
  182.       while(st == 5){ // Estado noturno
  183.           light_sens = light_sens_value(); // Verificação do valor de tensão do sensor LDR
  184.          
  185.           if(light_sens <= sens_threshold){
  186.               if(low_light == 1){
  187.                   count_1 = 0;
  188.                   low_light = 0;
  189.               }
  190.           }
  191.          
  192.           if(count_1 > maxCount_1 && low_light == 0){ //  Segunda verificação do LDR, para evitar falhas
  193.               if(light_sens <= sens_threshold){
  194.                   night = 0;
  195.                   count_1 = 0;
  196.               }
  197.              
  198.               low_light = 1;
  199.           }
  200.          
  201.           if(night == 0){ // Retorno ao estado inicial
  202.               st = 0;
  203.               break;
  204.           }
  205.          
  206.           if(semafCar != 4){ // Farol amarelo (carros)
  207.               semafCar = 4;
  208.               semafCar_st(2);
  209.           }
  210.          
  211.           if(semafPed != 4){ // Farol de pedestres desligado
  212.               semafPed = 4;
  213.               semafPed_st(3);
  214.           }
  215.          
  216.           if(count_2 >= maxCount_3){ // Farol amarelo de carros, piscando
  217.               car_att_NegVal();
  218.               count_2 = 0;
  219.           }
  220.       }
  221.              
  222.   }
  223.  
  224.   /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  225.   /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  226.   #ifdef PEX_RTOS_START
  227.     PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  228.   #endif
  229.   /*** End of RTOS startup code.  ***/
  230.   /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  231.   for(;;){}
  232.   /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
  233. } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
  234.  
  235. /* END main */
  236. /*!
  237. ** @}
  238. */
  239. /*
  240. ** ###################################################################
  241. **
  242. **     This file was created by Processor Expert 10.3 [05.09]
  243. **     for the Freescale Kinetis series of microcontrollers.
  244. **
  245. ** ###################################################################
  246. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement