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

How to fix code ADC + dsPic30f + 7-segment 4 digits

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



Joined: 18 Dec 2014
Posts: 1

View user's profile Send private message

How to fix code ADC + dsPic30f + 7-segment 4 digits
PostPosted: Tue May 26, 2020 3:15 am     Reply with quote

I have a problem about READING ADC TO DISPLAY TO 7-SEG 4 DIGITS LED using DSPIC30F3013.

CONNECTION: B0->B7 (connected to a->h); PIN_F4, F5, C13, C14 (connected to B-pin of 2N3906 transistor).

PROBLEM: I used to convert ADC value to R by function "R2=217.1-1.1*value" but in real, the number display in LED don't clear, overlap.

Please send me some recommendation! Thanks.

Code:

// 4-Digit digital R using dsPIC
// Common anode 7-segment display
// https://eee.humg.edu.vn
 
#include <30F3013.h>
#device ADC=8

#FUSES NOWDT                //No Watch Dog Timer
#FUSES HS                       //High Speed
#FUSES NOBROWNOUT      //No brownout reset
#FUSES NOPROTECT         

#use delay(clock=20M)

unsigned int dvi, chuc;
int16 R1,R2;

void seg(unsigned int num) {
  switch (num) {
  case 0 : //output_b(0xC0);
    {
      output_low(PIN_B0);
      output_low(PIN_B1);
      output_low(PIN_B2);
      output_low(PIN_B3);
      output_low(PIN_B4);
      output_low(PIN_B5);
      output_high(PIN_B6);
      output_high(PIN_B7);
      break;
    }
    case 1 : // output_b(0xF9);
    {
      output_high(PIN_B0);
      output_low(PIN_B1);
      output_low(PIN_B2);
      output_high(PIN_B3);
      output_high(PIN_B4);
      output_high(PIN_B5);
      output_high(PIN_B6); 
      output_high(PIN_B7);
      break;
    }
    case 2 : // output_b(0xB4);
    {
      output_low(PIN_B0);
      output_low(PIN_B1);
      output_high(PIN_B2);
      output_low(PIN_B3);
      output_low(PIN_B4);
      output_high(PIN_B5);
      output_low(PIN_B6); 
      output_high(PIN_B7);
      break;
    }
    case 3 : // output_b(0xB0);
    {
      output_low(PIN_B0);
      output_low(PIN_B1);
      output_low(PIN_B2);
      output_low(PIN_B3);
      output_high(PIN_B4);
      output_high(PIN_B5);
      output_low(PIN_B6);
      output_high(PIN_B7);
      break;
    }
    case 4 : // output_b(0x99); //s4
    {
      output_high(PIN_B0);
      output_low(PIN_B1);
      output_low(PIN_B2);
      output_high(PIN_B3);
      output_high(PIN_B4);
      output_low(PIN_B5);
      output_low(PIN_B6); 
      output_high(PIN_B7);
      break;
    }
    case 5 : // output_b(0x92);
    {
      output_low(PIN_B0);
      output_high(PIN_B1);
      output_low(PIN_B2);
      output_low(PIN_B3);
      output_high(PIN_B4);
      output_low(PIN_B5);
      output_low(PIN_B6);
      output_high(PIN_B7);
      break;
    }
    case 6 : // output_b(0x82);
    {
      output_low(PIN_B0);
      output_high(PIN_B1);
      output_low(PIN_B2);
      output_low(PIN_B3);
      output_low(PIN_B4);
      output_low(PIN_B5);
      output_low(PIN_B6);
      output_high(PIN_B7);
      break;
    }
    case 7 : // output_b(0xF8);
    {
      output_low(PIN_B0);
      output_low(PIN_B1);
      output_low(PIN_B2);
      output_high(PIN_B3);
      output_high(PIN_B4);
      output_high(PIN_B5);
      output_high(PIN_B6);  // 1100 0111
      output_high(PIN_B7);
      break;
    }
    case 8 : // output_b(0x80);
    {
      output_low(PIN_B0);
      output_low(PIN_B1);
      output_low(PIN_B2);
      output_low(PIN_B3);
      output_low(PIN_B4);
      output_low(PIN_B5);
      output_low(PIN_B6);  // 1100 0111
      output_high(PIN_B7);
      break;
    }
    case 9 : // output_b(0x90);
    {
      output_low(PIN_B0);
      output_low(PIN_B1);
      output_low(PIN_B2);
      output_low(PIN_B3);
      output_high(PIN_B4);
      output_low(PIN_B5);
      output_low(PIN_B6);
      output_high(PIN_B7);
      break;
    }
    case 10 : // output_b(0xC7); // 11000111   L
    {
      output_high(PIN_B0);
      output_high(PIN_B1);
      output_high(PIN_B2);
      output_low(PIN_B3);
      output_low(PIN_B4);
      output_low(PIN_B5);
      output_high(PIN_B6);
      output_high(PIN_B7);
      break;
    }
    case 11 : // output_b(0x98);  //  H
    {
      output_high(PIN_B0);
      output_low(PIN_B1);
      output_low(PIN_B2);
      output_high(PIN_B3);
      output_low(PIN_B4);
      output_low(PIN_B5);
      output_low(PIN_B6);
      output_high(PIN_B7);
      break;
    }
    }
}


