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 and seven segment display with PIC16F877A

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
john121



Joined: 14 Aug 2011
Posts: 1
Location: Rwanda

View user's profile Send private message

adc and seven segment display with PIC16F877A
PostPosted: Sun Aug 14, 2011 2:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 14, 2011 4:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 14, 2011 5:11 am     Reply with quote

I think the ADC defaults to 8 bit readings though you appear to want 10 bit mode.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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