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

ADC problem with output bridge diode
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
mereach



Joined: 14 Oct 2017
Posts: 13

View user's profile Send private message

ADC problem with output bridge diode
PostPosted: Sat Oct 14, 2017 5:54 am     Reply with quote

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: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 6:08 am     Reply with quote

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: 19553

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 8:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 8:30 am     Reply with quote

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: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 8:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 8:50 am     Reply with quote

Hi Jay , Sorry you have confronted a stupid person Smile 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

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 8:55 am     Reply with quote


_________________
mereach
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 9:18 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 9:32 am     Reply with quote

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: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 9:51 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 10:06 am     Reply with quote

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: 19553

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 10:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 11:00 am     Reply with quote

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: 19553

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 11:07 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 12:42 pm     Reply with quote

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
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