"Design MOD 10 Binary Counter ""Design a Embedded System Application using ST-Nucleo-F40, Which generates binary counting sequence on PTB port starting from 0 till 9. Each count incremented every 500msec. Four LEDs are connected on PTB Output port in active low configuration."

Dependencies:   mbed

Committer:
naray23
Date:
Wed Aug 16 10:57:17 2017 +0000
Revision:
0:55964073973e
First Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
naray23 0:55964073973e 1 #include "mbed.h"
naray23 0:55964073973e 2
naray23 0:55964073973e 3 enum HW_State{RST,RUN};
naray23 0:55964073973e 4
naray23 0:55964073973e 5 DigitalOut Reset(PTD4);
naray23 0:55964073973e 6 BusIn Input(PTA12,PTA4,PTA5,PTC8);
naray23 0:55964073973e 7
naray23 0:55964073973e 8 Serial pc(USBTX,USBRX);
naray23 0:55964073973e 9
naray23 0:55964073973e 10 enum HW_State FRDM_State;
naray23 0:55964073973e 11 int Program_State[3];
naray23 0:55964073973e 12 unsigned char command;
naray23 0:55964073973e 13 char curr_state;
naray23 0:55964073973e 14 char prev_state;
naray23 0:55964073973e 15 Timer t;
naray23 0:55964073973e 16 int timer_ms;
naray23 0:55964073973e 17
naray23 0:55964073973e 18
naray23 0:55964073973e 19 void Check_error(char curr_state,char prev_state)
naray23 0:55964073973e 20 {
naray23 0:55964073973e 21 if(((curr_state == 0) && ((prev_state == 0) || (prev_state == 9)) ||((prev_state+1) == curr_state))) //At Start or at Roll over
naray23 0:55964073973e 22 {
naray23 0:55964073973e 23 Program_State[0] = 0;
naray23 0:55964073973e 24 }
naray23 0:55964073973e 25 else
naray23 0:55964073973e 26 {
naray23 0:55964073973e 27 Program_State[0] = -1;
naray23 0:55964073973e 28 }
naray23 0:55964073973e 29 }
naray23 0:55964073973e 30
naray23 0:55964073973e 31
naray23 0:55964073973e 32 int main() {
naray23 0:55964073973e 33 Reset = 1;//Need to find what is the reset Methdology
naray23 0:55964073973e 34 FRDM_State=RST;
naray23 0:55964073973e 35 prev_state =0;
naray23 0:55964073973e 36 Input.mode(PullNone);
naray23 0:55964073973e 37
naray23 0:55964073973e 38 while(1) {
naray23 0:55964073973e 39 if(pc.readable())
naray23 0:55964073973e 40 {
naray23 0:55964073973e 41 command=pc.getc();
naray23 0:55964073973e 42 switch(command)
naray23 0:55964073973e 43 {
naray23 0:55964073973e 44 case 's':
naray23 0:55964073973e 45 case 'S': Reset =0;
naray23 0:55964073973e 46 FRDM_State=RUN;
naray23 0:55964073973e 47 t.start();
naray23 0:55964073973e 48 break;
naray23 0:55964073973e 49 case 'x':
naray23 0:55964073973e 50 case 'X': Reset =1;
naray23 0:55964073973e 51 FRDM_State=RST;
naray23 0:55964073973e 52 break;
naray23 0:55964073973e 53
naray23 0:55964073973e 54 }
naray23 0:55964073973e 55 }
naray23 0:55964073973e 56 if(FRDM_State == RUN)
naray23 0:55964073973e 57 {
naray23 0:55964073973e 58 curr_state=Input & Input.mask();
naray23 0:55964073973e 59 if(curr_state != prev_state)
naray23 0:55964073973e 60 {
naray23 0:55964073973e 61 t.stop();
naray23 0:55964073973e 62 timer_ms=t.read_ms();
naray23 0:55964073973e 63 t.reset();
naray23 0:55964073973e 64 t.start();
naray23 0:55964073973e 65 Check_error(curr_state,prev_state);
naray23 0:55964073973e 66 prev_state = curr_state;
naray23 0:55964073973e 67 if(( timer_ms> 494) && (timer_ms < 505))
naray23 0:55964073973e 68 {
naray23 0:55964073973e 69 Program_State[1] = 0;
naray23 0:55964073973e 70 }
naray23 0:55964073973e 71 else
naray23 0:55964073973e 72 {
naray23 0:55964073973e 73 Program_State[1] = -1;
naray23 0:55964073973e 74 }
naray23 0:55964073973e 75
naray23 0:55964073973e 76 if(curr_state == 0 && prev_state == 9)
naray23 0:55964073973e 77 {
naray23 0:55964073973e 78 pc.printf("%d,%d\n\r",Program_State[0],Program_State[1]);
naray23 0:55964073973e 79 }
naray23 0:55964073973e 80 }
naray23 0:55964073973e 81 }
naray23 0:55964073973e 82
naray23 0:55964073973e 83 }
naray23 0:55964073973e 84 }
naray23 0:55964073973e 85
naray23 0:55964073973e 86