ESP8266 Projects Blog
Home of CBDB / MPDMv4 /SmartMon Development boards (ESP-12/ESP-07).
Guidelines and ESP8266 programming examples using LUA, Eclipse and Arduino IDE, ESP Basic and many more!
I was looking a while ago to implement some Decimal-to-Binary conversion subroutine to be used for easy visualisation of the Registry content. Been able to see the values directly binary can help a lot when developping a driver for a IC or any piece of related software:
1. Decimal-to-Binary subroutine:
[decbin] i = 1 r = 0 binval = 0 nr = decval Do r = nr % 2 nr = int(nr/2) binval = binval + r*i i = i*10 Loop while nr > 0 wait
2. Test Program:
decval = 0 binval = 0 wprint "<br><br>Decimal value "
In the process of working on a new Driver Tutorial for the ESPBasic Series I am looking today to implement some Decimal-to-BCD and BCD-to-Decimal subroutines.
What I have done so far:
1. BCD-to-Decimal subroutine:
[DEC] hv = val >> 4 hl = val and 15 val = hv*10 + hl wait
2. Decimal-to-BCD subroutine:
[BCD] d = int( val / 10 ) d1 = d * 10 d2 = val - d1 val = d*16 + d2 wait
3. Test Program
let val = 96 textbox val button "To DEC", [DEC] button "To BCD", [BCD] wprint "<br><br>" button " Exit ", [TestExit] wait
[TestExit] end
If you have any other idea please feel free to share it, looking forward to see smarter solutions that that. I'm sure they are!
I was looking a few days ago for a I2C EEPROM to do some tests with ESPBasic and the results...can be seen below :)
1. MAIN Program
cls i2c.setup(4,5) 'choose your I2C bus pins
let eeprom_address = 0 'EEPROM memory start address i2c_address = 87 ' 0x57 - AT24C32 I2C address let i = 0 stw = "87654321" 'string that we want to save in EEPROM
i = 1 do stru = mid(stw,i,1) print "Value to Write: " & asc(stru) i2c.write(asc(stru)) i = i + 1 loop while i < stwl+1 i = 0 i2c.end() print "Write complete" wait
Of course, this is just a proof of concept driver example for the I2C 24C32 EEPROM but I think might help you with a good start :)
PCF8591 Module like the one from here or here, advertised as new versions.
For programming and uploading the driver and the software we will use the ESPBasic
Connection with the ESP8266 nEXT EVO Board is very easy, asPCF8591 Moduleconnector
is fully compatible with the nEXT Bus connector.
Driver implementation As been a I2C compatible device you need to have a standard I2C Bus Initialisation function as usual and also to know the I2C address of the device. For a detailed PCF 8591 Control Byte description go to PART1.
Software:
1. Main program:
let address = 72 'PCF8591 I2C Address i2c.setup(4,5) 'choose your I2C bus pins
cls let dac = 0 let dac_v = 0 let adc0_1 = 0 let adc0_v = 0 let v_cal = 0.0128
ESP Basic series - I2C driver example for the PCF8591 I2C 8-bit A/D and D/A converter. General view:
The PCF8591 is a single-chip, single-supply low-power 8-bit CMOS data acquisition device with four analog inputs, one analog output and a serial I2C-bus interface. Three address pins A0, A1 and A2 are used for programming the hardware address, allowing the use of up to eight devices connected to the I2C-bus without additional hardware. Address, control and data to and from the device are transferred serially via the two-line bidirectional I2C-bus.
The functions of the device include analog input multiplexing, on-chip track and hold function, 8-bit analog-to-digital conversion and an 8-bit digital-to-analog conversion. The maximum conversion rate is given by the maximum speed of the I2C-bus.
Features:
Single power supply
Operating supply voltage 2.5 V to 6.0 V
Low standby current
Serial input and output via I2C-bus
I2C address selection by 3 hardware address pins
Max sampling rate given by I2C-bus speed
4 analog inputs configurable as single ended or differential inputs
PCF8591 Module like the one from here or here, advertised as new versions.
For programming and uploading the driver and the software we will use the ESPBasic
Connection with the ESP8266 nEXT EVO Board is very easy, asPCF8591 Moduleconnector
is fully compatible with the nEXT Bus connector.
Driver implementation
As been a I2C compatible device you need to have a standard I2C Bus Initialisation function as usual and also to know the I2C address of the device.
Control Byte is the second byte sent to a PCF8591 device and is stored in its control register and is required to control the device function :
Things to take care about:
The upper nibble of the control register is used for enabling the analog output, and for programming the analog inputs as single-ended or differential inputs. The lower nibble selects one of the analog input channels defined by the upper nibble .
If the auto-increment flag is set, the channel number is incremented automatically after each A/D conversion. If the auto-increment mode is desired in applications where the internal oscillator is used, the analog output enable flag must be set in the control byte (bit 6). This allows the internal oscillator to run continuously, by this means preventing conversion errors resulting from oscillator start-up delay.
The analog output enable flag can be reset at other times to reduce quiescent power consumption.
The selection of a non-existing input channel results in the highest available channel number being allocated. Therefore, if the auto-increment flag is set, the next selected channel is always channel 0.
The most significant bits of both nibbles are reserved for possible future functions and must be set to logic 0. After a Power-On Reset (POR) condition, all bits of the control register are reset to logic 0. The D/A converter and the oscillator are disabled for power saving. The analog output is switched to a high-impedance state.
Software:
1. Main program:
let address = 72 'PCF8591 I2C Address i2c.setup(4,5) 'choose your I2C bus pins cls let dac = 0 let dac_v = 0 let v_cal = 0.0128 wprint " <b>PCF8591 - 8-bit A/D and D/A converter driver <br>DAC demo</b><br><i>by tech@esp8266-projects.com</i><br><br> " wprint " Input DAC value" textbox dac button " Set Value ",[SetDAC] wprint " <br>DAC Output (V)" textbox dac_v wprint "<br><br>" button " Stop Program", [Exit] wait
In the ESP Basic Web editor interface Type & Save your program as "PCF8591.bas" and Run it.
If all OK the result should look as below:
If you want your program to start automatically at reboot/power ON then just Save it as "default.bas" and also from Settings Tab enable the "Run default.bas at startup".
Be aware that at start-up/reboot, it is a delay before your program will start automatically.
In the next part about PCF8591 we will talk about the ADC Driver implementation.
As this time we will talk about the Software side we will design a very simple driver for our board that will include also a interactive Web command interface for the MPRSx8 Home Automation Dev Board Relays.
To keep things simple, we will just add in our Web Interface 8 ON/OFF Buttons and one general OFF one, from where we can turn ON/OFF all the Relays switches.
i2c.begin(address) ss = 0 xor 255 'XOR - Bit masking for the desired I/O pins i2c.write(ss) i2c.end() button "x1", [5] ' Button for Relay 1 button "x2", [6] button "x3", [7] button "x4", [8] button "x5", [1] button "x6", [2] button "x7", [3] button "x8", [4] ' Button for Relay 8 button "OFF", [9] ' General OFF for all the Relays wait
2. General OFF for all the Relays subroutine
[9] i2c.begin(address) ss = 0 xor 255 'XOR - Bit masking for the desired I/O pins i2c.write(ss) i2c.end() wait
3. Subroutines for each Button
[1] i2c.begin(address) ss = ss xor 1 'XOR - Bit masking for the desired I/O pins i2c.write(ss) i2c.end() wait
[2] i2c.begin(address) ss = ss xor 2 i2c.write(ss) i2c.end() wait
[3] i2c.begin(address) ss = ss xor 4 i2c.write(ss) i2c.end() wait
[4] i2c.begin(address) ss = ss xor 8 i2c.write(ss) i2c.end() wait
[5] i2c.begin(address) ss = ss xor 16 i2c.write(ss) i2c.end() wait
[6] i2c.begin(address) ss = ss xor 32 i2c.write(ss) i2c.end() wait
[7] i2c.begin(address) ss = ss xor 64 i2c.write(ss) i2c.end() wait
[8] i2c.begin(address) ss = ss xor 128 i2c.write(ss) i2c.end() wait
In the Web editor interface Save your program as "test_MPRSx8_1.bas" and Run it.
If all OK the result should look as below:
If you want your program to start automatically at reboot/power ON then just Save it as "default.bas" and also from Settings Tab enable the "Run default.bas at startup".
Be aware that at start-up/reboot, it is a delay before your program will start automatically.
For programming and uploading the driver and the software we will use ESP8266 Basic .
This is how is looking the setup on a breadboard, for a better visibility:
Software implementation:
Something to remember:
PCF8574 can SINK but NOT SOURCE much current - 100uA only (it cannot
output high, if you want). Look at the above example how is connected
the LED for SINKING current.
Each of the 8 GPIOs have a minimum guaranteed sinking current of 10 mA per bit at 5 V.
Each pin needs its own limiting resistor to prevent damage to the device!! keep under 25mA/pin.
Maximum device limit sink current in about 80mA. If you need more, look after PCA8574 (200mA max sink current!)
Now is time to put it at work also in ESP8266 Basic and see how is working.
The example for today is a implementation of a mutually-exclusive switch that can be handy for many, many things as for example for doing the selection of a source in a Audio mixer or a any other analog path. Or driving Power relays.
Mutually exclusive switch means that when the corresponding switch button is pressed ON must also automatically turn the other sources OFF.
For programming and uploading the driver and the software we will use ESP8266 Basic .
This is how is looking the setup on a breadboard, for a better visibility:
Software implementation:
Something to remember:
PCF8574 can SINK but NOT SOURCE much current - 100uA only (it cannot
output high, if you want). Look at the above example how is connected
the LED for SINKING current.
Each of the 8 GPIOs have a minimum guaranteed sinking current of 10 mA per bit at 5 V.
Each pin needs its own limiting resistor to prevent damage to the device!! keep under 25mA/pin.
Maximum device limit sink current in about 80mA. If you need more, look after PCA8574 (200mA max sink current!)
button "1", [1] 'Define push buttons for each I/O pin button "2", [2] button "3", [3] button "4", [4] button "5", [5] button "6", [6] button "7", [7] button "8", [8] button "OFF", [9] 'Button for turning OFF all wait
[9] i2c.begin(address) 'Start I2C communication with device at 'address' i2c.write(255) 'write the corresponding I/O pin value to register i2c.end() 'end I2C communication wait
From today I will start a new Series related with ESP Basic (www.esp8266basic.com)
When working with a lot of I2C devices one of the first thing that you need is a simple I2C scanner that can help you to identify the IC's and do some very basic troubleshooting.
For programming and uploading the driver and the software we will use ESP8266 Basic .
nEXT EVO + AN-1 extension board
I2C Scanner Software: UPDATE !! UPDATE !!UPDATE !!UPDATE !!
From ESPBasic Version 3.0 Alpha 43 you have also the i2c.setup function available!!
i2c.setup():
Will change the default pins for the i2c interface.
i2c.setup({SDA}, {SCL})
i2c.setup(4,5)
for address = 1 to 127 i2c.begin(address) stat = i2c.end()
if stat < 1 then ' print stat wprint "Found I2C device at address: 0x" & hex(address) wprint " - > " & address wprint " <br>" endif
next wait
And the scanning result in browser :
As you can see, has properly detected the I2C devices that are on the AN-1 Extension board:
- PCF8574 - 8Bit I/O EXT port - found at address 0x20 -> 32 - LM75 - Temperature sensor - found at address 0x48 -> 72 - MCP4728 - 4x12Bit DAC - found at address 0x60 -> 96 - MCP3421 - 18Bit ADC - found at address 0x68 -> 10
Please keep in mind that in ESP Basic you have hardcoded I2C pins yet, SDA -> GPIO0 and SCL -> GPIO2.
Don't forget to add if needed pullup resistors on both lines, 4K7 should be OK. In case of AN-1 they are NOT needed.