Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ###################################################################
- ** Filename : main.c
- ** Project : 1-4
- ** Processor : MKL46Z256VLL4
- ** Version : Driver 01.01
- ** Compiler : GNU C Compiler
- ** Date/Time : 2019-04-25, 11:47, # 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"
- /* 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 <math.h>
- int getNumberFromSignal(){
- int i;
- //i = ((GPIOE_PDIR&(1<<19))*8) + ((GPIOE_PDIR&(1<<18))*4) + ((GPIOE_PDIR&(1<<17))*2) + (GPIOE_PDIR&(1<<16));
- i = ((GPIOE_PDIR&(0b1111<<16))>>16);
- return i;
- }
- int getNumberByDigit(int num, int digit){
- return (num / pow(10, digit));
- }
- void display7segForE(){
- // 信号線の内容をそのまま7segに出力
- if(GPIOE_PDIR&(1<<19)) {
- GPIOB_PSOR = (1<<3);
- }else{
- GPIOB_PCOR = (1<<3);
- }
- if(GPIOE_PDIR&(1<<18)) {
- GPIOB_PSOR = (1<<2);
- }else{
- GPIOB_PCOR = (1<<2);
- }
- if(GPIOE_PDIR&(1<<17)) {
- GPIOB_PSOR = (1<<1);
- }else{
- GPIOB_PCOR = (1<<1);
- }
- if(GPIOE_PDIR&(1<<16)) {
- GPIOB_PSOR = (1<<0);
- }else{
- GPIOB_PCOR = (1<<0);
- }
- }
- void display7seg(int i){
- //カウントした値を7segに出力する
- if((i >> 3) & 1) {
- GPIOB_PSOR = (1<<3);
- }else{
- GPIOB_PCOR = (1<<3);
- }
- if((i >> 2) & 1) {
- GPIOB_PSOR = (1<<2);
- }else{
- GPIOB_PCOR = (1<<2);
- }
- if((i >> 1) & 1) {
- GPIOB_PSOR = (1<<1);
- }else{
- GPIOB_PCOR = (1<<1);
- }
- if(i & 1) {
- GPIOB_PSOR = (1<<0);
- }else{
- GPIOB_PCOR = (1<<0);
- }
- }
- /*lint -save -e970 Disable MISRA rule (6.3) checking. */
- int main(void)
- /*lint -restore Enable MISRA rule (6.3) checking. */
- {
- /* Write your local variable definition here */
- int i=0;
- int num[2];
- int pta1_pushing=0;
- int state=0;
- int digit=0;
- bool pta1_val, input_F;
- num[0]=0; num[1]=0;
- /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
- PE_low_level_init();
- /*** End of Processor Expert internal initialization. ***/
- /* Write your code here */
- /* For example: for(;;) { } */
- SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; // PTA のクロックを有効にする
- PORTA_PCR1 = PORT_PCR_MUX(1); // PTA1 を GPIO に設定する
- GPIOA_PDDR &= ~(1<<1); // PTA1 を入力に設定する
- SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK; // PTEのクロックを有効にする
- PORTE_PCR19 = PORT_PCR_MUX(1); // PTE19をGPIOに設定する
- PORTE_PCR18 = PORT_PCR_MUX(1); // PTE18をGPIOに設定する
- PORTE_PCR17 = PORT_PCR_MUX(1); // PTE17をGPIOに設定する
- PORTE_PCR16 = PORT_PCR_MUX(1); // PTE16をGPIOに設定する
- GPIOE_PDDR &= ~(0b1111<<16); //PTE19-16を入力に設定する
- SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;
- PORTB_PCR0 = PORT_PCR_MUX(1); // PTB0をGPIOに設定する
- PORTB_PCR1 = PORT_PCR_MUX(1); // PTB1をGPIOに設定する
- PORTB_PCR2 = PORT_PCR_MUX(1); // PTB2をGPIOに設定する
- PORTB_PCR3 = PORT_PCR_MUX(1); // PTB3をGPIOに設定する
- GPIOB_PDDR |= (0b1111<<0); //PTB3-0を出力に設定する
- SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; // PTDのクロックを有効にする
- PORTD_PCR5 = PORT_PCR_MUX(1);
- GPIOD_PDDR |= (1<<5);
- SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK; // PTEのクロックを有効にする
- PORTE_PCR29 = PORT_PCR_MUX(1);
- GPIOE_PDDR |= (1<<29);
- //プッシュスイッチの状態がOFFになるまでループで待つ
- while(1) {
- pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
- if(pta1_val == 0) {
- //7セグメントLEDに0(初めに表示するべき値)を表示する
- GPIOB_PCOR = (0b1111<<0);
- GPIOD_PSOR = (1<<5);
- GPIOE_PSOR = (1<<29);
- break;
- }
- }
- //入力状態: Fが入力されたら出力状態に、桁数オーバーor0桁からのFでエラー状態に
- //switch文で状態遷移する(0:初期, 1:入力, 2:出力状態, 3:error)
- while(1) {
- switch(state) {
- case 0: // 初期状態
- GPIOD_PCOR = (1<<5);
- GPIOE_PCOR = (1<<29);
- //プッシュスイッチの状態がONになるまでループで待つ(入力状態に移行)
- while(1) {
- pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
- if(pta1_val) {
- if(pta1_pushing == 1) {
- // nothing do
- }else{
- pta1_pushing = 1; //押下モードに移行
- }
- }else{
- if(pta1_pushing == 1) { //おしてたら
- pta1_pushing = 0;
- state = 1;
- break;
- }else{
- pta1_pushing = 0;
- }
- }
- }
- break;
- case 1: // 入力状態
- GPIOD_PCOR = (1<<5);
- GPIOE_PSOR = (1<<29);
- while(1) {
- pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
- input_F = (((GPIOE_PDIR&(1<<19))>>19) & ((GPIOE_PDIR&(1<<18))>>18) & ((GPIOE_PDIR&(1<<17))>>17) & ((GPIOE_PDIR&(1<<16))>>16)) ? 1 : 0;
- //信号線の内容をそのまま7segに出力
- display7segForE();
- if(pta1_val) {
- if(pta1_pushing == 1) {
- // nothing do
- }else{
- pta1_pushing = 1; //押下モードに移行
- }
- }else{
- if(pta1_pushing == 1) { //おしてたら
- pta1_pushing = 0;
- //信号線の入力がFなら出力状態に, 0桁ならエラーに
- if(input_F){
- if(digit == 0){
- state=3; //ERROR
- break;
- }else{
- state=2; //出力状態への移行
- break;
- }
- }else{
- //普通にボタンが押されたときは数値を記憶
- //入力線を10進に変換
- i = getNumberFromSignal();
- //numに追加
- num = 10*num + i;
- digit++;
- }
- }
- pta1_pushing = 0;
- }
- }
- break;
- case 2: // 出力状態
- GPIOD_PSOR = (1<<5);
- GPIOE_PCOR = (1<<29);
- for(int j=digit-1; j>=0; j--) {
- //digit桁の数字を順番に出力する
- int k = getNumberByDigit(num, j);
- num = num % (int) pow(10, j);
- display7seg(k);
- //Fが入力されたらwhileループを抜けて次の桁を表示する
- while(1) {
- pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
- input_F = (((GPIOE_PDIR&(1<<19))>>19) & ((GPIOE_PDIR&(1<<18))>>18) & ((GPIOE_PDIR&(1<<17))>>17) & ((GPIOE_PDIR&(1<<16))>>16)) ? 1 : 0;
- if(pta1_val) {
- if(pta1_pushing == 1) {
- // nothing do
- }else{
- pta1_pushing=1; //押下モードに移行
- }
- }else{
- if(pta1_pushing == 1) { //おしてたら
- //信号線の入力がFなら出力状態に, 0桁ならエラーに
- pta1_pushing = 0;
- if(input_F && (digit != 0)) {
- state=2;
- break;
- }
- }
- pta1_pushing = 0;
- }
- }
- }
- //消灯~w
- display7seg(16);
- state = 0;
- digit = 0; num = 0;
- break;
- case 3: // エラー
- GPIOD_PSOR = (1<<5);
- GPIOE_PSOR = (1<<29);
- //13がEににてるからそれしゅつりょく pta1が押されたら初期状態に戻る
- while(1) {
- pta1_val = GPIOA_PDIR&(1<<1) ? 1 : 0;
- GPIOE_PSOR = (0b1101<<16);
- if(pta1_val) {
- state = 0;
- break;
- }
- }
- break;
- default: //それ以外(つかわん)
- break;
- }
- }
- /*** 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.5 [05.21]
- ** for the Freescale Kinetis series of microcontrollers.
- **
- ** ###################################################################
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement