I've got an Analog to Digital convertor so I can use my Sharp IR-sensor.
When I use an Arduino, the sensor works perfectly but the Analog to Digital convertor is giving me some problems.
I use the SPI library XMOS provides (https://www.xmos.com/applications/module/spi?ver=all).
First I enable the ADC by setting the CS pin high (as SPI requires) followed by the command to select Channel 0 on the ADC (it has 4). Then I read a short number that is shifted 4 bits to the right. (The ADC first sends 12 bits with the MSB first and then those 12 bits again with the LSB first. The 4 last bits are thus obsolete)
This is the information about the ADC, pages 19-20. http://ww1.microchip.com/downloads/en/D ... 21298e.pdf
It also gives some hints on how to use it when the microcontroller only allows blocks of 8 bit. I saw in the code that the port has an 8 bit buffer so I tried that method. It is specified on page 21.
The data always is 3112, 3128, 3140 or 3156. This doesn't seem right to me. Bringing an object in front of the sensor also has no inluence on the output. There is no pattern in the appearance of the 4 values, they are in random order.
This is my code:
Code: Select all
out port CS = XS1_PORT_1A; // 1 0 sturen voor activeren en 1 voor deactiveren
spi_master_interface spi_if = {
XS1_CLKBLK_1,
XS1_CLKBLK_2,
XS1_PORT_1C,
XS1_PORT_1D,
XS1_PORT_1B};
int main() {
spi_init(spi_if, 8);
CS <: 1;
while(1){
CS <: 0;
spi_out_byte(spi_if, 0b00000110);
spi_out_byte(spi_if, 0b00000000);
printintln(spi_in_short(spi_if));
CS <: 1;
}
return 0;
}
I have completely no idea what is wrong. I guess something with activating the right channel.
These things are quite popular, especially the IR-sensor so I hope someone can help me...
Thanks!
EDIT:
I've made a screenshot of the waveform analyzer in the simulator. It doesn't show the part I need, the data that is sent from the ADC but the signals I send look correct to me. They correspond to the drawings in the datasheet.