Initial I2C Working

Dependencies:   mbed

Committer:
sk398
Date:
Wed Mar 29 12:20:34 2017 +0000
Revision:
1:444546e8cd20
Parent:
0:fbf82bf637bb
Child:
2:832cb4376d2a
working, useful for I2C Comms with NUCLEO Boards;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sk398 0:fbf82bf637bb 1 /********************************************************************************************
sk398 0:fbf82bf637bb 2
sk398 0:fbf82bf637bb 3 Filename: MCP9803.cpp
sk398 0:fbf82bf637bb 4
sk398 0:fbf82bf637bb 5 Original Author: Steven Kay
sk398 0:fbf82bf637bb 6
sk398 0:fbf82bf637bb 7 Development Group: Autonomous Systems Group, RAL Space
sk398 0:fbf82bf637bb 8
sk398 0:fbf82bf637bb 9 Original Creation Date: April 2017
sk398 0:fbf82bf637bb 10
sk398 0:fbf82bf637bb 11 Description: <Desc>
sk398 0:fbf82bf637bb 12
sk398 0:fbf82bf637bb 13 Revision History: Version 1.0 - Initial Release
sk398 0:fbf82bf637bb 14
sk398 0:fbf82bf637bb 15 *********************************************************************************************/
sk398 0:fbf82bf637bb 16
sk398 0:fbf82bf637bb 17 #include "MCP9803.h"
sk398 0:fbf82bf637bb 18
sk398 0:fbf82bf637bb 19 MCP9803::MCP9803(PinName sda, PinName scl, int Address, int frequency)
sk398 0:fbf82bf637bb 20 {
sk398 0:fbf82bf637bb 21 _I2C = new I2C(sda,scl);
sk398 0:fbf82bf637bb 22
sk398 0:fbf82bf637bb 23 _I2C -> frequency(frequency);
sk398 0:fbf82bf637bb 24
sk398 0:fbf82bf637bb 25 _I2C -> start();
sk398 0:fbf82bf637bb 26 _I2C -> stop();
sk398 0:fbf82bf637bb 27
sk398 0:fbf82bf637bb 28 chipAddress = Address;
sk398 1:444546e8cd20 29
sk398 1:444546e8cd20 30 inBuffer = (char *)malloc(1);
sk398 0:fbf82bf637bb 31 }
sk398 0:fbf82bf637bb 32
sk398 0:fbf82bf637bb 33 int MCP9803::ConfigSensor()
sk398 0:fbf82bf637bb 34 {
sk398 0:fbf82bf637bb 35
sk398 0:fbf82bf637bb 36
sk398 0:fbf82bf637bb 37 return 0;
sk398 0:fbf82bf637bb 38 }
sk398 0:fbf82bf637bb 39
sk398 0:fbf82bf637bb 40
sk398 0:fbf82bf637bb 41
sk398 0:fbf82bf637bb 42 int MCP9803::I2C_Write(char *dataOut,int dataLen)
sk398 0:fbf82bf637bb 43 {
sk398 0:fbf82bf637bb 44 char outBuffer[dataLen];
sk398 0:fbf82bf637bb 45
sk398 0:fbf82bf637bb 46 for(int i=0 ; i<dataLen ; i++)
sk398 0:fbf82bf637bb 47 {
sk398 0:fbf82bf637bb 48 outBuffer[i] = *(dataOut+i);
sk398 0:fbf82bf637bb 49 }
sk398 0:fbf82bf637bb 50
sk398 0:fbf82bf637bb 51 int sendStatus = _I2C -> write(chipAddress,outBuffer,dataLen,0);
sk398 0:fbf82bf637bb 52
sk398 0:fbf82bf637bb 53 return sendStatus;
sk398 0:fbf82bf637bb 54 }
sk398 0:fbf82bf637bb 55
sk398 0:fbf82bf637bb 56
sk398 0:fbf82bf637bb 57
sk398 0:fbf82bf637bb 58 char *MCP9803::I2C_Read(int dataLen)
sk398 0:fbf82bf637bb 59 {
sk398 1:444546e8cd20 60 setBufferSize(dataLen);
sk398 0:fbf82bf637bb 61
sk398 0:fbf82bf637bb 62 int receiveStatus = _I2C -> read(chipAddress,inBuffer,dataLen,0);
sk398 1:444546e8cd20 63
sk398 0:fbf82bf637bb 64 if(receiveStatus != 0)
sk398 0:fbf82bf637bb 65 {
sk398 0:fbf82bf637bb 66 for(int i = 0; i < dataLen; i++)
sk398 0:fbf82bf637bb 67 {
sk398 1:444546e8cd20 68 if(i % 2 == 0)
sk398 1:444546e8cd20 69 {
sk398 1:444546e8cd20 70 inBuffer[i] = 0xFF;
sk398 1:444546e8cd20 71 }
sk398 1:444546e8cd20 72 else
sk398 1:444546e8cd20 73 {
sk398 1:444546e8cd20 74 inBuffer[i] = 0x00;
sk398 1:444546e8cd20 75 }
sk398 0:fbf82bf637bb 76 printf("Buffer Value = %02x",inBuffer[i]);
sk398 0:fbf82bf637bb 77 }
sk398 1:444546e8cd20 78 return inBuffer;
sk398 0:fbf82bf637bb 79 }
sk398 0:fbf82bf637bb 80
sk398 0:fbf82bf637bb 81 else
sk398 0:fbf82bf637bb 82 {
sk398 0:fbf82bf637bb 83 return inBuffer;
sk398 0:fbf82bf637bb 84 }
sk398 0:fbf82bf637bb 85
sk398 0:fbf82bf637bb 86 }
sk398 0:fbf82bf637bb 87
sk398 1:444546e8cd20 88 void MCP9803::setBufferSize(int dataLen)
sk398 1:444546e8cd20 89 {
sk398 1:444546e8cd20 90 inBuffer = (char *)realloc(inBuffer, dataLen);
sk398 1:444546e8cd20 91 }
sk398 1:444546e8cd20 92
sk398 1:444546e8cd20 93 char *MCP9803::getBuffer()
sk398 1:444546e8cd20 94 {
sk398 1:444546e8cd20 95
sk398 1:444546e8cd20 96 }
sk398 1:444546e8cd20 97
sk398 1:444546e8cd20 98 void MCP9803::setBuffer()
sk398 1:444546e8cd20 99 {
sk398 1:444546e8cd20 100
sk398 1:444546e8cd20 101 }
sk398 1:444546e8cd20 102
sk398 1:444546e8cd20 103
sk398 0:fbf82bf637bb 104
sk398 0:fbf82bf637bb 105 /********************************************************************************************
sk398 0:fbf82bf637bb 106
sk398 0:fbf82bf637bb 107 Method: LinearMotor::LinearMotor
sk398 0:fbf82bf637bb 108
sk398 0:fbf82bf637bb 109 Description: Class initialiser sets up the Linear Motor's pins
sk398 0:fbf82bf637bb 110
sk398 0:fbf82bf637bb 111 Method Visibility: Public
sk398 0:fbf82bf637bb 112
sk398 0:fbf82bf637bb 113 Input Arguments
sk398 0:fbf82bf637bb 114 Name Type Description
sk398 0:fbf82bf637bb 115 -----------------------------------------------------------------------------------------
sk398 0:fbf82bf637bb 116 pwmPin Int The integer number of the connected hardware pin for
sk398 0:fbf82bf637bb 117 the PWM output to the linear motor
sk398 0:fbf82bf637bb 118
sk398 0:fbf82bf637bb 119 sensorPin Int The integer number of the connected hardware pin for
sk398 0:fbf82bf637bb 120 the Analog Sensor feedback from the Linear Motor
sk398 0:fbf82bf637bb 121
sk398 0:fbf82bf637bb 122 Output Arguments
sk398 0:fbf82bf637bb 123 ----------------------------------------------------------------------------------------
sk398 0:fbf82bf637bb 124 Name Type Description
sk398 0:fbf82bf637bb 125 N/A
sk398 0:fbf82bf637bb 126
sk398 0:fbf82bf637bb 127 *********************************************************************************************/