|
|
View previous topic :: View next topic |
Author |
Message |
jaethelegend Guest
|
simple arithmatic BCD_RTN |
Posted: Sun Jun 07, 2009 9:55 pm |
|
|
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
|
|
Posted: Sun Jun 07, 2009 10:30 pm |
|
|
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
|
|
Posted: Sun Jun 07, 2009 11:26 pm |
|
|
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. |
|
|
|
|
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
|