revised code

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**----------------------------------------------------------------------------
00002  
00003    \file main.cpp
00004 --                                                                           --
00005 --              ECEN 5803 Mastering Embedded System Architecture             --
00006 --                  Project 1 Module 4                                       --
00007 --                Microcontroller Firmware                                   --
00008 --                      main.cpp                                             --
00009 --                                                                           --
00010 -------------------------------------------------------------------------------
00011 --
00012 --  Designed for:  University of Colorado at Boulder
00013 --               
00014 --                
00015 --  Designed by:  Tim Scherr
00016 --  Revised by:  Student's name 
00017 -- 
00018 -- Version: 2.1
00019 -- Date of current revision:  2017-09-25   
00020 -- Target Microcontroller: Freescale MKL25ZVMT4 
00021 -- Tools used:  ARM mbed compiler
00022 --              ARM mbed SDK
00023 --              Freescale FRDM-KL25Z Freedom Board
00024 --               
00025 -- 
00026 -- Functional Description:  Main code file generated by mbed, and then
00027 --                           modified to implement a super loop bare metal OS.
00028 --
00029 --      Copyright (c) 2015, 2016 Tim Scherr  All rights reserved.
00030 --
00031 */
00032 
00033 #define MAIN
00034 #include "shared.h"
00035 #undef MAIN
00036 
00037 #define ADC_0                   (0U)
00038 #define CHANNEL_0               (0U)
00039 #define CHANNEL_1               (1U)
00040 #define CHANNEL_2               (2U)
00041 #define LED_ON                  (0U)
00042 #define LED_OFF                 (1U)
00043 #define ADCR_VDD                (65535U)    /*! Maximum value when use 16b resolution */
00044 #define V_BG                    (1000U)     /*! BANDGAP voltage in mV (trim to 1.0V) */
00045 #define V_TEMP25                (716U)      /*! Typical VTEMP25 in mV */
00046 #define M                       (1620U)     /*! Typical slope: (mV x 1000)/oC */
00047 #define STANDARD_TEMP           (25)
00048 
00049 extern volatile uint16_t SwTimerIsrCounter; 
00050 
00051 int os_cb_sections = 0;
00052 
00053 
00054 Ticker tick;             //  Creates a timer interrupt using mbed methods
00055 Timer main_loop;
00056 Timer timer0_loop;
00057  /****************      ECEN 5803 add code as indicated   ***************/
00058                 // Add code to control red, green and blue LEDs here - Control section added here(completed)
00059                 
00060 PinName const redLED = PTB18;  // pin number for red LED
00061 PinName const greenLED = PTB19; // pin number for green LED
00062 PinName const blueLED = PTD1; // pin number for blue LED
00063 //Set LEDs to be off right now  
00064 DigitalOut rled(redLED,1);
00065 DigitalOut gled(greenLED,1);
00066 DigitalOut bled(blueLED,1);
00067   
00068 Serial pc(USBTX, USBRX);     
00069  
00070 void flip()  
00071 {                
00072     gled = !gled;
00073 }
00074 
00075 uint16_t temperature = 0; // value that will be external to all variable. stores the value of temperature
00076 //uint16_t Vtemp; // value in mV of the temperature calculated
00077 //uint16_t frequency; // value that will store the frequency detected. Need it to be availaible to all functions
00078 //uint16_t flow; // value that will store the flow that is determine
00079 //uint16_t ADC_vortex_frequency_input; // value of the ADC_vortex_frequency_input
00080 //uint16_t Vrefl; // value of the ADC_vortex_frequency_input
00081 
00082  
00083 int main () 
00084 {
00085 /****************      ECEN 5803 add code as indicated   ***************/
00086                     //  Add code to call timer0 function every 100 uS - Completed already
00087     tick.attach(&timer0, 0.0001); // setup ticker to call timer0 very 100 uS                                                    
00088 
00089     // already being done in the code below pc.printf("Hello World!\n"); 
00090     uint32_t  count = 0;  
00091 
00092 
00093     
00094 // initialize serial buffer pointers
00095    rx_in_ptr =  rx_buf; /* pointer to the receive in data */
00096    rx_out_ptr = rx_buf; /* pointer to the receive out data*/
00097    tx_in_ptr =  tx_buf; /* pointer to the transmit in data*/
00098    tx_out_ptr = tx_buf; /* pointer to the transmit out */
00099     
00100    
00101   // Print the initial banner
00102     pc.printf("\r\nHello World!\n\n\r");
00103 
00104     /****************      ECEN 5803 add code as indicated   ***************/
00105     // uncomment this section after adding monitor code.   - completed
00106       
00107   
00108    UART_direct_msg_put("\r\nSystem Reset\r\nCode ver. ");
00109    UART_direct_msg_put( CODE_VERSION );
00110    UART_direct_msg_put("\r\n");
00111    UART_direct_msg_put( COPYRIGHT );
00112    UART_direct_msg_put("\r\n");
00113 
00114    set_display_mode();   
00115      
00116    
00117         //int i=0; // defined and declard i just for main loop duration in seconds
00118     //  float main_loop_start_time=0;
00119 //      float main_loop_end_time=0;
00120         
00121         adc_calibration(); // calibration completed on ADC channel before reading from them
00122         
00123     while(1)       /// Cyclical Executive Loop
00124     {
00125             //for (;i < 2; i++) {
00126       //  main_loop.start(); // start of the cyclical executive loop   
00127         count++;               // counts the number of times through the loop
00128 //      __enable_interrupts();
00129 //      __clear_watchdog_timer();
00130 
00131 
00132             
00133         serial();            // Polls the serial port
00134         chk_UART_msg();     // checks for a serial port message received
00135         monitor();           // Sends serial port output messages depending
00136                          //     on commands received and display mode
00137             
00138  /****************      ECEN 5803 add code as indicated   ***************/
00139 
00140    /****************      ECEN 5803 add code as indicated   ***************/
00141                 //  readADC()
00142    read_ADC(CHANNEL_0); // read in the reference low voltage
00143      read_ADC(CHANNEL_1); // read in virtual vortext frequency input on J10_4
00144      read_ADC(CHANNEL_2); // read in temperature from internal temp sensor in Vtemp (mV)
00145   temperature = STANDARD_TEMP-(V_BG*((Vtemp*10 - V_TEMP25)/M)); // calculate the temperature converted in degrees celcius
00146 //calculate frequency
00147        frequency_detect();   
00148 
00149 //  calculate flow()
00150   calculateFlow();
00151 
00152   PwmOutputs_flowmeter();  // use TMP0 channel 3  proporional rate to flow
00153 
00154  Pulse_Output_Frequency();  // use TMP0 channel 4  propotional rate to frequency
00155 
00156  // use the SPI port to send flow number
00157   //LCD_Display();
00158 
00159 
00160 //  End ECEN 5803 code addition
00161 
00162         if ((SwTimerIsrCounter & 0x1FFF) > 0x0FFF)
00163         {
00164             flip();  // Blink GREEN LED
00165         }
00166         if (LED_timer_flag == 1){
00167                     
00168                     rled = !rled; // Blink RED LED with 1 second period
00169                     LED_timer_flag =0;
00170          }
00171     //       main_loop.stop(); // end of the cyclical executive loop
00172                 
00173 }
00174 
00175 }