 |
 |
| View previous topic :: View next topic |
| Author |
Message |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Tue Sep 08, 2009 2:00 am |
|
|
I found one problem with the program. Although it compiles correctly, it doesn't seem to ever execute the accel_reg_read function. I placed a blinking LED code in the accel_reg_read with a fast blink, and accel_reg_write with a slow blink, and the led only blinks slowly indicating that the accel_reg_read function is never run. It's an int8 so I can't address it with a 'goto' command. I can't add it to the main function because that's nesting which you said and the compiler confirmed isn't allowed. I can't change it to a void function because it says 'error: return not allowed in void function'.. How else would I tell it to run the accel_reg_read function?
| Code: | #include "16f684.h"
#use delay(clock=20000000)
#Fuses FCMEN,RC,PROTECT,MCLR,BROWNOUT,CPD,WDT,NOPUT,IESO,NOPROTECT
#use SPI(DI=PIN_A0, DO=PIN_A1, CLK=PIN_A2, MODE=3)
int8 time;
int8 state;
void accel_reg_write(int8 addr, int8 data)
{
output_high(PIN_C0); //LED1
delay_ms(300);
output_low(PIN_C0); //LED1
delay_ms(300);
output_low(PIN_C1);
spi_xfer(addr);
spi_xfer(data);
output_high(PIN_C1);
state=1;
}
int8 accel_reg_read(int8 addr)
{
int8 ret;
output_low(pin_c1);
spi_xfer(addr|0x80);
ret = spi_xfer(0);
output_high(pin_c1);
return ret;
time=ret;// + 100);
output_high(PIN_C3); //LED2
delay_ms(20);
output_low(PIN_C3); //LED2
delay_ms(20);
state=0;
}
void main()
{
output_low(PIN_C0); //LED1
delay_ms(time);
accel_reg_write(0x2D, accel_reg_read(0x2D) | 0x08);
} |
|
|
 |
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Tue Sep 08, 2009 2:20 am |
|
|
The LED won't blink for accel_reg_read because the "return ret" line exits the function before then.
I'm not trying to be offensive here, but I think you should find a book on C somewhere, get a free compiler for the PC and have a bit of a go there, before trying microcontrollers. Debugging on the PC is a lot easier than microcontrollers. _________________ Andrew |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Sep 08, 2009 2:31 am |
|
|
I don't understand why you seem to know what you were doing with your first main routine but then you just completely dropped it and then didn't seem to have a clue about programming in C.
I can understand andrewg frustration at this.
Even your comment about using a goto to call a void function, no idea where you got that from!
Anyway, the reason it is not working is that you have omitted the while loop. The pic will curently run through your main code once, first calling accel_reg_read and then accel_reg_write and as it now drops off the bottom it goes to sleep because the compiler inserts a sleep instruction at the end of your code, this will (depending on the clock mode) either leave theIO lines in the curent state or turn them off and then the pic just stops (sleeps), indefinately.
So your LEDs should flicker when you first turn it on but then do nothing.
now as I mentioned, your first main routine had a while loop! |
|
 |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Wed Sep 09, 2009 12:25 am |
