Zorbing

[ES] Übung 2

May 15th, 2014
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. /*
  2.  * ES_Prak_01.c
  3.  *
  4.  * Created: 07.04.2014 14:42:04
  5.  *  Author: Ehlers
  6.  */
  7.  
  8. /*
  9.  * Definitions
  10.  */
  11.  
  12. #define F_CPU 1000000UL
  13.  
  14. #define I2C_DEVICE_ADDRESS (0xE0)
  15.  
  16. #define TEST_LED_DISPLAY 1
  17. #define TEST_ADC_READ 0
  18. #define TEST_I2C_READREGISTER 1
  19.  
  20. #define BUFFSIZE 20
  21.  
  22. /*
  23.  * Includes
  24.  */
  25.  
  26. #include <avr/io.h>
  27. // include this to get access to uint8_t, uint16_t and so on...
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <util/delay.h>
  31.  
  32. // include the header for I2C communication
  33. #include "i2c_master.h"
  34. #include "uart.h"
  35.  
  36.  
  37. /*
  38.  * Exercise 01
  39.  */
  40.  
  41. void led_init()
  42. {
  43.     // configure Port B as output (set all bits in DDRB high)  
  44.     DDRB = ~0; //0b11111111
  45. }
  46.  
  47. void led_display(uint16_t value)
  48. {
  49.     uint8_t mask = 0;
  50.     // create mask
  51.     for (uint8_t i = value / 128; i >= 0; i--)
  52.     {
  53.         // shift the bit mask and set the lsb high
  54.         mask = (mask << 1) | 1;
  55.     }
  56.     // set the output of Port B
  57.     PINB = mask;
  58. }
  59.  
  60. /*
  61.  * Exercise 02
  62.  */
  63.  
  64. void adc_init()
  65. {
  66.     // set prescaler to 128
  67.     ADCSRA = (1 << ADEN) | (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2);
  68.  
  69.     // set AVCC (VCC = 5V) as reference
  70.     ADMUX = (1 << REFS0);
  71.  
  72.     // start blind conversion
  73.     adc_read(0);
  74. }
  75.  
  76. uint16_t adc_read(uint8_t channel)
  77. {
  78.     // create mux mask
  79.     uint8_t muxMask = (1 << MUX0) | (1 << MUX1) | (1 << MUX2) | (1 << MUX3) | (1 << MUX4);
  80.     // clear all channel bits
  81.     ADMUX &= ~muxMask;
  82.     // set the passed channel (and clear not allowed bits)
  83.     ADMUX |= (channel & muxMask);
  84.  
  85.     // start conversion
  86.     ADCSRA |= (1 << ADSC);
  87.     // wait until the conversion is complete
  88.     while (ADCSRA & (1 << ADSC));
  89.  
  90.     // read value of the low byte
  91.     uint16_t value = ADCL;
  92.     // add the high byte (and shift its value)
  93.     value |= (ADCH << 8);
  94.     return value;
  95. }
  96.  
  97. /*
  98.  * Exercise 03
  99.  */
  100.  
  101. void i2c_writeRegister(uint8_t add, uint8_t reg, uint8_t dat)
  102. {
  103.     // connect to address add
  104.     i2c_start(add & ~1);
  105.  
  106.     // chose register to reg
  107.     i2c_write(reg);
  108.     // write dat into register reg
  109.     i2c_write(dat);
  110.  
  111.     // stop connection
  112.     i2c_stop();
  113. }
  114.  
  115. uint8_t i2c_readRegister(uint8_t add, uint8_t reg)
  116. {
  117.     // connect to address add
  118.     i2c_start(add & ~1);
  119.     // chose register to reg
  120.     i2c_write(reg);
  121.  
  122.     // start read connection
  123.     i2c_rep_start(add | 1);
  124.     uint8_t dat = 0;
  125.     // read value from reg into dat
  126.     i2c_readNak(&dat);
  127.  
  128.     // stop connection
  129.     i2c_stop();
  130.     // return read data
  131.     return dat;
  132. }
  133.  
  134. int main(void)
  135. {
  136.     // Initialization
  137.     sei();
  138.     uart_init();
  139.  
  140.     // initialize the AD converter
  141.     adc_init();
  142.  
  143.     // declare string buffer
  144.     char buff[BUFFSIZE];
  145.  
  146.     while(1)
  147.     {
  148.         #if TEST_LED_DISPLAY == 1
  149.  
  150.         // test led_display
  151.         for (uint16_t testNum = 0; testNum < 1024; testNum++)
  152.         {
  153.             led_display(testNum);
  154.             _delay_ms(8);
  155.         }
  156.  
  157.         #elif TEST_ADC_READ == 1
  158.  
  159.         // test adc_read
  160.         led_display(adc_read(0));
  161.  
  162.         #elif TEST_I2C_READREGISTER == 1
  163.  
  164.         // test i2c_readRegister
  165.         uint16_t distance = i2c_readRegister(I2C_DEVICE_ADDRESS, 3);
  166.         distance |= (i2c_readRegister(I2C_DEVICE_ADDRESS, 2) << 8);
  167.  
  168.         snprintf(buf, BUFFSIZE, "%u\n", distance);
  169.         uart_sendstring(buff);
  170.         _delay_ms(1024);
  171.  
  172.         #else
  173.  
  174.         // TODO
  175.  
  176.         #endif
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment