+ Post New Thread
Results 1 to 12 of 12
  1. #1
    Newbie level 5
    Points: 670, Level: 5

    Join Date
    Jun 2009
    Posts
    10
    Helped
    1 / 1
    Points
    670
    Level
    5

    C code for ATmega 16 SPI communication

    Can anyone help me with SPI ?

    •   Alt5th June 2009, 11:43

      advertising

        
       

  2. #2
    Full Member level 5
    Points: 3,032, Level: 12

    Join Date
    Feb 2008
    Location
    India
    Posts
    251
    Helped
    39 / 39
    Points
    3,032
    Level
    12

    atmega16 spi

    what is the problem?
    just tell it lot of people will help you


    1 members found this post helpful.

  3. #3
    Newbie level 5
    Points: 670, Level: 5

    Join Date
    Jun 2009
    Posts
    10
    Helped
    1 / 1
    Points
    670
    Level
    5

    atmega16 spi example

    I want an example of program... I want to comunicate with an Convertor Sigma Delta



  4. #4
    Full Member level 5
    Points: 3,032, Level: 12

    Join Date
    Feb 2008
    Location
    India
    Posts
    251
    Helped
    39 / 39
    Points
    3,032
    Level
    12

    atmega spi tutorial

    here is the program for SPI
    I used this code for all of my programs


    Code:
    #include <avr/io.h>
    
    
    void InitSPI(void)
    {
    DDRB = (1<<PB4)|(1<<PB5) | (1<<PB7);	 // Set MOSI , SCK , and SS output
    SPCR = ( (1<<SPE)|(1<<MSTR) | (1<<SPR1) |(1<<SPR0));	// Enable SPI, Master, set clock rate fck/128  
    }
    
    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;
    }


    3 members found this post helpful.

  5. #5
    Newbie level 5
    Points: 670, Level: 5

    Join Date
    Jun 2009
    Posts
    10
    Helped
    1 / 1
    Points
    670
    Level
    5

    atmega spdr

    ups... i do a mistake.. you can send me again ?

    Added after 2 minutes:

    i need to use miso mosi and sck to comunicate with it...



    •   Alt5th June 2009, 12:02

      advertising

        
       

  6. #6
    Full Member level 5
    Points: 3,032, Level: 12

    Join Date
    Feb 2008
    Location
    India
    Posts
    251
    Helped
    39 / 39
    Points
    3,032
    Level
    12

    atmega 16 spi

    just copy the code and save it as a c file
    or you can directly include this code in your program

    first initialize the SPI and then use the read or write functions to access the content of the SPI



  7. #7
    Newbie level 5
    Points: 670, Level: 5

    Join Date
    Jun 2009
    Posts
    10
    Helped
    1 / 1
    Points
    670
    Level
    5

    atmega spi code

    oki.. thank you...



  8. #8
    Junior Member level 3
    Points: 1,292, Level: 8

    Join Date
    May 2009
    Location
    PAk
    Posts
    30
    Helped
    0 / 0
    Points
    1,292
    Level
    8

    atmega8 spi example

    Can anyone help me to interface spi sensor with pic OR at89c51 controller
    ?



  9. #9
    Newbie level 2
    Points: 342, Level: 3

    Join Date
    Jan 2011
    Posts
    2
    Helped
    0 / 0
    Points
    342
    Level
    3

    Re: C code for ATmega 16 SPI communication

    Hello

    I am having a problem with SPI communication between an Atmega32 and a ad7705(SPI). It works on one atmega32 chip that was produced in 2006 but it does not work on a handful of other atmega32 and atmega32a chips all produced in 2010.

    I have tried various init parameters including reducing the clock rate to fck/128 all with the same result. I tried writing a small standalone program to test just the SPI function in the hope that there was some unintended effect from my main application.

    The problem occurs when I wait for transmission:
    while(!(SPSR & (1<<SPIF)));
    This will loop forever. In my case i catch an error after 100000 iterations.

    I also noticed that the problem occurs with no SPI devices attached after sending init commands. I would expect this. There must be something that is not correct with my code or fuses on these newer chips but i have no clue what it could be.

    I am setting the PB5 and PB7 as output as well as by SS line. I am setting SPE and MSTR. I have tried various SPCR settings, all work on the 06 chip but not on the 2010 chips.

    Does anyone have any thoughts on what i can try?

    Thanks
    Jon



  10. #10
    Newbie level 2
    Points: 342, Level: 3

    Join Date
    Jan 2011
    Posts
    2
    Helped
    0 / 0
    Points
    342
    Level
    3

    Re: C code for ATmega 16 SPI communication

    I found the problem I posted today. I am driving the chip select on two SPI devices with IO lines and not using the SS PB4 line. I was able to write one byte to the SPI but the second write operation would never complete. I had to set the unused SS PB4 to out. Now it works. Funny how it worked on the chip from 06 though...



  11. #11
    Newbie level 2
    Points: 331, Level: 3

    Join Date
    Feb 2011
    Posts
    2
    Helped
    0 / 0
    Points
    331
    Level
    3

    Re: C code for ATmega 16 SPI communication

    I am looking for some sample code for SPI communication between the ATMega328P (Master) and the ADE7763 (slave). I want my clock frequency to be fck/8. I want pin PB2 on the micro to be the designated SS connection to the ADE7763. For a test program, could somebody tell me how to read the temperature from the ADE7763. The temperature is on register 0x26 if I remember correctly from the datasheet.

    Using the subroutines given above by sau_sol, how might I go about making a main program to do this?

    Thanks!!



  12. #12
    Newbie level 5
    Points: 536, Level: 4

    Join Date
    Jul 2011
    Posts
    8
    Helped
    0 / 0
    Points
    536
    Level
    4

    Re: C code for ATmega 32 with ad7705 SPI communication

    #include "avr/io.h"
    #include <avr/interrupt.h>
    #include "stdio.h"
    #define F_CPU 11059200UL
    #include <util/delay.h>

    volatile double AdcOutput;
    volatile unsigned char Temp,Temp1,Temp2;
    volatile unsigned int k;

    /************************************************
    SPI INITIALISATION
    *************************************************/

    void SPIinit(void)
    {
    DDRB |= (1<<PB4)|(1<<PB5) | (1<<PB7); // Set MOSI , SCK , and SS output
    SPCR |= ((1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0)); // Enable SPI, Master, set clock rate fck/128

    }

    /************************************************
    Writetoreg
    *************************************************/

    void Writetoreg( int byteword)
    {
    SPDR = byteword; // put the byteword into data register
    while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
    byteword=SPDR; //just for clearing spif
    }


    /************************************************
    Read
    *************************************************/

    unsigned int Read(char reglength)
    {
    int b;
    for(b=0;b<reglength;b++)
    {
    SPDR = 0xFF;
    while(!(SPSR &(1<<SPIF))); // Wait for transmission complete
    if(b==0)
    {
    k=SPDR*256;
    }
    else
    k=k+SPDR;
    }

    return k;
    }

    /************************************************
    ADC INITIALISATION
    *************************************************/

    void ADCinit(void)
    {
    SPIinit();
    Writetoreg(0x20); //Active channel is Ain(+) Ain(-), next operation as write to clock register
    Writetoreg(0x0B); //(for clock register) master clock enabled, 2MHz Clock, set output rate to 20Hz*///0x08
    Writetoreg(0x10); //Active channel is Ain1(+) Ain1(-), next operation as write to the setup register
    Writetoreg(0x44); //(for setup register) gain = 1, bipolar mode, buffer off, clear FSYNC and perform a Self Calibration*/
    }

    /************************************************
    Lcd out
    *************************************************/

    void lcdout(unsigned char Temp)
    {
    PORTA &= 0b11101111; // LOW TO PA4 'E' PIN

    Temp1 = Temp; // EXAMPLE VALUE 25
    Temp2 = Temp1;
    Temp2 = Temp2 >>4; // ,, 02

    Temp2 = Temp2 & 0x0F; // 1011 + 2

    Temp = (PORTA);
    Temp = Temp & 0xF0;
    Temp = Temp | Temp2; // 1011 + 2
    PORTA = Temp;


    PORTA |= 0b00010000;
    PORTA &= 0b11101111;


    Temp = Temp1; // 25
    Temp2 = Temp;
    Temp2 = Temp2 & 0x0F; // 05
    Temp = (PORTA); // 1011+5
    Temp = Temp & 0xF0; // 1011+5
    Temp = Temp | Temp2;
    PORTA = Temp;
    PORTA |= 0b00010000;
    PORTA &= 0b11101111;
    _delay_us (50);
    }
    /************************************************
    lcd initialisation
    *************************************************/

    void LcdInit(void)
    {
    DDRA = 0xFF;
    PORTA &= 0b11011111;
    _delay_ms (20);
    lcdout(0x38);
    _delay_ms (05);
    lcdout(0x38);
    _delay_ms (100);
    lcdout(0x38);
    _delay_ms (02);
    lcdout(0x28);
    _delay_ms (02);
    lcdout(0x08);
    _delay_ms (02);
    lcdout(0x01);
    _delay_ms (02);
    lcdout(0x06);
    _delay_ms (02);
    lcdout(0x0F);
    _delay_ms (02);
    lcdout(0x0C);
    _delay_ms (02);
    }


    void calculate(volatile double data)
    {
    volatile double data2;

    data = data * 2.5 /65535;

    Temp = data;
    lcdout(Temp|0x30);
    lcdout('.');

    Temp = data*100;
    Temp =(Temp/10)%10;
    lcdout(Temp|0x30);

    Temp = data*100;
    Temp =(Temp%10);
    lcdout(Temp|0x30);

    data2 = data*1000;
    Temp =((int)data2%10);
    lcdout(Temp|0x30);

    data = data*10000;
    Temp =((int)data%10);
    lcdout(Temp|0x30);

    }
    void display_number(volatile long num)
    {
    int num3;

    num3=num/10000;
    lcdout(num3|0x30);
    num =num%10000;

    num3 =num/1000;
    lcdout(num3|0x30);
    num =(int)num%1000;

    num3 =num/100;
    lcdout(num3|0x30);
    num =(int)num%100;

    num3 =num/10;
    lcdout(num3|0x30);
    num3 =(int)num%10;

    lcdout(num3|0x30);
    }

    /************************************************
    Main loop
    *************************************************/

    int main()
    {
    DDRA = 0xFF; //Config DDRA as O/P for LCD display

    DDRC = 0x40; //Config PC6 o/p for laser
    PORTC |= 0x40;

    LcdInit(); //lcd initialization
    ADCinit(); //adc initialization

    for(;;)
    {
    do
    {
    Writetoreg(0x08);
    Read(1);
    }
    while(k&0x80);
    // read from adc
    Writetoreg(0x38); //set the next operation for 16 bit read from the data register
    AdcOutput = Read(2);
    _delay_ms (1000);
    PORTA &= 0b11011111;
    lcdout(0x0C5);
    PORTA |= 0b00100000;
    calculate(AdcOutput);
    }
    return 0;
    }


    try this, good luck



+ Post New Thread
Please login

LinkBacks (?)