Ashley's Week 7 Report
This Week: I tried to run my code from last week once we installed the oscillator but I was still getting "0" readings for the temperature. I messed around with it for a little bit Thursday but wasn't having any luck. Today (Tuesday), Andrew and I met up to work on reading the temperature some more and also tried to read the RMS voltage. We weren't getting the SPI communication to function correctly, so with the help of Cliff, we were able to monitor the clock cycles and the data passing across the 2 data lines between the ADE7763 and the ATMega328. After lots of trial and error and eventually cross checking between the two datasheet to see how exactly data needed to be read, we were successful in reading values. The temperature was reading in at 15 degrees Celsius (which is low, but the datasheet states that there can be up to a +/- 25 degree Celsius offset before calibration). When placing our finger on the chip, the temperature raised up to 20 or so and went back down when removing the finger. Once we saw that we were reading correctly, we tried to read the RMS Voltage register. Our numbers our fluctuating a bit and not sure what needs to be done to make them be correct, but we were also simply reading the value of the register without doing any initial set ups or calculations that may need to be done when reading RMS Voltage. Below is a screen shot of some of the readings we got from the RMS Voltage register.
Next Week: Andrew and I will to talk to Kevin about what type of power values he is needing to read in so that we can figure out what registers to read and calculations that might need to be done in order to provide him with those values. For now, we are going to use the Active Energy and Apparent Energy registers until we hear or decide differently. I will take my current code, start a new program and implement that code into it so that my main function displays values from both of these registers. This will involve tailoring my current subroutines for reading RMS voltage to the other two registers. I will also look over the RMS Voltage reading info so Andrew and I can hopefully get correct values reading with that before the next report.
-------------------------------------------------------------
ADE7763 RMS VOLTAGE CODE
-------------------------------------------------
int main(void){
setBitPortB(2); //ADE7763 SS line on PB2; Set to high.
InitSPI();
uint32_t temp;
usart_init();
sei();
while(1){
char str[25];
while(!uart_buffer_empty()){
char c = usart_getc();
if(c == 'p'){
//SPI_Write(0x09, 0x2C);
temp = readRMS(); //SPI_Read_Temp(0x26);
//the SPI_Write and SPI_Read_Temp functions can be substituted in instead of the readRMS if you want to read temperature.
usart_prints("\r");
usart_prints("The RMS Voltage is: ");
sprintf(str, "%ld\n",temp);
usart_prints(str);
//usart_putc(temp);
}
}
}
}
void setBitPortB(int bit) //sets bit to 1
{
PORTB = PINB | (1<<bit);
}
void clearBitPortB(int bit) //sets bit to 0
{
PORTB = PINB & (~(1<<bit));
}
//SPI STUFF
uint32_t readRMS(void){
//SPI_Write(0x09, 0x2C);
uint32_t temp = SPI_Read(0x17);
return temp;
}
void InitSPI(void)
{
DDRB = (1<<PB2)|(1<<PB3) | (1<<PB5); // Set MOSI (PB3) , SCK (PB5), and SS(PB2) output
/* Enable SPI, Master, set clock rate fck/8 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<CPHA);
SPSR = (1<<SPI2X);
//idle clock is low. want CPOL = 0, CPHA = 1;
}
void SPI_Write(unsigned int addr,unsigned char data)
{
// Activate the CS pin
clearBitPortB(2); //SPI_PORT &= ~(1<<SPI_CS);
unsigned int communication = ADE7763_WRITE_OPCODE | addr;
// Start ADE7763 Communication Write transmission
SPDR = communication; //ADE7763_WRITE_OPCODE;
// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));
// Start Data High Bytes transmission
SPDR = (data & 0xFF00) >> 8;
// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));
// Start Data Low Bytes transmission
SPDR = data & 0x00FF;
// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));
// CS pin is not active
setBitPortB(2);//SPI_PORT |= (1<<SPI_CS);
}
uint32_t SPI_Read(unsigned int addr)
{
// Activate the CS pin
clearBitPortB(2); //SPI_PORT &= ~(1<<SPI_CS);
unsigned int communication = ADE7763_READ_OPCODE | addr;
//unsigned char data;
// Start ADE7763 Communication Read transmission
SPDR = communication; //ADE7763_READ_OPCODE;
// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));
_delay_us(4);
SPDR = 0b00000000;
while(!(SPSR & (1<<SPIF)));
uint32_t v1 = SPDR;
_delay_us(1);
SPDR = 0b00000000;
while(!(SPSR & (1<<SPIF)));
uint32_t v2 = SPDR;
_delay_us(1);
SPDR = 0b00000000;
while(!(SPSR & (1<<SPIF)));
uint32_t v3 = SPDR;
_delay_us(1);
setBitPortB(2);//SPI_PORT |= (1<<SPI_CS);
uint32_t voltage = (16<<v1)|(8<<v2)|v3;
return voltage;
}
int SPI_Read_Temp(unsigned int addr)
{
// Activate the CS pin
clearBitPortB(2); //SPI_PORT &= ~(1<<SPI_CS);
unsigned int communication = ADE7763_READ_OPCODE | addr;
//unsigned char data;
// Start ADE7763 Communication Read transmission
SPDR = communication; //ADE7763_READ_OPCODE;
// Wait for transmission complete
while(!(SPSR & (1<<SPIF)));
_delay_us(4);
SPDR = 0b00000000;
while(!(SPSR & (1<<SPIF)));
int temp = SPDR;
_delay_us(1);
setBitPortB(2);//SPI_PORT |= (1<<SPI_CS);
int thi = (temp>>4)&0b0000000000001111;
int tlo = (temp&0b0000000000001111);
int temperature = thi*10 + tlo;
return temperature;
}
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home