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

simple arithmatic BCD_RTN

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







simple arithmatic BCD_RTN
PostPosted: Sun Jun 07, 2009 9:55 pm     Reply with quote

Hi, I am using CCP1 port to measure the length of pulses.

I have couple of problems

1), I am using BCD_ routine which separates the 4 digits for displaying onto the LCD which is
Code:

void BCD_RTN(buffer){
   thou_digit = buffer/1000;
   buffer-= thou_digit*1000;
   hund_digit = buffer/100;
   buffer -= hund_digit*100;
   ten_digit = buffer/10;
   one_digit = buffer-ten_digit*10;
}

However, the algorithm doesn't seem to separate correct digits.
For example, if a variable passed on to the function had the value of
1243, or 0x04DB, it yields, thou_digit = 0, hund_digit = 2, ten_digit = 2, one_digit = 0, which is very off.

Is it because I am using unsigned int16 variable? Is there anything I should keep in mind when computing with those type of variable??

Below is my complete coding.

Thanks in advance
Code:

#include<C:\Documents and Settings\Microsoft\My Documents\PIC\Header\16f886.h>
#include<C:\Documents and Settings\Microsoft\My Documents\PIC\Header\def_16f886.h>
#fuses INTRC_IO,DEBUG

#use delay(clock = 1000000)

#define LCD_PORT PORTA
#define LCD_TRIS TRISA
#include<C:\Documents and Settings\Microsoft\My Documents\PIC\Header\LCD4bit.h>
unsigned int16 this_time = 0; //stores the time that was just captured
unsigned int16 last_time = 0; //stores the time that was captured last.
unsigned int16 diff = 0;
//unsigned int16 frequency_in_KHz = 0;
unsigned int16 thou_digit = 0;
unsigned int16 hund_digit = 0;
unsigned int16 ten_digit = 0;
unsigned int16 one_digit = 0;

void Init_rtn(){
   LCD_INIT(0x28);
   osccon = 0x45;
   TRISC2 = 1; //CCP1 input
   PORTC2 = 0;
   GIE = 1;
   PEIE = 1;
   CCP1IE = 1;
   PIR1 = 0x00;
   CCP1CON = 0x05;// Capture mode, every rising edge
   
   T1CON = 0x01;
   TMR1L = 0x00;
   TMR1H = 0x00;
   goto_xy(0,0);
   send_char("Difference");
   send_cmd(CURSOR_OFF_BLINK_OFF);
}

#int_ccp1
void ccp1_isr(){
   last_time = this_time;//Last "this_time" becomes "last_time"
   this_time = CCPR1H<<8;
   this_time = this_time|CCPR1L;
   //Wrap around has not occured.
   if(this_time>last_time) diff = this_time-last_time;
   
   //wrap around has occured.
   else diff = (2^16) -1 - last_time + this_time;
   delay_us(10);
   //frequency_in_KHz = 4*(10^3)/diff;
   
}
void BCD_RTN(buffer){
   thou_digit = buffer/1000;
   buffer-= thou_digit*1000;
   hund_digit = buffer/100;
   buffer -= hund_digit*100;
   ten_digit = buffer/10;
   one_digit = buffer-ten_digit*10;
}


void disp_rtn(void){
   BCD_rtn(diff);
   goto_xy(5,1);
   send_char(ascii_num_table[thou_digit]);
   send_char(ascii_num_table[hund_digit]);
   send_char(ascii_num_table[ten_digit]);
   send_char(ascii_num_table[one_digit]);
}


void main(void){
 Init_rtn();
   
 while(1){
   disp_rtn();
  }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 10:30 pm     Reply with quote

Do you want to display a number on a normal 2x16 LCD ?
Use printf to convert the number to ASCII. It's much easier.
Also use the re-direction feature of printf, to send the output
to the lcd_putc() routine, so it will be displayed on the LCD.
http://www.ccsinfo.com/forum/viewtopic.php?t=33390
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 11:26 pm     Reply with quote

I agree, that the code is a kind of reinventing the square wheel.
Regarding it's failure, you may want to correct it to
Code:
void BCD_RTN(int16 buffer);

CCS has a default integer data type, it's int8.
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