Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.42 KB | None | 0 0
  1. /* ###################################################################
  2. **     Filename    : main.c
  3. **     Project     : 1-4
  4. **     Processor   : MKL46Z256VLL4
  5. **     Version     : Driver 01.01
  6. **     Compiler    : GNU C Compiler
  7. **     Date/Time   : 2019-04-25, 11:47, # 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. /* Including shared modules, which are used for whole project */
  34. #include "PE_Types.h"
  35. #include "PE_Error.h"
  36. #include "PE_Const.h"
  37. #include "IO_Map.h"
  38. /* User includes (#include below this line is not maintained by Processor Expert) */
  39. #include <math.h>
  40. int getNumberFromSignal(){
  41.     int i;
  42.     //i = ((GPIOE_PDIR&(1<<19))*8) + ((GPIOE_PDIR&(1<<18))*4) + ((GPIOE_PDIR&(1<<17))*2) + (GPIOE_PDIR&(1<<16));
  43.     i = ((GPIOE_PDIR&(0b1111<<16))>>16);
  44.     return i;
  45. }
  46.  
  47.   int getNumberByDigit(int num, int digit){
  48.     return (num / pow(10, digit));
  49.   }
  50.  
  51.   void display7segForE(){
  52.     // 信号線の内容をそのまま7segに出力
  53.     if(GPIOE_PDIR&(1<<19)) {
  54.       GPIOB_PSOR = (1<<3);
  55.     }else{
  56.       GPIOB_PCOR = (1<<3);
  57.     }
  58.     if(GPIOE_PDIR&(1<<18)) {
  59.       GPIOB_PSOR = (1<<2);
  60.     }else{
  61.       GPIOB_PCOR = (1<<2);
  62.     }
  63.     if(GPIOE_PDIR&(1<<17)) {
  64.       GPIOB_PSOR = (1<<1);
  65.     }else{
  66.       GPIOB_PCOR = (1<<1);
  67.     }
  68.     if(GPIOE_PDIR&(1<<16)) {
  69.       GPIOB_PSOR = (1<<0);
  70.     }else{
  71.       GPIOB_PCOR = (1<<0);
  72.     }
  73.   }
  74.  
  75.   void display7seg(int i){
  76.  
  77.  
  78.     //カウントした値を7segに出力する
  79.     if((i >> 3) & 1) {
  80.       GPIOB_PSOR = (1<<3);
  81.     }else{
  82.       GPIOB_PCOR = (1<<3);
  83.     }
  84.     if((i >> 2) & 1) {
  85.       GPIOB_PSOR = (1<<2);
  86.     }else{
  87.       GPIOB_PCOR = (1<<2);
  88.     }
  89.     if((i >> 1) & 1) {
  90.       GPIOB_PSOR = (1<<1);
  91.     }else{
  92.       GPIOB_PCOR = (1<<1);
  93.     }
  94.     if(i & 1) {
  95.       GPIOB_PSOR = (1<<0);
  96.     }else{
  97.       GPIOB_PCOR = (1<<0);
  98.     }
  99.   }
  100. /*lint -save  -e970 Disable MISRA rule (6.3) checking. */
  101. int main(void)
  102. /*lint -restore Enable MISRA rule (6.3) checking. */
  103. {
  104.   /* Write your local variable definition here */
  105.     int i=0;
  106.     int num[2];
  107.     int pta1_pushing=0;
  108.     int state=0;
  109.     int digit=0;
  110.     bool pta1_val, input_F;
  111.     num[0]=0; num[1]=0;
  112.   /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  113.   PE_low_level_init();
  114.   /*** End of Processor Expert internal initialization.                    ***/
  115.  
  116.   /* Write your code here */
  117.   /* For example: for(;;) { } */
  118.   SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK;             // PTA のクロックを有効にする
  119.         PORTA_PCR1 = PORT_PCR_MUX(1);             // PTA1 を GPIO に設定する
  120.         GPIOA_PDDR &= ~(1<<1);             // PTA1 を入力に設定する
  121.         SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK;             // PTEのクロックを有効にする
  122.         PORTE_PCR19 = PORT_PCR_MUX(1);             // PTE19をGPIOに設定する
  123.         PORTE_PCR18 = PORT_PCR_MUX(1);             // PTE18をGPIOに設定する
  124.         PORTE_PCR17 = PORT_PCR_MUX(1);             // PTE17をGPIOに設定する
  125.         PORTE_PCR16 = PORT_PCR_MUX(1);             // PTE16をGPIOに設定する
  126.         GPIOE_PDDR &= ~(0b1111<<16);             //PTE19-16を入力に設定する
  127.  
  128.         SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;
  129.         PORTB_PCR0 = PORT_PCR_MUX(1);             // PTB0をGPIOに設定する
  130.         PORTB_PCR1 = PORT_PCR_MUX(1);             // PTB1をGPIOに設定する
  131.         PORTB_PCR2 = PORT_PCR_MUX(1);             // PTB2をGPIOに設定する
  132.         PORTB_PCR3 = PORT_PCR_MUX(1);             // PTB3をGPIOに設定する
  133.         GPIOB_PDDR |= (0b1111<<0);             //PTB3-0を出力に設定する
  134.  
  135.         SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; // PTDのクロックを有効にする
  136.         PORTD_PCR5 = PORT_PCR_MUX(1);
  137.         GPIOD_PDDR |= (1<<5);
  138.         SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK; // PTEのクロックを有効にする
  139.         PORTE_PCR29 = PORT_PCR_MUX(1);
  140.         GPIOE_PDDR |= (1<<29);
  141.  
  142.  
  143.         //プッシュスイッチの状態がOFFになるまでループで待つ
  144.         while(1) {
  145.           pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
  146.           if(pta1_val == 0) {
  147.             //7セグメントLEDに0(初めに表示するべき値)を表示する
  148.             GPIOB_PCOR = (0b1111<<0);
  149.             GPIOD_PSOR = (1<<5);
  150.             GPIOE_PSOR = (1<<29);
  151.             break;
  152.           }
  153.         }
  154.  
  155.  
  156.  
  157.         //入力状態: Fが入力されたら出力状態に、桁数オーバーor0桁からのFでエラー状態に
  158.         //switch文で状態遷移する(0:初期, 1:入力, 2:出力状態, 3:error)
  159.         while(1) {
  160.           switch(state) {
  161.           case 0:                   // 初期状態
  162.             GPIOD_PCOR = (1<<5);
  163.             GPIOE_PCOR = (1<<29);
  164.           //プッシュスイッチの状態がONになるまでループで待つ(入力状態に移行)
  165.             while(1) {
  166.               pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
  167.  
  168.               if(pta1_val) {
  169.                 if(pta1_pushing == 1) {
  170.                   // nothing do
  171.                 }else{
  172.                   pta1_pushing = 1; //押下モードに移行
  173.                 }
  174.               }else{
  175.                 if(pta1_pushing == 1) {    //おしてたら
  176.                   pta1_pushing = 0;
  177.                   state = 1;
  178.                   break;
  179.                 }else{
  180.                   pta1_pushing = 0;
  181.                 }
  182.               }
  183.             }
  184.             break;
  185.           case 1:                   // 入力状態
  186.             GPIOD_PCOR = (1<<5);
  187.             GPIOE_PSOR = (1<<29);
  188.             while(1) {
  189.               pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
  190.               input_F = (((GPIOE_PDIR&(1<<19))>>19) & ((GPIOE_PDIR&(1<<18))>>18) & ((GPIOE_PDIR&(1<<17))>>17) & ((GPIOE_PDIR&(1<<16))>>16)) ? 1 : 0;
  191.  
  192.               //信号線の内容をそのまま7segに出力
  193.               display7segForE();
  194.  
  195.               if(pta1_val) {
  196.                 if(pta1_pushing == 1) {
  197.                   // nothing do
  198.                 }else{
  199.                   pta1_pushing = 1; //押下モードに移行
  200.                 }
  201.               }else{
  202.                 if(pta1_pushing == 1) {    //おしてたら
  203.                   pta1_pushing = 0;
  204.                   //信号線の入力がFなら出力状態に, 0桁ならエラーに
  205.                   if(input_F){
  206.                     if(digit == 0){
  207.                       state=3; //ERROR
  208.                       break;
  209.                     }else{
  210.                       state=2; //出力状態への移行
  211.                       break;
  212.                     }
  213.                   }else{
  214.                     //普通にボタンが押されたときは数値を記憶
  215.                     //入力線を10進に変換
  216.                     i = getNumberFromSignal();
  217.                     //numに追加
  218.                     num = 10*num + i;
  219.                     digit++;
  220.                   }
  221.                 }
  222.                 pta1_pushing = 0;
  223.               }
  224.             }
  225.             break;
  226.           case 2:                   // 出力状態
  227.             GPIOD_PSOR = (1<<5);
  228.             GPIOE_PCOR = (1<<29);
  229.             for(int j=digit-1; j>=0; j--) {
  230.               //digit桁の数字を順番に出力する
  231.               int k = getNumberByDigit(num, j);
  232.               num = num % (int) pow(10, j);
  233.               display7seg(k);
  234.               //Fが入力されたらwhileループを抜けて次の桁を表示する
  235.               while(1) {
  236.                 pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
  237.                 input_F = (((GPIOE_PDIR&(1<<19))>>19) & ((GPIOE_PDIR&(1<<18))>>18) & ((GPIOE_PDIR&(1<<17))>>17) & ((GPIOE_PDIR&(1<<16))>>16)) ? 1 : 0;
  238.                 if(pta1_val) {
  239.                   if(pta1_pushing == 1) {
  240.                     // nothing do
  241.                   }else{
  242.                     pta1_pushing=1;  //押下モードに移行
  243.                   }
  244.                 }else{
  245.                   if(pta1_pushing == 1) {  //おしてたら
  246.                     //信号線の入力がFなら出力状態に, 0桁ならエラーに
  247.                     pta1_pushing = 0;
  248.                     if(input_F && (digit != 0)) {
  249.                       state=2;
  250.                       break;
  251.                     }
  252.                   }
  253.                   pta1_pushing = 0;
  254.                 }
  255.               }
  256.             }
  257.             //消灯~w
  258.             display7seg(16);
  259.             state = 0;
  260.             digit = 0; num = 0;
  261.             break;
  262.           case 3:                   // エラー
  263.             GPIOD_PSOR = (1<<5);
  264.             GPIOE_PSOR = (1<<29);
  265.           //13がEににてるからそれしゅつりょく pta1が押されたら初期状態に戻る
  266.             while(1) {
  267.               pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
  268.               GPIOE_PSOR = (0b1101<<16);
  269.               if(pta1_val) {
  270.                 state = 0;
  271.                 break;
  272.               }
  273.             }
  274.             break;
  275.           default:                   //それ以外(つかわん)
  276.             break;
  277.           }
  278.  
  279.         }
  280.   /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  281.   /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  282.   #ifdef PEX_RTOS_START
  283.     PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  284.   #endif
  285.   /*** End of RTOS startup code.  ***/
  286.   /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  287.   for(;;){}
  288.   /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
  289. } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
  290.  
  291. /* END main */
  292. /*!
  293. ** @}
  294. */
  295. /*
  296. ** ###################################################################
  297. **
  298. **     This file was created by Processor Expert 10.5 [05.21]
  299. **     for the Freescale Kinetis series of microcontrollers.
  300. **
  301. ** ###################################################################
  302. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement