View previous topic :: View next topic |
Author |
Message |
Kickelle
Joined: 02 May 2016 Posts: 8
|
Magnetic Rotary Encoder AS5048A and SPI, please Help |
Posted: Mon May 02, 2016 11:39 am |
|
|
hello guys,
I am looking for some help after some desperate attempts to make a simple AS5048A to work with a 18F4520 Dev Board from waveshare. Here are the datasheet and schematic:
AS5048A: http://ams.com/eng/content/download/438523/1341157/143015
Board schematic: http://www.waveshare.com/w/upload/d/d2/Open18F4520-16F877A-Schematic.pdf
My CCS Version is 5.015
and I work with MPLAB X IDE v3.26.
Here is the Code:
Code: |
#include <18F4520.h>
#FUSES HS
#FUSES NOWDT //No Watch Dog Timer
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES NOPUT //No Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#use delay(crystal=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
//#use spi(Master,Enable=PIN_A5,ENABLE_ACTIVE=0, BITS=16, DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3, MODE=1, MSB_FIRST)
#use spi(MASTER, SPI1, FORCE_HW, ENABLE=PIN_A5, ENABLE_ACTIVE=0, BITS=16, MODE=1)
int16 result;
void main()
{
while(true){
output_low(PIN_A5); // AS5048 start transfert when CS is pulled low
result=spi_xfer(0xFFFF);
output_high(PIN_A5); // transfert over
result&=0x3FFF; // forcing the 2 last bits to 0. Angle Value is only coded on 14 bits.
printf("%lu ", result); //printing out the value via a RS232 Adapter PL2303
//delay_ms(250);
}
}
|
I am trying to send the angle value of the AS5048 on a Terminal via the PL2303 Serial to USB Converter. Here is what I receive:
Code: |
0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 255 16383 0 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 0 16383 16383 0 0 0 |
I tried Hardware SPI, Software SPI, different set of pins, different SPI configuration, but the result vary from random numbers to Zeros.
I tried another code found in a post of the forum but it's not working either because the problem of the guy hasn’t been solved :
https://ccsinfo.com/forum/viewtopic.php?p=165342
By the way, the following code works perfectly for the AS5048A on Arduino so I have no idea why its equivalent doesn’t work on my PIC:
Code: |
#include "SPI.h"
unsigned int result = 0;
unsigned int result1 = 0;
unsigned int result2 = 0;
void setup ()
{
Serial.begin(115200);
SPI.setDataMode (SPI_MODE1);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV64); //you can chose faster SPI frequency
}
void loop () {
digitalWrite(SS, LOW);
result1 = SPI.transfer(0xff);
result2 = SPI.transfer(0xff);
digitalWrite(SS, HIGH);
result1 &= 0b00111111;
result1 = result1 << 8;
result = (result1 | result2) >> 1;
Serial.print("Result: ");
Serial.print(" = ");
Serial.println(result, DEC);
delay(100);
}
|
I really don't know what to do, the procedure from the datasheet is simple but no way to make it work.
Bye
Last edited by Kickelle on Mon May 02, 2016 12:29 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 02, 2016 11:46 am |
|
|
The AS5048A says the chip select is active low.
Quote: | CSn - Digital input with schmitt trigger
SPI chip select - active low |
But then you tell the compiler that it's active high:
Quote: | #use spi(MASTER, SPI1, FORCE_HW, ENABLE=PIN_A5, ENABLE_ACTIVE=1, BITS=16, MODE=1) |
That's not going to work.
See the AS5048A data sheet (page 4):
http://ams.com/eng/content/download/438523/1341157/143015 |
|
|
Kickelle
Joined: 02 May 2016 Posts: 8
|
|
Posted: Mon May 02, 2016 11:53 am |
|
|
yes sorry about that, it was LOW initially but I tried to put it HIGH just in case. It gives the exact same result (: I just corrected it. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 02, 2016 12:32 pm |
|
|
Your schematic is a generic dev board. Can you post a true schematic
that shows the connections between your PIC and the AS5048A ?
How to post an image on the CCS forum:
Go to this website: http://postimage.org/
Upload your image. Select family safe. Then click the first button for
"Hotlink to Forum" to get a link to the image.
Then go to the CCS forum and type Ctrl-V to paste the link into a post.
Use the Preview button to check how it will look before you press Submit.
If postimage.org doesn't work in your country, then use Google to find
another free image hosting site for forums. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon May 02, 2016 2:41 pm |
|
|
Glaring thing, is the old one. 3.3v logic device trying to drive a PIC SPI input. The PIC SPI input is a Schmitt input. Requires a signal that goes up to 0.8* Vdd. So 4v, on a 5v PIC. The chip is actually a 3.3v device. Though it runs off 5v, it's logic output goes up to Vddcore-0.5v. Vddcore is 3 to 3.6v.....
You need a level translator between the unit and the PIC.
Or use software SPI, on pins with TTL input thresholds. |
|
|
Kickelle
Joined: 02 May 2016 Posts: 8
|
|
Posted: Mon May 02, 2016 3:03 pm |
|
|
Here is the schematic, PCM Programmer. I did it really quickly but we can see the main feature and where the connections are made. I hope it will help.
http://postimg.org/image/grvj1co29/
Yes Indeed Ttelmah, I run it in 3.3V, I'll modify the board to run it in 5V and we'll see how it goes.
thank you guys. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 02, 2016 3:43 pm |
|
|
You have the SDO and SDI lines crossed.
Your schematic shows RC5/SDO connected to pin 3, MISO on the
AS5048A. You have two output pins connected to each other, and the
same thing with the input pins. |
|
|
Kickelle
Joined: 02 May 2016 Posts: 8
|
|
Posted: Mon May 02, 2016 4:10 pm |
|
|
ok.... it was just that. I feel so bad. it works perfectly now, even in 3.3V thank you so much!
but still, on the datasheet (Page 11) it shows MISO to MISO and MOSI to MOSI. Why ? I don't get it....
And wiring it this way with my arduino was fine.... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 02, 2016 4:21 pm |
|
|
Quote: | it works perfectly now |
I'm curious. What data do you get now ? Post a sample of your current output. |
|
|
Kickelle
Joined: 02 May 2016 Posts: 8
|
|
Posted: Mon May 02, 2016 4:38 pm |
|
|
here are the datas I receive on the terminal :
Code: |
2645 2646 2643 2645 2644 2646 2646 2639 2644 2644 2644 2643 2643 2647 2644 2643 2644 2646 2646 2646 2644 2643 2645 2646 2644 2643 2642 2641 2645 2643 2643 2645 2647 2645 2642 2644 2645 2644 2645 2645 2644 2644 2643 2643 2641 2644 2644 2645 2644 2645 2643 2644 2645 2647 2647 2640 2644 2643 2646 2642 2644 2642 2646 2647 2644 2645 2642 2643 2645 2643 2647 2648 2643 2645 2644 2621 1986 1031 876 36 16091 16095 16096 16095 16098 16095 16095 16117 16136 16143 16143 16142 15971 15611 15370 14897 14477 14128 13542 13204 12998 13002 12980 12393 12171 11852 11612 11353 11288 11174 11094 11092 11094 11095 11021 10814 10148 9835 9285 8843 8525 8152 7638 7226 7113 6901 6786 6643 6472 6240 6056 5814 5648 5407 5236 5049 4811 4497 4101 3883 3873 3894 3894 3896 3895 3896 3894 3894 3897 3896 3896 3896 3894 3897 3897 3897 3898 3896 3895 3895 3896 3896 3896 3896 3893 3896 3895 3895 3894 3897 3896 3896 3894 3894 3896 3894 3896 3895 3896 3896 3894 3896 3893 3895 3895 3896 3895 3895 3898 3894 3894 3897 3895 3898 3894 3898 3896 3894 3899 3895 3897 3896 3897 3896 3896 3898 3894 3898 3897 3898 3898 3897 3894 3895 3896 3894 3897 3896 3898 3894 3894 3894 3895 3895 3898
|
You can clearly see when I started to turn the magnet. the datas sent change significantly.
could you enlight me on this SPI problem please ?
Quote: |
but still, on the datasheet (Page 11) it shows MISO to MISO and MOSI to MOSI. Why ? I don't get it....
And wiring it this way with my arduino was fine....
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 02, 2016 5:38 pm |
|
|
Right but the PIC doesn't use that terminology. It uses SDO and SDI.
For the PIC, SDO means Serial Data Out. That pin is outputting data.
An output must go to an input. Therefore it must go to the slave pin that
is called Slave In. So SDO goes to the MOSI pin. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue May 03, 2016 12:34 am |
|
|
You are basically overvoltaging the chip, and getting away with it.....
The maximum specified voltage for VDDcore, is 3.6v.
You are endangering the chip. It is working, but risks failure.
The unit can run off 5v, or 3.3v. Running off 5v, you must leave VDDcore disconnected from the supply, and have a 10uF decoupling capacitor on this. The chip then enables an internal LDO voltage regulator to generate 3.3v for Vdd core.
You are overvoltaging the chip. It is likely to fail in a matter of anything from minutes to days....
Properly connected for 5v operation, the output will not reliably go high enough to drive the PIC SPI input.
You could use the I2C interface instead of SPI, and select the 'SMBUS' option on this. This lowers the input voltage required on the PIC to SMBUS levels (your chip supports this), then these can happily be driven by the 3.3v device.
Read the data sheet. Look at the connection diagrams for 5v and 3.3v operation (you are using the 3.3v one with the chip at 5v....). Look at Voh, Vih etc. for the pins. |
|
|
Kickelle
Joined: 02 May 2016 Posts: 8
|
|
Posted: Tue May 03, 2016 2:33 am |
|
|
Ok PCM Programmer, it was simple after all. Thank you very much!
Ttelmah, my schematic isn't accurate at all, I salvaged the board with the AS5048 already installed on it, I didn't have time to properly extract the schematic out of it so I mostly paid attention to the SPI pins connections. the board, coming from the industry has been designed to work under 3.3V so no problem here. This being said, I'll definitely pay attention to what you wrote when I'll design one for this component myself!
Thank you so much guys for being so reactive, that's one of the reason I chose CCS in the first place, an awesome forum! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue May 03, 2016 2:42 am |
|
|
Problem is that if the SPI input is seeing your signals, it almost certainly implies the chip _is_ running at 5v.
Doing so, _will_ kill it.
The Arduino, has TTL input levels on it's SPI. |
|
|
Kickelle
Joined: 02 May 2016 Posts: 8
|
|
Posted: Tue May 03, 2016 3:26 am |
|
|
I guess I'll have to check that, AS5048 is an awesome little chip, that would be a shame to fry it! Thanks for the info, after some years on Arduino, it's hard to come back on PIC, everything need to be thoroughly checked. Arduino isn't that good after all, it oversimplify everything. |
|
|
|