SPI与Atmega32和At42QT2100

时间:2015-06-18 12:57:35

标签: c avr spi atmel

从Atmega3216PU到at42qt2100进行通信时遇到问题。

数据表是:Atmega32PU16AT42QT2100

这是我的代码:

#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#define F_CPU  16000000
#define UART_BAUD_RATE  9600
#define UART_BAUD_REGISTERS  (((F_CPU / (UART_BAUD_RATE * 16UL))) - 1)

int printCHAR(char character, FILE *stream)
{
    while ((UCSRA & (1 << UDRE)) == 0) {};
    UDR = character;
    return 0;
}
FILE uart_str = FDEV_SETUP_STREAM(printCHAR, NULL, _FDEV_SETUP_RW);

void setup(){
    // Init SIO
    UCSRB |= (1 << RXEN) | (1 << TXEN);
    UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);

    UBRRL = UART_BAUD_REGISTERS;

    fprintf(&uart_str, "");
    stdout = &uart_str;
    printf("");

    //Init spi
    //set MOSI, SCK and SS output, all others input
    DDRB |= (1<<DDB7)|(1<<DDB5)|(1<<DDB4);
    SPCR |= (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0)|(1<<CPHA)|(1<<CPOL);

}

void loop(){

    uint8_t data = 0b01000000; //Sending this sequence should return device id
    printf("Sending: %d \n",data);
    _delay_ms(10000);
    PORTB &= 0b11101111; // SS auf LOW!! (Pin4)
    SPDR = data;
    while(!(SPSR & (1<<SPIF)));
    PORTB |= (1<<PB4); // SS auf HIGH!! (Pin4)
    printf("Receiving: %d \n",SPDR);
    _delay_ms(10000);
}

void main(){
    setup();
    while(1){
        loop();
    }
}

我应该从at42qt2100获得设备ID 108的答案,但我总是得到0.这是输出:发送:64接收:0

有人看到配置错误吗?

1 个答案:

答案 0 :(得分:0)

我快速概述了您参考的AT42QT2100数据表,我立即看到了一些问题:

  1. 在SS设置为低电平后,主机必须等待> 2us(在自由运行模式下)才能启动SCK。您正在立即启动SPI传输,如果您运行的是16MHz,则意味着在将SS设置为低电平后,您可能会在62.5ns后立即启动传输。
  2. 数据表还说主机必须在10ms内发送3个数据字节,否则事务将被视为错误,通信交换将被重置。您发送1个字节,然后在发送下一个字节之前等待20秒。
  3. 数据表显示设备设置在发送3个字节后变为活动。这意味着必须再发送3个字节才能获得对命令的响应。
  4. 你应该做更多的事情:

    #define NUM_DATA_BYTES   3
    void loop(){
        uint8_t sendData[NUM_DATA_BYTES] =
        {
            0b01000000, /* Device Version Command and default values. */
            0b00000000, /* Default Value for Byte 2. */
            0b11000000, /* Default Value for Byte 3. */
        };
        uint8_t receiveData[NUM_DATA_BYTES] = { 0 };
        uint8_t i;
    
        PORTB &= 0b11101111; // SS auf LOW!! (Pin4)
    
         /* Ensure that >2us delay requirement is met.  Although, the 
            for() loop below will probably introduce enough delay. */
        _delay_ms(1);
    
        /* Send command data. */
        for (i = 0; i < NUM_DATA_BYTES; i++)
        {
                SPDR = sendData[i];
                while(!(SPSR & (1<<SPIF)));
        }
        PORTB |= (1<<PB4); // SS auf HIGH!! (Pin4)
    
        /* Delay here may be unnecessary, but it ensures that timing
           issues do not occur. */
        _delay_ms(1);
        PORTB &= 0b11101111; // SS auf LOW!! (Pin4)
        _delay_ms(1);
    
        /* Receive device response data. */
        for (i = 0; i < NUM_DATA_BYTES; i++)
        {
                SPDR = sendData[i];
                while(!(SPSR & (1<<SPIF)));
                receiveData[i] = SPDR;
    
                printf("Receiving byte %d: %d \n", (i + 1), receiveData[i]);
        }
        PORTB |= (1<<PB4); // SS auf HIGH!! (Pin4)
    
        _delay_ms(10000);
    }
    

    我确信这段代码并不完美,但它应该有助于让您走上正轨。祝你好运!