|
|
View previous topic :: View next topic |
Author |
Message |
waqas_shafique
Joined: 06 Feb 2011 Posts: 8 Location: Pakistan
|
software spi problem |
Posted: Sun Feb 06, 2011 10:30 pm |
|
|
I wrote a code in mikroC for SPI based TC72 temperature sensor. The code was working fine, then I converted the code into CCS C code using software spi. Now the problem is that data is being sent to the sensor (Proteus simulation) but there is no response from sensor. My code is Code: |
#include<18f452.h>
#use delay(clock=4M)
void main() {
#use spi(di=PIN_d1, DO=PIN_d2,CLK=PIN_d0,mode=0,BITS=8,BAUD=10000,DIAGNOSTIC=pin_d7,STREAM=SPI)
unsigned char msb_data;
set_tris_b(0b00000000);
output_bit( PIN_d3, 1);//chip enable
spi_xfer(spi,0x80);//Control register address
spi_xfer(spi,0x04); // continious conversion mode ,device turn on
output_bit( PIN_d3, 0);//chip disable
delay_mS(200); // wait for first conversion, 200 milisecond from datasheet
output_bit( PIN_d3, 1); //chip enable
spi_xfer(spi,0x02); //read address
msb_data=spi_xfer(spi,0xff); // read MSB ,0xff is dummy data
output_bit( PIN_d3, 0); //chip disable
output_b(msb_data);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Feb 07, 2011 4:00 am |
|
|
Big problem _clock_.
Proteus is 'thick'. If you tell it that the chip has a 4MHz clock, it does not check that the fuses 'agree' with this.
You are specifying 'clock=4M', but not how this is generated.
Either use:
Code: |
#use delay(crystal=4M)
|
Which will tell the compiler that the 4Mhz involves an external crystal, and it will then should setup the oscillator for this, or 'be explicit':
Code: |
#fuses XT
#use delay(clock=4M)
|
and tell the compiler which oscillator setup to use.
Currently, the compiler will be setting the oscillator fuse to the default (external RC), which won't work....
Best Wishes |
|
|
waqas_shafique
Joined: 06 Feb 2011 Posts: 8 Location: Pakistan
|
|
Posted: Mon Feb 07, 2011 4:38 am |
|
|
Thanks. I tried both of your suggestions to set clock but still response is same. In proteus,the SPI debugger is showing the data from controller but the response from sensor is"??". |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Feb 07, 2011 4:50 am |
|
|
Quote: | In proteus,the SPI debugger is showing the data from controller but the response from sensor is"??". |
Doesn't sound like a compiler related problem. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Feb 07, 2011 5:14 am |
|
|
What is your actual clock?.
The point is that Proteus has no understanding/awareness of your real hardware. Now you say that the "response from the sensor is '??'", but what you post, would only output one byte (and the processor would go to sleep immediately after doing this), so obviously does not match what you are actually running.
You need to start with 'what hardware have I got' - crystal frequency etc., _and then set the compiler options to match this_. Proteus will not mind if these are wrong, but the real chip _will_.
Then do some basic testing. A 'one second' LED flasher, and verify that the flashes really _do_ take one second. Only once you know you have the chip running _and running at the right speed_, move on to testing the SPI.
It is like working on a racing car. When putting it together, you _would_ check that the steering wheel turns in the right direction, and does turn the wheels, and that the brakes do come on, _before_ trying to actually drive it...
Best Wishes |
|
|
waqas_shafique
Joined: 06 Feb 2011 Posts: 8 Location: Pakistan
|
|
Posted: Tue Feb 08, 2011 4:56 am |
|
|
thanks , the problem solved. The problem was that the enabling pin was not set as output pin. |
|
|
|
|
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
|