View previous topic :: View next topic |
Author |
Message |
john121
Joined: 14 Aug 2011 Posts: 1 Location: Rwanda
|
adc and seven segment display with PIC16F877A |
Posted: Sun Aug 14, 2011 2:27 am |
|
|
Code: | #include<16f877a.h>
#fuses HS,NOWDT
#use delay(clock=20000000)
#include<stdio.h>
#include<STDLIB.H>
#use standard_io(A)
#use standard_io(B)
#use standard_io(D)
#define PORTB
#DEFINE PORTC
#define PORTA
const char digit[10]={0b1111001,0b0100100,0b0110000,0b0011001,0b001001 0,0b0000010,0b1111000,0b0000000,0b0010000};
char display[2];
long value;
long volt,volt1,volt2;
long value2;
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
enable_interrupts(int_timer0);
enable_interrupts(global);
set_tris_A(0xFF);
set_tris_B(0X00);
set_tris_D(0x00);
while(TRUE)
{
setup_comparator(NC_NC_NC_NC);
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(AN0);
setup_vref(FALSE);
{
delay_us(100);
set_adc_channel(0);
value=read_adc();
read_adc(ADC_START_ONLY);
value=read_adc(ADC_READ_ONLY);
volt=(value/1023)*5;
value2=volt*100;
volt1=value2/10;
volt2=value2-(volt1*10);
display[0]=volt1;
output_B(digit[display[0]]);
output_high(PIN_D0);
delay_ms(10);
display[1]=volt2;
output_B(digit[display[1]]);
output_high(PIN_D1);
delay_ms(10);
}
}
}
|
Who can help me with that code in order to read a convienable temperature on seven segment display? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sun Aug 14, 2011 4:15 am |
|
|
Some comments inline:
Code: |
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
enable_interrupts(int_timer0);
enable_interrupts(global);
//Probably pointless - you are using standard_io
set_tris_A(0xFF);
set_tris_B(0X00);
set_tris_D(0x00);
while(TRUE) {
//Setups don't want to be continually repeated - move these
setup_comparator(NC_NC_NC_NC);
//Read the data sheet. Is/8 legal for a 20MHz clock?.
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(AN0);
setup_vref(FALSE);
//What is this bracket meant to be doing?....
{
//delay needs to be _after_ setting the channel
delay_us(100);
set_adc_channel(0);
value=read_adc();
//This is pointless - you have already started, and read the ADC
read_adc(ADC_START_ONLY);
value=read_adc(ADC_READ_ONLY);
//Value is an integer 0 to 1023, what is this/1023 going to give
//in integer arithmetic?....
volt=(value/1023)*5;
value2=volt*100;
//Try
//value2 = (value*500.0)/1023
volt1=value2/10;
volt2=value2-(volt1*10);
display[0]=volt1;
output_B(digit[display[0]]);
output_high(PIN_D0);
delay_ms(10);
display[1]=volt2;
output_B(digit[display[1]]);
output_high(PIN_D1);
delay_ms(10);
}
}
}
|
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sun Aug 14, 2011 5:11 am |
|
|
I think the ADC defaults to 8 bit readings though you appear to want 10 bit mode. |
|
|
|