void convert(int a)
{
   chuc=a/10;
   dvi=a%10;
}


void hienthi(){

         output_high(PIN_F4);
         output_high(PIN_C14);
         output_high(PIN_C13);
         output_high(PIN_F5);
         delay_ms(1);
         
         output_low(PIN_F5);      // Bat LED don vi
         seg(dvi);                // Hien chu so hang don vi
         delay_ms(2);
         output_high(PIN_F5);       //Tat LED don vi
         
         output_low(PIN_C14);      // Bat LED chuc
         seg(chuc);                // Hien chu so hang chuc
         delay_ms(2);
         output_high(PIN_F4);

}


void main(){

   float value;
   int R1,R2;
   int min=138, max=180;

   setup_adc_ports(sAN8);
   setup_adc(ADC_CLOCK_INTERNAL);

   while (TRUE)
   {
      set_adc_channel(8);
      value = read_adc();
      delay_us(20);
     
      if(value<134)
      { 
         output_high(PIN_F4);
         output_high(PIN_C14);
         output_high(PIN_F5);
         output_low(PIN_C13);
         seg(11);             // Hien thi chu H (High Resistance)
         
      }
      else if((value>=138)&&(value<=179))
      {

         R2=217.1-1.1*value; // Dien tro tu 66k 3 pha den 20k 3 pha
         convert(R2);
         delay_ms(1);
         hienthi();

      }
      else if(value>182)
      {
     
         output_high(PIN_C13);
         output_high(PIN_C14);
         output_high(PIN_F5);
         output_low(PIN_F4);      // Tat tat ca cac LED
         seg(10);             // Hien thi chu L (Low Resistance)
      }
   }

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue May 26, 2020 1:04 pm     Reply with quote

Problem is there are only a tiny number of comments to explain what
lines are used for, and no details of how the hardware is wired.

Now 'why' are you outputting the bit patterns bit by bit?.
Much easier to output the bytes. Even better these can come from an
array of values. In your case you are using a 16bit processor, so the
'output_b' function will output all 16bits, but you can make a byte
wide version by just writing directly to the LAT.
So:
Code:

#byte LATB=getenv("SFR:LATB")

    set_tris_c(0xFF00);
    LATB=0xC0;

Outputs the whole byte.

Then I'm puzzled by your accesses here:
Code:

         output_low(PIN_F5);      // Bat LED don vi
         seg(dvi);                // Hien chu so hang don vi
         delay_ms(2);
         output_high(PIN_F5);       //Tat LED don vi
         
         output_low(PIN_C14);      // Bat LED chuc
         seg(chuc);                // Hien chu so hang chuc
         delay_ms(2);
         output_high(PIN_F4);

Assuming F5, is the common select for one digit, what is happening
with C14 and F4?. If C14 is the select for the next digit, it goes on,
and you don't then turn it off.
The sequence has to be output bit pattern, select on, wait, select off.
It'd honestly be much better to have this being done by a timer
interrupt.
First tick second select off, output pattern for first digit turn first select
on.
Next tick, first select off, output pattern for second digit, second select
on.
Repeat.
This way the display will be steady while other code is being run.
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 26, 2020 1:59 pm     Reply with quote

The other 'debugging' technique is to use KNOWN values and display them. Say 0000, 1234,5678,9999 in a loop with a 1/2 second delay.

Do this BEFORE trying to read/send ADC data to the display.

Jay
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