View previous topic :: View next topic |
Author |
Message |
mereach
Joined: 14 Oct 2017 Posts: 13
|
ADC problem with output bridge diode |
Posted: Sat Oct 14, 2017 5:54 am |
|
|
I am new in CCS C programming language. Is there anyone help me on an issue ? The problem is about ADC application with 16F676 microcontroller.
With using ADC function, I am trying to read effective voltage from out of bridge diode with AN4. If effective voltage is bigger than 3V, C3,C5,A4 ports will be high level. As you know output is always change 0-5V DC with sAN4 port. Program as below, I do not know where is my fault.
Thanks
Code: |
#include <main.h>
#fuses NOWDT, NOPROTECT, NOMCLR
#use delay(internal=4000000)
#use FIXED_IO( A_outputs=PIN_A4 )
#use FIXED_IO( C_outputs=PIN_C5,PIN_C3 )
int16 digital;
float voltage;
void main()
{
set_tris_a(0b00000000);
output_a(0x00);
output_low(pin_c5);
output_low(pin_c3);
output_low(pin_a4);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(sAN4);
delay_us(20);
while(1)
{
set_adc_channel(sAN4);
delay_us(20);
digital=read_adc();
voltage=digital*0.00488281;//5V/1024
output_bit(pin_c3,voltage>3);
output_bit(pin_c5,voltage>3);
output_bit(pin_a4,voltage>3);
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9250 Location: Greensville,Ontario
|
|
Posted: Sat Oct 14, 2017 6:08 am |
|
|
here's a start, there may be more but program cannot be compiled/tested as it is...
1) first line of program MUST contain the PIC processor header.
2) delete the fixed_IO() lines. Let the smart compiler do all this for you.
3) this
setup_adc(ADC_CLOCK_INTERNAL);
is incorrect. Any clock speed>1MHz, consult the ADC section of the PIC for the proper value required.
4) output_bit() does NOT allow a 'conditional', the bit VALUE must be a '1' or a '0' !
when posting 'code', please use the 'code' button (top right of screen) so that it will 'format' on the screen and allow us to copy/test your code. In this case, without a proceessor that can't happen. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Sat Oct 14, 2017 8:10 am |
|
|
Also as the second line (immediately after the processor header file include), it needs:
#device ADC=10
Otherwise it'll only return an 8bit ADC value..... |
|
|
mereach
Joined: 14 Oct 2017 Posts: 13
|
|
Posted: Sat Oct 14, 2017 8:30 am |
|
|
Thank you all , here is my code , it can work in ISIS simulation , just i encounter "ADC conversion period (5e-07) is possibly invalid for device clock frequency" message. But it can not work in real circuit... :(
Code: |
#include <16F676.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOBROWNOUT //No brownout reset
#use delay(internal=4000000)
#use FIXED_IO( A_outputs=PIN_A4 )
#use FIXED_IO( C_outputs=PIN_C5,PIN_C3 )
#define BUZZER PIN_A4
#define SENSOR PIN_C0
#define ROLE PIN_C3
#define ALARMLED PIN_C5
int16 digital;
float voltage;
void main()
{
setup_adc_ports(4);
setup_adc(ADC_CLOCK_INTERNAL);
while(TRUE)
{
set_adc_channel(4);
delay_us(20);
digital=read_adc();
voltage=digital*0.0048828125;//5V/1024
output_bit(pin_c3,voltage>3);
output_bit(pin_c5,voltage>3);
output_bit(pin_a4,voltage>3);
}
} |
_________________ mereach |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9250 Location: Greensville,Ontario
|
|
Posted: Sat Oct 14, 2017 8:38 am |
|
|
Great, NOW we know that ISIS is involved..
SIGH............
Please read the PIC101 sticky up top. ISIS (aka Proteus) is a useless 'simulator' for real PIC designs. I could write a book about why but life's too short.
Here's what to do.
1) cut code for a 1Hz LED program. It'll be 10 lines long. Compile and download your PIC. Power up and confirm LED flashes at a 1Hz rate.
ONLY AFTER this occours, then try your ADC program.
BTW you STILL have the incorrect adc clock in your program.
BTW you STILL have a conditional in the output_bit() function.
This code cannot compile.
I suggest you add the fuse PUT as well.
Jay |
|
|
mereach
Joined: 14 Oct 2017 Posts: 13
|
|
Posted: Sat Oct 14, 2017 8:50 am |
|
|
Hi Jay , Sorry you have confronted a stupid person here below last codes and still there is no light LED on C5
Code: |
#include <16F676.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOBROWNOUT //No brownout reset
#FUSES PUT //No brownout reset
#use delay(internal=4000000)
int16 digital;
float voltage;
void main()
{
setup_adc_ports(4);
setup_adc(ADC_CLOCK_DIV_64);
while(TRUE)
{
set_adc_channel(4);
delay_us(20);
digital=read_adc();
voltage=digital*0.0048828125;//5V/1024
if(voltage>3)
{
output_high(pin_c3);
output_high(pin_c5);
output_high(pin_a4);
}
}
} |
_________________ mereach |
|
|
mereach
Joined: 14 Oct 2017 Posts: 13
|
|
Posted: Sat Oct 14, 2017 8:55 am |
|
|
_________________ mereach |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Sat Oct 14, 2017 9:18 am |
|
|
setup_adc_ports(4);
Wrong......
Code: |
setup_adc_ports(sAN4 | VSS_VDD);
set_adc_channel(4);
|
setup_adc_ports is a function to configure the settings that feed the multiplexer. It needs the correct constants to select the channels that are to be enabled to the multiplexer, and the reference.
set_adc_channel, is then the function to actually select 'which' channel to connect for the reading....
You can also do this once outside the 'while' loop. Once selected the channel remains selected till you change it. |
|
|
mereach
Joined: 14 Oct 2017 Posts: 13
|
|
Posted: Sat Oct 14, 2017 9:32 am |
|
|
Yes, after your code correction, i have not encountered "possibly invalid clock frequency" bla bla... but my circuit can not still work. _________________ mereach |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9250 Location: Greensville,Ontario
|
|
Posted: Sat Oct 14, 2017 9:51 am |
|
|
re:
setup_adc(ADC_CLOCK_DIV_64);
WHY did you choose this option?
According to table 7-1 of the datasheet,that's a shaded value and NOT to be used when running at 4MHz.
Also have you got a 1HZ LED program working with an LED on C5 yet ??
Jay |
|
|
mereach
Joined: 14 Oct 2017 Posts: 13
|
|
Posted: Sat Oct 14, 2017 10:06 am |
|
|
Thank you Jay, one more time you found my fault. I have set it as setup_adc(ADC_CLOCK_DIV_16); but now LED is always blinking ... But i want LED is ON if voltage is above 3V.
I do not have 1HZ LED program.
Additional note is as you can see schematic input supply is 50Hz. _________________ mereach |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Sat Oct 14, 2017 10:32 am |
|
|
Code: |
#define VTOCOUNT(x) (int16)(x/(5/1024.0))
void main()
{
int16 digital, level;
setup_adc_ports(sAN4 | VSS_VDD);
setup_adc(ADC_CLOCK_DIV_8); //This is the best option at 4MHz
set_adc_channel(4);
level=VTOCOUNT(3.0); //integer count for 3.0v
while(TRUE)
{
delay_us(10);
digital=read_adc();
if (digital>level)
{
output_high(PIN_C3); //turn pins on if above 3volts
output_high(PIN_C4);
output_high(PIN_A4);
}
else
{
output_low(PIN_C3); //off if equal or below
output_low(PIN_C4);
output_low(PIN_A4);
}
}
}
|
Now this does potentially mean the LED's may flicker if you have a voltage exactly 'at' 3V.
Everything is done in integer. Stop trying to convert things to float. This is a very slow process and uses a lot of code space. Though I do the conversion 'apparently' using float here, it will be done at compile time not run time, and only once. |
|
|
mereach
Joined: 14 Oct 2017 Posts: 13
|
|
Posted: Sat Oct 14, 2017 11:00 am |
|
|
I tried... Same result. i will check again. i can not prefer to assume everything is integer because i should adjust it as 2.5 or 3.5 etc. _________________ mereach |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Sat Oct 14, 2017 11:07 am |
|
|
It is flickering, because it is now tracking the real voltage.....
The output of the bridge, goes to zero every half cycle, and peaks at 1.4* the RMS voltage.
You need a capacitor either across R1, or at the top of R2 on the bridge voltage to smooth it to DC.
Now my conversion macro would allow you to put in another value like 2.5 or 3.5. Key is that you would only use this when the voltage changes, so it is not used in the actual comparison loop.
Look at figure3 here:
<http://www.bristolwatch.com/ele/basic_ac_rectification.htm>
This is what you have.... |
|
|
mereach
Joined: 14 Oct 2017 Posts: 13
|
|
Posted: Sat Oct 14, 2017 12:42 pm |
|
|
Do you think we can build any other solution without capacitor ?
For example, if we take random sample then calculate average of voltage...
I have not heard before and could not find any other example about vtocount(X) ... Is it a function ? _________________ mereach |
|
|
|