Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 28, 2022, 10:21:10 10:21


Login with username, password and session length


Pages: [1]
Print
Author Topic: looking for inputs on led bargraph display in ccs c  (Read 2793 times)
0 Members and 1 Guest are viewing this topic.
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« on: April 05, 2022, 02:04:24 02:04 »

i made a embedded software for pic in ccs c for displaying a led bargraph with 8 leds and i was just wondering if there
could be another solution for what i am trying to do. i am using lost if else statements to do what i want.

could there be some other solution for this? perhaps more efficient code.

thanx in advance.

i added the proteus files too.
« Last Edit: April 05, 2022, 02:40:59 02:40 by sphinx » Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
kreutz
Active Member
***
Offline Offline

Posts: 174

Thank You
-Given: 779
-Receive: 121


« Reply #1 on: April 05, 2022, 03:06:38 15:06 »

Take a look at https://www.edn.com/microcontrollers-single-i-o-port-line-drives-a-bar-graph-display/
It is a simpler alternative with less uC resources but requires more hardware.

Also here: https://docs.arduino.cc/built-in-examples/display/BarGraph
« Last Edit: April 05, 2022, 03:11:52 15:11 by kreutz » Logged
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« Reply #2 on: April 05, 2022, 03:26:28 15:26 »

i do not mind using the i/o on mcu, i was interested for a perhaps more efficient code. i would prefer
if leds are lit up as 1 + 1 + 1 + 1 .... and not only one thats lit up.

« Last Edit: April 05, 2022, 03:36:47 15:36 by sphinx » Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
sadman
Hero Member
*****
Offline Offline

Posts: 611

Thank You
-Given: 1565
-Receive: 2182



« Reply #3 on: April 06, 2022, 02:12:53 14:12 »

although the link which share here a nice example driving like LM3914 using 16F88 but code is in ASM

https://320volt.com/super-vu-metre-nokta-logaritmik-farkli-fonksiyonlar/
« Last Edit: April 06, 2022, 02:17:24 14:17 by sadman » Logged
tanveerriaz
Junior Member
**
Offline Offline

Posts: 80

Thank You
-Given: 26
-Receive: 157


« Reply #4 on: April 06, 2022, 02:27:45 14:27 »

LM3914 and LM3915 with stm8
https://vrtp.ru/index.php?showtopic=5557&st=540
Logged
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« Reply #5 on: April 06, 2022, 04:28:13 16:28 »

thnax guys for the inputs on this, my project is simply
reading a voltage input 0-5vdc
set a pwm output 0-100% depending on input
show 8-10 leds as graph on what is set but input voltage

using a lm3914 and mcu makes it sort more expensive than a simple mcu since i dont need that many i/o's
a single lm3914 is more expensive than mcu, kinda dont see the point of using it maybe just to learn on how to use
lm3914 $4
pic 3$

i am more looking to simplify all the if .. then cases and prehaps lower memory concumption, not that its needed since
 around 30% is used but i thought it could be good to know or i could use a smaller mcu

i could maybe make a non mcu pwm and lm3914 just to learn, this will make brain hurt a bit more than a mcu solution Wink
but could bee fun to do
Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
kreutz
Active Member
***
Offline Offline

Posts: 174

Thank You
-Given: 779
-Receive: 121


« Reply #6 on: April 06, 2022, 07:02:11 19:02 »

Let's say you read the 0-5V input range with a 16 bits ADC, and use a 16 bits pwm timer. Then just read the 8 most significative bits of the ADC reading into a variable then use a look-up table to output 8 bits in parallel to the LED driver pins. the look up table is the one responsible to fill up the led bar. The look up table could be as simple as you want.
« Last Edit: April 06, 2022, 07:05:04 19:05 by kreutz » Logged
Checksum8
Active Member
***
Offline Offline

Posts: 118

Thank You
-Given: 111
-Receive: 91


« Reply #7 on: April 06, 2022, 07:09:26 19:09 »

You could replace the "If Then" statements with "select case" to make it easier to read.
Logged
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« Reply #8 on: April 06, 2022, 07:46:44 19:46 »

i have looked in ccs c manual and i cant find the "select case" function 
Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
Checksum8
Active Member
***
Offline Offline

Posts: 118

Thank You
-Given: 111
-Receive: 91


