Facebook
From Sharp Wolf, 6 Years ago, written in C.
This paste is a reply to adc from Gamboge Ostrich - view diff
Embed
Download Paste or View Raw
Hits: 430
  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 PA0
  13. #define CSS PA1
  14. #define PORT_SPI  PORTA
  15. #define CS_HIGH PORTA |= (1 << CS);
  16. #define CS_LOW PORTA &= ~(1 << CS)
  17. #define CS_HIGHH PORTA |= (1 << CSS);
  18. #define CS_LOWW PORTA &= ~(1 << CSS);
  19. #define EEMEM __attribute__((section(".eeprom")))
  20.  
  21.  
  22. void InitSpi();
  23. void SendSpi(uint8_t bajt);
  24. uint8_t ReceiveSpi( void );
  25. uint8_t SendAndReceive(uint8_t data);
  26. uint16_t MCP3304(uint8_t channel, uint8_t chooseMCP);
  27. void displayAmperage(uint32_t pomiar);
  28. void displayVoltage(uint32_t pomiar);
  29.  
  30. int main(void)
  31. {
  32.  
  33.     lcd_init();
  34.     InitSpi();
  35.  
  36.         while(1)
  37.         {
  38.                 lcd_cls();
  39.  
  40.         uint32_t pomiarU = MCP3304(0,1);
  41.         uint32_t pomiarA = MCP3304(0,2);
  42.         uint32_t pomiar2U = MCP3304(2,1);
  43.         uint32_t pomiar2A = MCP3304(2,2);
  44.         uint32_t pomiar3U = MCP3304(6,1);
  45.         uint32_t pomiar3A = MCP3304(4,2);
  46.  
  47.         displayVoltage(pomiarU);
  48.         displayAmperage(pomiarA);
  49.         displayVoltage(pomiar2U);
  50.         displayAmperage(pomiar2A);
  51.         displayVoltage(pomiar3U);
  52.         displayAmperage(pomiar3A);
  53.  
  54.          _delay_ms(1000);
  55.         }
  56.  
  57. }
  58.  
  59. void displayAmperage(uint32_t pomiar)
  60. {
  61.         if(pomiar > 4096)
  62.     {
  63.                 pomiar = (8192 - pomiar)/2;
  64.                 lcd_str("-");
  65.     }
  66.  
  67.     uint32_t result = (pomiar * 2.56 / 4096) * 1000 * 10 ;// /2 dla drugiego mcp
  68.     uint16_t unity = result % 1000;
  69.     uint16_t dozens = result / 1000;
  70.  
  71.     lcd_int(dozens);
  72.     lcd_str(".");
  73.  
  74.     if(unity < 10)
  75.     {
  76.         lcd_int(0);
  77.         lcd_int(0);
  78.     }
  79.     else if( unity < 100)
  80.     {
  81.         lcd_int(0);
  82.     }
  83.  
  84.     lcd_int(unity);
  85.     lcd_str(" A ");
  86. }
  87.  
  88. void displayVoltage(uint32_t pomiar)
  89. {
  90.     if(pomiar > 4096)
  91.     {
  92.         pomiar = (8192 - pomiar);
  93.         lcd_str("-");
  94.     }
  95.  
  96.     uint32_t wyn = (pomiar * 2.56 / 4096) * 32000  ;// /2 dla drugiego mcp
  97.     uint16_t unity = wyn % 1000;
  98.     uint16_t dozens = wyn / 1000;
  99.  
  100.     lcd_int(dozens);
  101.     lcd_str(".");
  102.  
  103.     if(unity < 10)
  104.     {
  105.         lcd_int(0);
  106.         lcd_int(0);
  107.     }
  108.     else if( unity < 100)
  109.     {
  110.         lcd_int(0);
  111.     }
  112.  
  113.     lcd_int(unity);
  114.     lcd_str(" V ");
  115. }
  116.  
  117. void InitSpi()
  118.    {
  119.        DDRB |= (1 << MOSI) | (1 << SCK) | (1 << PB4);
  120.        PORTB |= (1 << PB4);
  121.        DDRA |= (1 << CS) | (1 << CSS);
  122.        // Define the following pins as output
  123.        //DDRA |= (1 << CS);
  124.  
  125.        SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(SPIE);
  126.        //SPSR |= (1 << SPI2X);
  127.        CS_HIGH;
  128.        CS_HIGHH;
  129.    }
  130.  
  131. //wysyłanie do adc
  132. void SendSpi(uint8_t bajt)
  133. {
  134.         SPDR = bajt;
  135.         while( !(SPSR & (1 << SPIF)) );
  136.  
  137. }
  138.  
  139. //odbiór z adc
  140. uint8_t ReceiveSpi( void )
  141. {
  142.         CS_HIGH;
  143.         _delay_us(5);
  144.         CS_LOW;
  145.     while( !(SPSR & (1 << SPIF)) );
  146.         return SPDR;
  147. }
  148.  
  149.  
  150. uint8_t SendAndReceive(uint8_t data)
  151. {
  152.         SPDR = data;
  153.         while( !(SPSR & (1 << SPIF)) );
  154.         return SPDR;
  155. }
  156.  
  157. uint16_t MCP3304(uint8_t channel, uint8_t chooseMCP)
  158. {
  159.          uint8_t send_one = 0x08 | (channel >> 1);
  160.          uint8_t send_two = 0x00 | (channel << 7);
  161.          uint8_t send_three = 0x00;
  162.  
  163.          uint16_t sum;
  164.          uint8_t wyn1;
  165.          uint8_t wyn2;
  166.          uint32_t fullSum = 0;
  167.  
  168.          for(int i = 0; i < 5; ++i)
  169.          {
  170.              if(chooseMCP == 1)
  171.              {
  172.                CS_LOW;
  173.                       SendAndReceive(send_one);
  174.                       wyn1 = SendAndReceive(send_two);
  175.                       wyn2 = SendAndReceive(send_three);
  176.                CS_HIGH;
  177.              }
  178.              else
  179.              {
  180.                    CS_LOWW;
  181.                              SendAndReceive(send_one);
  182.                              wyn1 = SendAndReceive(send_two);
  183.                              wyn2 = SendAndReceive(send_three);
  184.                    CS_HIGHH;
  185.              }
  186.  
  187.              wyn1 = wyn1 & 0b00011111;
  188.  
  189.              sum = wyn1;
  190.              sum = sum << 8;
  191.              sum |= wyn2;
  192.  
  193.              fullSum += sum;
  194.  
  195.          }
  196.          sum = fullSum/5;
  197.          return sum;
  198. }