Initial I2C Working

Dependencies:   mbed

Committer:
sk398
Date:
Wed Mar 29 08:33:10 2017 +0000
Revision:
0:fbf82bf637bb
Child:
1:444546e8cd20
Initial version - I2C working

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 0:fbf82bf637bb 29 }
sk398 0:fbf82bf637bb 30
sk398 0:fbf82bf637bb 31 int MCP9803::ConfigSensor()
sk398 0:fbf82bf637bb 32 {
sk398 0:fbf82bf637bb 33
sk398 0:fbf82bf637bb 34
sk398 0:fbf82bf637bb 35 return 0;
sk398 0:fbf82bf637bb 36 }
sk398 0:fbf82bf637bb 37
sk398 0:fbf82bf637bb 38
sk398 0:fbf82bf637bb 39
sk398 0:fbf82bf637bb 40 int MCP9803::I2C_Write(char *dataOut,int dataLen)
sk398 0:fbf82bf637bb 41 {
sk398 0:fbf82bf637bb 42 char outBuffer[dataLen];
sk398 0:fbf82bf637bb 43
sk398 0:fbf82bf637bb 44 for(int i=0 ; i<dataLen ; i++)
sk398 0:fbf82bf637bb 45 {
sk398 0:fbf82bf637bb 46 outBuffer[i] = *(dataOut+i);
sk398 0:fbf82bf637bb 47 }
sk398 0:fbf82bf637bb 48
sk398 0:fbf82bf637bb 49 int sendStatus = _I2C -> write(chipAddress,outBuffer,dataLen,0);
sk398 0:fbf82bf637bb 50
sk398 0:fbf82bf637bb 51 return sendStatus;
sk398 0:fbf82bf637bb 52 }
sk398 0:fbf82bf637bb 53
sk398 0:fbf82bf637bb 54
sk398 0:fbf82bf637bb 55
sk398 0:fbf82bf637bb 56 char *MCP9803::I2C_Read(int dataLen)
sk398 0:fbf82bf637bb 57 {
sk398 0:fbf82bf637bb 58 char inBuffer[dataLen];
sk398 0:fbf82bf637bb 59
sk398 0:fbf82bf637bb 60 int receiveStatus = _I2C -> read(chipAddress,inBuffer,dataLen,0);
sk398 0:fbf82bf637bb 61
sk398 0:fbf82bf637bb 62 if(receiveStatus != 0)
sk398 0:fbf82bf637bb 63 {
sk398 0:fbf82bf637bb 64 for(int i = 0; i < dataLen; i++)
sk398 0:fbf82bf637bb 65 {
sk398 0:fbf82bf637bb 66 inBuffer[i] = 0xFF - i;
sk398 0:fbf82bf637bb 67 printf("Buffer Value = %02x",inBuffer[i]);
sk398 0:fbf82bf637bb 68 }
sk398 0:fbf82bf637bb 69 return inBuffer;
sk398 0:fbf82bf637bb 70 }
sk398 0:fbf82bf637bb 71
sk398 0:fbf82bf637bb 72 else
sk398 0:fbf82bf637bb 73 {
sk398 0:fbf82bf637bb 74 return inBuffer;
sk398 0:fbf82bf637bb 75 }
sk398 0:fbf82bf637bb 76
sk398 0:fbf82bf637bb 77 }
sk398 0:fbf82bf637bb 78
sk398 0:fbf82bf637bb 79
sk398 0:fbf82bf637bb 80 /********************************************************************************************
sk398 0:fbf82bf637bb 81
sk398 0:fbf82bf637bb 82 Method: LinearMotor::LinearMotor
sk398 0:fbf82bf637bb 83
sk398 0:fbf82bf637bb 84 Description: Class initialiser sets up the Linear Motor's pins
sk398 0:fbf82bf637bb 85
sk398 0:fbf82bf637bb 86 Method Visibility: Public
sk398 0:fbf82bf637bb 87
sk398 0:fbf82bf637bb 88 Input Arguments
sk398 0:fbf82bf637bb 89 Name Type Description
sk398 0:fbf82bf637bb 90 -----------------------------------------------------------------------------------------
sk398 0:fbf82bf637bb 91 pwmPin Int The integer number of the connected hardware pin for
sk398 0:fbf82bf637bb 92 the PWM output to the linear motor
sk398 0:fbf82bf637bb 93
sk398 0:fbf82bf637bb 94 sensorPin Int The integer number of the connected hardware pin for
sk398 0:fbf82bf637bb 95 the Analog Sensor feedback from the Linear Motor
sk398 0:fbf82bf637bb 96
sk398 0:fbf82bf637bb 97 Output Arguments
sk398 0:fbf82bf637bb 98 ----------------------------------------------------------------------------------------
sk398 0:fbf82bf637bb 99 Name Type Description
sk398 0:fbf82bf637bb 100 N/A
sk398 0:fbf82bf637bb 101
sk398 0:fbf82bf637bb 102 *********************************************************************************************/