Tuesday, February 22, 2011

Ashley's Week 5 Report

This week: I didn't get as far as expected.  I have the read, write and initialize subroutines for the SPI communication.  I have a subroutine for implementing the SPI communication to the ADE7763 called readADE.  The SPI subroutines should work for the ethernet chip as well, I will simply just need to implement them into a readEthernet subroutine.  I don't think that my readADE is completely correct so I will try to get some help on this within the next day or two to make sure that it is ready to go for testing.  Below are my modified main program as well as the read, write and SPI initialization subroutines.  Andrew and I met briefly this morning to discuss our progress as well as when we wanted to get together to test the reading of data from the ADE.  I also told him the connections I would need him to make between the micro and the ADE so that my test program will work.

Next week: I will have the test program completed by the end of the week so that I can come in on either Friday or Tuesday to test that the micro can read in the temperature from the ADE7763.  I'll connect to the computer via RS232 and print the value to the secureCRT terminal.  

------------------------------------------------------

int main(void){

setBitPortB(2); //ADE7763 SS line on PB2; Set to high.
InitSPI();
char temp;

usart_init();
sei();

while(1){
char str[25];
char temp_reg = 0x26;

while(!uart_buffer_empty()){
char c = usart_getc();

if(c == 'p'){
temp = readADE();

usart_prints("\r");
usart_prints("The temperature is: ");
sprintf(str, "%d\n",temp);
usart_prints(str);
}
}
}
}

char readADE(void){
clearBitPortB(2); //pull PB2 low to start communication mode with ADE7763
_delay_ms(10);

char temp;

WriteByteSPI(0x26);
temp = ReadByteSPI(0x26);

setBitPortB(2);  //pull PB2 high to end communication mode

return temp;

}

void InitSPI(void)
{
DDRB = (1<<PB4)|(1<<PB5) | (1<<PB7); // Set MOSI , SCK , and SS output
   /* Enable SPI, Master, set clock rate fck/8 */
   SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
   SPSR = (1<<SPI2X);
}

void WriteByteSPI(unsigned char byte)
{

SPDR = byte; //Load byte to Data register
while(!(SPSR & (1<<SPIF))); // Wait for transmission complete

}

char ReadByteSPI(char addr)
{
SPDR = addr; //Load byte to Data register
while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
addr=SPDR;
return addr;
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home