CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

crystal
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
andys



Joined: 23 Oct 2006
Posts: 175

View user's profile Send private message

crystal
PostPosted: Sat Dec 23, 2006 9:16 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Dec 24, 2006 1:05 am     Reply with quote

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

View user's profile Send private message

crystal
PostPosted: Sun Dec 24, 2006 1:24 pm     Reply with quote

18pic4550, crystal :4MHZ
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 24, 2006 1:45 pm     Reply with quote

Do you have a crystal or an oscillator ?

A crystal looks like this:
http://rocky.digikey.com/WebLib/ECS/Web%20Photos/HC-49US.jpg

An oscillator looks like this:
http://rocky.digikey.com/WebLib/CTS/Web%20photos/MXO45.jpg


Note: They can look different from those packages. Both the photos
above show "through hole" packages. Crystals and oscillators
can also be in surface mount packages.

If you can tell for sure if you have a crystal or an oscillator, it will
help me to post the correct sample code.
andys



Joined: 23 Oct 2006
Posts: 175

View user's profile Send private message

crystal
PostPosted: Sun Dec 24, 2006 4:07 pm     Reply with quote

It is a crystal
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 24, 2006 4:51 pm     Reply with quote

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








PostPosted: Sun Dec 24, 2006 7:34 pm     Reply with quote

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








PostPosted: Sun Dec 24, 2006 7:36 pm     Reply with quote

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

View user's profile Send private message

crystal
PostPosted: Thu Dec 28, 2006 2:05 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Dec 28, 2006 2:28 pm     Reply with quote

It controls the conversion time of the A/D.

See section 25.5 in the 18F Reference manual on the A/D:
http://ww1.microchip.com/downloads/en/DeviceDoc/39525.pdf
Guest








PostPosted: Tue Jan 02, 2007 10:24 am     Reply with quote

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







PostPosted: Tue Jan 02, 2007 10:48 am     Reply with quote

#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








PostPosted: Tue Jan 02, 2007 2:59 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jan 02, 2007 3:10 pm     Reply with quote

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








PostPosted: Tue Jan 02, 2007 4:48 pm     Reply with quote

Its Version 3.249, 33336
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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