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 support@ccsinfo.com

Learning CCS C coding Help!

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



Joined: 03 Feb 2009
Posts: 8

View user's profile Send private message Send e-mail

Learning CCS C coding Help!
PostPosted: Wed Oct 07, 2009 7:04 am     Reply with quote

I'm always delighted anytime I come to this forum. I always want to use CCS C to code for my projects but some doubt and inability to use certain features makes me tilt to using my other compilers. For example, I don't know how to use CCS C to program 16X2 LCD without having the R/W pin connected to the PIC port, But is easily done with mikroC, Basic etc.
However, I still prefer CCS C. Therefore, I want to learn and understand all that will keep me on this highly supported compiler.

First Question:
How can I code for 16 X 2 or 20 X 2 LCD without connecting the R/W pin to PIC port.
Thanks.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Oct 07, 2009 8:20 am     Reply with quote

Look at the code library at the flex_lcd driver.
You will include this file.
Make adjustments to port as required.
Then notice the note about what to do if not using R/W pin.

Compile and load. Done.
codewiz



Joined: 03 Feb 2009
Posts: 8

View user's profile Send private message Send e-mail

Code explanation
PostPosted: Thu Oct 08, 2009 2:09 am     Reply with quote

Code:

   setup_adc_ports(ALL_ANALOG);             // init the ADC;
   setup_adc(ADC_CLOCK_DIV_32);          // Tad=1.6us;
   set_adc_channel(acc_chan);
   delay_us(10);
   for(i=0; i<32; i++) {     
      acc_v=read_adc();
      acc_v_temp+=acc_v;
      if(i==31) {
         acc_v_avg=acc_v_temp>>5;
         if(acc_v_avg<=acc_min_l) low_acc=1;
         if(acc_v_avg>=acc_max_h) high_acc=1;
         acc_v_temp=0;
         acc_v_avg=0;
codewiz



Joined: 03 Feb 2009
Posts: 8

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 08, 2009 2:44 am     Reply with quote

Code:

#define acc_chan     1

int acc_v , acc_v_avg ;

long int acc_v_temp ;

acc_min_l = 60 ;
acc_max_h = 98;

I really want to understand what this does "acc_v_temp+=acc_v; " because it is adding an 8-bit register ( acc_v) to 16-bit register (acc_v_temp) and storing the value in the 16bit register. Since the 16bit register was zero before the addition, does it mean that the result is a 16 bit value equivalent to the 8-bit value?



Then " acc_v_avg=acc_v_temp>>5; " Please, can somebody help me break this down. This is what I understand : acc_v_temp is divided by 32 (>>5) and the result stored in an 8bit register?

why is ">>" often used with ADC? Please clarify me.


thank you treitmey for the LCD reply.
Ttelmah
Guest







PostPosted: Thu Oct 08, 2009 7:50 am     Reply with quote

This line is standard C.
When performing arithmetic, C always 'promotes' lower types to higher types, if two numeric types are involved. So, if you add a 16bit value, and a 8bit value, the 8bit type will be promoted to a 16bit value, and the arithmetic will be performed using 16bit maths. So if you had an 8bit value (say255), and you performed this sum, the first time, the 16bit value will become 255, then the second time, it'll become 510, etc..
In the case you give, yes, it'll become the 16bit version of the 8bit value _the first time through_, but obviously, if the code was repeated, you would get the 16bit 'sum'.
Shifts are quicker than divisions. Generally a division when using the ADC, is being used as a way of giving some averaging, so speed may matter. CCS, is 'smart' in this regard, and if you code "val/=32", it'll realise that division by 32, can be performed using >>5, and will do the later anyway.
You only give 'parts' of the code, but it sounds like a standard averaging algorithm.

Best Wishes
codewiz



Joined: 03 Feb 2009
Posts: 8

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 08, 2009 12:35 pm     Reply with quote

Thanks Ttelmah.
You have cleared my doubt.
It means >> 1 is the same as divide by 2 and << 1 means multiply by 2.
Thanks for bringing up the average issue. You are right.
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