Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <inttypes.h>
- #include <avr/io.h>
- #include <avr/pgmspace.h>
- #include "xlcd.h"
- #include "xmega_a4u.h"
- /*! Defining an example slave address. */
- #define SLAVE_ADDRESS 0x25
- /*! Defining number of bytes in buffer. */
- #define NUM_BYTES 8
- /*! CPU speed 2MHz, BAUDRATE 100kHz and Baudrate Register Settings */
- #define CPU_SPEED F_CPU
- #define BAUDRATE F_TWI
- #define F_TWI 100000
- #define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE)
- #define TWI_BAUD(F_CPU, F_TWI) ((F_CPU / (2 * F_TWI)) - 5)
- static volatile uint8_t buff[8],touchx_bf[13],touchy_bf[13],touchlsb_bf[8];
- void twi_write(uint8_t addr, uint8_t byte){
- /*SEND SLAVE ADDRESS + W BIT */
- TWIC_MASTER_ADDR = 0X4a;
- /*WAIT FOR THE WRITE INTERUPT FLAG TO SET HIGH*/
- while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));
- /*SEND HIGH BYTE OF EEPROM MEMORY ADDRESS*/
- TWIC_MASTER_DATA = addr;
- while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));
- /*SEND LOW BYTE OF EEPROM MEMORY ADDRESS*/
- TWIC_MASTER_DATA = byte;
- while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));
- /* MASTER SENDS ACK AND ISSUES A STOP */
- // TWIC_MASTER_CTRLC = 0X03;
- // TWIC_MASTER_ADDR = 0X4a;
- // /*WAIT FOR THE WRITE INTERUPT FLAG TO SET HIGH*/
- // while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));
- //
- // /*SEND HIGH BYTE OF EEPROM MEMORY ADDRESS*/
- // TWIC_MASTER_DATA = 0x04;
- // while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));
- //
- // /*SEND LOW BYTE OF EEPROM MEMORY ADDRESS*/
- // TWIC_MASTER_DATA = 0x20;
- // while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));
- //
- /* MASTER SENDS ACK AND ISSUES A STOP */
- TWIC_MASTER_CTRLC = 0X03;
- }
- uint8_t twi_read(uint8_t addr){
- TWIC_MASTER_ADDR = 0X4a;
- /*WAIT FOR THE WRITE INTERUPT FLAG TO SET HIGH*/
- while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));
- /*SEND HIGH BYTE OF EEPROM MEMORY ADDRESS*/
- TWIC_MASTER_DATA = addr;
- while(!(TWIC_MASTER_STATUS & TWI_MASTER_WIF_bm));
- TWIC_MASTER_ADDR = 0X4b;
- while(!(TWIC_MASTER_STATUS & TWI_MASTER_RIF_bm));
- // buff[offset] = TWIC_MASTER_DATA;
- TWIC_MASTER_CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc;
- return TWIC_MASTER_DATA;
- }
- // void debug(uint8_t x, uint8_t y){
- // lcd_gotoxy(x,y);
- // itoa(TWIC_MASTER_STATUS,temp,2);
- // lcd_puts(temp);
- //
- // }
- int main(void)
- {
- setup_clk_32M_int();
- lcd_init(LCD_DISP_ON);
- lcd_clrscr();
- uint16_t touchx,touchy;
- uint8_t str1[8], str2[8];
- TWIC_MASTER_BAUD = 0x9B;
- /*SET WIEN, ENABLE and INTLVL bits high.*/
- TWIC_MASTER_CTRLA |= 0xD8;
- /*Force TWI bus idle mode.*/
- TWIC_MASTER_STATUS |= 0x01;
- PORTA.DIRSET = PIN1_bm; //reset_io
- PORTA.DIRSET = PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
- PORTA.DIRCLR = PIN2_bm | PIN3_bm; //PIN2 INT; PIN3 SYNC;
- // PORTA.PIN0CTRL = PORT_ISC_RISING_gc;
- // PORTA.INT0MASK = PIN2_bm;
- // PORTA.INTCTRL = PORT_INT0LVL_LO_gc;
- //
- //short reset
- // PORTA.OUTCLR = PIN1_bm;
- // _delay_ms(5);
- PORTA.OUTSET = PIN1_bm | PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
- // PORTA.OUTCLR |= PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
- // PMIC.CTRL |= PMIC_HILVLEN_bm | PMIC_LOLVLEN_bm;
- // sei();
- twi_write(0x05,0x01);
- twi_write(0x20,0x08);
- twi_write(0x21,0x05);
- twi_write(0x22,0x06);
- twi_write(0x23,0xF2);
- twi_write(0x24,0xF6);
- twi_write(0x25,0x84);
- twi_write(0x26,0x02);
- twi_write(0x2A,0x04);
- twi_write(0x2B,0x07);
- twi_write(0x2C,0x07);
- twi_write(0x2D,0x0A);
- twi_write(0x31,0x03);
- twi_write(0x32,0x01);
- twi_write(0x33,0x03);
- twi_write(0x34,0x01);
- twi_write(0x50,0xCB);
- twi_write(0x51,0xCB);
- twi_write(0x58,0xCB);
- twi_write(0x59,0xCB);
- twi_write(0x05,0x02);
- // twi_write(0x04,0x02);
- // twi_write(0x04,0x01);
- for (;;) {
- buff[0]=twi_read(0x11);
- buff[1]=twi_read(0x12);
- buff[2]=twi_read(0x13);
- buff[3]=twi_read(0x90);
- buff[4]=twi_read(0x91);
- touchx=((buff[0] << 4) | ((0xF0 & buff[2]) >> 4));
- touchy=((buff[1] << 4) | ((0x0F & buff[2])));
- itoa(touchx,touchx_bf,10);
- itoa(touchy,touchy_bf,10);
- itoa(buff[3],str1,16);
- itoa(buff[4],str2,16);
- // if(((touchx <= 340) && (touchx >= 300)) && (touchy >= 310)){
- // PORTA.OUTCLR = PIN4_bm;
- // }
- // if(((touchx <= 410) && (touchx >= 350)) && ((touchy >= 150) && (touchy <= 191))){
- // PORTA.OUTSET = PIN4_bm;
- // }
- // if(!(PORTA.IN & PIN2_bm)){ PORTA.OUTTGL = PIN4_bm;}
- // if(!(PORTA.IN & PIN3_bm)){ PORTA.OUTTGL = PIN5_bm;}
- lcd_gotoxy(0,0);
- lcd_puts(touchx_bf);
- lcd_gotoxy(0,1);
- lcd_puts(touchy_bf);
- lcd_gotoxy(0,2);
- lcd_puts(str1);
- lcd_gotoxy(10,2);
- lcd_puts(str2);
- _delay_ms(128);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement