Answered Essay: Need to create state machine for this project given these constraints. T

Need to create state machine for this project given these constraints. The micro-controller is the ATmega2560.

Overall am must compile. This can be tested using the ch eckmark icon. 1. The pragr 2. The project must be pushed to GitLab and an appropriate response from the server must be received: Got your subnission. Thanks! 3. Arduino libraries are not allowed at all for this lab for the exception of debug functionality using Serial.printin. switch.cpp 1. A function implementaticn that returns void and has no parameters called initswitchPB3 must be present and is used in the main function to initialize the switch on the pin named P83 2. P83 must be initial ized with an input pull-up resistor 3. Fin-Channtpls mustbe eabdfor piri PB3 led.cpp 1. A functio impentaticn thal rtuns void and has n pataels called inilEDmst be present and is used in the main function to initialize all LED pins as outputs. 2. Fins named PAO, PA1, PAZ, and PA3 must be used to contral the LEDs 3. A function implementation called turnOnLEDWithChar that returns void and has a parometer called num of type unsigned unsigned char must be present. 4. The turnOnl EDWithChar function must be one line of code, see LED ㏄ntral section for more details. switch.cpp 1. A function implementation that returns void and has no parameters called initSwitch must be present and is usedl in the main function to initialize pin 32 on the development board as an input. timer.cpp 1. A function implementation that rcturns void and has no parameters called initTimerO must be present and must initialize timer 0. 2. A function implementation that rts id and has no pas called delayMs must bpresnt and must implement a precise millisecond delay and can work at least up to 100 millisecondls. For loops are allowec. main.cpp 1. A call to initLEDO and initswitchPB3) must be present in the main function. 2. An infinite while loop must be prescnt. 3. A state machine for the entire project must be implemented using a typedef enum for states. 4. An ISR to handle the switch being pressed must be present and be used to change states appropriately 5. The switch press must be debounced using an appropriately dlesigned state machine. 6. When the switch is pressed and released, the LEDs change the pace at which they link (either half or twice depending on the state) .LEDs must blink either every 100 ms or 200 ms depending on the state.

This program must compile. This can be tested using the checkmark icon. The project must be pushed to GitLab and an appropriate response from the server must be received: “Got your submission. Thanks!” Arduino libraries are not allowed at all for this lab for the exception of debug functionality using Serial.println. A function implementation that returns void and has no parameters called initswitchPB3 must be present and is used in the main function to initialize the switch on the pin named PB3 PB3 must be initialized with an input pull-up resistor Pin-Change interrupts must be enabled for pin PB3 A function implementation that returns void and has no parameters called initLED must be present and is used in the main function to initialize all LED pins as outputs. Fins named PA0, PA1, PA2, and PA3 must be used to control the LEDs A function implementation called turnOnLEDWithChar that returns void and has a parameter called num of type unsigned unsigned char must be present. The turnOnLEDWithChar function must be one line of code, see LED control section for more details. A function implementation that returns void and has no parameters called initSwitch must be present and is used in the main function to initialize pin 32 on the development board as an input. A function implementation that returns void and has no parameters called initTimer0 must be present and must initialize timer 0. A function implementation that returns void and has no pas called delayMs must be present and must implement a precise millisecond delay and can work at least up to 100 milliseconds. For loops are allowed. A call to initLED0 and initswitchPB3() must be present in the main function. An infinite while loop must be present. A state machine for the entire project must be implemented using a typedef enum for states. An ISR to handle the switch being pressed must be present and be used to change states appropriately. The switch press must be debounced using an appropriately designed state machine. When the switch is pressed and released, the LEDs change the pace at which they link (either half or twice depending on the state). LEDs must blink either every 100 ms or 200 ms depending on the state.

Expert Answer

 

main.cpp
——————————————-
#include <Arduino.h>
#include <avr/io.h>
#include “led.h”
#include “switch.h”
#include “timer.h”

/*
* Define a set of states that can be used in the state machine using an enum
*/
typedef enum stateType_enum {
countSlow, pressSlow, countFast, pressFast
} stateType;

// typedef enum stateType_enum{
//   waitPress, dbPress, waitRelease, dbRelease
// } stateType;
// volatile stateType state = waitPress;
// volatile int multiplier;
volatile stateType state = countSlow;
volatile int num = 0;

int main(){

initSwitchPB3();
initLED();
sei(); // enable global interrupts
Serial.begin(9600);

DDRB |= (1 << DDB7);

/*
* Implement a state machine in the while loop which achieves the assignment
* requirements.
*/
while(1){
/* After 100 ms, increment the binary number represented by the LEDs
*/
switch(state){
case countSlow:
Serial.println(“In state: countSlown”);
turnOnLEDWithChar(num);
_delay_ms(200);
break;
case pressSlow:
Serial.println(“In state: pressSlown”);
turnOnLEDWithChar(num);
_delay_ms(200);
break;
case countFast:
Serial.println(“In state: countFastn”);
turnOnLEDWithChar(num);
_delay_ms(100);
break;
case pressFast:
Serial.println(“In state: pressFastn”);
turnOnLEDWithChar(num);
_delay_ms(100);
break;
}
num++;
if(num == 16) {
num = 0;
}

}
return 0;
}

