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.
Dependencies: HDSP253X mbed PCF8574_Bus
main.cpp
- Committer:
- wim
- Date:
- 2011-08-31
- Revision:
- 0:2467aed99127
- Child:
- 1:e180256ba6fb
File content as of revision 0:2467aed99127:
/* mbed bus - Main
* Copyright (c) 2011 Wim Huiskamp
*
* Released under the MIT License: http://mbed.org/license/mit
*
* version 0.2 Initial Release
*/
#include "mbed.h"
#include "BusDefines.h"
#include "PCF8574_DataBus.h"
#include "PCF8574_AddressBus.h"
#include "PCF8574_EnableBus.h"
#include "MBED_ControlBus.h"
// Debug stuff
#define __DEBUG
#include "Dbg.h"
// mbed Interface Hardware definitions
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut heartbeatLED(LED4);
// Host PC Communication channels
Serial pc(USBTX, USBRX);
//I2C Bus
I2C i2c(D_SDA, D_SCL);
// Bus Interface Hardware definitions
PCF8574_DataBus databus = PCF8574_DataBus(i2c, D_I2C_DATA_BUS); //Copy constructors..
PCF8574_AddressBus addressbus = PCF8574_AddressBus(i2c, D_I2C_ADDR_BUS);
PCF8574_EnableBus enablebus = PCF8574_EnableBus(i2c, D_I2C_ENA_BUS);
MBED_ControlBus controlbus = MBED_ControlBus(D_WR, D_RD, D_DTR, D_CDBUF, D_CDINT);
// Dummy delay
#define DEVICE_WAIT_MS 0
// Variables for Heartbeat and Status monitoring
Ticker heartbeat;
bool heartbeatflag=false;
// Cycle Timer
Timer cycletimer;
int cyclecount = 0;
const int maxcount = 10;
// Local functions
void clear_screen() {
//ANSI Terminal Commands
pc.printf("\x1B[2J");
pc.printf("\x1B[H");
}
void init_interfaces() {
// Init Host PC communication, default is 9600
pc.baud(D_BAUDRATE);
// Init I/F hardware
i2c.frequency(100000);
//Done, Tell me about it
myled1 = 1;
DBG("Init Interfaces Done\r");
}
// Heartbeat monitor
void pulse() {
heartbeatLED = !heartbeatLED;
}
void heartbeat_start() {
heartbeat.attach(&pulse, 0.5);
heartbeatflag = true;
}
void heartbeat_stop() {
heartbeat.detach();
heartbeatflag = false;
}
void show_LEDS () {
static int state = 0;
switch (state) {
case 0:
myled1 = 1;
myled2 = 0;
myled3 = 0;
state = 1;
break;
case 1:
myled1 = 0;
myled2 = 1;
myled3 = 0;
state = 2;
break;
case 2:
myled1 = 0;
myled2 = 0;
myled3 = 1;
state = 0;
break;
}
}
// The next two functions are examples of low-level reading and writing to a device that is connected on the mbed bus.
// In your own application you can develop a Class for each specific slave device and include modified versions of the
// functions below as 'private' functions. This allows you to hardcode the device CS_pin signals, define specific delays
// when needed, change the sequence of CS, WR etc or mask out certain address or databits when they are not used in a certain case.
//
/*---------------------------------------------------------------------------*\
|
| Function: write
|
| Description: Low level data write routine for device. Takes in data
| and address and CS pin to identify the device and writes
| data to the display. For simplicity, entire address byte
| is written, even though top two bits are unused inputs.
| After performing the operation, address lines are set
| all high, in order to eliminate current drain through
| pullup resistors (0.5mA per pin with 10K pullups)
|
| Parameters: address - full address in bits 0-5
| device - enum CS_Pin for Chip Select pin
| data - data byte to write out
|
| Returns: Nothing.
|
\*---------------------------------------------------------------------------*/
void write(uint8_t address, CS_Pin device, uint8_t data)
{
// // Switch databus buffer to outputs (note: this is the default state)
// controlbus.busdir(WRITE);
// // Switch databus to outputs
// databus.busdir(WRITE);
// Write out the address on to the addressbus and wait
addressbus.write(address);
wait_ms(DEVICE_WAIT_MS);
// Set CE low and wait
enablebus.chipselect(device, LOW);
wait_ms(DEVICE_WAIT_MS);
// Write data to the databus
databus.write(data);
wait_ms(DEVICE_WAIT_MS);
// Set WR low, wait, then set high and wait
controlbus.WR(LOW);
wait_ms(DEVICE_WAIT_MS);
controlbus.WR(HIGH);
wait_ms(DEVICE_WAIT_MS);
// Set CE high and wait
enablebus.chipselect(device, HIGH);
wait_ms(DEVICE_WAIT_MS);
// // Switch databus back to inputs
// databus.busdir(READ);
// // Switch databus buffer back to inputs
// controlbus.busdir(READ);
// // Set address lines all high to minimise power through pullups
// addressbus.write(0xFF);
}
/*---------------------------------------------------------------------------*\
|
| Function: read
|
| Description: Low level data read routine for a Device. Takes in
| address and CS pin to identify the device and then
| reads data from the device.
| After performing the operation, address lines are set
| all high, in order to eliminate current drain through
| pullup resistors (0.5mA per pin with 10K pullups)
|
| Parameters: address - 8 bit address
| device - enum CS_Pin for Chip Select pin
| Returns: data - data byte read
|
\*---------------------------------------------------------------------------*/
uint8_t read(uint8_t address, CS_Pin device)
{
uint8_t data = 0;
// Switch databus to inputs (default state is output)
databus.busdir(READ);
// Switch databus buffer to inputs
controlbus.busdir(READ);
// Write out the address on to the addressbus and wait
addressbus.write(address);
wait_ms(DEVICE_WAIT_MS);
// Set CE low and wait
enablebus.chipselect(device, LOW);
wait_ms(DEVICE_WAIT_MS);
// Set RD low and wait
controlbus.RD(LOW);
wait_ms(DEVICE_WAIT_MS);
// Read the data byte from databus
data = databus.read();
// set RD high and wait
controlbus.RD(HIGH);
wait_ms(DEVICE_WAIT_MS);
// Set CE high and wait
enablebus.chipselect(device, HIGH);
wait_ms(DEVICE_WAIT_MS);
// // Set address lines all high to minimise power through pullups
// addressbus.write(0xFF);
// Switch databus buffer back to outputs
controlbus.busdir(WRITE);
// Switch databus to outputs
databus.busdir(WRITE);
// Return read data to caller
return data;
}
int main() {
int address, result;
uint8_t data, dummy;
init_interfaces();
heartbeat_start();
clear_screen();
DBG("Start Main Loop\r");
//Testing stuff
//Test cycletime
cycletimer.start();
cycletimer.reset();
while (1) {
for (address=0; address<256; address++) {
//data = read(address, CS_SWITCH);
dummy = ~address;
write(address, LATCHEN_1, dummy);
// wait(0.05);
}
// Just for Info, lets see how fast this cycle is...
cyclecount++;
if (cyclecount == maxcount) {
pc.printf("Freq = %d Hz\r", (cyclecount * 256 * 1000) / cycletimer.read_ms());
cyclecount = 0;
cycletimer.reset();
}
show_LEDS ();
}
DBG("I'll be back...\r\r");
}
HDSP-253X Smart Alphanumeric Displays