|
|
I apoligize for my inconsistent knowledge, I still am new to C for microcontrollers, though I've programmed in C++ and similar languages before. I was having an off day when I made the last post. Thank you andrewg and Wayne, the Return being before the LED blink code was obvious, and I did forget to re-insert the while(1) command in the main function. Both things I should've noticed right away. I was unaware the PICC compiler could compile programs to run on a PC. I'll certainly give that a go and sharpen my C skills.
| Wayne_ wrote: |
Even your comment about using a goto to call a void function, no idea where you got that from!
|
I should explain this. I didn't literally mean I was going to use a 'goto'. That was more a figure of speech. I meant to express that if indeed the program wasn't running that bit of code, I was unsure how to make it do so. I didn't know if there was some function I was missing. Something to make it 'goto' that area of code within the program. My C is a little rusty, but for the most part I only don't understand the hardware specific functions right away. I really do appreciate all of your help, and my brain is a lot more clear and focused now. I just have one more question relating to reading registers through the SPI if you don't mind...
You gave me that bit of code that sets the accelerometer to measure mode, and makes it ready for use. I'm just wondering if the following code is correct for reading an axis's value from a register?
| Code: |
int8 x;
void Xwrite (int8 addr)
{
output_low(PIN_C1);
spi_xfer(addr);
output_high(PIN_C1);
}
int8 Xread (int8 addr)
{
output_low(pin_c1);
spi_xfer(addr|0x80);
x = spi_xfer(0);
output_high(pin_c1);
return x;
}
void main()
{
while(1)
{
Xwrite(0x33, Xread(0x33) | 0x08); //0x33 = Xaxis data 1
}
}
|
It's basically the same as your provided code, except I changed the registers that it addresses. The compiler gives me an error "expecting close parenthases" on the main function Xwrite command, and it highlights "Xread".
Other than that, the rest of the code you provided works perfectly. My boss is expecting a working SPI accelerometer so your help is much greater than you know. I was a PICBASIC programmer less than a month ago, and he expects me to just know these things. |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Sep 09, 2009 2:02 am |
|
|
Your version of void Xwrite (int8 addr) only takes 1 parameter. Because the compiler knows this it thinks there should be a close bracket ) after the first parameter in the function call!
You need to fix your void Xwrite (int8 addr) routine to accept a data value as well and write that to the spi bus. As it stands it is pointless to just write the address out!
You have to be careful when re-writing code not to make silly mistakes like this. |
|
 |
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Wed Sep 09, 2009 7:44 am |
|
|
Once you've fixed the read code as mentioned by Wayne_, then your code looks OK. As mentioned in the datasheet, you could consider using a multi-byte read to read all the relevant registers. The data sheet doesn't give an example, but I imagine you write the address byte out with the MB bit set, then use spi_xfer to read all six registers in sequence. You might need to experiment. _________________ Andrew |
|
 |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Sat Sep 12, 2009 9:14 pm |
