Skip to content

Instantly share code, notes, and snippets.

Created December 17, 2012 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3bf4afa7b8eba06fca1e to your computer and use it in GitHub Desktop.
Save anonymous/3bf4afa7b8eba06fca1e to your computer and use it in GitHub Desktop.
#include <LiquidCrystal.h>
HardwareSPI spiSA9904(2);
const unsigned long LIMIT = 0xFFFFFF; //upperlimit for 24 bits
int CountSamples = 0;
const int nSamples = 1;
bool sample = false;
float U = 0.0f;
float f = 0.0f;
unsigned long iLCDLoop = millis();
LiquidCrystal lcd(23, 24, 25, 26, 27, 28);
void setup()
{
//Serial2.begin(115200);
lcd.begin(20, 4);
pinMode(BOARD_SPI2_NSS_PIN, OUTPUT);
digitalWrite(BOARD_SPI2_NSS_PIN, LOW);
const int interruptPinSA9904B = 35;
pinMode(interruptPinSA9904B, INPUT_FLOATING);
attachInterrupt(interruptPinSA9904B, _F50sampleSA9904, FALLING);
}
void loop()
{
if(sample) //read all registers starting from active
{
F50sampleSA9904();
ZobrazDataLCD();
sample = false;
}
}
void ZobrazDataLCD()
{
if( millis() > iLCDLoop + 800 )
{
lcd.setCursor(0, 0);
lcd.print(millis()/1000, DEC);
lcd.setCursor(0, 2);
lcd.print(U,3);
lcd.setCursor(0, 3);
lcd.print(f,3);
//Serial2.println(U,3);
//Serial2.println(f,3);
iLCDLoop = millis();
}
}
void _F50sampleSA9904() // F50 impulse
{
CountSamples++;
if(CountSamples == nSamples)
{
sample = true;
CountSamples = 0;
}
}
void F50sampleSA9904()
{
byte rx[36] = {};
unsigned long reg1[4] = {};
readSA9904B(0, rx, 36);
reg1[2] = convertbutolong(rx, 6);
reg1[3] = convertbutolong(rx, 9);
U = float(reg1[2]);
f = float(reg1[3]);
}
void readSA9904B(byte startreg, byte *data, byte bytesreq)
{
spiSA9904.begin(SPI_281_250KHZ, MSBFIRST, SPI_MODE_0);
digitalWrite(BOARD_SPI2_NSS_PIN, 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(BOARD_SPI2_NSS_PIN, LOW);
spiSA9904.end();
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment