Guest User

Untitled

a guest
Feb 24th, 2015
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4. #include <stdlib.h>
  5.  
  6. #ifndef F_CPU
  7. #warning "F_CPU war noch nicht definiert, wird nun nachgeholt mit 8000000"
  8. #define F_CPU 8000000UL  
  9. #endif
  10.  
  11. #define BAUD 9600UL
  12.  
  13. //Berechnungen
  14. #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)   // clever runden
  15. #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))     // Reale Baudrate
  16. #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler.
  17.  
  18. #if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
  19. #error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch!
  20. #endif
  21.  
  22.  
  23. #define DD_MOSI 5
  24. #define DD_MISO 6
  25. #define DD_SCK 7
  26. #define DDR_SPI DDRB
  27.  
  28. #define REG_SYNCVALUE1    0x2F
  29.  
  30. void init_UART(void)
  31. {
  32.     UBRRH = UBRR_VAL >> 8;
  33.     UBRRL = UBRR_VAL & 0xFF;
  34.  
  35.     UCSRB |= (1<<TXEN);                           // UART TX einschalten
  36.     UCSRC = (1<<URSEL)|(1 << UCSZ1)|(1 << UCSZ0); // Asynchron 8N1
  37. }
  38.  
  39. int uart_putc(unsigned char c)
  40. {
  41.     while (!(UCSRA & (1<<UDRE)))
  42.     {
  43.     }                            
  44.  
  45.     UDR = c;
  46.     return 0;
  47. }
  48.  
  49. void uart_puts (char *s)
  50. {
  51.     while (*s)
  52.     {  
  53.         uart_putc(*s);
  54.         s++;
  55.     }
  56. }
  57.  
  58. void uart_puts2 ( unsigned char s[] )
  59. {
  60.     int i = 0;
  61.     while ( s[i] != 0x00 )
  62.     {
  63.         uart_putc(s[i]);
  64.         i++;
  65.     }
  66. }
  67.  
  68. void SPI_MasterInit(void)
  69. {
  70.  
  71.     DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(0<<DD_MISO);
  72.     SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(0<<DORD)|(0<<CPOL)|(0<<CPHA);
  73.    
  74. }
  75.  
  76.  
  77.  
  78. uint8_t spi_transfer(uint8_t val)
  79. {
  80.  
  81.     SPDR = val;
  82.  
  83.     while(!(SPSR & (1<<SPIF)));
  84.    
  85.     return SPDR;
  86. }
  87.  
  88.  
  89. int main()
  90. {
  91.     uint8_t wr = 0x55;
  92.     char s[8];
  93.  
  94.     init_UART();
  95.     uart_puts("-----Begin------\r\n\r\n");
  96.  
  97.     SPI_MasterInit();
  98.  
  99.     DDRD |= (1<<PD7);
  100.     PORTD |= (1<<PD7); // SS
  101.  
  102.    
  103.  
  104.     while(1)
  105.     {
  106.         PORTD &= ~(1<<PD7);
  107.         spi_transfer(REG_SYNCVALUE1 | 0x80);
  108.         spi_transfer(wr);
  109.         PORTD |= (1<<PD7);
  110.    
  111.         PORTD &= ~(1<<PD7);
  112.         spi_transfer(REG_SYNCVALUE1 & 0x7F);
  113.         uint8_t resu = spi_transfer(0);
  114.         PORTD |= (1<<PD7);
  115.        
  116.         uart_puts( itoa(s,resu, 10) );
  117.    
  118.         _delay_ms(5000);
  119.     }
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment