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 support@ccsinfo.com

problem with adc using PIC16F876A

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







problem with adc using PIC16F876A
PostPosted: Thu Sep 28, 2006 11:59 am     Reply with quote

Hi. I know this is the second post I've posted on this topic, but I'm not quite sure where this post belong. Maybe I have more luck here.

I'm having a huge problem. I'm using the code below on an ICD2 to read a value from the analog input via the adc and converting it to pwm. The code works fine on the IDC2 but for some reason it will not work on my self-built board. Not working means CCP1 stays low. I'm assuming the software is fine and this is a hardware problem. I'm using a 50k pot connected to 5V and Gnd only to input on A0. The voltage at A0 varies between 0V and 5V as the pot is adjusted, but why is there no output on CCP1? Please help. I'm going crazy!!

#include "16F876A.h"
#use delay(clock = 4000000)

unsigned int16 duty = 0;
unsigned int16 analoogWaarde;

main()
{
// Pin setup
set_tris_A(1) ;
// Port A as input
set_tris_C(0x0) ;

// ADC Setup
setup_adc_ports(RA0_ANALOG); // Set RA0 as analog
setup_adc(ADC_CLOCK_INTERNAL );
set_adc_channel(0);

// PWM Setup
setup_ccp1(CCP_PWM); // Configure CCP1 as PWM
setup_timer_2(T2_DIV_BY_16, 77, 1); // Timer 2 for 800Hz
set_pwm1_duty(duty); // Duty Cycle


while(1)
{
analogValue = read_adc() ; duty = analogValue ; set_pwm1_duty(duty); }


}
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Thu Sep 28, 2006 12:01 pm     Reply with quote

#fuses statement?

Are you certain you removed all the debug enabling bits from the fuse statement and from the #use statements?

What is your clock source, is your clock running?
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Guest








PostPosted: Thu Sep 28, 2006 12:14 pm     Reply with quote

I've disabled and enabled all nessesary fuses in the configuration bits setting in MPLAB. The settings are as followed:
Oscilator = RC
Watchdog Timer = ON (should this be off?)
Brown Out Detect = ON (shoulf this be off?)
Power up timer = OFF
Low Voltage Program = Enabled
Flash Program Write - Write Protection Off
Data EE Read Protect = OFF
Code Protect = OFF

I'm using a 4MHz crystal. I'm assuming it works because I programmed a port to complement after a few seconds and by connecting a LED to the port it blinked.
Rikus
Guest







PostPosted: Thu Sep 28, 2006 12:25 pm     Reply with quote

Thanx for your help. I found the problem. The firts time (testing the blinking LED) the configuration bits settings were correct, but the secund (third, forth, etc) time the settings were changed for some reason. After changing it to the original settings, the adc worked fine.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 28, 2006 4:53 pm     Reply with quote

Quote:
The settings are as followed:
Oscillator = RC
Watchdog Timer = ON (should this be off?)
Brown Out Detect = ON (shoulf this be off?)
Power up timer = OFF
Low Voltage Program = Enabled
Flash Program Write - Write Protection Off
Data EE Read Protect = OFF
Code Protect = OFF

I'm using a 4MHz crystal. I'm assuming it works because I
programmed a port to complement after a few seconds and by
connecting a LED to the port it blinked.

You have several incorrect fuse settings. Your oscillator fuse is wrong.
You don't need Watchdog timer enabled during development.
You shouldn't have Low Voltage Programming enabled. (unless you
are using it, which is very uncommon).

Also, it's important to put the #fuses statement in your program.
That way, you won't have to remember and manually set the fuses
in MPLAB every time. They are right there, in the file, and get put
into the HEX file when the program is compiled. Also, the program
is self-documenting with respect to the fuses, when you have a #fuses
statement. Try these settings:
Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
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