« Reply #9 on: April 06, 2022, 08:50:07 20:50 »

I am not familiar with CCS, but a quick search it may be called "Switch case"?

Found this
https://www.ccsinfo.com/forum/viewtopic.php?t=34291



« Last Edit: April 06, 2022, 08:59:50 20:59 by Checksum8 » Logged
sadman
Hero Member
*****
Offline Offline

Posts: 611

Thank You
-Given: 1565
-Receive: 2182



« Reply #10 on: April 07, 2022, 10:06:01 10:06 »

here is optimized ccs code for adc to bargraph

sadman
Logged
folkeu
Active Member
***
Offline Offline

Posts: 196

Thank You
-Given: 117
-Receive: 250


« Reply #11 on: April 07, 2022, 11:55:28 11:55 »

Hi,
Than can help you too :
http://www.sonsivri.to/forum/index.php?topic=52263.msg159103#msg159103
Thanks
Logged
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« Reply #12 on: April 08, 2022, 06:17:09 18:17 »

@sadman this is the source you sent, it works just perfect but i am trying to understand some things with it.

#include<18f4550.h>

//Use High speed Crystal
#fuses HS

//Set the frequency for delay function
#use delay(clock=20M)

void main(void){
   unsigned int16 adcRead;
this is the variable that analog input is read to, will that value still be 10 bit ?


   char ledBar[9]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};
i sort of get what this does, but i am trying to learn more about it and would like to know what it is called so i can read more about it.
now i figured out what this type is called it is an array

   //RA0 As Input
   set_tris_a(0x01);

   //PORTD as output
   set_tris_d(0x00);

   //Set RA0 To Analog
   setup_adc_ports(AN0);

   //Select ADC internal RC Clock
   setup_adc(ADC_CLOCK_INTERNAL);

   //Select channel 0 for conversion
   set_adc_channel(0);

   while(1){
      adcRead=read_adc();
      //Wait for completion

      while(!adc_done());
      adcRead=(adcRead*9)/256; can you explain this math a bit more , i cant figure this one out on how it works.

      //Output the result to PORTD
      output_D(ledBar[adcRead]);
      delay_ms(50);
   }
}

this is not only for @sadman anyone can of course reply to this.
« Last Edit: April 08, 2022, 09:51:01 21:51 by sphinx » Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
sadman
Hero Member
*****
Offline Offline

Posts: 611

Thank You
-Given: 1565
-Receive: 2182



« Reply #13 on: April 09, 2022, 02:12:10 14:12 »

@sadman this is the source you sent, it works just perfect but i am trying to understand some things with it.

#include<18f4550.h>

//Use High speed Crystal
#fuses HS

//Set the frequency for delay function
#use delay(clock=20M)

void main(void){
   unsigned int16 adcRead;
this is the variable that analog input is read to, will that value still be 10 bit ?
adc value is set to 8bit


   char ledBar[9]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};
i sort of get what this does, but i am trying to learn more about it and would like to know what it is called so i can read more about it.
now i figured out what this type is called it is an array
bargrapph scale into 9 number

   //RA0 As Input
   set_tris_a(0x01);

   //PORTD as output
   set_tris_d(0x00);

   //Set RA0 To Analog
   setup_adc_ports(AN0);

   //Select ADC internal RC Clock
   setup_adc(ADC_CLOCK_INTERNAL);

   //Select channel 0 for conversion
   set_adc_channel(0);

   while(1){
      adcRead=read_adc();
      //Wait for completion

      while(!adc_done());
      adcRead=(adcRead*9)/256; can you explain this math a bit more , i cant figure this one out on how it works.
adc value multiply by bargraph total vale which is 9 then dived by 256

      //Output the result to PORTD
      output_D(ledBar[adcRead]);
      delay_ms(50);
   }
}

this is not only for @sadman anyone can of course reply to this.
Logged
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« Reply #14 on: April 09, 2022, 05:53:50 17:53 »

sorry to bother again

thats why i couldnt figure it out with 10 or 16 bit that math didnt make sense
now i get the math and the workings of the array

but the one that still twists my brain is that 8bit adc value where is that set?

thnx for your help on this.
Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« Reply #15 on: April 09, 2022, 10:15:16 22:15 »

i find this in more than one device/header file in ccs c compiler, i quite to understand how to call the read_adc function in 8bit or 16bit mode.
man my head hurts today i can really figure this out.

