Exercises the DAC on the STM32F303K8 NUCLEO32 BOARD
Fork of Nucleo_FrequencyCounter_Timed by
Revision 2:29a5b859318a, committed 2016-02-25
- Comitter:
- EmbeddedSam
- Date:
- Thu Feb 25 14:22:29 2016 +0000
- Parent:
- 1:85b1605ed4e0
- Commit message:
- This just shows how to exercise the DAC on the Nucleo F303K8 it changes a PWM duty cycle and makes the DAC correspond to the value.
Changed in this revision
diff -r 85b1605ed4e0 -r 29a5b859318a OneWire_Methods.cpp --- a/OneWire_Methods.cpp Wed Feb 24 12:33:26 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,175 +0,0 @@ -#include "OneWire_Methods.h" -#include "mbed.h" - -//Peripherals and typedefs -extern DigitalInOut one_wire_pin; - -unsigned char OneWire_ReadByte(void) -{ - unsigned char result=0; - - for (int i = 0; i < 8; i++){ - result >>= 1; // shift the result to get it ready for the next bit - // if result is one, then set MS bit - if (OneWire_ReadBit()){result |= 0x80;} - } - return result; -} - -void OneWire_WriteByte(unsigned char byte_to_write) -{ - for (int i = 0; i<8; i++) // Loop to write each bit in the byte, LS-bit first - { - OneWire_WriteBit(byte_to_write & 0x01); - byte_to_write >>= 1; // shift the data byte for the next bit - } -} - -bool OneWire_ReadBit(void) -{ - bool result; - one_wire_pin.output(); - one_wire_pin = 0; - OneWire_Delay('A'); - one_wire_pin.input(); - one_wire_pin.mode(PullUp); - OneWire_Delay('E'); - result = one_wire_pin.read(); - OneWire_Delay('F'); - return result; -} - -void OneWire_WriteBit (bool bit_to_write){ - - if (bit_to_write == 1) - { - // Write '1' bit - one_wire_pin.output(); - one_wire_pin = 0; - OneWire_Delay('A'); - one_wire_pin.input(); - one_wire_pin.mode(PullUp); - OneWire_Delay('B'); - } - else - { - one_wire_pin.output(); - one_wire_pin = 0; - OneWire_Delay('C'); - one_wire_pin.input(); - OneWire_Delay('D'); - } -} -bool OneWire_Reset (void) -{ - //Checks whether there is a device connected, returns true or false - //Debugging console output can be used if it is defined - bool result = false; - one_wire_pin.output(); - one_wire_pin.write(0); //Pull the line high - OneWire_Delay('H'); - one_wire_pin.input(); - one_wire_pin.mode(PullUp); - OneWire_Delay('I'); - if(one_wire_pin.read() == 0){ result = true;} - OneWire_Delay('J'); - #ifdef Debugging - pc.printf("\n\rResult from reset: %d",result); - #endif - return result; -} - -void OneWire_TestDelays(void) -{ - //Cycles the output pin through all the delays so you - //can see whether they are all the right timings. - one_wire_pin.output(); - for(int i=0;i<6;i++){ - one_wire_pin = !one_wire_pin; //toggle the pin 6 times to signal start of test at 100us - wait_us(100); - } - OneWire_Delay('A'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('A'); - one_wire_pin = !one_wire_pin; - - OneWire_Delay('B'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('B'); - one_wire_pin = !one_wire_pin; - - OneWire_Delay('C'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('C'); - one_wire_pin = !one_wire_pin; - - OneWire_Delay('D'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('D'); - one_wire_pin = !one_wire_pin; - - OneWire_Delay('E'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('E'); - one_wire_pin = !one_wire_pin; - - OneWire_Delay('F'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('F'); - one_wire_pin = !one_wire_pin; - - OneWire_Delay('G'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('G'); - one_wire_pin = !one_wire_pin; - - OneWire_Delay('H'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('H'); - one_wire_pin = !one_wire_pin; - - - OneWire_Delay('I'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('I'); - one_wire_pin = !one_wire_pin; - - OneWire_Delay('J'); - one_wire_pin = !one_wire_pin; - OneWire_Delay('J'); - one_wire_pin = !one_wire_pin; - -} - -//Functions -void OneWire_Delay(char letter) -{ - //Recommended delays from Maxim (standard speed not overdrive) - //https://www.maximintegrated.com/en/app-notes/index.mvp/id/126 - - //Letter Speed Recommended (µs) - //A Standard 6 - //B Standard 64 - //C Standard 60 - //D Standard 10 - //E Standard 9 - //F Standard 55 - //G Standard 0 - //H Standard 480 - //I Standard 70 - //J Standard 410 - switch(letter) - { - case 'A': wait_us(1); break; - case 'B': wait_us(64); break; - case 'C': wait_us(60); break; - case 'D': wait_us(10); break; - case 'E': wait_us(5); break; - case 'F': wait_us(55); break; - case 'G': wait_us(0); break; - case 'H': wait_us(480); break; - case 'I': wait_us(70); break; - case 'J': wait_us(410); break; - default: break; - } -}
diff -r 85b1605ed4e0 -r 29a5b859318a OneWire_Methods.h --- a/OneWire_Methods.h Wed Feb 24 12:33:26 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -void OneWire_Delay(char letter); -void OneWire_TestDelays(void); -bool OneWire_Reset(void); -void OneWire_WriteBit(bool bit_to_write); -bool OneWire_ReadBit(void); -void OneWire_WriteByte(unsigned char byte_to_write); -unsigned char OneWire_ReadByte(void);
diff -r 85b1605ed4e0 -r 29a5b859318a PID.lib --- a/PID.lib Wed Feb 24 12:33:26 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/aberk/code/PID/#6e12a3e5af19
diff -r 85b1605ed4e0 -r 29a5b859318a ds2781.cpp --- a/ds2781.cpp Wed Feb 24 12:33:26 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,346 +0,0 @@ -#include "ds2781.h" -#include "OneWire_Methods.h" - -/* -------------------------------------------------------------------------- - This file includes the functions needed to access and modify the registers - in a DS2781 using the 1-Wire protocol. The DS2781 is an IC that measures - voltage, current, accumulated current and temperature. It implements - capacity estimation algorithms for rechargeable batteries. However, this - file only includes routines to access the electrical parameters and not - the age-estimation registers. - -------------------------------------------------------------------------- ----------------------- - NOTE_1: The functions that return parameters, do so in the units reported - in the description of each function. The user should implement the scaling - on his/her own. - -------------------------------------------------------------------------- */ - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : ReadVoltage * - * Overview : Returns the voltage measured at the VIN input of the DS2781 * - * in units of 9.76mV * - * Return type : 16-bit unsigned int * - * Parameters : None * - * Time : < 4.3ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -int ReadVoltage (void) -{ - uint16_t result = 0; - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( READ_DATA ); - OneWire_WriteByte( 0x0C ); //Register Address - result = OneWire_ReadByte() << 8; //MSB - result |= OneWire_ReadByte() ; //LSB - } - return (result >> 5); -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : ReadCurrent * - * Overview : Returns the current measured through Rsns external to DS2781 in * - * units of 1.5625uV/Rsns. Positive current indicates discharge * - * Return type : 16-bit unsigned int * - * Parameters : None * - * Time : < 4.3ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -uint16_t ReadCurrent (void) -{ - uint16_t result = 0; - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( READ_DATA ); - OneWire_WriteByte( 0x0E ); //Register Address - result = ((uint16_t)OneWire_ReadByte() ) << 8; //MSB - result |= ((uint16_t)OneWire_ReadByte() ); //LSB - } - return result; -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : ReadAccumulatedCurrent * - * Overview : Returns the accumulated current at the DS2781 in units of * - * 1.526nVhr/Rsns * - * Return type : 32-bit unsigned long * - * Parameters : None * - * Time : < 5.8ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -uint32_t ReadAccumulatedCurrent (void) -{ - unsigned long result = 0; - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( READ_DATA ); - OneWire_WriteByte( 0x10 ); //Register Address - result = ((unsigned long)OneWire_ReadByte() ) << 24; //MSB - result |= ((unsigned long)OneWire_ReadByte() ) << 16; - result |= ((unsigned long)OneWire_ReadByte() ) << 8; - result |= ((unsigned long)OneWire_ReadByte() ); //LSB - } - return (result >> 4); -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : ResetAccumulatedCurrent * - * Overview : Resets the accumulated current register at the DS2781 * - * Return type : Void * - * Parameters : None * - * Time : < 4.2ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -void ResetAccumulatedCurrent (void) -{ - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( WRITE_DATA ); - OneWire_WriteByte( 0x10 ); //Register Address - OneWire_WriteByte( 0x00 ); //MSB - OneWire_WriteByte( 0x00 ); //LSB - } -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : ReadNetAddress * - * Overview : Returns the net address of the DS2781 * - * Return type : 64-bit unsigned long long * - * Parameters : None * - * Time : < 7.3ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -uint32_t ReadNetAddress (void) -{ - uint16_t result = 0; - if( OneWire_Reset() == true) - { - OneWire_WriteByte( READ_NETADDRESS ); - //result = ((Quint16_t)OneWire_ReadByte() ); //MSB - //result |= ((Quint16_t)OneWire_ReadByte() ) << 8; - //result |= ((Quint16_t)OneWire_ReadByte() ) << 16; - //result |= ((Quint16_t)OneWire_ReadByte() ) << 24; - //result |= ((Quint16_t)OneWire_ReadByte() ) << 32; - //result |= ((Quint16_t)OneWire_ReadByte() ) << 40; - //result |= ((Quint16_t)OneWire_ReadByte() ) << 48; - //result |= ((Quint16_t)OneWire_ReadByte() ) <<56; //LSB - } - return result; -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : ReadTemperature * - * Overview : Returns the temperature measured by the DS2781 in units of * - * 0.125°C * - * Return type : 16-bit unsigned int * - * Parameters : None * - * Time : < 4.3ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -uint16_t ReadTemperature (void) -{ - uint16_t result = 0; - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( READ_DATA ); - OneWire_WriteByte( 0x0A ); //Register Address - result = ((uint16_t)OneWire_ReadByte() ) << 8; //MSB - result |= ((uint16_t)OneWire_ReadByte() ); //LSB - } - return (result >> 5); -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : ReadCurrentOffset * - * Overview : Returns the value of the current offset register of the DS2781 * - * in units of 1.56uV/Rsns * - * Return type : 8-bit uint8_t * - * Parameters : None * - * Time : < 3.6ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -uint8_t ReadCurrentOffset (void) -{ - uint8_t result = 0; - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( READ_DATA ); - OneWire_WriteByte( 0x7B ); //Register Address - result = OneWire_ReadByte(); - } - return result; -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : WriteCurrentOffset * - * Overview : Writes to the current offset register of the DS2781 in units of * - * 1.56uV/Rsns * - * Return type : Void * - * Parameters : Byte to be written to the register in 2's complement * - * Time : < 3.6ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -void WriteCurrentOffset (uint8_t offset) -{ - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( WRITE_DATA ); - OneWire_WriteByte( 0x7B ); //Register Address - OneWire_WriteByte( offset ); - } -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : AdjustCurrentOffset * - * Overview : Adjusts the value of the current offset register of the DS2781 * - * by taking into account the offset at no current. Should only * - * be called when the battery is supplying no current * - * Return type : Void * - * Parameters : None * - * Time : < 3.62s * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -void AdjustCurrentOffset (void) -{ - char offset = 0; - - WriteCurrentOffset ( 0x0 ); //Reset Current Offset Register - - //Delay100MSx(36); //Wait 3.6s for current register to update - - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( READ_DATA ); - OneWire_WriteByte( 0x0F ); //Current Register LSB - offset = OneWire_ReadByte(); - } - - offset = 256 - offset; //2's complement Negating - - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( WRITE_DATA ); - OneWire_WriteByte( 0x7B ); //Current Offset Register - OneWire_WriteByte( offset ); - } -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : UpdateControlRegister * - * Overview : Writes to the Control register of the DS2781 using the values * - * supplied as a byte parameter. Writes to EEPROM addresses are * - * ignored for up to 15ms after this function is called. * - * Return type : Void * - * Parameters : None * - * Time : < 6.4ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -void UpdateControlRegister (uint8_t control) -{ - if( OneWire_Reset() == true ) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( WRITE_DATA ); - OneWire_WriteByte( 0x60 ); //Register Address - OneWire_WriteByte( control ); - } - - if( OneWire_Reset() == true ) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( COPY_DATA ); - OneWire_WriteByte( 0x60 ); //Register Address - } -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : ReadRAM * - * Overview : Reads a byte from the shadow RAM of the DS2781 at the given * - * memory address * - * Return type : 8-bit uint8_t * - * Parameters : Address of register to be read * - * Time : < 3.6ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -uint8_t ReadRAM (uint8_t addr) -{ - uint8_t result = 0; - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( READ_DATA ); - OneWire_WriteByte( addr ); //Register Address - result = OneWire_ReadByte(); - } - return result; -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : WriteRAM * - * Overview : Writes the given byte to the shadow RAM of the DS2781 at the * - * given memory address * - * Return type : Void * - * Parameters : Byte to be written, address of register * - * Time : < 3.6ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -void WriteRAM (uint8_t byte, uint8_t addr) -{ - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( WRITE_DATA ); - OneWire_WriteByte( addr ); //Register Address - OneWire_WriteByte( byte ); - } -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : CopyEEPROM * - * Overview : This function copies the contents of the EEPROM shadow RAM to * - * EEPROM cells for the EEPROM block containing thr given address. * - * Writes to EEPROM addresses are ignored for up to 15ms after this * - * function is called. * - * Return type : Void * - * Parameters : Memory address of shadow RAM to be copied * - * Time : < 2.9ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -void CopyEEPROM (uint8_t addr) -{ - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( COPY_DATA ); - OneWire_WriteByte( addr ); - } -} - -/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* - * Function name : RecallEEPROM * - * Overview : This function copies the contents of the EEPROM cells to the * - * shadow RAM for the EEPROM block containing the given address. * * - * Return type : Void * - * Parameters : Memory address of EEPROM to be copied * - * Time : < 2.9ms * - *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ - -void RecallEEPROM (uint8_t addr) -{ - if( OneWire_Reset() == true) - { - OneWire_WriteByte( SKIP_NETADDRESS ); - OneWire_WriteByte( RECALL_DATA ); - OneWire_WriteByte( addr ); - } -} -/* EOF */ \ No newline at end of file
diff -r 85b1605ed4e0 -r 29a5b859318a ds2781.h --- a/ds2781.h Wed Feb 24 12:33:26 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -#include "stdint.h" - -/* ***** net address commands ***** */ -#define READ_NETADDRESS 0x33 -#define SKIP_NETADDRESS 0xCC - - -/* ***** function commands ***** */ -#define READ_DATA 0x69 -#define WRITE_DATA 0x6C -#define COPY_DATA 0x48 -#define RECALL_DATA 0xB8 -#define LOCK_EEPROM 0x6A //DO NOT USE - - -/* ***** function prototypes ***** */ -/* Function details can be found in the .c file */ -int ReadVoltage (void); -uint16_t ReadCurrent (void); -uint32_t ReadAccumulatedCurrent (void); -void ResetAccumulatedCurrent (void); -uint32_t ReadNetAddress (void); -uint16_t ReadTemperature (void); -uint8_t ReadCurrentOffset (void); -void WriteCurrentOffset (uint8_t offset); -void AdjustCurrentOffset (void); -void UpdateControlRegister (uint8_t control); -uint8_t ReadRAM (uint8_t addr); -void WriteRAM (uint8_t byte, uint8_t addr); -void CopyEEPROM (uint8_t addr); -void RecallEEPROM (uint8_t addr); -
diff -r 85b1605ed4e0 -r 29a5b859318a main.cpp --- a/main.cpp Wed Feb 24 12:33:26 2016 +0000 +++ b/main.cpp Thu Feb 25 14:22:29 2016 +0000 @@ -8,112 +8,79 @@ */ #include "mbed.h" -#include "PID.h" -#include "OneWire_Methods.h" -#include "ds2781.h" + #define CURRENT_CHECK_PERIOD_US 50 //Checks ths current of the motor every 100us #define TICKS_PER_REVOLUTION 12.0 //The encoder has 12 slots -#define TICK_CHECK_PERIOD 0.1 //Checks ths speed every 100ms +#define TICK_CHECK_PERIOD_US 10000 //Checks ths speed every 10ms #define TICKS_PER_SECOND_MAX 1800.0 //This was found through experimentation with motor, max speed = 8900RPM = ~1800 ticks per second #define TICKS_PER_SECOND_MIN 1*TICKS_PER_REVOLUTION //This is equal to 1RPS (60RPM) at however many ticks per revolution #define SETPOINT_RPM 4000 //Arbitrarily picked /* Peripherals */ -DigitalOut motorEnable(D8); PwmOut motorPWM(D6); -Ticker periodicTimer1, periodicTimer2, periodicTimer3; -InterruptIn freqInputPin(D11); -AnalogIn motorA_CurrentMinus(A1); -AnalogIn motorA_CurrentPlus(A0); -DigitalInOut one_wire_pin(D9); +AnalogOut PWM_Duty_Cycle_DAC_Out(A5); //Used for plotting motor resonse on a scope +Ticker periodicTimer1; //Peripherals used for debugging Serial pc(USBTX, USBRX); // tx, rx just used for debugging -DigitalOut debuggingLine(D2); -/* Objects */ -PID controller(0.22, 0.01, 0.001, TICK_CHECK_PERIOD); - -/* Motor Variables */ -float speed_rpm, speed_rps, ticks_per_second; -volatile unsigned int tick_counter, final_count; -float setPointRPM; -float MotorACurrent; -//Onewire battery variables -int BatteryVoltageReading; -float BatteryVoltage; - -void freqInputPin_Interrupt() { - //Triggers on the rising edge of the frequency signal - tick_counter++; -} -void check_TickCount(){ - final_count = tick_counter; - //pc.printf("\n\rTick Count is : %d", final_count); - tick_counter = 0; - controller.setProcessValue(final_count); - //motorPWM = (float)1.0-controller.compute(); -} - -void check_Current(){ - MotorACurrent = (3.3f*motorA_CurrentPlus.read()) - (3.3f*motorA_CurrentMinus.read()); +float normalise(float real_min, float real_max, float scaled_min, float scaled_max, int value){ + return (((value - real_min) * (scaled_max - scaled_min)) / (real_max - real_min)) + scaled_min; } void Print_Variables_To_PC(){ - //BatteryVoltageReading = ReadVoltage(); - //BatteryVoltage = BatteryVoltageReading*0.00967; - - pc.printf("\n\r Speed in RPM is : %.2f", (float)(final_count*(1.0/TICK_CHECK_PERIOD))*(60.0/TICKS_PER_REVOLUTION)); - pc.printf("\n\r Motor Current %.2f", MotorACurrent); - // pc.printf("\n\r Battery Voltage %.2f", BatteryVoltage); + //pc.printf("put whatever you want here to be sent to PC once a second %d, %d, %f",var1,var2,var3); } - int main() { - //Setup encoder interface - freqInputPin.rise(&freqInputPin_Interrupt); //chain interrupt to rising edge - periodicTimer1.attach(&check_TickCount, TICK_CHECK_PERIOD); //Check the speed every interval - periodicTimer3.attach_us(&check_Current, CURRENT_CHECK_PERIOD_US); //Check the current every interval see defines above - - //Setup PID Speed Controller - controller.setInputLimits(TICKS_PER_SECOND_MIN, TICKS_PER_SECOND_MAX); //From 60RPM to 8900RPM This needs to be changed for your motor - controller.setOutputLimits(0.0, 1.0); //Maximum duty cycle = 1.0, minimum = 0 - controller.setMode(1); //1 = auto mode - setPointRPM = ((SETPOINT_RPM/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval - controller.setSetPoint(setPointRPM); - //Setup PWM - motorEnable = 1; motorPWM.period_us(100); //Set up for 10KHz PWM Switching Frequency - //Setup Debugging - periodicTimer2.attach(&Print_Variables_To_PC, 1.0); //Display stuff on serial port once a second + //Setup Debugging + periodicTimer1.attach(&Print_Variables_To_PC, 1.0); //Display stuff on serial port once a second + + motorPWM = 0; + PWM_Duty_Cycle_DAC_Out = motorPWM; + + float wait_time = 0.5; - while(1) { - motorPWM = 0.5; - wait(10); + while(1) { + wait(wait_time); + motorPWM = 0.1; + PWM_Duty_Cycle_DAC_Out = motorPWM; + + wait(wait_time); + motorPWM = 0.2; + PWM_Duty_Cycle_DAC_Out = motorPWM; + + wait(wait_time); + motorPWM = 0.3; + PWM_Duty_Cycle_DAC_Out = motorPWM; + + wait(wait_time); + motorPWM = 0.4; + PWM_Duty_Cycle_DAC_Out = motorPWM; + + wait(wait_time); + motorPWM = 0.5; + PWM_Duty_Cycle_DAC_Out = motorPWM; + + wait(wait_time); + motorPWM = 0.6; + PWM_Duty_Cycle_DAC_Out = motorPWM; + + wait(wait_time); + motorPWM = 0.7; + PWM_Duty_Cycle_DAC_Out = motorPWM; + + wait(wait_time); + motorPWM = 0.8; + PWM_Duty_Cycle_DAC_Out = motorPWM; + + wait(wait_time); + motorPWM = 0.9; + PWM_Duty_Cycle_DAC_Out = motorPWM; + } - motorPWM = 0.8; - wait(5); -// setPointRPM = ((1500/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval -// controller.setSetPoint(setPointRPM); -// wait(10); -// setPointRPM = ((2000/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval -// controller.setSetPoint(setPointRPM); -// wait(10); -// setPointRPM = ((3000/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval -// controller.setSetPoint(setPointRPM); -// wait(10); -// setPointRPM = ((500/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval -// controller.setSetPoint(setPointRPM); -// wait(10); -// setPointRPM = ((2000/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval -// controller.setSetPoint(setPointRPM); -// wait(10); -// setPointRPM = ((1200/60.0)*TICKS_PER_REVOLUTION)/(1.0/TICK_CHECK_PERIOD ); //convert setpoint rpm to ticks per interval -// controller.setSetPoint(setPointRPM); -// wait(10); - - } }