Varying frequency by potentiometer working.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 // Lab 3 Example Program 2 00002 // ----------------------- 00003 // This code uses the Ticker API to create a periodic interrupt 00004 // Devices used in this way must not call locks; a variant of 00005 // the AnalogOut device w/o locking is created for this. 00006 // 00007 // Updated for mbed 5 00008 00009 // THIS VERSION HAS NO DEBUGGING CODE 00010 00011 #include "mbed.h" 00012 #include "sineTable.h" 00013 00014 // -------------------------- 00015 // This declaration introduces a derived version of the mbed AnalogOut 00016 // class with the locking removed. 00017 // You do NOT NEED TO UNDERSTAND this 00018 class AnalogOut_unsafe : public AnalogOut { 00019 public: 00020 AnalogOut_unsafe (PinName pin) : AnalogOut (pin) {} 00021 protected: 00022 virtual void lock() {} 00023 virtual void unlock() {} 00024 }; 00025 // -------------------------- 00026 00027 Ticker tick ; // Creates periodic interrupt 00028 AnalogOut_unsafe ao(PTE30) ; // Analog output 00029 AnalogIn ain(A0) ; // Analog input 00030 00031 // Function called periodically 00032 // Write new value to AnalogOut 00033 volatile int index = 0 ; // index into array of sin values 00034 void writeAout() { 00035 ao.write_u16(sine[index]) ; 00036 index = (index + 1) % 64 ; 00037 } 00038 00039 EventQueue queue; // creates an event queue, to call read ADC 00040 00041 Serial pc(USBTX, USBRX); // tx, rx, for debugging 00042 00043 // This thread runs the event queue 00044 Thread eventThread ; 00045 00046 // Message type 00047 typedef struct { 00048 uint16_t analog; /* Analog input value */ 00049 } message_t; 00050 00051 // Mail box 00052 Mail<message_t, 3> mailbox; 00053 00054 // Function called every 10ms to read ADC 00055 // Average using a low pass filter 00056 // Every 10th value is sent to mailbox 00057 volatile int samples = 0 ; 00058 volatile uint16_t smoothed = 0 ; 00059 00060 void readA0() { 00061 00062 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; 00063 samples++ ; 00064 00065 if (samples == 10) { 00066 // send to thread 00067 message_t *mess = mailbox.alloc() ; // may fail but does not block 00068 if (mess) { 00069 mess->analog = smoothed ; 00070 mailbox.put(mess); // fails but does not block if full 00071 } 00072 samples = 0; 00073 } 00074 } 00075 00076 // Control the frequency of updates 00077 // Alternative between two frequencies 00078 int main() { 00079 int update_us = 1000 ; // 1ms 00080 int volts = 0 ; 00081 int timeDelay = 0; 00082 int frequency = 0; 00083 00084 // Start the event queue 00085 eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); 00086 00087 // call the readA0 function every 10ms 00088 queue.call_every(10, readA0) ; 00089 00090 while (true) { 00091 osEvent evt = mailbox.get(); // wait for mail 00092 00093 if (evt.status == osEventMail) { 00094 message_t* mess = (message_t*)evt.value.p ; 00095 volts = (mess->analog * 330) / 0xffff ; 00096 mailbox.free(mess) ; // free the message space 00097 } 00098 00099 frequency = (1+volts*49/329); // Freq Range: 1~50Hz. V = maximum voltage of ADC 00100 timeDelay = 1000000/(frequency*64); // Time is in microseconds 00101 00102 update_us = timeDelay ; 00103 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut 00104 00105 } 00106 }
Generated on Tue Aug 16 2022 11:08:30 by
1.7.2