from header file
////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
//                SET_ADC_CHANNEL(), READ_ADC(), ADC_DONE()
// ADC Prototypes:
_bif void setup_adc(int16 mode);
_bif int8 read_adc(void);
_bif int8 read_adc(int8 mode);
_bif int16 read_adc(void);
_bif int16 read_adc(int8 mode);
_bif int1 adc_done(void);
// Constants used for SETUP_ADC() are:
Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
sadman
Hero Member
*****
Offline Offline

Posts: 611

Thank You
-Given: 1565
-Receive: 2182



« Reply #16 on: April 10, 2022, 06:33:05 06:33 »

sorry to bother again

thats why i couldnt figure it out with 10 or 16 bit that math didnt make sense
now i get the math and the workings of the array

but the one that still twists my brain is that 8bit adc value where is that set?

thnx for your help on this.
Logged
sam_des
Active Member
***
Offline Offline

Posts: 249

Thank You
-Given: 115
-Receive: 138


« Reply #17 on: April 10, 2022, 06:37:34 06:37 »

Hi,
I don't user CCS, but AFAIK, For CCS, there is one compile-time directive "device". You can set many options to select compiler behavior as well as what lib functions compiler pulls in.

For ADC add this directive to your main.c or main header,
Code:
#device adc=10

This will tell compiler to that device you're using has 10-BIT adc & corresponding read_adc() will be called. I hope compiler flags error when user specifies 10 or higher BIT result, but device have only 8-BIT ADC.
I guess(not sure though) default value for this directive is 8, so if you don't specify any then compiler will use 8-BIT ADC functions.

There are 8, 10, 11, 12, 16-BIT ADCs supported by CCS. Except 8-BIT all else will call "int16 read_adc(int8 mode) or int16 read_adc()" to allow differential ADCs in some of new PICs which may give negative results.

Hope that helps. For more refer to CCS help.

sam_des


Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« Reply #18 on: April 11, 2022, 01:52:29 01:52 »

i know so far that the "      adcRead=read_adc();" reads the value but i cant figure out on how it make the difference of 8 bit or 16bit
since its not specified with "#DEVICE ADC= directive"

thats what puzzles me

either its 8bit deafult if one does not specify in the program, but it is not documented in manual as far i can see.
i will search their forum for info on this.
Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« Reply #19 on: April 11, 2022, 07:18:28 07:18 »

this is a reply to my own question
if the "#DEVICE ADC= directive" with selected bit size 8,10,11,12,16 is ommited in the code then the compiler defaults
to 8bit mode according to a user on ccs forum

now i know a little more and how to read _bif functions in device file for a selected chip

sometimes things have no need to be in the code  but better if added for more nice for clearity
if one uses diffenrent compilers its not easy to remember all quirks of the different compilers

thanx for all the inputs you all it is all very appresicated.
Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
sam_des
Active Member
***
Offline Offline

Posts: 249

Thank You
-Given: 115
-Receive: 138


« Reply #20 on: April 11, 2022, 04:07:50 16:07 »

Glad to know that you got the clarification.

That is the curse of using 3rd party/built-in peripheral libraries.
It may seem easier path at the beginning but you never know what compiler is doing and how and when behind user's back when simple innocuous looking c-statement is written.
That's why I always prefer to write my own libraries.

sam_des
Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
sphinx
Hero Member
*****
Offline Offline

Posts: 745

Thank You
-Given: 537
-Receive: 199



« Reply #21 on: April 11, 2022, 08:02:36 20:02 »

the "sadman" version of software is 900 bytes smaller of the hex file when using the array version
of setting the output LEDS. not that it is needed in this case, but if one is on the limit of memory
then this could be a good solution for solving this.

in my case i am now using 9% of 4k rom so i wasnt really in any trouble

now i am going to look for a chip with less memory to try my source with

thanx for all help/input, i just wanted to share from what i learned while trying to find another solution not really needed but good to know.

just checked my old version is using 504 bytes and the new version is using 346 bytes thats quite a difference in size, if one is close to the
limit this can make a big difference.
« Last Edit: April 12, 2022, 08:51:12 08:51 by sphinx » Logged

the more you learn, the lesser you get to understand. is it then good know alot but not understand what you know or.......
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC