View previous topic :: View next topic |
Author |
Message |
andys
Joined: 23 Oct 2006 Posts: 175
|
crystal |
Posted: Sat Dec 23, 2006 9:16 pm |
|
|
I have problems with crystal on the board and i wonder if my code to programming microcontroller is correct (actually i don't know if this line is wrong when the oscilltor is external crystall):
setup_adc(ADC_CLOCK_DIV_2); |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 24, 2006 1:05 am |
|
|
There is a table in the A/D section of your PIC's data sheet that
will tell you the correct divisor to use, based on your crystal frequency.
Post your crystal frequency and your PIC's part number. |
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
crystal |
Posted: Sun Dec 24, 2006 1:24 pm |
|
|
18pic4550, crystal :4MHZ |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
crystal |
Posted: Sun Dec 24, 2006 4:07 pm |
|
|
It is a crystal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 24, 2006 4:51 pm |
|
|
The program below was tested on a PicDem2-Plus board, with a 4 MHz
crystal installed in location Y1. I compiled it with PCH vs. 3.249.
Here is the output in the terminal window, as I turned the trimpot
(on pin A0) from 0 up to the maximum value. This slowly changes
the value on pin A0 from 0 volts to +5 volts.
Code: |
0000
0000
0000
007D
00B5
0103
0154
0155
0155
01CD
0215
026A
02D6
0384
03FC
03FD
03FE
03FE
03FE
03FE
03FF
03FF
|
To find the correct divisor for the setup_adc() function, look in this
section in the 18F4550 data sheet:
TABLE 21-1: TAD vs. DEVICE OPERATING FREQUENCIES
That table shows that for a PIC frequency between 2.87 MHz
and 5.71 MHz, you should use an ADC divisor of 4. For that reason,
I have used the constant ADC_CLOCK_DIV_4 in the code below:
Code: |
#include <18F4550.h>
#device adc=10
#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//============================
void main()
{
int16 result;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_4);
set_adc_channel(0);
while(1)
{
result = read_adc();
printf("%LX \n\r", result);
delay_ms(500);
}
} |
|
|
|
Guest
|
|
Posted: Sun Dec 24, 2006 7:34 pm |
|
|
Dear PCM Programmer,
This is related to the previous question.
SITUATION:
I am using a PIC18F2520 as a Slave. The slave processes its analog inputs and prepares a data packet with 8 values from its 10 bit ADC. And then using I2C it sends it to the Master.
The Master is some other microcontroller that has a forever main loop that keeps querying for the data packet from the slave, and then gets the data packet from the slave. It gets the data packet from the slave constantly at some constant rate that I am not aware of.
Q. I noticed that the I2C slave example uses interrupts. I am worried about a problem. What if the master quarries for the data packet while the slave is preparing the data packet. How can I make the interrupt wait? Initially I was thinking of removing the interrupt and putting the I2C in the main forever loop. But I could not compile the code without using the interrupt. It did not let me use the state command and some others as well.
Is there an easy way to make the interrupt wait? And if we make the interrupt wait then I guess there is no difference than actually having it in a forever loop where it every time checks for query after it prepares the data packet. How do I do this? |
|
|
Guest
|
|
Posted: Sun Dec 24, 2006 7:36 pm |
|
|
OOPSS..
THE ABOVE POST IS ON THE WRONG PAGE.
PLEASE INGNORE IT.
I WILL POST IT ON THE CORRECT PAGE. |
|
|
andys
Joined: 23 Oct 2006 Posts: 175
|
crystal |
Posted: Thu Dec 28, 2006 2:05 pm |
|
|
setup_adc(ADC_CLOCK_DIV_4);
What exacly is the meaning of (Div_4) and why is necessary??? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Guest
|
|
Posted: Tue Jan 02, 2007 10:24 am |
|
|
Dear PCM Programmer,
I tried the code on this page in order to be able to read a 10bit ADC on a PIC18F2520 as a 16 bit variable.
But for some reason I always get 0s in the MSByte.
I am trying to read a 10 bit A/D value as a 16 bit variable.
Any suggestions??
P.S. I am using a PIC18F2520 |
|
|
Ttelmah Guest
|
|
Posted: Tue Jan 02, 2007 10:48 am |
|
|
#device ADC=10
Add this immediately after the first line of your program, that defines the processor.
By default, the ADC wakes up in 8bit mode. ADC=10, switches it to return a 16 bit value, but with the top 6 bits zero. ADC=16, 'left justifies' the value, returning it in the top byte, and top two bits of the second byte.
Remember also, that you must use a _long_ int in CCS C(or an INT16), to hold a 16bit value, or the top bits will be lost.
Best Wishes |
|
|
Guest
|
|
Posted: Tue Jan 02, 2007 2:59 pm |
|
|
I do have adc=10.
Surprisingly, it works fine when I am dealing with float, but refuses to work when I am dealing with 16bit number.
The only diff in the float code is that I am reading it as a float and saving it as a float.
Any suggestions!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 02, 2007 3:10 pm |
|
|
Quote: |
I tried the code on this page in order to be able to read a 10bit ADC
on a PIC18F2520 as a 16 bit variable.
But for some reason I always get 0s in the MSByte.
I am trying to read a 10 bit A/D value as a 16 bit variable.
Any suggestions?? |
Post your compiler version.
The version is a number such as 3.191, 3.249, or 4.018, etc.
You can find it at the top of your .LST file. The .LST file is
in your project directory. |
|
|
Guest
|
|
Posted: Tue Jan 02, 2007 4:48 pm |
|
|
Its Version 3.249, 33336 |
|
|
|