View previous topic :: View next topic |
Author |
Message |
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Tue Sep 02, 2008 6:59 am |
|
|
Quote: | Code: |
spi_write(controlo);
//delay_ms(50); <<-- you need some delay here to let ADC convert!
RB2=spi_read(0); /*guarda os 1ºs 8 bits*/
delay_ms(25);
RB3=spi_read(0); /*guarda os 2ºs 8 bits*/ // adc recebe o byte respectivo para o pin a ler |
|
|
|
|
madeiras
Joined: 12 Feb 2008 Posts: 14
|
|
Posted: Wed Sep 03, 2008 4:04 am |
|
|
SET wrote: | Quote: | Code: |
spi_write(controlo);
//delay_ms(50); <<-- you need some delay here to let ADC convert!
RB2=spi_read(0); /*guarda os 1ºs 8 bits*/
delay_ms(25);
RB3=spi_read(0); /*guarda os 2ºs 8 bits*/ // adc recebe o byte respectivo para o pin a ler |
|
|
I´ve already tried with different delays, but still don´t work. it´s strange, i´ve already used this code and the same circuit with a ATMEL and it worked! i will try the suggestion of PCM do implement the circuit on that pdf.
in mean while, if someone see and error on my code, please say, tk´s you all! |
|
|
Ttelmah Guest
|
|
Posted: Wed Sep 03, 2008 4:21 am |
|
|
Speed.
PCM Programmer already raised this, and you told him the crystal was 20MHz. What you didn't say was that you were using the PLL, and running at 48MHz....
At 48MHz, you are trying to clock the SPI 50% faster than the chip supports.
You need to drop the SPI clock rate below 500KHz. To get this, you need to change it to using the Timer2/2 clock source, rather than the master oscillator. SPI_CLK_T2. Then set Timer2 to output a frequency not over 1Mhz. So, say the 1:1 prescaler, and division by 48:
setup_timer_2(T2_DIV_BY_1,47,1);
This then makes your SPI rate 'legal'.
Best Wishes |
|
|
madeiras
Joined: 12 Feb 2008 Posts: 14
|
|
Posted: Wed Sep 03, 2008 3:54 pm |
|
|
Thanks to all, I finally put this to work! Thanks for the help!
It was the time of the adc, that wasn't enough to convert. Problem solved. |
|
|
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Thu Sep 04, 2008 3:34 am |
|
|
Its not just a coding issue, its understanding what the devices actually do - thats why it jumped out at me, you were sending the ADC setup/convert command and then (a few tens of nanoseconds later) expecting the ADC result to be there |
|
|
|