Facebook
From ja, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 251
  1. #include <avr/io.h>
  2. #include <avr/pgmspace.h>
  3. #include <avr/eeprom.h>
  4. #include <avr/delay.h>
  5. #include <stdbool.h>
  6.  
  7. #include "lcdrunn.h"
  8.  
  9. #define MOSI PB5
  10. #define MISO PB6
  11. #define SCK PB7
  12. #define CS PB4
  13. #define PORT_SPI  PORTB
  14. #define CS_HIGH PORTA |= (1 << CS);
  15. #define CS_LOW PORTA &= ~(1 << CS)
  16.  
  17. #define EEMEM __attribute__((section(".eeprom")))
  18.  
  19.  
  20.  void InitSpi()
  21.    {
  22.        DDRB |= (1 << MOSI) | (1 << SCK) | (1 << CS) ;
  23.        // Define the following pins as output
  24.        //DDRA |= (1 << CS);
  25.  
  26.        SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(SPIE);
  27.        SPSR |= (1 << SPI2X);
  28.  
  29.    }
  30.  
  31. //wysyłanie do adc
  32. void SendSpi(uint8_t bajt)
  33. {
  34.         SPDR = bajt;
  35.         while( !(SPSR & (1 << SPIF)) );
  36.  
  37. }
  38.  
  39. //odbiór z adc
  40. uint8_t ReceiveSpi( void )
  41. {
  42.     while( !(SPSR & (1 << SPIF)) );
  43.         return SPDR;
  44. }
  45.  
  46. int main(void)
  47. {
  48.     lcd_init(); //inicjalizacja wyswietlacza
  49.     InitSpi();
  50.     uint8_t send[3] = {0x0C, 0x00, 0x00};
  51.     CS_HIGH;
  52.         while(1)
  53.         {
  54.             lcd_cls();
  55.  
  56.         uint16_t sum = 978;
  57.         uint8_t wyn1 = 12;
  58.         uint8_t wyn2 = 21;
  59.  
  60.         CS_LOW;
  61.         SendSpi(send[0]);
  62.         ReceiveSpi();
  63.         SendSpi(send[1]);
  64.         wyn1 = ReceiveSpi();
  65.         wyn1 = wyn1 & 0b00111111; // albo 0b00011..
  66.         SendSpi(send[2]);
  67.         wyn2 = ReceiveSpi();
  68.         CS_HIGH;
  69.  
  70.         _delay_us(20);
  71.         sum = wyn1;
  72.         sum = sum << 8;
  73.         sum |= wyn2;
  74.  
  75.         lcd_int(sum);
  76.         _delay_ms(1000);
  77.         }
  78.  
  79. }
  80.  
  81.