Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TCTF_Control_Main by
MCP23008.cpp
00001 #include "mbed.h" 00002 #include "MCP23008.h" 00003 #include "MODSERIAL.h" 00004 00005 00006 namespace { 00007 const uint8_t I2C_WRITE = 0x00; 00008 const uint8_t I2C_READ = 0x01; 00009 00010 /* MCP23008 registers */ 00011 const uint8_t IODIR = 0x00; 00012 const uint8_t GPIO = 0x09; 00013 const uint8_t PULLUP = 0x06; 00014 00015 //Channel addresses 00016 const uint8_t CHNL_0 = 0x01; 00017 const uint8_t CHNL_1 = 0x02; 00018 const uint8_t CHNL_2 = 0x04; 00019 const uint8_t CHNL_3 = 0x08; 00020 }; 00021 00022 MCP23008::MCP23008 (PinName sda, PinName scl, uint8_t address, int freq): i2c( sda, scl ){ 00023 addrI2C = address; 00024 i2c.frequency(freq); 00025 } 00026 00027 void MCP23008::setAddress(int address){ 00028 addrI2C = address; 00029 } 00030 00031 void MCP23008::writeOutput(int chn1, int chn2, int chn3, int chn4){ 00032 char data[2]; 00033 char data2[2]; 00034 uint8_t val = 0x00; 00035 00036 //Create mask for all bits that should be on 00037 if(chn1) val = val | CHNL_0; 00038 if(chn2) val = val | CHNL_1; 00039 if(chn3) val = val | CHNL_2; 00040 if(chn4) val = val | CHNL_3; 00041 00042 data[0] = GPIO; //register address 00043 data[1] = val; //GPIO bit map 00044 data2[0] = PULLUP; 00045 data2[1] = 0x00; 00046 00047 i2c.write((addrI2C<<1)|(I2C_WRITE), data, 2); 00048 i2c.write((addrI2C<<1)|(I2C_WRITE), data2, 2); 00049 //wait(0.1); 00050 } 00051 00052 void MCP23008::init(){ 00053 char io_setup[2]; 00054 00055 io_setup[0] = IODIR; 00056 io_setup[1] = 0x00; //turn all pins to outputs 00057 00058 i2c.write((addrI2C<<1)|(I2C_WRITE), io_setup, 2); 00059 //wait(0.1); 00060 00061 //Set all outputs to off 00062 GPIO_Pin_Status = 0x00; 00063 }
Generated on Fri Jul 15 2022 01:55:39 by
1.7.2
