Tuesday, February 15, 2011

Ashley's Week 4 Report

This Week:  I have done a bit more research on SPI communication in general.  I realized that I had  misunderstood how it functioned and thought it to be more similar to I2C communication in that you have to address the specific slave that you want to communicate with.  In my situation, and in most, the microcontroller is defined as the master, and the ethernet chip and the power chip are the two slaves.  In SPI, there are 4 connections between the master and the slave device.  Three of these connections are shared by the two slaves: the MISO, MOSI and SCK pins.  The fourth pin is the SS and differs between the two slaves.  Any I/O pin can be used as an SS, so I will probably be using PB1 and PB2 as my two SS connections.  The way that you determine which slave device you are communication with is to trigger a change in voltage on that slaves SS line.  The case for both of my slave devices is that a falling edge will start communication mode.  For example, the ADE7763's SS line will be on PB1.  PB1 will be pulled high as the default.  When I want to communicate with the ADE7763, I will pull PB1 low, write to the communication register that I want to read a value, then read the actual value, and pull it back high when I am finished to exit communication mode.  Below is sample code that I've started for this.  For testing, I will read the RMS Voltage.

Next Week:  I will finish the getRMSV() routine and see if there are any other initialization steps to be taken in the main program.  I hope to get together with Andrew to test the program by the time we have our 5 week meeting.


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

int main(void)
{

setBitPortB(1); //ADE7763 SS line on PB1; set to High
SPI_MasterInit();

uint8_t rmsV;

usart_init();
sei();

while(1)
{
char str[25];

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

if(c == 'p'){

clearBitPortB(0); //pull PB1 low to start communication mode with ADE7763
_delay_ms(10);
rmsV = getRMSV();
                setBitPortB(1); //exit communication mode

//printing to terminal
usart_prints("\r");
usart_prints("The RMS Voltage is: ");
sprintf(str,"%d\n",rmsV);
usart_prints(str);
}


}
}


}

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
void SPI_MasterInit(void)
{
   /* Set MOSI and SCK output, all others input */
   DDRB = (1<<PB2)|(1<<PB1);

   /* Enable SPI, Master, set clock rate fck/8 */
   SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
   SPSR = (1<<SPI2X);
}

uint8_t getRMSV()
{
uint8_t rmsV = 1;
//send write to communication register
//read rms volage

return rmsV;


}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home