|
|
View previous topic :: View next topic |
Author |
Message |
Grkan13
Joined: 06 Apr 2011 Posts: 14
|
I2C touch button IC |
Posted: Mon Dec 16, 2013 11:19 am |
|
|
Hello,
I'm working with PIC18F46k22 and compiler version 5.008
For the touch button control I'm using this chip.
http://www.semtech.com/images/datasheet/sx9512_9513_ag.pdf
I've never worked with I2C, so can you guide me how to start?
I've found PCM Programmer I2C control macro, it shows the address the SX9512 as 0x56.
Touch status is registered to 0x01 address, how can i read that register?
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 16, 2013 6:41 pm |
|
|
The routines to read and write registers will be similar to the ones shown
in this code for the HMC5883L.
http://www.ccsinfo.com/forum/viewtopic.php?t=50207&start=4
The SX9512 i2c write address is 0x56 and the i2c read address is 0x57.
Edit the #define statements to use those addresses.
Post the Vdd voltage for your PIC, and also post the Vdd and SVdd
voltages used for the SX9512 on your board. |
|
|
Grkan13
Joined: 06 Apr 2011 Posts: 14
|
|
Posted: Wed Dec 18, 2013 3:53 pm |
|
|
Thank you PCM Programmer,
I've updated my code tried to read some registers but i get 0x00 from all the registers. I'm going to try with 24lc128 to write then read.
Vdd of the PIC, Vdd and SVdd of SX9512 is all connected each other and its 5V.
I'm using 2k resistors as pull up for SDA and SCL.
I've checked with oscilloscope and the clock rate of SCL is 62.5Khz.
edit: Can you also help me about, If I want to change just specific bit of any register, how can I do? As I understand according to the this code, we sent 8 bits every single time. Is it possible to change just 2nd bit of 0x01 register?
Here is my code;
Code: |
#include <18F46K22.h>
#device ADC=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //Nobrownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(internal=4MHz)
#use i2c(Master, sda=PIN_c4, scl=PIN_c3)
#include "flex_lcd.c"
// i2c slave addresses
#define SX9512_WRT_ADDR 0x56
#define SX9512_READ_ADDR 0x57
void sx9512_write_reg(int8 reg, int8 data)
{
i2c_start();
i2c_write(sx9512_WRT_ADDR);
i2c_write(reg);
i2c_write(data);
i2c_stop();
}
int8 sx9512_read_reg(int8 reg)
{
int8 retval;
i2c_start();
i2c_write(sx9512_WRT_ADDR);
i2c_write(reg);
i2c_start();
i2c_write(sx9512_READ_ADDR);
retval = i2c_read(0);
i2c_stop();
return(retval);
}
void main()
{
int16 var=0;
lcd_init();
While(TRUE)
{
var=sx9512_read_reg(0X01);
}
}
|
Last edited by Grkan13 on Wed Dec 18, 2013 4:59 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 18, 2013 4:11 pm |
|
|
1. I don't see where you display the data. There should be a printf
statement directed to the LCD, to display the register value.
2. What about the connection to the NRST pin on the SX9512 ? That pin
should be connected to +5v. Do you have that connection ?
3. In your code below, you have declared 'var' as 16-bit, but the read
register function only returns an 8-bit value. It reads an 8-bit register.
So 'var' should be declared as int8:
Quote: |
int16 var=0;
var=sx9512_read_reg(0X01);
int8 sx9512_read_reg(int8 reg)
{ |
4. The SX9512 has a power-up time of 10ms. You need to have a delay
of at least that long (preferably longer, for safety) at the start of main().
Though, in the case of your program, the lcd_init() routine will provide
that delay.
5. The register you're reading at address 0x01 is the TouchStatus register.
It's Read-Only and it normally reads as all 0's. I wouldn't choose that
register as the first one to try to read, to prove that the chip is working.
A better test would be to write to the Interrupt Request Mask register
(addr = 0x09). First write 0x00 as the data and read it back. You should
get 0x00. Next, write 0x54. If you read back 0x54, then it's working.
The bottom two bits always read as 0's. That's given in the data sheet. |
|
|
Grkan13
Joined: 06 Apr 2011 Posts: 14
|
|
Posted: Wed Dec 18, 2013 5:11 pm |
|
|
1- I guess i just took it out, before posting normally i have this line after reading.
printf(lcd_putc"%d", var);
2-Yes, it is connected Vdd.
3-I've missed that. thanks I was trying to work with eeprom before that. I forgot to change that variable.
5- Ok as i see now it's working. I can read what i write to 0x09.
I have another added question that I've added later.
Can you also help me about, If I want to change just specific bit of any register, how can I do? As I understand according to the this code, we sent 8 bits every single time. Is it possible to change just 2nd bit of 0x01 register?
PCM Programmer, you are the life saver, thank you so much for your helps. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 18, 2013 5:17 pm |
|
|
Quote: | If I want to change just specific bit of any register, how can I do |
If the register is a R/W register, then read it into a local variable.
Then OR the local variable with the bit you want to set high. If you want
to clear a bit, then AND it with a bitmask, where the bit that you want to
clear is 0, and all the other bits are 1's. Then write the local variable
back to register. If you don't understand the bitwise AND and OR
operations, then you can use the CCS functions, bit_set() and bit_clear()
to change a bit in the local variable. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|