|
|
I think my code is still a little off, I'm getting a very strange result in the circuit with this code. The areas I'm not sure about are the main function, as well as the Xread and Xwrite sections.
When I power up the circuit, the LED blinks as it should, but the accelerometer doesn't detect tilt. Instead, as my hand gets closer to the accelerometer, the servo begins to wig out, turning back and forth in 15ms intervals. As my hand gets further from it, the servo does it less. This accelerometer seems to be detecting proximity. The servo reverts to it's starting position when I take my hand away, which is a 150us pulse.
If it's not something wrong with the code, then I might assume the accelerometer went bad somehow.
| Code: |
#include "16f684.h"
#use delay(clock=20000000)
#Fuses FCMEN,RC,PROTECT,MCLR,BROWNOUT,CPD,WDT,NOPUT,IESO,NOPROTECT
#use SPI(DI=PIN_A0, DO=PIN_A1, CLK=PIN_A2, MODE=3)
int16 time;
int8 servo;
int8 state;
short doonce; //used to set into measure mode one time at power up
void servo()
{
//the following prevent the servo from over-turning and breaking itself
if (time<100)
{
time=150;
}
if (time>500)
{
time=450;
}
if (state < 100)
{
output_high(pin_c3); //servo
delay_us(time);
output_low(pin_c3); //servo
delay_ms(15);
state++;
}
else if (state >= 100)
{
output_low(pin_c3); //servo
state=0;
}
}
void timewrite (int8 addr, int8 data)
{
output_low(PIN_C1);
spi_xfer(addr);
spi_xfer(data);
output_high(PIN_C1);
}
int8 timeread (int8 addr)
{
output_low(pin_c1);
spi_xfer(addr|0x08);
time = spi_xfer(0);
output_high(pin_c1);
servo();
return time;
}
void accel_reg_write(int8 addr, int8 data)
{
if (doonce==0)
{
output_low(PIN_C1);
spi_xfer(addr);
spi_xfer(data);
output_high(PIN_C1);
}
}
int8 accel_reg_read(int8 addr)
{
if (doonce==0)
{
int8 ret;
output_low(pin_c1);
spi_xfer(addr|0x80);
ret = spi_xfer(0);
output_high(pin_c1);
doonce=1;
return ret;
}
}
void main()
{
while(1)
{
output_high(PIN_C0); //LED
delay_us(time);
output_low(PIN_C0); //LED
delay_us(time);
accel_reg_write(0x2D, accel_reg_read(0x2D) | 0x08); //do one time
timewrite(0x33, timeread(0x33) | 0x08); //retrieve data from Xaxis register?
}
}
//X, Y, Z registers = 8 bit
//X 0x32 DATA0, ox33 DATA1
//Y 0x34 DATA0, 0x35 DATA1
//Z 0x36 DATA0, 0x37 DATA1
|
|
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Sep 12, 2009 10:09 pm |
|
|
| Quote: | #include "16f684.h"
#use delay(clock=20000000)
#Fuses FCMEN,RC,PROTECT,MCLR,BROWNOUT,CPD,WDT,NOPUT,IESO,NOPROTECT
|
The 16F684 data sheet says the RC oscillator uses an external R-C
circuit, and that the maximum frequency is 4 MHz, not 20 MHz.
| Quote: |
5. RC – External Resistor-Capacitor (RC) with FOSC/4 output on RA4.
Oscillator Frequency:
DC — 4 MHz RC Oscillator mode
|
| Quote: | | #Fuses FCMEN,RC,PROTECT,MCLR,BROWNOUT,CPD,WDT,NOPUT,IESO,NOPROTECT |
The watchdog timer is enabled in the fuses, but there are no restart_wdt()
statements in the program.
The conclusion has to be that you are not running this on real hardware.
Or, you are changing the fuses in your programmer before you program
the PIC. |
|
 |
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Sat Sep 12, 2009 11:14 pm |
|
|
Apart from the things PCM Programmer pointed out, also check any unused pins on the PIC. Unused pins are inputs by default. They should either be tied high or low using a resistor (eg 100K), or set to a high or low state in your code. Pins left floating can cause all sorts of weird problems. _________________ Andrew |
|
 |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Sun Sep 13, 2009 12:41 am |
|
|
Good to know andrewg. I'll make sure to set the unused pins to low.
I will also remove the WDT fuse because I'm not even going to use the watchdog timer, and I'll fix the RC oscillator, though I'm not using any external circuitry and the delays seem to be working just fine. I am running this all on real hardware, I have a PIC connected to the mentioned components, powered by a 9V supply. The accelerometer is being powered by I think 3-4 volts.
I mainly wasn't sure if the timewrite "timewrite(0x33, timeread(0x33) | 0x08);" function was accurate, I know 0x33 is the register holding the X axis data1 value, but I wasn't sure I changed andrews code correctly, and I'm assuming data1 is the register I want to read, instead of data0 (0x32).. |
|
 |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Tue Sep 15, 2009 1:23 am |