// while(1){
//     switch(state){
//       case waitPress:
//         break;
//       case dbPress:
//         _delay_ms(50);
//         state = waitRelease;
//         break;
//       case waitRelease:
//         break;
//       case dbRelease:
//         _delay_ms(50);
//         if(multiplier == 1){
//           OCR1AH = 0xF4;
//           OCR1AL = 0x24;
//           multiplier = 2;
//         }
//         else{
//           OCR1AH = OCR1AH >> 1;
//           OCR1AL = OCR1AL >> 1;
//           multiplier = 1;
//         }
//         state = waitPress;
//         break;
//     }
//   }
//
//   return 0;
// }

/* Implement an Pin Change Interrupt which handles the switch being
* pressed ant released. When the switch is pressed an released, the LEDs
* change at twice the original rate. If the LEDs are already changing at twice
* the original rate, it goes back to the original rate.
*/
// Interrupt Service Routine
ISR(TIMER1_COMPA_vect){
PORTB ^= (1 << PORTB7); // toggles the bit
_delay_ms(20);
}
ISR(PCINT0_vect){
// Serial.println(“Got a change.”);
// if(state == pressSlow){
//   state = countFast;
// }
// else if(state == pressFast){
//   state = countSlow;
// }
if(state == countSlow) {
_delay_ms(20);
state = pressSlow;
} else if(state == pressSlow) {
_delay_ms(20);
state = countFast;
} else if(state == countFast) {
_delay_ms(20);
state = pressFast;
} else {
_delay_ms(20);
state = countSlow;
}
}
————————————————————————–
led.cpp
———————————–
#include <avr/io.h>
#include <util/delay.h>
#include “led.h”

/* Initialize PA0, PA1, PA2, and PA3 to outputs
*/
void initLED(){
DDRA |= (1 << DDA0) | (1 << DDA1) | (1 << DDA2) | ( 1 << DDA3); // set LEDs to output
}

/* This must be one line of code.
* In this function you will be giving a number “num” which will be represented
* in binary by four LEDs. You must effectively assign the lowest four bits of
* “num” to the appropriate bits of PORTA.
*/
void turnOnLEDWithChar(unsigned char num) {
PORTA = (PORTA & 0xF0) | (num & 0x0F);
}
—————————————————————————-
led.h
—————————————-
#ifndef LED_H
#define LED_H

void initLED();
void turnOnLEDWithChar(unsigned char num);

#endif
————————————————————————–
timer.cpp
—————————————-
#include “timer.h”

/* Initialize timer 1, you should not turn the timer on here.
* You will need to use CTC mode
*/
void initTimer1(){
// set the timer mode to be “CTC”
TCCR1A &= ~(1 << WGM10 | 1 << WGM11);
TCCR1B &= ~(1 << WGM13);
TCCR1B |= (1 << WGM12);

}

/* This delays the program an amount specified by unsigned int delay.
* Use timer 1. Keep in mind that you need to choose your prescalar wisely
* such that your timer is precise to 1 millisecond and can be used for 125
* 100 milliseconds
*
* NOTE : USE 256
*/
void delayMs(unsigned int delay){
// set clock/prescalar for timer 0 to be clk/256
TCCR1B &= ~(1 << CS10 | 1 << CS11);
TCCR1B |= (1 << CS12);
// set the OCR1A to 62,500 – 1
// this counts up to approximately one second
if(delay == 1) {
OCR1AH = 0x18;
OCR1AL = 0x6A;
} else if(delay == 0){
OCR1AH = 0xC;
OCR1AL = 0x35;
}
//enable the interrupt for the COMPA event
TIMSK1 |= (1 << OCIE1A);
}
—————————————————————————————-
timer.h
———————————-

#ifndef TIMER_H
#define TIMER_H

#include <avr/io.h>

void initTimer1();
void delayMs(unsigned int delay);

#endif
————————————————————————————-
switch.cpp
———————————–
#include “switch.h”
#include <avr/io.h>

/*
* Initializes pull-up resistor on PB3 and sets it into input mode
*/
void initSwitchPB3(){
PORTB |= (1 << PORTB3);
DDRB &= ~(1 << DDB3);
// enable pin-change interrupts
PCICR |= (1 << PCIE0);
// enable interrupts on PB3
PCMSK0 |= ( 1 << PCINT2 | 1 << PCINT3);
}
———————————————————————————–
switch.h
————————————
#ifndef SWITCH_H
#define SWITCH_H

void initSwitchPB3();

#endif

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?