|
|
View previous topic :: View next topic |
Author |
Message |
mukeshp11
Joined: 10 Dec 2008 Posts: 15
|
ADC not working on PIC 16F690 |
Posted: Tue Oct 04, 2011 11:12 am |
|
|
Code: | void main()
{
set_tris_b(0x70);
set_tris_a(0x04);
setup_timer_2(T2_DIV_BY_1, 255, 1);
clear_interrupt(INT_TIMER2);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
setup_adc_ports(sAN2 | VSS_VDD );
setup_adc(ADC_CLOCK_DIV_8 );
set_adc_channel(2);
delay_us(20);
while(1)
{
adc_value = read_adc();
delay_us(10);
duty_cycle = (unsigned char)((float) (adc_value * 255) / 1023.0);
set_pwm1_duty(duty_cycle);
}
}
|
I guess the above code is proper but don't know why its not working. My microcontroller is PIC 16F690.
In the above code my ADC is not working I am giving channel 2 some DC volts from function generator and watching the results. But am not getting any result.
I mean read_adc function don't work.
Kindly help in this regard.
Regards
Mukesh |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9240 Location: Greensville,Ontario
|
|
Posted: Tue Oct 04, 2011 1:44 pm |
|
|
Before you say 'adc don't work' try a very,very simple program of reading the ADC and displaying the result either to a local LCD or send to a PC terminal program or send to an 8 bit port that has LEDs attached.
You might also get rid of the set tris....lines . If you misconfigure the ports DDR you'll not get the results you expect.
Also you might want to 'play computer' and see what results you get with the pwm calculation you've entered.
Also you've enabled an ISR without appropriate handler....unless you've not shown us the whole program
Also no #fuses line......
So this isn't a complete program and cannot be downloaded and tested by anyone here... |
|
|
mukeshp11
Joined: 10 Dec 2008 Posts: 15
|
ADC NOT WORKING ON PIC 16F690 |
Posted: Tue Oct 04, 2011 9:55 pm |
|
|
hHi temtronic,
Thanks for your reply, Below shown is the whole code. As am not having LCD configured I am putting the adc value in pwm duty and checking the pwm waveforms according to the adc input.
I have configured channel 10 for adc input, the variable read_adc gives value which is used for pwm.
But adc part is not working out for me.
Kindly have a look at the code below and suggest.
Rgds,
Mukesh
Code: |
#if defined(__PCM__) // Preprocessor directive that chooses the compiler
#include <16f690.h> // Preprocessor directive that selects the chip
#device adc=10
#fuses HS, NOWDT, NOPROTECT
#use delay(clock=10000000)
//#include <STDLIB.h>
#endif
#include <math.h>
#include <STDLIB.h>
//pwm pins
#define P1A PIN_C5
#define P1B PIN_C4
#define P1C PIN_C3
#define P1D PIN_C2
//global variables
unsigned int duty_cycle=0;
int8 adc_value;
void pulse_steering(void);
void main()
{
set_tris_c(0x00);
set_tris_b(0x70);
set_tris_a(0x04);
setup_timer_2(T2_DIV_BY_1, 255, 1); //19.53 khz switching frequency =51us sampling time
clear_interrupt(INT_TIMER2);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL); ////enable_interrupts(GLOBAL);
output_low(P1A);
output_low(P1B);
output_low(P1C);
output_low(P1D);
output_high(PIN_C1);
setup_adc_ports(sAN10 | VSS_VDD );
setup_adc(ADC_CLOCK_DIV_8 );
set_adc_channel(10);
delay_us(20);
while(1)
{
adc_value = read_adc();
duty_cycle = (unsigned char)((float) (adc_value * 255) / 1023.0);
}
}
#int_timer2 //timer 2 over flow
void timer2_isr(void)
{
set_pwm1_duty(duty_cycle);
pulse_steering();
}
void pulse_steering()
{
if(input(PIN_B6) && input(PIN_B5))
{
setup_CCP1(CCP_PWM_H_H | CCP_PULSE_STEERING_A);
output_high(P1D);
output_low(P1C);
output_high(P1B);
}
else if(!input(PIN_B6) && !input(PIN_B5))
{
setup_CCP1(CCP_PWM_H_H | CCP_PULSE_STEERING_B);
output_low(P1A);
output_high(P1D);
output_high(P1C);
}
else if(!input(PIN_B6) && input(PIN_B5))
{
setup_CCP1(CCP_PWM_H_H | CCP_PULSE_STEERING_C);
output_low(P1A);
output_high(P1D);
output_high(P1B);
}
else if(input(PIN_B6) && !input(PIN_B5))
{
setup_CCP1(CCP_PWM_H_H | CCP_PULSE_STEERING_D);
output_low(P1A);
output_high(P1C);
output_high(P1B);
}
else
{
output_low(P1D);
output_low(P1C);
output_low(P1B);
output_low(P1A);
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Oct 05, 2011 1:40 am |
|
|
You have adc_value declared as an int8.
You have the adc setup to return a 10bit value (won't fit)...
You then take the adc value that has been trimmed down to 8bits, and multiply by 255, still using 8bit arithmetic, so get a result from 0 to 255, convert this to a 'float', and divide by 1023. So at this point you have a value from 0 to 0.249. Convert this to a char (giving 0 to 0), and set the duty cycle with this.
The result will always be 0....
Seriously, none of the fiddling is needed. declare adc_value as an int_16, read this, and set the pwm duty cycle with this value.
You need to understand variable size limits, and when a cast applies.
Best Wishes |
|
|
|
|
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
|