revised code

Dependencies:   mbed

Committer:
nmaududi
Date:
Sat Oct 05 21:17:17 2019 +0000
Revision:
2:64a34ae90bb1
Parent:
1:9fa7cc80f1a7
revised version for module 4;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nmaududi 0:4fb921928934 1 /**----------------------------------------------------------------------------
nmaududi 0:4fb921928934 2
nmaududi 0:4fb921928934 3 \file main.cpp
nmaududi 0:4fb921928934 4 -- --
nmaududi 0:4fb921928934 5 -- ECEN 5803 Mastering Embedded System Architecture --
nmaududi 0:4fb921928934 6 -- Project 1 Module 4 --
nmaududi 0:4fb921928934 7 -- Microcontroller Firmware --
nmaududi 0:4fb921928934 8 -- main.cpp --
nmaududi 0:4fb921928934 9 -- --
nmaududi 0:4fb921928934 10 -------------------------------------------------------------------------------
nmaududi 0:4fb921928934 11 --
nmaududi 0:4fb921928934 12 -- Designed for: University of Colorado at Boulder
nmaududi 0:4fb921928934 13 --
nmaududi 0:4fb921928934 14 --
nmaududi 0:4fb921928934 15 -- Designed by: Tim Scherr
nmaududi 0:4fb921928934 16 -- Revised by: Student's name
nmaududi 0:4fb921928934 17 --
nmaududi 0:4fb921928934 18 -- Version: 2.1
nmaududi 0:4fb921928934 19 -- Date of current revision: 2017-09-25
nmaududi 0:4fb921928934 20 -- Target Microcontroller: Freescale MKL25ZVMT4
nmaududi 0:4fb921928934 21 -- Tools used: ARM mbed compiler
nmaududi 0:4fb921928934 22 -- ARM mbed SDK
nmaududi 0:4fb921928934 23 -- Freescale FRDM-KL25Z Freedom Board
nmaududi 0:4fb921928934 24 --
nmaududi 0:4fb921928934 25 --
nmaududi 0:4fb921928934 26 -- Functional Description: Main code file generated by mbed, and then
nmaududi 0:4fb921928934 27 -- modified to implement a super loop bare metal OS.
nmaududi 0:4fb921928934 28 --
nmaududi 0:4fb921928934 29 -- Copyright (c) 2015, 2016 Tim Scherr All rights reserved.
nmaududi 0:4fb921928934 30 --
nmaududi 0:4fb921928934 31 */
nmaududi 0:4fb921928934 32
nmaududi 0:4fb921928934 33 #define MAIN
nmaududi 0:4fb921928934 34 #include "shared.h"
nmaududi 0:4fb921928934 35 #undef MAIN
nmaududi 0:4fb921928934 36
nmaududi 0:4fb921928934 37 #define ADC_0 (0U)
nmaududi 0:4fb921928934 38 #define CHANNEL_0 (0U)
nmaududi 0:4fb921928934 39 #define CHANNEL_1 (1U)
nmaududi 0:4fb921928934 40 #define CHANNEL_2 (2U)
nmaududi 0:4fb921928934 41 #define LED_ON (0U)
nmaududi 0:4fb921928934 42 #define LED_OFF (1U)
nmaududi 0:4fb921928934 43 #define ADCR_VDD (65535U) /*! Maximum value when use 16b resolution */
nmaududi 0:4fb921928934 44 #define V_BG (1000U) /*! BANDGAP voltage in mV (trim to 1.0V) */
nmaududi 0:4fb921928934 45 #define V_TEMP25 (716U) /*! Typical VTEMP25 in mV */
nmaududi 0:4fb921928934 46 #define M (1620U) /*! Typical slope: (mV x 1000)/oC */
nmaududi 0:4fb921928934 47 #define STANDARD_TEMP (25)
nmaududi 0:4fb921928934 48
nmaududi 0:4fb921928934 49 extern volatile uint16_t SwTimerIsrCounter;
nmaududi 0:4fb921928934 50
bcis93 1:9fa7cc80f1a7 51 int os_cb_sections = 0;
bcis93 1:9fa7cc80f1a7 52
nmaududi 0:4fb921928934 53
nmaududi 0:4fb921928934 54 Ticker tick; // Creates a timer interrupt using mbed methods
nmaududi 0:4fb921928934 55 Timer main_loop;
nmaududi 0:4fb921928934 56 Timer timer0_loop;
nmaududi 0:4fb921928934 57 /**************** ECEN 5803 add code as indicated ***************/
nmaududi 0:4fb921928934 58 // Add code to control red, green and blue LEDs here - Control section added here(completed)
nmaududi 0:4fb921928934 59
nmaududi 0:4fb921928934 60 PinName const redLED = PTB18; // pin number for red LED
nmaududi 0:4fb921928934 61 PinName const greenLED = PTB19; // pin number for green LED
nmaududi 0:4fb921928934 62 PinName const blueLED = PTD1; // pin number for blue LED
nmaududi 0:4fb921928934 63 //Set LEDs to be off right now
nmaududi 0:4fb921928934 64 DigitalOut rled(redLED,1);
nmaududi 0:4fb921928934 65 DigitalOut gled(greenLED,1);
nmaududi 0:4fb921928934 66 DigitalOut bled(blueLED,1);
nmaududi 0:4fb921928934 67
nmaududi 0:4fb921928934 68 Serial pc(USBTX, USBRX);
nmaududi 0:4fb921928934 69
nmaududi 0:4fb921928934 70 void flip()
nmaududi 0:4fb921928934 71 {
nmaududi 0:4fb921928934 72 gled = !gled;
nmaududi 0:4fb921928934 73 }
nmaududi 0:4fb921928934 74
bcis93 1:9fa7cc80f1a7 75 uint16_t temperature = 0; // value that will be external to all variable. stores the value of temperature
nmaududi 0:4fb921928934 76 //uint16_t Vtemp; // value in mV of the temperature calculated
nmaududi 0:4fb921928934 77 //uint16_t frequency; // value that will store the frequency detected. Need it to be availaible to all functions
nmaududi 0:4fb921928934 78 //uint16_t flow; // value that will store the flow that is determine
nmaududi 0:4fb921928934 79 //uint16_t ADC_vortex_frequency_input; // value of the ADC_vortex_frequency_input
nmaududi 0:4fb921928934 80 //uint16_t Vrefl; // value of the ADC_vortex_frequency_input
nmaududi 0:4fb921928934 81
nmaududi 0:4fb921928934 82
nmaududi 0:4fb921928934 83 int main()
nmaududi 0:4fb921928934 84 {
nmaududi 0:4fb921928934 85 /**************** ECEN 5803 add code as indicated ***************/
nmaududi 0:4fb921928934 86 // Add code to call timer0 function every 100 uS - Completed already
nmaududi 0:4fb921928934 87 tick.attach(&timer0, 0.0001); // setup ticker to call timer0 very 100 uS
nmaududi 0:4fb921928934 88
nmaududi 0:4fb921928934 89 // already being done in the code below pc.printf("Hello World!\n");
nmaududi 0:4fb921928934 90 uint32_t count = 0;
nmaududi 0:4fb921928934 91
nmaududi 0:4fb921928934 92
nmaududi 0:4fb921928934 93
nmaududi 0:4fb921928934 94 // initialize serial buffer pointers
nmaududi 0:4fb921928934 95 rx_in_ptr = rx_buf; /* pointer to the receive in data */
nmaududi 0:4fb921928934 96 rx_out_ptr = rx_buf; /* pointer to the receive out data*/
nmaududi 0:4fb921928934 97 tx_in_ptr = tx_buf; /* pointer to the transmit in data*/
nmaududi 0:4fb921928934 98 tx_out_ptr = tx_buf; /* pointer to the transmit out */
nmaududi 0:4fb921928934 99
nmaududi 0:4fb921928934 100
nmaududi 0:4fb921928934 101 // Print the initial banner
nmaududi 0:4fb921928934 102 pc.printf("\r\nHello World!\n\n\r");
nmaududi 0:4fb921928934 103
nmaududi 0:4fb921928934 104 /**************** ECEN 5803 add code as indicated ***************/
nmaududi 0:4fb921928934 105 // uncomment this section after adding monitor code. - completed
nmaududi 0:4fb921928934 106
nmaududi 0:4fb921928934 107
nmaududi 0:4fb921928934 108 UART_direct_msg_put("\r\nSystem Reset\r\nCode ver. ");
nmaududi 0:4fb921928934 109 UART_direct_msg_put( CODE_VERSION );
nmaududi 0:4fb921928934 110 UART_direct_msg_put("\r\n");
nmaududi 0:4fb921928934 111 UART_direct_msg_put( COPYRIGHT );
nmaududi 0:4fb921928934 112 UART_direct_msg_put("\r\n");
nmaududi 0:4fb921928934 113
nmaududi 0:4fb921928934 114 set_display_mode();
nmaududi 0:4fb921928934 115
nmaududi 0:4fb921928934 116
nmaududi 0:4fb921928934 117 //int i=0; // defined and declard i just for main loop duration in seconds
nmaududi 0:4fb921928934 118 // float main_loop_start_time=0;
nmaududi 0:4fb921928934 119 // float main_loop_end_time=0;
nmaududi 0:4fb921928934 120
nmaududi 0:4fb921928934 121 adc_calibration(); // calibration completed on ADC channel before reading from them
nmaududi 0:4fb921928934 122
nmaududi 0:4fb921928934 123 while(1) /// Cyclical Executive Loop
nmaududi 0:4fb921928934 124 {
nmaududi 0:4fb921928934 125 //for (;i < 2; i++) {
nmaududi 0:4fb921928934 126 // main_loop.start(); // start of the cyclical executive loop
nmaududi 0:4fb921928934 127 count++; // counts the number of times through the loop
nmaududi 0:4fb921928934 128 // __enable_interrupts();
nmaududi 0:4fb921928934 129 // __clear_watchdog_timer();
nmaududi 0:4fb921928934 130
nmaududi 0:4fb921928934 131
nmaududi 0:4fb921928934 132
nmaududi 0:4fb921928934 133 serial(); // Polls the serial port
nmaududi 0:4fb921928934 134 chk_UART_msg(); // checks for a serial port message received
nmaududi 0:4fb921928934 135 monitor(); // Sends serial port output messages depending
nmaududi 0:4fb921928934 136 // on commands received and display mode
nmaududi 0:4fb921928934 137
nmaududi 0:4fb921928934 138 /**************** ECEN 5803 add code as indicated ***************/
nmaududi 0:4fb921928934 139
nmaududi 2:64a34ae90bb1 140 /**************** ECEN 5803 add code as indicated ***************/
nmaududi 2:64a34ae90bb1 141 // readADC()
nmaududi 2:64a34ae90bb1 142 read_ADC(CHANNEL_0); // read in the reference low voltage
nmaududi 2:64a34ae90bb1 143 read_ADC(CHANNEL_1); // read in virtual vortext frequency input on J10_4
nmaududi 2:64a34ae90bb1 144 read_ADC(CHANNEL_2); // read in temperature from internal temp sensor in Vtemp (mV)
nmaududi 2:64a34ae90bb1 145 temperature = STANDARD_TEMP-(V_BG*((Vtemp*10 - V_TEMP25)/M)); // calculate the temperature converted in degrees celcius
nmaududi 2:64a34ae90bb1 146 //calculate frequency
nmaududi 2:64a34ae90bb1 147 frequency_detect();
nmaududi 0:4fb921928934 148
nmaududi 2:64a34ae90bb1 149 // calculate flow()
nmaududi 2:64a34ae90bb1 150 calculateFlow();
nmaududi 0:4fb921928934 151
nmaududi 0:4fb921928934 152 PwmOutputs_flowmeter(); // use TMP0 channel 3 proporional rate to flow
nmaududi 0:4fb921928934 153
nmaududi 2:64a34ae90bb1 154 Pulse_Output_Frequency(); // use TMP0 channel 4 propotional rate to frequency
nmaududi 0:4fb921928934 155
nmaududi 2:64a34ae90bb1 156 // use the SPI port to send flow number
nmaududi 2:64a34ae90bb1 157 //LCD_Display();
nmaududi 0:4fb921928934 158
nmaududi 0:4fb921928934 159
nmaududi 0:4fb921928934 160 // End ECEN 5803 code addition
nmaududi 0:4fb921928934 161
nmaududi 0:4fb921928934 162 if ((SwTimerIsrCounter & 0x1FFF) > 0x0FFF)
nmaududi 0:4fb921928934 163 {
nmaududi 0:4fb921928934 164 flip(); // Blink GREEN LED
nmaududi 0:4fb921928934 165 }
nmaududi 2:64a34ae90bb1 166 if (LED_timer_flag == 1){
nmaududi 0:4fb921928934 167
nmaududi 0:4fb921928934 168 rled = !rled; // Blink RED LED with 1 second period
nmaududi 0:4fb921928934 169 LED_timer_flag =0;
nmaududi 2:64a34ae90bb1 170 }
nmaududi 0:4fb921928934 171 // main_loop.stop(); // end of the cyclical executive loop
nmaududi 0:4fb921928934 172
nmaududi 0:4fb921928934 173 }
nmaududi 0:4fb921928934 174
nmaududi 0:4fb921928934 175 }