View previous topic :: View next topic |
Author |
Message |
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
ADC and Seven segment |
Posted: Tue Aug 31, 2010 2:03 am |
|
|
Hi,
I'm trying to display ADC value using Pot of 1K on 3 seven segments.
I'm getting values which are recurring like 222 or 555, 000 etc.
I am not able to troubleshoot the exact problem.
My code is as follows.
Code: |
#include <16f877a.h>
#device ICD=TRUE ADC=8
#fuses HS,NOLVP,NOWDT
#use delay (clock=40000000)
#use rs232 (baud= 9600,xmit = PIN_C6)
const char TABLE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0X82,0XF8,0X80,0X98};
void display(int x)
{
int i,j,k,temp; //define four temporary variable
temp=x; //temporary keep AD convert result
i=temp/0x64; //get display hundred bit
j=(temp%0x64)/0xa; //get display ten bit
k=(temp%0x64)%0xa; //get display Entries bit
output_d(TABLE[i]); //get the display hundred bit code from table
output_low(PIN_A3);
delay_ms(10); //delay some time,ensure display brightness
output_d(TABLE[j]); //get the display ten bit code from table
output_low(PIN_A4); //RA4 OUTPUT low,light ten bit display
delay_ms(10); //delay some time,ensure display brightness
output_d(TABLE[k]); //get the display Entries bit code from table
output_low(PIN_A5); //RA5 OUTPUT low,light Entries bit display
delay_ms(10); //delay some time,ensure display brightness
}
void main ()
{
long int z;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
while(1)
{
OUTPUT_LOW(PIN_c0);
set_adc_channel( 0 );
z = read_adc();
display(z);
output_high(PIN_C0);
delay_ms(50);
}
} |
Please let me know where i'm going wrong.
Thanks in advance.[/code] |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Aug 31, 2010 3:56 am |
|
|
I assume you are latching the output for your 7 seg displays :-
Code: |
output_d(TABLE[i]); //get the display hundred bit code from table
output_low(PIN_A3);
delay_ms(10); //delay some time,ensure display brightness
output_d(TABLE[j]); //get the display ten bit code from table
output_low(PIN_A4); //RA4 OUTPUT low,light ten bit display
delay_ms(10); //delay some time,ensure display brightness
output_d(TABLE[k]); //get the display Entries bit code from table
output_low(PIN_A5); //RA5 OUTPUT low,light Entries bit display
delay_ms(10); //delay some time,ensure display brightness
|
using PIN_A3, PIN_A4 and PIN_A5, should you be taking these high then low to latch after you have set the output ?
once set low they curently remain low! |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Tue Aug 31, 2010 6:31 am |
|
|
thanks for your input but now that i have tried the high-low logic. the values are not constant.
like the segments switch on and off....i know its quite embarrassing but the segment displays the value and switches off.
i'm want to keep the values constant unless there is any change in the ADC input.
how do i do that..i tired playing with the delay logic but its not helping much. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Aug 31, 2010 8:19 am |
|
|
I think we need a little more info on your hardware. I am starting to think that you need to constantly update your display as there is no persistance, no latch for each display.
Pins A3, A4 and A5 look like they switch each one on and off either by directly providing power or via a transistor or fet.
If this is the case then you will need a routine to update each display inturn which you will call constantly in your main loop or preferably via an interrupt. You will then need to write your code so you get a decent refresh rate whilst maintaining a suitable brightness.
we need some hardware info.
Have fun
*edit*
I have just realised that this is the case.
You just need to place your lines in the correct place :-
Code: |
output_d(TABLE[i]); //get the display hundred bit code from table
output_low(PIN_A3);
delay_ms(10); //delay some time,ensure display brightness
output_high(PIN_A3);
output_d(TABLE[j]); //get the display ten bit code from table
output_low(PIN_A4); //RA4 OUTPUT low,light ten bit display
delay_ms(10); //delay some time,ensure display brightness
output_high(PIN_A4);
output_d(TABLE[k]); //get the display Entries bit code from table
output_low(PIN_A5); //RA5 OUTPUT low,light Entries bit display
delay_ms(10); //delay some time,ensure display brightness
output_high(PIN_A5);
|
|
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Tue Aug 31, 2010 9:15 am |
|
|
I read your code at a glance:
1) #use delay (clock=40000000) 40Mhz
The maximum Operating Speed in a PIC16F877a is 20Mhz
2) #device ICD=TRUE ADC=8
The resulting AD conversion is stored in a 8 bit variable, you a using:
long int z; and displaying its value.
Humberto |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Tue Aug 31, 2010 11:36 pm |
|
|
Hi,
Thank you all for your reply.
Well I'm using transistor for switching on the segments. I guess I would I have to write the routine for the same.
However, I'm not sure how to begin, and also can anyone tell me if there are other ways of latching the segments? I read about the decoder but I'm not sure if I would be able to work with it.
Please do reply with your suggestions.
|
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Sep 01, 2010 1:53 am |
|
|
Did you read my post and try the code ?
Notice the location on the output_high instructions, AFTER the delay!
What this code now does is:-
Set the output for the first 7 seg display
Turn the first display on
Wait 10 ms (this gives it enough time to go to full brightness)
Turn the first display off.
Repeat this for each display.
Assuming this routine gets called freaquently enough there should be enough persistance in the leds to keep it lit for long enough until you re-enable it. The longer your code takes to call the routine again the more chance there will be flicker.
I would also use a #define for the delay values so you can trim the timing to get the best results. If possible, reduce the delays!
Using 3 x 8 bit latches, one for each display would mean you don't need to keep refreshing the output. Use the A3, A4 and A5 pins to latch the output for each latch (code similar to what I posted but the delays may be reduced). Requires more hardware but you don't need to worry about refresh rates or flicker. |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Wed Sep 01, 2010 3:55 am |
|
|
Thanks Wayne...
I tried decreasing the delay and its working perfectly fine.
Feels so good :D
Thanks alot!!! |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Tue Sep 07, 2010 6:47 am |
|
|
Ok, now when I try with different voltages, i.e with .18v to .94v I get values which are moving extremely fast.
The values are not constant. It was working fine with pot of 1K. :(
thanks
sid |
|
|
|