Advertisement
Guest User

Untitled

a guest
May 18th, 2009
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.68 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/wdt.h>
  4. #include <avr/interrupt.h>
  5. #include <util/twi.h>
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include "serial.h"
  9.  
  10. uint16_t loopCounter = 0;
  11. uint8_t nunchuck_buffer[6];
  12.  
  13. void twi_init()
  14. {
  15.     TWBR = 72;
  16.     TWSR = 0;  
  17. }
  18.  
  19. int8_t twi_start_condition()
  20. {
  21.     TWCR = (1<<TWINT)|(1<<TWSTA)| (1<<TWEN) | (0<<TWIE);
  22.  
  23.     loopCounter = 0;
  24.     while (!(TWCR & (1<<TWINT)) && loopCounter < 0xFFFF) {loopCounter++; _delay_us(1);}
  25.     if ((TWSR & 0xF8) != TW_START)
  26.         return 0;
  27.     else
  28.         return 1;
  29. }
  30.  
  31. int8_t twi_send_address_write(const int8_t address)
  32. {
  33.     TWDR = (address << 1);
  34.     TWCR = (1<<TWINT) | (1<<TWEN);
  35.     loopCounter = 0;
  36.     while (!(TWCR & (1<<TWINT)) && loopCounter < 0xFFFF) {loopCounter++; _delay_us(1);}
  37.     if ((TWSR & 0xF8) != TW_MT_SLA_ACK)
  38.         return 0;
  39.     else
  40.         return 1;
  41. }
  42.  
  43. int8_t twi_send_address_read(const int8_t address)
  44. {
  45.     TWDR = (address << 1) | 1;
  46.     TWCR = (1<<TWINT) | (1<<TWEN);
  47.     loopCounter = 0;
  48.     while (!(TWCR & (1<<TWINT)) && loopCounter < 0xFFFF) {loopCounter++; _delay_us(1);}
  49.     if ((TWSR & 0xF8) != TW_MR_SLA_ACK)
  50.         return 0;
  51.     else
  52.         return 1;
  53. }
  54.  
  55. int8_t twi_send_byte(const int8_t byte)
  56. {
  57.     TWDR = byte;
  58.     TWCR = (1<<TWINT) | (1<<TWEN);
  59.     loopCounter = 0;
  60.     while (!(TWCR & (1<<TWINT)) && loopCounter < 0xFFFF) {loopCounter++; _delay_us(1);}
  61.     if ((TWSR & 0xF8) != TW_MT_DATA_ACK)
  62.         return 0;
  63.     else
  64.         return 1;
  65. }
  66.  
  67. void twi_stop_transmission()
  68. {
  69.     TWCR = (1<<TWINT)|(1<<TWEN)| (1<<TWSTO);
  70. }
  71.  
  72. int8_t nunchuck_init()
  73. {
  74.     if(!twi_start_condition()) {
  75.         uart_puts("nunchuck_init(): Error transmitting start condition\n");
  76.         return 1;
  77.     }
  78.  
  79.     if(!twi_send_address_write(0x52)) {
  80.         uart_puts("nunchuck_init(): Error sending address\n");
  81.         return 1;
  82.     }
  83.  
  84.     if(!twi_send_byte(0x40)) {
  85.         uart_puts("nunchuck_init(): Error sending byte 0x40\n");
  86.         return 1;
  87.     }
  88.    
  89.     if(!twi_send_byte(0x00)) {
  90.         uart_puts("nunchuck_init(): Error sending byte 0x00\n");
  91.         return 1;
  92.     }
  93.  
  94.     twi_stop_transmission();
  95.     return 0;
  96. }
  97.  
  98. int8_t nunchuck_request_data()
  99. {
  100.     if(!twi_start_condition()) {
  101.         uart_puts("nunchuck_request_data(): Error transmitting start condition\n");
  102.         return 1;
  103.     }
  104.    
  105.     if(!twi_send_address_write(0x52)) {
  106.         uart_puts("nunchuck_request_data(): Error sending address\n");
  107.         return 1;
  108.     }
  109.    
  110.     if(!twi_send_byte(0x00)) {
  111.         uart_puts("nunchuck_request_data(): Error sending byte 0x00\n");
  112.         return 1;
  113.     }
  114.    
  115.     twi_stop_transmission();
  116.  
  117.     _delay_ms(1);
  118.  
  119.     if(!twi_start_condition()) {
  120.         uart_puts("Error transmitting start condition 3\n");
  121.         return 1;
  122.     }
  123.    
  124.     if(!twi_send_address_read(0x52)) {
  125.         uart_puts("Error sending address 3\n");
  126.         return 1;
  127.     }
  128.  
  129.     for(int8_t i = 0; i < 6; i++) {
  130.         TWCR = (1<<TWINT) | (1<<TWEN) | _BV(TWEA);
  131.  
  132.         loopCounter = 0;
  133.         while (!(TWCR & (1<<TWINT)) && loopCounter < 0xFFFF) {loopCounter++; _delay_us(1);}
  134.         if ((TWSR & 0xF8) != TW_MR_DATA_ACK) {
  135.             uart_puts("nunchuck_request_data(): nack");
  136.             return 1;
  137.         }
  138.         nunchuck_buffer[i] = (TWDR ^ 0x17) + 0x17; // that's true cryptography
  139.     }
  140.     TWCR = (1<<TWINT) | (1<<TWEN); // send NAK
  141.     twi_stop_transmission();
  142.  
  143.     return 0;
  144. }
  145.  
  146. int8_t nunchuck_reset_and_read()
  147. {
  148.     TWCR = 0;
  149.     _delay_ms(5);
  150.     twi_init();
  151.  
  152.     nunchuck_init();
  153.  
  154.     _delay_ms(20);
  155.  
  156.     nunchuck_request_data();
  157.     _delay_ms(200);
  158.     return 0;
  159. }
  160.  
  161. void nunchuck_dump_raw_data()
  162. {
  163.     for (int8_t i = 0; i < 6; i++) {
  164.         uart_putint(nunchuck_buffer[i]);
  165.         uart_putc('|');
  166.     }
  167.     uart_putc('\n');
  168. }
  169.  
  170. inline int8_t nunchuck_button_z()
  171. {
  172.     return (nunchuck_buffer[5] & 1) ^ 1;
  173. }
  174.  
  175. inline int8_t nunchuck_button_c()
  176. {
  177.     return ((nunchuck_buffer[5] >> 1) & 1) ^ 1;
  178. }
  179.  
  180. inline uint8_t nunchuck_joystick_x()
  181. {
  182.     return nunchuck_buffer[0];
  183. }
  184.  
  185. inline uint8_t nunchuck_joystick_y()
  186. {
  187.     return nunchuck_buffer[1];
  188. }
  189.  
  190. inline uint16_t nunchuck_accel_x()
  191. {
  192.     uint16_t ret = nunchuck_buffer[2];
  193.     ret <<= 2;
  194.     ret = ret + ((nunchuck_buffer[5] >> 2) & 0x03);
  195.     return ret;
  196. }
  197.  
  198. inline uint16_t nunchuck_accel_y()
  199. {
  200.     uint16_t ret = nunchuck_buffer[3];
  201.     ret <<= 2;
  202.     ret = ret + ((nunchuck_buffer[5] >> 4) & 0x03);
  203.     return ret;
  204. }
  205.  
  206. inline uint16_t nunchuck_accel_z()
  207. {
  208.     uint16_t ret = nunchuck_buffer[4];
  209.     ret <<= 2;
  210.     ret = ret + ((nunchuck_buffer[5] >> 6) & 0x03);
  211.     return ret;
  212. }
  213.  
  214. void dumme_funktion()
  215. {
  216.     uart_puts("WTF? WTF? WTF? WTF? WTF?WTF? WTF? WTF? WTF? WTF?WTF? WTF? WTF? WTF? WTF?WTF? WTF? WTF? WTF? WTF?WTF? WTF? WTF? WTF? WTF?");
  217. }
  218.  
  219. void nunchuck_dump()
  220. {
  221.     uart_puts("\nButtons: ");
  222.     if (nunchuck_button_z())
  223.         uart_putc('Z');
  224.     else
  225.         uart_putc('_');
  226.     if (nunchuck_button_c())
  227.         uart_putc('C');
  228.     else
  229.         uart_putc('_');
  230.  
  231.     uart_puts(" Joystick: x-axis: ");
  232.     for (int8_t i = 0; i <= 0xF; i++) {
  233.         uart_putc(nunchuck_joystick_x() > (i<<4) ? 'X' : '-');
  234.     }
  235.     uart_puts(" y-axis: ");
  236.     for (int8_t i = 0; i <= 0xF; i++) {
  237.         uart_putc(nunchuck_joystick_y() > (i<<4) ? 'X' : '-');
  238.     }
  239.     uart_puts("\nx-axis: ");
  240.     uint16_t temp = nunchuck_accel_x() >> 2;
  241.     for (int8_t i = 0; i <= 0xF; i++) {
  242.         uart_putc(temp > (i<<4) ? 'X' : '-');
  243.     }
  244.     uart_puts(" y-axis: ");
  245.     temp = nunchuck_accel_y() >> 2;
  246.     for (int8_t i = 0; i <= 0xF; i++) {
  247.         uart_putc(temp > (i<<4) ? 'X' : '-');
  248.     }
  249.     uart_puts(" z-axis: ");
  250.     temp = nunchuck_accel_z() >> 2;
  251.     for (int8_t i = 0; i <= 0xF; i++) {
  252.         uart_putc(temp > (i<<4) ? 'X' : '-');
  253.     }
  254.     uart_putc('\n');
  255.  
  256. }
  257.  
  258. void bench()
  259. {
  260.     _delay_ms(1000);
  261.     while(1) {
  262.         uart_puts("Starting\n");
  263.         for (uint16_t i = 0; i < 100; i++) {
  264.             nunchuck_reset_and_read();
  265.         }
  266.         uart_puts("DONE!\n\n");
  267.     }
  268. }
  269.  
  270. int main()
  271. {
  272.     sei();
  273.     wdt_disable();
  274.     uart_init();
  275.     uart_putc('x');
  276. //  bench();
  277.     while(1) {
  278. //      uart_putc('y');
  279.         nunchuck_reset_and_read();
  280. //      uart_putc('z');
  281. //      nunchuck_dump_raw_data();
  282.         nunchuck_dump();
  283.     }
  284.  
  285.     return 0;
  286. }
  287.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement