sphinx
Hero Member
Offline
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
kreutz
Active Member
Offline
Posts: 183
Thank You
-Given: 995
-Receive: 125
|
|
« Reply #1 on: April 05, 2022, 03:06:38 15:06 » |
|
|
|
« Last Edit: April 05, 2022, 03:11:52 15:11 by kreutz »
|
Logged
|
|
|
|
sphinx
Hero Member
Offline
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
|
tanveerriaz
Active Member
Offline
Posts: 111
Thank You
-Given: 46
-Receive: 378
|
|
« Reply #4 on: April 06, 2022, 02:27:45 14:27 » |
|
|
|
|
Logged
|
|
|
|
sphinx
Hero Member
Offline
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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 but could bee fun to do
|
|
|
Logged
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
kreutz
Active Member
Offline
Posts: 183
Thank You
-Given: 995
-Receive: 125
|
|
« 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
Posts: 132
Thank You
-Given: 124
-Receive: 102
|
|
« 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
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
Checksum8
Active Member
Offline
Posts: 132
Thank You
-Given: 124
-Receive: 102
|
|
« 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
Posts: 710
Thank You
-Given: 1839
-Receive: 2907
Sow The Seeds of Love
|
|
« 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
Posts: 221
Thank You
-Given: 127
-Receive: 312
|
|
« Reply #11 on: April 07, 2022, 11:55:28 11:55 » |
|
|
|
|
Logged
|
|
|
|
sphinx
Hero Member
Offline
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
sadman
Hero Member
Offline
Posts: 710
Thank You
-Given: 1839
-Receive: 2907
Sow The Seeds of Love
|
|
« 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
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
sphinx
Hero Member
Offline
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
sadman
Hero Member
Offline
Posts: 710
Thank You
-Given: 1839
-Receive: 2907
Sow The Seeds of Love
|
|
« 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
Senior Member
Offline
Posts: 256
Thank You
-Given: 128
-Receive: 151
|
|
« 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, 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
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
sphinx
Hero Member
Offline
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
sam_des
Senior Member
Offline
Posts: 256
Thank You
-Given: 128
-Receive: 151
|
|
« 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
Posts: 920
Thank You
-Given: 614
-Receive: 271
|
|
« 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
|
laws of physics are not laws at all, just assumptions and formulas that work as long as we don't figure something new that wrecks the calculations. the infinite onion try to peel that one
|
|
|
|