|
|
I finally figured out the strange problem I was having. My PIC was bad! I just replaced it and learned a bit more about SPI. I hope one of you can tell me if this looks correct, though it's not working...
| Code: |
int8 time;
int8 timeread (int8 addr)
{
output_low(pin_c1); //begin
spi_xfer(addr);
time=spi_xfer(addr,time); //I believe this will read the contents of register 0x33, and save them into "time"??
output_high(pin_c1); //end
servo();
return 0;
}
void main();
while(1);
{
timeread(0x33);
}
|
If this is correct, then I seem to finally understand how SPI communication works. |
|
 |
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Tue Sep 15, 2009 8:02 am |
|
|
You should re-read the manual entry for spi_xfer. The two-parameter call is always the stream ID first, data second. "addr" is not a stream ID. Just transfer a zero like my function did. The chip ignores it. _________________ Andrew |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Sep 15, 2009 8:33 am |
|
|
Edited: Ignore, I totally messed up on this one
Why does your code keep changing from what looks ok to bad each time you post ?
Good but with a mistake! a previous post
| Code: |
int8 timeread (int8 addr)
{
output_low(pin_c1);
// Wrong spi_xfer(addr|0x80); // Send address with read bit set use 0x80 NOT 0x08
spi_xfer((addr << 1)|0x01); // Send address with read bit set use
time = spi_xfer(0); // Send dummy byte to clock out register value
output_high(pin_c1);
servo();
return time;
}
|
BAD your last post ! Just scrap it!
| Code: |
output_low(pin_c1); //begin
spi_xfer(addr);
time=spi_xfer(addr,time); //I believe this will read the contents of register 0x33, and save them into "time"??
output_high(pin_c1); //end
|
Edit: The address bit is wrong. I don't know why I got this wrong other than viewing other code on here as I am working on SPI and I2C as we speak.
The read/write bit is the lowest bit, 1 for a read 0 for a write.
Depending on the SDO/ALT pin of your device the address is either
if SDO/ALT is high
The address is 0x1D
Which translates to an addr value of 0x3A for a write and 0x3B for a read
if SDO/ALT is low
The address is 0x53
Which translates to an addr value of 0xA6 for a write and 0xA7 for a read
You can do the calc with
(addr << 1) | rw;
Last edited by Wayne_ on Wed Sep 16, 2009 2:50 am; edited 2 times in total |
|
 |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Sep 15, 2009 8:51 am |
|
|
Editted: Ignore this, it is I2C related!
You main routine is also wrong.
| Code: |
accel_reg_write(0x2D, accel_reg_read(0x2D) | 0x08); //do one time
timewrite(0x33, timeread(0x33) | 0x08); //retrieve data from Xaxis register?
|
The way your code and device works you have to write the address and register number then write the address and read the register value
| Code: |
addr = 0x1D; // or 0x53 if SDO/ALT pin low
accel_reg_write(addr, 0x2D);
val = accel_reg_(addr);
timewrite(addr, 0x33);
time = timeread(addr);
|
you need to change your spi_xfer read calls to
| Code: |
spi_xfer((addr << 1) | 0x01);
|
Last edited by Wayne_ on Wed Sep 16, 2009 2:49 am; edited 1 time in total |
|
 |
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Tue Sep 15, 2009 9:09 pm |
|
|
My code keeps changing because I keep learning new things, and most recently I read a user-uploaded tutorial that was wrong. The guy said that the SPI_XFER function could be used in this form: spi_xfer(writebit, readbit) it looked a little easier to understand, so I tried it. It was incorrect.
My problem is just understanding how the function spi_xfer can be used to read and write. I'm trying my hardest to figure out what is going on in every step of the code you both have provided.
After reading the data sheet over and over, and every SPI tutorial I could find on the internet, I understand how SPI works, and what needs to happen. I'm just having trouble understanding the specific spi function and how it determines whether information is to be sent, recieved, or used as an address. I've been trying to make it all as simple in my head as possible, making notes along the way.
spi_xfer(0x01); //sends 0x01 or '1' over the spi
result=spi_xfer(0xXX); //result = the value of register 0xXX
Now, how would I say 'transfer 0x01 to register 0xXX?
These are the two forms that I understand, but when the rest is added, it confuses me:
spi_xfer((0xXX << 1) | 0x01);
I see that it's transferring to 0xXX via SPI in the first set of parentheses, but how does the SPI know if it's information to write into a bit, or the address of a register, or the address of a bit within a register? In the data sheet, it lists measure mode as bit D3 in register 0x2D, and it must be set to 1. This is where I get confused because it seems this should be achieved in one command such as:
spi_xfer("0x01"(0xD3,0x2D))
which would transfer "1" to bit D3 in register 2D, in a perfect world.
In your code, I don't see how you came up with 0x1D, or 0x53:
| Code: | | "addr = 0x1D; // or 0x53 if SDO/ALT pin low" |
|
|
 |
|
|
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
|