ATMega16 SPI MCP41010

OP #2454085
Lesenswert?

Ich habe ein Porblem mit der SPI Kommunikation!!!

ATMega16 -> MCP41010
MOSI -> SI
SCK -> SCK
SS -> CS(inv.)

Mein Code:

#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

#include "uart.h"
#include "ADC.h"


/* define CPU frequency in Mhz here if not defined in Makefile */
#ifndef F_CPU
#define F_CPU 16000000UL
#endif

/* 9600 baud */
#define UART_BAUD_RATE  9600

void adc(char mux)
{
  int ad = ADC_Read(mux);
  char buffer[10];
  itoa(ad,buffer,10);
  uart_puts(buffer);
  uart_puts("\n\r");
  return;
}

void spi_init(void)
{
  SPCR|=(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //SPI enable, Master, 
Freq/128;
}

void spi_write(char data)
{
  PORTB |= (1<<PB4); //CS high
  SPDR = 0x11; // 11 für write und pot1
  SPDR = data;
  PORTB &= ~(1<<PB4); //CS low
}

int main(void)
{
  DDRD &=~((1<<PD2)|(1<<PD3)|(1<<PD4));
  DDRA = 0x00;
  DDRD|=(1<<PD7)|(1<<PD6)|(1<<PD5);
  DDRB |= (1<<PB5)|(1<<PB7); //MOSI u SCK als OUTPUT
  DDRB |= (1<<PB4); // SS als OUTPUT
    uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU));
  ADC_Init();
  spi_init();
    sei();
  while (1)
  {
    int read = uart_getc();
    if (read==48) adc(0);
    if (read==49) adc(1);
    if (read==50) adc(2);
    if (read==51) adc(3);
    if (read==52) adc(4);
    if (read==53) adc(5);
    if (read==54) adc(6);
    if (read==55) adc(7);
    if (PIND &(1<<PD2)) spi_write(0xFF); //255
    else spi_write(0x50); // 80
    _delay_ms(1000);
  }
}


Kann mir jemand sagen was ich falsch mache??? Der wert des potis 
verändert sich nicht!!!

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren