Tuesday, March 1, 2011

Ashley's Week 6 Report

This week:  The first thing I did was tweek my code a bit from the previous post.  Andrew, Kevin and I met up for about 5 hours Saturday afternoon to work on our 3 components of the lab.  First Andrew and I spent quite a bit of time trying to get our microcontroller to connect to AVR Studio.  Once we accomplished that, we loaded my temperature test code and tried to connect to secureCRT so that we could see the output.  We were having issues with our USART communication and after a lot of trial and error, realized that we needed to change the clock divisions under the fuses.  At this point we were able to read a value and display it in the terminal.  The value was "6" and we knew this was not right as it did not change when applying heat to the chip.  I realized that I was not doing all that needed to be done in the code to perform the SPI communication.  Andrew also realized that he needed to add an oscillator to the chip.. this could play a large role in our problem as well because it would control the clock cycles.  We decided we would work on that for the next meeting.  Since I had the USART communication working, Kevin changed his program so that he could accept values from me (an outside source as opposed to the random generator he was using on his computer).  We were thrilled to see that the "6" supposedly being read in from the ADE7763 was transfered to Kevin's program and storing in his database.  We also loaded his graph and this new data was being updated to it.  We decided to hardcode a "7" and send it from the microcontroller to his program in order to see fluctuation in data on his graph.  It was comforting to know that we at least had this sort of communication down in the event that we couldn't get the ethernet portion functioning later on.
Monday, I spent a lot of time searching for sample code or libraries for the ethernet portion.  One of the example programs included the Read and Write subroutines that I have listed below.  The ethernet transfers 32bits during communication.  The first byte determines read or write.  The second two bytes are the address and the fourth byte is the data byte.  In the case of reading, the data byte is a dummy variable 0x00.    Today, I rewrote my ADE spi read and write subroutines using some of the logic that the ethernet code showed.  You can see these updated subroutines below.  From my understanding, 24bits are transfered during communication.  The first byte is written to the communication register to determine reading or writing and the address of the register.  The 2nd and 3rd bytes are data bytes.  I'm still trying to figure out the read portion, so there is a segment of that subroutine commented out because I'm not sure if I need it or not yet or if it needs to be changed.  I tried it with and without that portion today, but am reading "0" for the temperature.  We haven't added the oscillator yet so that may be the problem.  


Next week:  We will install the oscillator in the next couple of days so that I can try my updated code and see what needs to be changed on my part to read the temperature successfully.  At that point, I will write a subroutine to read the register that we'll need for our power measurement.  If time allows, I'll work on an initialization routine for the ethernet and continue to search the internet for more information on it.


-------------------------------------------------------------
ADE7763 SPI CODE
-------------------------------------------------------------



#define SPI_PORT PORTB
#define SPI_DDR DDRB
#define SPI_CS PORTB2


#define ADE7763_WRITE_OPCODE 0b10000000
#define ADE7763_READ_OPCODE 0b00000000




unsigned char SPI_Read(unsigned int addr);
void SPI_Write(unsigned int addr,unsigned char data);


void InitSPI(void);
char readTemp(void);


int main(void){


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


  usart_init();
  sei();


  while(1){
   char str[25];
  
   while(!uart_buffer_empty()){
   char c = usart_getc();
//usart_putc(c);


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


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


//usart_putc(temp);


   }
   }
}
}




//SPI STUFF
char readTemp(void){


SPI_Write(0x09, 0x2C);
unsigned char temp = SPI_Read(0x26);
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);
   SPSR = (1<<SPI2X);
}


void SPI_Write(unsigned int addr,unsigned char data)
{
  // Activate the CS pin
  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
  SPI_PORT |= (1<<SPI_CS);
}
unsigned char SPI_Read(unsigned int addr)
{
  // Activate the CS pin
  SPI_PORT &= ~(1<<SPI_CS);


  unsigned int communication = ADE7763_READ_OPCODE | addr;


  // Start ADE7763 Communication Read transmission
  SPDR = communication; //ADE7763_READ_OPCODE;


  // Wait for transmission complete
  while(!(SPSR & (1<<SPIF)));
  
/* unsigned char data= 0x0000;
  // 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
  SPI_PORT |= (1<<SPI_CS);
  */


  return(SPDR);
}







-------------------------------------------------------------
ETHERNET SPI CODE
-------------------------------------------------------------

#define SPI_PORT PORTB
#define SPI_DDR  DDRB
#define SPI_CS   PORTB1

#define WIZNET_WRITE_OPCODE 0xF0
#define WIZNET_READ_OPCODE 0x0F


void SPI_Write(unsigned int addr,unsigned char data)
{
  // Activate the CS pin
  SPI_PORT &= ~(1<<SPI_CS);

  // Start Wiznet W5100 Write OpCode transmission
  SPDR = WIZNET_WRITE_OPCODE;

  // Wait for transmission complete
  while(!(SPSR & (1<<SPIF)));

  // Start Wiznet W5100 Address High Bytes transmission
  SPDR = (addr & 0xFF00) >> 8;

  // Wait for transmission complete
  while(!(SPSR & (1<<SPIF)));

  // Start Wiznet W5100 Address Low Bytes transmission
  SPDR = addr & 0x00FF;

  // Wait for transmission complete
  while(!(SPSR & (1<<SPIF)));   

  // Start Data transmission
  SPDR = data;

  // Wait for transmission complete
  while(!(SPSR & (1<<SPIF)));

  // CS pin is not active
  SPI_PORT |= (1<<SPI_CS);
}
unsigned char SPI_Read(unsigned int addr)
{
  // Activate the CS pin
  SPI_PORT &= ~(1<<SPI_CS);

  // Start Wiznet W5100 Read OpCode transmission
  SPDR = WIZNET_READ_OPCODE;

  // Wait for transmission complete
  while(!(SPSR & (1<<SPIF)));

  // Start Wiznet W5100 Address High Bytes transmission
  SPDR = (addr & 0xFF00) >> 8;

  // Wait for transmission complete
  while(!(SPSR & (1<<SPIF)));

  // Start Wiznet W5100 Address Low Bytes transmission
  SPDR = addr & 0x00FF;

  // Wait for transmission complete
  while(!(SPSR & (1<<SPIF)));   

  // Send Dummy transmission for reading the data
  SPDR = 0x00;

  // Wait for transmission complete
  while(!(SPSR & (1<<SPIF)));  

  // CS pin is not active
  SPI_PORT |= (1<<SPI_CS);

  return(SPDR);
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home