Example controlling galvanomirrors (X and Y) using the spi DAC MCP4922 and the new platform FRDM_K64F

Dependencies:   mbed

Committer:
mbedalvaro
Date:
Sun May 25 11:28:28 2014 +0000
Revision:
0:26b228dde70c
Child:
2:383b2acec6e4
first commit, program compiles but yet not tried on the mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 0:26b228dde70c 1
mbedalvaro 0:26b228dde70c 2 #ifndef hardwareIO_h
mbedalvaro 0:26b228dde70c 3 #define hardwareIO_h
mbedalvaro 0:26b228dde70c 4
mbedalvaro 0:26b228dde70c 5 #include "mbed.h"
mbedalvaro 0:26b228dde70c 6
mbedalvaro 0:26b228dde70c 7
mbedalvaro 0:26b228dde70c 8 //SPI library (for ADC MCP4922 chip) uses the following pins, but we don't have to set them and inputs or outputs
mbedalvaro 0:26b228dde70c 9 // (this is done when the library is initialized, which is done by pre-instantiation:
mbedalvaro 0:26b228dde70c 10 #define SCK_PIN D13 //SPI Clock
mbedalvaro 0:26b228dde70c 11 #define MISO_PIN D12 //SPI input (data comming from DAC, here not connected)
mbedalvaro 0:26b228dde70c 12 #define MOSI_PIN D11 //SPI output (data going to DAC)
mbedalvaro 0:26b228dde70c 13
mbedalvaro 0:26b228dde70c 14 //**** CHIP SELECT pins for MP4922 DAC (mirrors and red laser)
mbedalvaro 0:26b228dde70c 15 // VERY IMPORTANT: the chip select for the DACs should be different from the default SPI "SS" pin (Slave Select), which is 53 by default (and will be used by the Ethernet Shield).
mbedalvaro 0:26b228dde70c 16 #define CS_DAC_MIRRORS D7 //Chip Select of the first DAC (mirrors)
mbedalvaro 0:26b228dde70c 17
mbedalvaro 0:26b228dde70c 18 //**** LASERS pins:
mbedalvaro 0:26b228dde70c 19 #define LASER_RED_PIN D8 // USED (TTL control).
mbedalvaro 0:26b228dde70c 20 #define LASER_GREEN_PIN D9 // NOT YET USED USED (TTL control)
mbedalvaro 0:26b228dde70c 21 #define LASER_BLUE_PIN D10 // NOT YET USED USED (TTL control)
mbedalvaro 0:26b228dde70c 22
mbedalvaro 0:26b228dde70c 23 //**** MIRRORS:
mbedalvaro 0:26b228dde70c 24 //The DAC is 12 bytes capable (Max=4096), but we will limit this a little.
mbedalvaro 0:26b228dde70c 25 #define MAX_AD_MIRRORS 4095 // note: 4095 is the absolute maximum for the SPI voltage (5V). This is for checking hardware compliance, but max and min angles can be defined for X and Y in each LivingSpot instance.
mbedalvaro 0:26b228dde70c 26 #define MIN_AD_MIRRORS 0 // note: 0 is 0 volts for the SPI voltage.
mbedalvaro 0:26b228dde70c 27 // We assume that the center of the mirror is at MAX_AD_MIRRORS/2 = 2000:
mbedalvaro 0:26b228dde70c 28 #define CENTER_AD_MIRROR_X 2047 // This MUST BE the direction of the photodetector.
mbedalvaro 0:26b228dde70c 29 #define CENTER_AD_MIRROR_Y 2047 // This MUST BE the direction of the photodetector.
mbedalvaro 0:26b228dde70c 30
mbedalvaro 0:26b228dde70c 31
mbedalvaro 0:26b228dde70c 32 extern DigitalOut Laser_Red, Laser_Green, Laser_Blue;
mbedalvaro 0:26b228dde70c 33
mbedalvaro 0:26b228dde70c 34 // ==================================================================================================================================================================
mbedalvaro 0:26b228dde70c 35
mbedalvaro 0:26b228dde70c 36 class HardwareIO {
mbedalvaro 0:26b228dde70c 37 public:
mbedalvaro 0:26b228dde70c 38
mbedalvaro 0:26b228dde70c 39
mbedalvaro 0:26b228dde70c 40 void init(void);
mbedalvaro 0:26b228dde70c 41
mbedalvaro 0:26b228dde70c 42 void showLimitsMirrors( int times );
mbedalvaro 0:26b228dde70c 43
mbedalvaro 0:26b228dde70c 44 // SPI control for DAC for mirrors and red laser power (low level):
mbedalvaro 0:26b228dde70c 45 void writeOutX(unsigned short value);
mbedalvaro 0:26b228dde70c 46 void writeOutY(unsigned short value);
mbedalvaro 0:26b228dde70c 47 void writeOutXY(unsigned short valueX, unsigned short valueY) {writeOutX(valueX); writeOutY(valueY);};
mbedalvaro 0:26b228dde70c 48
mbedalvaro 0:26b228dde70c 49
mbedalvaro 0:26b228dde70c 50 //Displaying lasers:
mbedalvaro 0:26b228dde70c 51 // Again: for the moment laser are TTL but these could be analog. Now, it is just: powerValue > 0 ==> 'true'; else 'false'
mbedalvaro 0:26b228dde70c 52 // Red laser:
mbedalvaro 0:26b228dde70c 53 void setRedPower(int powerRed);
mbedalvaro 0:26b228dde70c 54 // Green laser:
mbedalvaro 0:26b228dde70c 55 void setGreenPower(int powerGreen);
mbedalvaro 0:26b228dde70c 56 // Blue laser:
mbedalvaro 0:26b228dde70c 57 void setBluePower(int powerBlue);
mbedalvaro 0:26b228dde70c 58 // Setting all colors at once:
mbedalvaro 0:26b228dde70c 59 void setRGBPower(unsigned char color); // we will use the 3 LSB bits to set each color
mbedalvaro 0:26b228dde70c 60
mbedalvaro 0:26b228dde70c 61
mbedalvaro 0:26b228dde70c 62
mbedalvaro 0:26b228dde70c 63 private:
mbedalvaro 0:26b228dde70c 64
mbedalvaro 0:26b228dde70c 65 };
mbedalvaro 0:26b228dde70c 66
mbedalvaro 0:26b228dde70c 67
mbedalvaro 0:26b228dde70c 68 extern HardwareIO IO; // allows the object IO to be used in other .cpp files (IO is pre-instantiated in hardwareIO.cpp)
mbedalvaro 0:26b228dde70c 69 // NOTE: IO encapsulates many IO functions, but perhaps it is better not to have an IO object - just use each IO function separatedly (as with pc object for instance)
mbedalvaro 0:26b228dde70c 70 extern Serial pc; // allows pc to be manipulated by other .cpp files, even if pc is defined in the hardwareIO.cpp
mbedalvaro 0:26b228dde70c 71
mbedalvaro 0:26b228dde70c 72
mbedalvaro 0:26b228dde70c 73 #endif