Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
aliveli319
PostPosted: Jun 04, 2008 - 02:43 PM
Newbie


Joined: May 18, 2008
Posts: 14
Location: Germany

Hello,

I want to read a sensor value with EVK1100 by using I2C bus. Does some one know how to do it?

Actually, I tried to read that value by modifing TWI master example in the AVR32 Studio examples, but I couldn't.

Can anybody give me some hints about it!

Thanks!!
 
 View user's profile Send private message  
Reply with quote Back to top
aliveli319
PostPosted: Jun 06, 2008 - 03:01 PM
Newbie


Joined: May 18, 2008
Posts: 14
Location: Germany

No answer again Sad
Anyway I send my little program which finds device addresses connected to EVK1100 via I2C interface. May be it helps beginners like me in the future...

Regards!

Code:

#include <avr32/io.h>
#include "board.h"
#include "gpio.h"
#include "pm.h"
#include "intc.h"
#include "twi.h"
#include <stdio.h>
#include "dip204.h"
#include "spi.h"
#include "delay.h"

#define TWI_SPEED             100000      // Speed of TWI

int main(void)
{
   //SPI CONFIGURATIONS FOR LCD DISPLAY
      static const gpio_map_t DIP204_SPI_GPIO_MAP =
        {
          {DIP204_SPI_SCK_PIN,  DIP204_SPI_SCK_FUNCTION },  // SPI Clock.
          {DIP204_SPI_MISO_PIN, DIP204_SPI_MISO_FUNCTION},  // MISO.
          {DIP204_SPI_MOSI_PIN, DIP204_SPI_MOSI_FUNCTION},  // MOSI.
          {DIP204_SPI_NPCS_PIN, DIP204_SPI_NPCS_FUNCTION}   // Chip Select NPCS.
        };
        // Switch the CPU main clock to oscillator 0
        pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);

        // add the spi options driver structure for the LCD DIP204
        spi_options_t spiOptions =
        {
          .reg          = DIP204_SPI_NPCS,
          .baudrate     = 1000000,
          .bits         = 8,
          .spck_delay   = 0,
          .trans_delay  = 0,
          .stay_act     = 1,
          .spi_mode     = 0,
          .modfdis      = 1
        };

        // Assign I/Os to SPI
        gpio_enable_module(DIP204_SPI_GPIO_MAP,
                           sizeof(DIP204_SPI_GPIO_MAP) / sizeof(DIP204_SPI_GPIO_MAP[0]));
        // Initialize as master
        spi_initMaster(DIP204_SPI, &spiOptions);
        // Set selection mode: variable_ps, pcs_decode, delay
        spi_selectionMode(DIP204_SPI, 0, 0, 0);
        // Enable SPI
        spi_enable(DIP204_SPI);
        // setup chip registers
        spi_setupChipReg(DIP204_SPI, &spiOptions, FOSC0);
        // initialize delay driver
        delay_init( FOSC0 );
        // initialize LCD
        dip204_init(backlight_PWM, TRUE);
      
        dip204_hide_cursor();
       
       // END OF THE SPI CONFIGURATIONS

       
  static const gpio_map_t TWI_GPIO_MAP =
  {
    {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION},
    {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION}
  };
  twi_options_t opt;
 
  int status,i;

  // TWI gpio pins configuration
  gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0]));

  int SENSOR_ADDRESS=0x00,k=0x01,j=0;   
  int all_addresses[127];
  int n;
  for (n=0;n<128;n++)
     all_addresses[n]=0;
  // options settings
     opt.pba_hz = FOSC0;
     opt.speed = TWI_SPEED;
//search loop
  for (i=0;i<256;i++){
      SENSOR_ADDRESS=SENSOR_ADDRESS+k;
      // options settings
      opt.chip = SENSOR_ADDRESS;
    
      char c[8];
      sprintf(c, "%d",SENSOR_ADDRESS);
      dip204_set_cursor_position(1,4);
      dip204_write_string(c);
      // initialize TWI driver with options
        status = twi_master_init(&AVR32_TWI, &opt); //MASTER INIT
        if (status == TWI_SUCCESS)
        {
          // display test result to user
           dip204_set_cursor_position(1,1);
           dip204_write_string("Init:PASSED");
           dip204_set_cursor_position(1,2);
           char c[10];
           sprintf(c, "%d",SENSOR_ADDRESS);
           dip204_write_string(c);
           all_addresses[j]=SENSOR_ADDRESS;
           j++;
                     }
        else
        {
          // display test result to user
           dip204_set_cursor_position(1,1);
          dip204_write_string("Init:FAILED");
        }
  } //for
  if(j>0){
     dip204_clear_display();
        dip204_set_cursor_position(1,1);
        dip204_write_string("Init:PASSED");
 
  int m;
  dip204_set_cursor_position(1,3);
  for(m=0; m<j; m++){
     char d[10];
     sprintf(d, "%d",all_addresses[m]);
     dip204_write_string(d);
     dip204_write_string(" ");
  }//for
  }//if
       while(1);
      
 
} //main
 
 View user's profile Send private message  
Reply with quote Back to top
sma
PostPosted: Jun 06, 2008 - 09:08 PM
Posting Freak


Joined: Jan 14, 2007
Posts: 1342
Location: France

what is the failure?
Did you scope the I2C signals and see anything wrong?
Did you your sensor I2C address?

-sma
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits