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

A/D Problem with PIC18F2431

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
moistsam



Joined: 09 Jun 2007
Posts: 7
Location: Thessaloniki, Greece

View user's profile Send private message Send e-mail

A/D Problem with PIC18F2431
PostPosted: Fri Jun 22, 2007 12:04 pm     Reply with quote

I write the simple code below using CCS 3.249. I am trying to vary the duration of a pulse with the Potentiometer (PWM) but this code doesn't work with PIC18F2431. I tested this code with 16F877 and 16F876 and works fine.
The problem is in the analog channel because the variable value doesn't change when i display it in an LCD. I try but i couldn't find the reason why it happens.
Code:

#include <18f2431.h>
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOPUT,NOCPD,NOBROWNOUT,NOLVP
#use delay(clock=20000000)

void main()
{
   int16 value;
   
   setup_ccp1(CCP_PWM);
   setup_timer_2(T2_DIV_BY_4, 63, 1);

   setup_adc_ports(sAN1|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(1);
   delay_us(20);

   while(1)
   {
      value=read_adc();
      set_pwm1_duty(value);
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 22, 2007 12:15 pm     Reply with quote

Change the #device adc line to specify 8-bit mode.
moistsam



Joined: 09 Jun 2007
Posts: 7
Location: Thessaloniki, Greece

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 22, 2007 12:23 pm     Reply with quote

I made the change but the problem still exist. I see in the oscilloscope the pulse with its maximum duty cycle but i can not change it.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 22, 2007 12:46 pm     Reply with quote

I don't have a 18F2431, but I do have a 18F4431 (a similar chip) and I
was able to make it work. I installed PCH vs. 3.249. I changed only
one line in your program -- I changed it to provide an 8-bit ADC result:
Code:
#device adc=8


I tested it on a PicDem2-Plus board. It has a 10K trimpot on pin A0,
so I jumpered it over to pin A1, to fit your program.

When I run the program, I get a 19.5 KHz PWM waveform on pin C2.
The duty cycle varies from 0% to 100% as I turn the trimpot from one
side to the other.
moistsam



Joined: 09 Jun 2007
Posts: 7
Location: Thessaloniki, Greece

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 22, 2007 2:00 pm     Reply with quote

I have three PIC18F2431 and i tested all of them but they don't work. They are samples from microchip. Is there any possibility for these samples to have a problem at the analog channels? I write another code which is a simple 4-bit counter and works fine. The only problem is the analog channels!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 22, 2007 2:17 pm     Reply with quote

1. What device is supplying the analog voltage to your PIC ?
Try using a 10K trimpot.

2. What is the range of the analog voltage ?
Is it from 0v to 5v ? Measure it with a voltmeter or a scope.

3. Are you certain that you have connected the analog voltage to the
correct pin on your PIC ? On the DIP package, AN1 is on Pin 3.
moistsam



Joined: 09 Jun 2007
Posts: 7
Location: Thessaloniki, Greece

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 22, 2007 2:29 pm     Reply with quote

Just now i checked the things that you have mentioned but the problem exist. I also have the PICDEM2 PLUS and ICD2 Programmer. I use a 10k potentiometer and i use the PICDEMs power supply. I also tested my code with the 18F4550 to see if something is going on with the 18F series but all is ok. I thing that something happens with this specific microcontrollers.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 22, 2007 2:54 pm     Reply with quote

Quote:
I also have the PICDEM2 PLUS and ICD2 Programmer. I use a 10k potentiometer and i use the PICDEMs power supply.

I made a mistake in the trimpot value. The PicDem2-Plus board
(both versions) has a 5K trimpot.

But on that board, the built-in 5K trimpot is connected to pin RA0.
Are you using that trimpot for your tests ?

You should use it. Change your code to use A/D channel 0,
as shown in bold below.
Quote:
setup_adc_ports(sAN0 | VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
moistsam



Joined: 09 Jun 2007
Posts: 7
Location: Thessaloniki, Greece

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 22, 2007 3:06 pm     Reply with quote

Yes it is a 5k pot. I use an external 10k pot with A1. I don't think that this is the problem. I checked all the connections. As i said in the first post in the 28pin package i checked the 16F876 microcontroller and worked fine.
moistsam



Joined: 09 Jun 2007
Posts: 7
Location: Thessaloniki, Greece

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 22, 2007 3:48 pm     Reply with quote

PCM programmer thank you for your help and your time. Your help was very valuable. I will find another 18F2431 and test it again. Thanks a lot.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 22, 2007 3:50 pm     Reply with quote

I found the problem. The PicDem2-Plus board's 28-pin socket is not
intended to work with the pinout of the 18F2431. It's only intended
to be used with a PIC such as the 16F876.

The 18F2431 needs pin 7 (DIP package) to be connected to the AVdd
voltage (+5v). On the PicDem2-Plus board, this pin is "RA5" and is
not connected to anything except the PIC sockets and the header pins.

You could jumper it to +5v. But remember to remove that jumper
when you are done testing the 18F2431.
moistsam



Joined: 09 Jun 2007
Posts: 7
Location: Thessaloniki, Greece

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 22, 2007 4:07 pm     Reply with quote

Now it works!!!! Thank you again for your help. In this package i used to work only with 16F876 and my mind didn't go to this problem.
Thanks for your time!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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