hi,
why spi not working after detach usb. maple rev 5 is powered with external 5v. i used maple ide 12. lcd 4x20 works well.
thx
hi,
why spi not working after detach usb. maple rev 5 is powered with external 5v. i used maple ide 12. lcd 4x20 works well.
thx
code is here:
//#include <string.h>
#include <Wire.h> // For some strange reasons, Wire.h must be included here
#include <FRAM.h>
#include <LiquidCrystal.h>
char MSG_DEVICE_NAME[] __FLASH__ = "PowerMeter";
char MSG_DEVICE_NAME1[] __FLASH__ = "Graviton, SF";
char MSG_DEVICE_NAME2[] __FLASH__ = "Poprad, Slovakia";
char MSG_DEVICE_VER[] __FLASH__ = "V.: ";
HardwareSPI spiSA9904(2);
#define _DeviceAddress_I2C_FRAM B1010000
FRAMClass fram = FRAMClass(_DeviceAddress_I2C_FRAM, FRAM_24C64);
unsigned long opozdenie = millis();
unsigned long oldmillis = millis();
int CountSamples = 0;
const int nSamples = 1;
long zlepackety = 0;
long vsetkypackety = 0;
//#define SPI_DEBUG //Set this to 1 for SPI debug to enable
struct sCAL
{
int volt; //voltage correction
};
struct sSetup
{
float v_nom; //applied volatge [V]
float i_max; //max load current [A]
};
//chip-related constants
const long RATE = 320000; //register update rate
const long CLOCKFREQ = 3579545; //clock frequency
const float CRYSTAL_DIV256 = (float)CLOCKFREQ / 256.0f; //constant in frequency calculation, see application note.
const unsigned long LAST_10 = 0x3FF; //mask for last 10 bits in 24 bit number
const unsigned long LIMIT = 0xFFFFFF; //upperlimit for 24 bits
const int PHASE = 3; //3phase device
float epc; //energy per count
sSetup Setup;
sCAL CAL[PHASE];
float energy_A; // Ws
float energy_R; // Vars
//float aWs; // Ws skutocne po prepocitani casu
//float aVars; // Vars skutocne po prepocitani casu
float A;// kW/h
float R;// kVar/h
float U;
float I;
float f;
float Fi;
float cosFi;
float sinFi;
bool ph_cross0; // Voltage zero crossover
bool ph_seq; // Phase sequence error
bool ph_err; // Missing phase
bool ph_errph[3]; // Missing phases
long cas_merania;
void setup_meter()
{
epc = (Setup.v_nom * Setup.i_max) / RATE;
//if(VycitajDataZIC() == true)
// set_volt_cal();
// first = true;
}
void defaults()
{
Setup.v_nom = 230.0f;
Setup.i_max = 50.0f;
CAL[0].volt = 810;//795;
CAL[1].volt = 826;//795;
CAL[2].volt = 827;//795;
// MinW = 4.5;
}
bool sample = false;
const uint8 nRows = 4; //number of rows on LCD
const uint8 nColumns = 20; //number of columns on LCD
const byte LCD_backlight_pin = 29;
LiquidCrystal lcd(23, 24, 25, 26, 27, 28);
void setup()
{
Wire.begin(0x00, PORTI2C2, I2C_FAST_MODE | I2C_DUTY_16_9);
// spiSA9904.begin(SPI_281_250KHZ, MSBFIRST, SPI_MODE_0);
// lcd.setBacklightPin(LCD_backlight_pin, POSITIVE);
lcd.begin(nColumns, nRows);
// lcd.createChar(0, arrow_up);
// lcd.createChar(1, arrow_down);
// lcd.createChar(3, bell);
// lcd.setBacklight(LCDbackLightON);
lcd.setCursor(0, 0);
lcd.print(MSG_DEVICE_NAME);
lcd.setCursor(0, 1);
lcd.print(MSG_DEVICE_NAME1);
lcd.setCursor(0, 2);
lcd.print(MSG_DEVICE_NAME2);
lcd.setCursor(0, 3);
lcd.print(MSG_DEVICE_VER);
lcd.print(__DATE__);
delay(2000);
defaults();
setup_meter();
// SerialUSB.println("Starting...");
pinMode(BOARD_LED_PIN, OUTPUT);
pinMode(spiSA9904.nssPin(), OUTPUT);
digitalWrite(spiSA9904.nssPin(), LOW);
const int interruptPinSA9904B = 35;
pinMode(interruptPinSA9904B, INPUT_FLOATING);
attachInterrupt(interruptPinSA9904B, _F50sampleSA9904, FALLING);
lcd.clear();
}
unsigned long iLCDLoop = millis();
void loop()
{
if(sample) //read all registers starting from active
{
int F50time = millis()-oldmillis;
oldmillis = millis();
// lcd.setCursor(10, 0);
// lcd.print(F50time);
//#ifdef SPI_DEBUG
// SerialUSB.print("F50 time:;");
// SerialUSB.print(F50time);
// SerialUSB.print("; ");
//#endif
// if(F50time < 18 | F50time > 22)
// SerialUSB.println(";ERR F50time;");
F50sampleSA9904();
//#ifdef SPI_DEBUG
// SerialUSB.println("");
//#endif
ZobrazDataLCD();
sample = false;
}
}
void ZobrazDataLCD()
{
if( millis() > iLCDLoop + 800 )
{
// SerialUSB.print("lcd s:");
// SerialUSB.print(millis());
LCDClearRow(0);
lcd.setCursor(0, 0);
lcd.print(vsetkypackety,DEC);
LCDClearRow(1);
lcd.setCursor(0, 1);
lcd.print(zlepackety,DEC);
for(int i = 0; i< 20; i++)
{
// byte read1 = fram.read_byte(i);
// lcd.print(char(read1));
}
// SerialUSB.print("lcd k:");
// SerialUSB.println(millis());
LCDClearRow(2);
lcd.setCursor(0, 2);
lcd.print(U,3);
LCDClearRow(3);
lcd.setCursor(0, 3);
lcd.print(f,3);
iLCDLoop = millis();
}
}
void LCDClearRow(byte row)
{
lcd.setCursor(0, row);
for(byte i = 0; i < nColumns; i++)
lcd.print(" ");
}
void _F50sampleSA9904() // F50 impulse
{
CountSamples++;
if(CountSamples == nSamples)
{
sample = true;
CountSamples = 0;
}
}
void F50sampleSA9904()
{
byte rx[37] = {};
unsigned long reg1[4] = {};
readSA9904B(0, rx, 36);
vsetkypackety++;
reg1[0] = convertbutolong(rx, 0);
reg1[1] = convertbutolong(rx, 3);
reg1[2] = convertbutolong(rx, 6);
reg1[3] = convertbutolong(rx, 9);
A = SamplesA(0, reg1[0]);
R = SamplesR(0, reg1[1]);
U = SamplesU(0, reg1[2]);
f = SamplesFregvency(reg1[3]);
//#ifdef SPI_DEBUG
// SerialUSB.print(";A=;");
// SerialUSB.print(A,3);
// SerialUSB.print(";R=;");
// SerialUSB.print(R,3);
// SerialUSB.print(";U=;");
// SerialUSB.print(U,3);
// SerialUSB.print(";f=;");
// SerialUSB.print(f,3);
//#endif
if(f < 40 || f > 60)
{
// SerialUSB.println(";ERR;");
zlepackety++;
}
}
float SamplesA(byte ph, long regA)
{
float a = 0;
a = float(wrap(regA));
return a;
}
float SamplesR(byte ph, long regR)
{
float r = 0;
r = float(wrap(regR));
return r;
}
float SamplesU(byte ph, long regU)
{
float u = 0;
if (CAL[ph].volt != 0 && Setup.v_nom != 0)
u = (float)(regU / (float)CAL[ph].volt * Setup.v_nom);
return u;
}
float SamplesFregvency(long regFregvency)
{
long fmr = regFregvency & LAST_10;
float ff = 0;
if (fmr != 0)
ff = (float)(CRYSTAL_DIV256 / fmr);
return ff;
}
long wrap(long value)
{
int sign = 1;
if( abs(value) > 0x7FFFFF) //val>8388607?
{
if(value>0)
sign=-1;
value=(0x1000000+(sign*value))*sign; //(16777216(+or-)value)*(+or-)
}
return (value);
}
void readSA9904B(byte startreg, byte *data, byte bytesreq)
{
spiSA9904.begin(SPI_281_250KHZ, MSBFIRST, SPI_MODE_0);
digitalWrite(spiSA9904.nssPin(), HIGH);
spiSA9904.transfer(0x01);
spiSA9904.transfer(startreg | 0x80);
spiSA9904.begin(SPI_281_250KHZ, MSBFIRST, SPI_MODE_1);
for(byte i = 0; i < bytesreq; i++)
data[i]=spiSA9904.transfer(0x00);
digitalWrite(spiSA9904.nssPin(), LOW);
spiSA9904.end();
#ifdef SPI_DEBUG
SerialUSB.print(";registers; ");
for(byte i=0;i<bytesreq;i++)
{
SerialUSB.print(data[i], HEX);
SerialUSB.print(";");
}
#endif
}
unsigned long convertbutolong(byte *buf, int idx)
{
return ((unsigned long)buf[idx]<<16 | (unsigned long)buf[idx+1]<<8 | (unsigned long)buf[idx+2]) & LIMIT;
}
stafil - would you please take note of the "Guidelines for Posting" http://forums.leaflabs.com/topic.php?id=994
Specifically it asks:
When posting code:
I think your example is so long and complex that people might find it quite difficult to give help. Have you got a shorter and simpler example which has the bug?
I have a maple rev 5 is connected to 5V via a usb dupe program. The program communicates with SA9904 spi2 correctly. when I unplug the USB and press RESET and communication after spi2 not work. Please advise me.
starfil - Have you got a simpler example program which still has the bug?
When you try to reduce the program, you might identify the bug anyway.
By showing us a shorter program, it is more likely to someone will read it and try to help.
here is a simple code. https://gist.github.com/3bf4afa7b8eba06fca1e
Thanks to all who answered me. I've solved it. I do not know why must be NSS pin after setting up spi.begin. But that's no longer the case. Well it would be if it was mentioned in the documentation.
this-> spi-> begin (SPI_281_250KHZ, MSBFIRST, SPI_MODE_0);
pinMode (this-> spi-> nssPin (), OUTPUT);
Thank you
You must log in to post.