daily pastebin goal
9%
SHARE
TWEET

twi_test.c

a guest Mar 5th, 2016 167 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <inttypes.h>
  2. #include <avr/io.h>
  3. #include <avr/pgmspace.h>
  4. #include "xlcd.h"
  5. #include "xmega_a4u.h"
  6.  
  7. /*! Defining an example slave address. */
  8. #define SLAVE_ADDRESS   0x25
  9.  
  10. /*! Defining number of bytes in buffer. */
  11. #define NUM_BYTES   8
  12.  
  13. /*! CPU speed 2MHz, BAUDRATE 100kHz and Baudrate Register Settings */
  14. #define CPU_SPEED       F_CPU
  15. #define BAUDRATE    F_TWI
  16. #define F_TWI       100000
  17. #define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE)
  18. #define TWI_BAUD(F_CPU, F_TWI) ((F_CPU / (2 * F_TWI)) - 5)
  19.  
  20. static volatile uint8_t buff[8],touchx_bf[13],touchy_bf[13],touchlsb_bf[8];
  21.  
  22.  
  23.  
  24. void twi_write(uint8_t addr, uint8_t byte){
  25.        /*SEND SLAVE ADDRESS + W BIT */     
  26.     TWIC_MASTER_ADDR = 0X4a;  
  27.       /*WAIT FOR THE WRITE INTERUPT FLAG TO SET HIGH*/
  28.     while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));  
  29.                        
  30.        /*SEND HIGH BYTE OF EEPROM MEMORY ADDRESS*/
  31.     TWIC_MASTER_DATA = addr;           
  32.     while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));  
  33.    
  34.        /*SEND LOW BYTE OF EEPROM MEMORY ADDRESS*/
  35.     TWIC_MASTER_DATA = byte;
  36.     while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));  
  37.    
  38.        /* MASTER SENDS ACK AND ISSUES A STOP */
  39. //     TWIC_MASTER_CTRLC = 0X03;
  40.    
  41. //     TWIC_MASTER_ADDR = 0X4a;  
  42. //       /*WAIT FOR THE WRITE INTERUPT FLAG TO SET HIGH*/
  43. //     while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));   
  44. //                     
  45. //        /*SEND HIGH BYTE OF EEPROM MEMORY ADDRESS*/
  46. //     TWIC_MASTER_DATA = 0x04;        
  47. //     while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));   
  48. //  
  49. //        /*SEND LOW BYTE OF EEPROM MEMORY ADDRESS*/
  50. //     TWIC_MASTER_DATA = 0x20;
  51. //     while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));   
  52. //  
  53.        /* MASTER SENDS ACK AND ISSUES A STOP */
  54.     TWIC_MASTER_CTRLC = 0X03;
  55.  
  56. }
  57. uint8_t twi_read(uint8_t addr){
  58.  
  59.     TWIC_MASTER_ADDR = 0X4a;  
  60.       /*WAIT FOR THE WRITE INTERUPT FLAG TO SET HIGH*/
  61.     while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));  
  62.                        
  63.        /*SEND HIGH BYTE OF EEPROM MEMORY ADDRESS*/
  64.     TWIC_MASTER_DATA = addr;           
  65.     while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));
  66.  
  67.        
  68.     TWIC_MASTER_ADDR = 0X4b;  
  69.     while(!(TWIC_MASTER_STATUS & TWI_MASTER_RIF_bm));
  70.    
  71. //     buff[offset] = TWIC_MASTER_DATA;
  72.     TWIC_MASTER_CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc;
  73.    
  74.     return TWIC_MASTER_DATA;
  75. }
  76.  
  77. // void debug(uint8_t x, uint8_t y){
  78. //   lcd_gotoxy(x,y);
  79. //   itoa(TWIC_MASTER_STATUS,temp,2);
  80. //   lcd_puts(temp);
  81. //    
  82. // }
  83.  
  84. int main(void)
  85. {
  86.   setup_clk_32M_int();
  87.   lcd_init(LCD_DISP_ON);
  88.   lcd_clrscr();
  89.  
  90.   uint16_t touchx,touchy;
  91.   uint8_t str1[8], str2[8];
  92.     TWIC_MASTER_BAUD = 0x9B;   
  93.        /*SET WIEN, ENABLE and INTLVL bits high.*/
  94.     TWIC_MASTER_CTRLA |= 0xD8; 
  95.        /*Force TWI bus idle mode.*/
  96.     TWIC_MASTER_STATUS |= 0x01;
  97.  
  98.   PORTA.DIRSET  = PIN1_bm;      //reset_io
  99.   PORTA.DIRSET  = PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
  100.   PORTA.DIRCLR  = PIN2_bm | PIN3_bm;    //PIN2 INT; PIN3 SYNC;
  101.  
  102. //   PORTA.PIN0CTRL = PORT_ISC_RISING_gc;
  103. //   PORTA.INT0MASK = PIN2_bm;
  104. //   PORTA.INTCTRL   = PORT_INT0LVL_LO_gc;
  105. //  
  106.   //short reset
  107. //   PORTA.OUTCLR   = PIN1_bm;
  108. //   _delay_ms(5);
  109.   PORTA.OUTSET  = PIN1_bm | PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
  110. //   PORTA.OUTCLR  |= PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
  111. //     PMIC.CTRL |= PMIC_HILVLEN_bm | PMIC_LOLVLEN_bm;
  112. //   sei();
  113.  
  114.   twi_write(0x05,0x01);
  115.   twi_write(0x20,0x08);
  116.   twi_write(0x21,0x05);
  117.  
  118.   twi_write(0x22,0x06);
  119.   twi_write(0x23,0xF2);
  120.   twi_write(0x24,0xF6);
  121.  
  122.   twi_write(0x25,0x84);
  123.   twi_write(0x26,0x02);
  124.   twi_write(0x2A,0x04);
  125.   twi_write(0x2B,0x07);
  126.   twi_write(0x2C,0x07);
  127.  
  128.   twi_write(0x2D,0x0A);
  129.  
  130.   twi_write(0x31,0x03);
  131.   twi_write(0x32,0x01);
  132.  
  133.   twi_write(0x33,0x03);
  134.   twi_write(0x34,0x01);
  135.  
  136.   twi_write(0x50,0xCB);
  137.   twi_write(0x51,0xCB);
  138.   twi_write(0x58,0xCB);
  139.   twi_write(0x59,0xCB);
  140.  
  141.   twi_write(0x05,0x02);
  142.  
  143. //   twi_write(0x04,0x02);
  144. //   twi_write(0x04,0x01);
  145.  
  146.   for (;;) {
  147.       buff[0]=twi_read(0x11);
  148.       buff[1]=twi_read(0x12);
  149.       buff[2]=twi_read(0x13);
  150.       buff[3]=twi_read(0x90);
  151.       buff[4]=twi_read(0x91);
  152.      
  153.       touchx=((buff[0] << 4) | ((0xF0 & buff[2]) >> 4));
  154.       touchy=((buff[1] << 4) | ((0x0F & buff[2])));
  155.  
  156.       itoa(touchx,touchx_bf,10);
  157.       itoa(touchy,touchy_bf,10);
  158.       itoa(buff[3],str1,16);
  159.       itoa(buff[4],str2,16);
  160.      
  161. //       if(((touchx <= 340) && (touchx >= 300)) && (touchy >= 310)){
  162. //  PORTA.OUTCLR = PIN4_bm;
  163. //       }
  164. //       if(((touchx <= 410) && (touchx >= 350)) && ((touchy >= 150) && (touchy <= 191))){
  165. //  PORTA.OUTSET = PIN4_bm;
  166. //       }
  167. //       if(!(PORTA.IN & PIN2_bm)){ PORTA.OUTTGL = PIN4_bm;}
  168. //       if(!(PORTA.IN & PIN3_bm)){ PORTA.OUTTGL = PIN5_bm;}
  169.       lcd_gotoxy(0,0);
  170.       lcd_puts(touchx_bf);
  171.       lcd_gotoxy(0,1);
  172.       lcd_puts(touchy_bf);
  173.       lcd_gotoxy(0,2);
  174.       lcd_puts(str1);
  175.       lcd_gotoxy(10,2);
  176.       lcd_puts(str2);
  177.  
  178.  
  179.       _delay_ms(128);
  180.   }
  181. }
RAW Paste Data
Top