• AVR Freaks

AnsweredHot!Difference between capture and compare modes with examples

Page: << < ..678910.. > >> Showing page 7 of 13
Author
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 05:14:23 (permalink)
0
Hi
 
I did this demonstration today, and recorded that on YouTube.
 
Here's the link:
https://www.youtube.com/watch?v=2WW4F9BUgTs

 
But also there are weird things in the MPLAB simulator.
For example; with the same code:
 
The problem is with the value in the CCPR1, it's just a little change in the value and the pin in the simulator hangs! I don't know why!
 
If the value in the CCPR1 is 0x00EF the pin is going ON/OFF, but when the value is 0x00FF then the pin hangs to HIGH! Just incrementing the value in the CCPR1 should not put a very long delay. The interesting part is that if the value is 0x0100 then the pin is going ON/OFF OK.
 
So, I don't know what's the reason!
 
void compare(void)
{
    ADCON0=0x00;ADCON1=0x0f;CMCON=0x07; // disable ADC, comparators
    OSCCON=0x66; // internal oscillator 4MHz
    TRISC=0x00; // port settings most important CCP1 at port C as output
    INTCON=0x00; // compare settings & disable all interrupts
    T1CON=0x00; // TMR1 16-bits
    T3CON=0x00; // adjust TMR1 for capture/compare
    CCPR1=0x00FF; // the other test values are 0x00EF 0x0100
    CCP1CON=0x08;TMR1=0;
    T1CONbits.TMR1ON=1; // turn on timer1
while (1) {
    while(PIR1bits.CCP1IF==0); // when the flag is set, this line breaks!
    PIR1bits.CCP1IF=0;
    CCP1CONbits.CCP1M0^=1;
 }
}

 
 
 
 
post edited by eagle1 - 2018/07/22 05:37:02
JorgeF
Super Member
  • Total Posts : 3340
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 05:51:40 (permalink)
0
Hi
1and0
JorgeF
The logic analyzer on MPLAB just draws a graphic line of the data in the trace buffer.
It may or may show the state at the time of a brekpoint if that data is in the trace buffer.
The trace buffer stops recording when full, but the program keeps running.

Tested with the MPLAB 8.92 Simulator just now and the CCP graphic line does toggle at the breakpoint.

I 'm using the same setup.
And you are right, the line toogles at the breakpoint but the graphic is incomplete, with a default trace buffer size setting.
When the trace buffer gets full, it keps recording the latest state and drops the oldest ones.
I had to increase the trace buffer size above 64K in order to see the first 2 transitions of the CCP pin.
OTOH if we activate the "Break on trace buffer full" option, with default size, teh break happens before the second transition.
 
This is expected behaviour, but I'm not sure the OP has the technical background to correctly interpret a parcial trace, and without the 2 transitions its not possible to measure the pulse width.

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 05:53:31 (permalink) ☼ Best Answerby eagle1 2018/07/25 10:10:49
+1 (1)
eagle1
Hi
 
I did this demonstration today, and recorded that on YouTube.
 
Here's the link:
https://www.youtube.com/watch?v=2WW4F9BUgTs


In the video, you expressed surprise that changing the initial value in CCPR1 doesn't change the flash rate.
I explained this in post#108, and you acknowledged in #109 that it will only match once per timer rollover, to it doesn't matter what you set it to, the flash rate is the same as the timer rollover rate.
 
Try changing
while (1) { 
    while(PIR1bits.CCP1IF==0); // when the flag is set, this line breaks!
    PIR1bits.CCP1IF=0;
    CCP1CONbits.CCP1M0^=1;
 }

to
while (1) {
    while(PIR1bits.CCP1IF==0); // when the flag is set, this line breaks!
    PIR1bits.CCP1IF=0;
    CCPR1 += 5000;    //interrupt again in 5000 timer counts
    CCP1CONbits.CCP1M0^=1;
 }

then try varying the "5000" value.
 
 
 
 





This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
JorgeF
Super Member
  • Total Posts : 3340
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 06:11:46 (permalink)
+1 (1)
Hi
eagle1
Here's the link:
https://www.youtube.com/watch?v=2WW4F9BUgTs

But also there are weird things in the MPLAB simulator.
For example; with the same code:
 
The problem is with the value in the CCPR1, it's just a little change in the value and the pin in the simulator hangs! I don't know why!
 
void compare(void)
 
{
    ADCON0=0x00;ADCON1=0x0f;CMCON=0x07; // disable ADC, comparators
    OSCCON=0x66; // internal oscillator 4MHz
    TRISC=0x00; // port settings most important CCP1 at port C as output
    INTCON=0x00; // compare settings & disable all interrupts
    T1CON=0x00; // TMR1 16-bits
    T3CON=0x00; // adjust TMR1 for capture/compare
    CCPR1=0x00FF; // the other test values are 0x00EF 0x0100
    CCP1CON=0x08;TMR1=0;
    T1CONbits.TMR1ON=1; // turn on timer1
while (1) {
    while(PIR1bits.CCP1IF==0); // when the flag is set, this line breaks!
    PIR1bits.CCP1IF=0;
    CCP1CONbits.CCP1M0^=1;
 }
}


Relating to your video.
 
You are not seeing any change in the flashing rate, because the match is happening at intervals of 0xffff.
Only the very first transition happens after 0x000f clocks.
After that all the transitions will happen with intervals of 0x0ffff.
All the matches happen when the counter reaches 0x000f.
Only the first after a count from 0x0000 to 0x000f, from there on all the matches happen after a count from 0x000f to 0x000f, due to timer rollover.
 
 
Relating to your test setup.
 
The resistor from MCLR to VDD, you can forget if the programmer/debugger in use takes care of holding MCLR high, and you leave it connected to the target.
I also don't bother to use it because my Pickit3 takes care of the MCLR pin.
 
But the one thing you have mssing is the decoupling capacitor on the power pins of the PIC.
A simple 0.1 uF ceramic capacitor like what you can see in this photo can save you lot's of headaches.
 
post edited by JorgeF - 2018/07/22 06:18:20

Attached Image(s)


Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
JorgeF
Super Member
  • Total Posts : 3340
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 06:34:46 (permalink)
+1 (1)
Hi
 
A side note about the tests shown in the video
eagle1
https://www.youtube.com/watch?v=2WW4F9BUgTs

Even whit the maximum count of Timer1 the flashing is quite fast.
Use the Timer1 prescaller to reduce the clock speed for the Timer1 or set the OSCON for a slower PIC clock so the effects of changing the CCPR1 value will be easier to spot with a naked eye.
 
EDIT ADD:
 
Just setting the Timer1 pre-scaller to a 1:8 ratio might do, also made changes with steps above 256 to make it easier to spot the difference.
post edited by JorgeF - 2018/07/22 07:17:26

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
1and0
Access is Denied
  • Total Posts : 9287
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 07:38:51 (permalink) ☄ Helpfulby eagle1 2018/07/22 07:59:43
+1 (1)
qɥb
In the video, you expressed surprise that changing the initial value in CCPR1 doesn't change the flash rate.
I explained this in post#108, and you acknowledged in #109 that it will only match once per timer rollover, to it doesn't matter what you set it to, the flash rate is the same as the timer rollover rate.

That is either a brainfart or an indication that OP does not understand how the timer and/or compare mode work.
 
@OP: Timer1 is a 16-bit timer and as such has 65536 counts per rollover. For _any_ initial value of CCPR1, it ALWAYS takes 65536 counts to get back to that same value.
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 07:58:00 (permalink)
+1 (1)
qɥb
In the video, you expressed surprise that changing the initial value in CCPR1 doesn't change the flash rate.
I explained this in post#108, and you acknowledged in #109 that it will only match once per timer rollover, to it doesn't matter what you set it to, the flash rate is the same as the timer rollover rate.

 Didn't understood it in this way Smile: Smile
 
I thought that since the match and according to the value in the CCPR1, the timer should count up until it reaches the compare value, then it forces the pin HIGH/LOW. Then the timer also continue counting and overflow again until it reaches the compare value again! Oh, wait a minute .... LOL I'm so sorry I got the idea.
 
The rate should be the same because the timer should take the same time from any value to rollover to the same point!!! WOW I just realized that I'm really impressed right now!
 
then try varying the "5000" value.
 
 
That's brilliant! Of course, now I learned that the initial value before the "while" loop could be important in very sensitive applications. But the most important I learned is changing the value inside the loop for different frequencies, it's relatively similar to PWM.
 
Also in compare mode I think I can control the ON/OFF timer by adding another trigger event and load the CCPR1 with a different value.
 
Also, I think I can run an array which has a set of values to encode something; I think the PWM can do the same.
 
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 08:29:19 (permalink)
0
I have another problem with the capture mode.
 
I set Stimulus with a clock Stimulus as an input for the CCP1 pin.
 
Run uart output, but the readings aren't stable! What could be the reason.
 
Here's the code:
void capture(void)
{
    volatile uint8_t counter = 0;
    uint16_t data1=0,data2=0,period;
    ADCON0=0x00;ADCON1=0x0f;CMCON=0x07; // disable ADC, comparators
    OSCCON=0x66;
    TRISC=0x04;T1CON=0x80; // CCP1 at port C as input, timer1 16-bits
    CCP1CON=0x05;INTCON=0x00; // Capture settings & disable all interrupts
    init_uart();
    T1CONbits.TMR1ON=1; // turn on timer1
   while (1)
   {
        while(PIR1bits.CCP1IF==0); // wait for capture input signal
        PIR1bits.CCP1IF=0; // clear the capture flag bit
        data1 = CCPR1; // copy the 1st data of capture data register
        while(PIR1bits.CCP1IF==0); // wait for another capture input signal
        PIR1bits.CCP1IF=0; // clear the capture flag bit
        data2 = CCPR1; // copy the 2nd data of capture data register
        period = data2 - data1;
        printf("period %d\n",period);
   }
}

 
Here's the readings:
First I wanted to get the reading of each variable
data1 -20986
data2 -20530
data1 -12986
data2 -12786
data1 -4786
data2 -4586
data1 2414
data2 2456


Second I applied the period math which get the subtraction of "data1" and "data2"
 -56
456
200
200
-56
456
200
200
-56
456
200
200
-56
456
200
 
The input clock is 100us.
 
JorgeF
Super Member
  • Total Posts : 3340
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 08:50:20 (permalink) ☄ Helpfulby eagle1 2018/07/22 08:56:00
+1 (1)
Hi
 
As your variables, data1, data2 and period, are "unsigned", you should use "%ud" "%u" instead of "%d" in the formating string of the "printf" function.
 
Note that "printf" is a very heavy function, depending on the clock speed and the stimulus frequency you risk losing events while the PIC is busy with printf.
 
EDIT: Correct typo
post edited by JorgeF - 2018/07/22 11:18:45

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 08:59:20 (permalink)
0
JorgeF
Hi
 
As your variables, data1, data2 and period, are "unsigned", you should use "%ud" instead of "%d" in the formating string of the "printf" function.
 
Note that "printf" is a very heavy function, depending on the clock speed and the stimulus frequency you risk losing events while the PIC is busy with printf.


"%ud" added "d" with the numbers, so only "%u" worked with me and got this results:
 
period 65480
period 200
period 65480
period 200
period 65480
period 456
period 200
period 456
period 200
period 456
period 200
period 456
period 200
period 456
period 200
period 456
period 200
period 456
period 200
period 456
period 200
period 456
period 200
period 456
period 200
period 456
period 200
period 456
period 200
period 200
period 65480
period 200
period 65480
period 200
period 200
 
So I think the true reading is 200, you're right maybe the CPU misses readings, but I'm getting the idea.
1and0
Access is Denied
  • Total Posts : 9287
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 09:23:42 (permalink) ☄ Helpfulby eagle1 2018/07/22 14:51:49
+1 (1)
You should always get a period of 200.  Try this:
printf("%u - %u = %u\n", data2, data1, period); 

 
1and0
Access is Denied
  • Total Posts : 9287
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 10:20:57 (permalink) ☄ Helpfulby eagle1 2018/07/22 14:52:23
+1 (1)
As Jorge said, printf() is the problem. Try this
while (1) { 
    PIR1bits.CCP1IF=0; // clear the capture flag bit
    while(PIR1bits.CCP1IF==0); // wait for capture input signal
    data1 = CCPR1; // copy the 1st data of capture data register
 
    PIR1bits.CCP1IF=0; // clear the capture flag bit
    while(PIR1bits.CCP1IF==0); // wait for another capture input signal
    data2 = CCPR1; // copy the 2nd data of capture data register
 
    period = data2 - data1;
    printf("%5u - %5u = %5u\n", data2, data1, period);
}

post edited by 1and0 - 2018/07/22 10:44:40
JorgeF
Super Member
  • Total Posts : 3340
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 11:16:41 (permalink) ☄ Helpfulby eagle1 2018/07/22 14:53:15
+1 (1)
Hi
 
In order not to get in this limit situations where code gets in the way, you should use test data that keeps things in a safe working range.
In this case you have a 1us instruction cycle time, so you should consider stimulus one order of magnitude above, better too use 100ms.
 
This limit situations do exist in real life, but, for now, you are only studying the operation of the CCP module, so play it safe.
When you get to fully understand how things work under normal circunstances, then go fight the tricky situations.
post edited by JorgeF - 2018/07/22 11:25:09

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
JorgeF
Super Member
  • Total Posts : 3340
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 11:20:28 (permalink) ☄ Helpfulby eagle1 2018/07/22 14:53:48
+1 (1)
Hi
eagle1
JorgeF
Hi
 
As your variables, data1, data2 and period, are "unsigned", you should use "%ud" instead of "%d" in the formating string of the "printf" function.

"%ud" added "d" with the numbers, so only "%u" worked with me and got this results:

You are correct, its plain "%u".
 

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
PStechPaul
Super Member
  • Total Posts : 2289
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 13:28:32 (permalink)
0
If you want to measure the period or duty cycle of an external signal, you only need to print or display it two to four times a second to be able to see it. So you could use a loop where you capture the time between two rising edges (for period), or from a rising edge to a falling edge. You could even capture rise1, fall1, and rise2, and then disable the module or stop the clock. Now you can take whatever time is needed to format and print/display the results, and delay 200-500 mSec before taking the next sample.
 
If you want to use just one CCP module to read the period and the duty cycle, set it up for every rising or falling edge. You can read the state of the signal at the first capture to see if it was high or low, and just reject it if low, so you will capture rise1 first.
 
If you want to capture every transition, you would need to store the values in an array or file, because there may not be enough time to calculate, format, and print. Once the sampling has captured enough data, you could perform whatever data processing you wish on the results.
 
If you want to determine the stability and variation of the waveform, you could compare successive samples and keep track of maximum and minimum deviations.
 
For a smoother reading, you can perform a rolling average of the samples.
 
I would probably use interrupts to save the captured time points, but waiting for the CCP1IF flag in a loop is OK if there are not many other things going on.

 
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 19:19:51 (permalink)
0
Where should be the default location for libraries for MPLAB X?
 
I found this one:
Program Files\Microchip\xc8\v1.45\include\plib
 
And also, in project folder, I added my LCD library and the code compiled OK. I want to install it in more general location so I can add it for more projects.
JorgeF
Super Member
  • Total Posts : 3340
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 19:38:49 (permalink) ☄ Helpfulby eagle1 2018/07/22 21:52:38
+2 (2)
Hi
eagle1
I found this one:
Program Files\Microchip\xc8\v1.45\include\plib

That is the default location for the "plibs" instalation the specific version (1.45) of XC8.
Be carefull that this folder can be deleted or its contents changed if you uninstall or reinstall the XC8 compiler.
 
Its better that you create a separate folder for your own libraries, and don't put it under the system controled folders like "program files" and "program_files(X86)".
 
When you want to use your own libs, just add the path in the project properties in MPLAB-X.
 

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
JorgeF
Super Member
  • Total Posts : 3340
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 20:00:31 (permalink) ☄ Helpfulby eagle1 2018/07/22 21:54:48
+1 (1)
Hi
 
Back to the question around "printf" and its large code and execution time footprint .
If you just need to convert numbers to ASCII and vice versa, there are specialized functions like "itoa" there are smaller and faster than "printf". If you need do add some text, place the strings or string section in ROM and assemble the final result by sending to the "UART" the text parts and the converted numbers in sequence.
Its a bit more coding but it pays when using an 8bit PIC.
 
Still on the subject, there is also the "double-dabble" (AKA shift-add 3) algorhitm to speed up things.
https://en.wikipedia.org/wiki/Double_dabble
 
I leave you here an implementation of it in ASM embedded in a 'C' function.
As 1and0 says, its a "too literal" implementation of the algorhitm as described in the Wikipedia.
I'm aware of it, but its also a "begginers" implementation that can easily be compared to the description.
For advanced versions visit http://www.microchip.com/forums/m853241.aspx

// Buffer for convertion of a 16bit binary number
// to a 5 digit ASCII string + NULL terminator
unsigned char Bin2AscArray[6];

// Binary --> BCD --> ASCII string
// In: binary number in Bin2AscArray[4:5]
// Out: ASCII string null terminated in Bin2AscArray
void Bin2Asc(void)
{
// Bin2AscArray status
// Bin2AscArray [0] - dont't care
// Bin2AscArray [1] - dont't care
// Bin2AscArray [2] - dont't care
// Bin2AscArray [3] - Binary high byte
// Bin2AscArray [4] - Binary low byte
// Bin2AscArray [5] - dont't care
 
#asm
banksel _Bin2AscArray
// Convert 16bit binary number to 5 digits in packed BCD format
// using the double-dabble algorithm
// AKA Shift-Add 3 beginners version (AKA for dummies))
// https://en.wikipedia.org/wiki/Double_dabble
// More advanced versions can be found here http://www.microchip.com/forums/m853241.aspx
// clear packed BCD buffer
clrf _Bin2AscArray+0
clrf _Bin2AscArray+1
clrf _Bin2AscArray+2
// Only need adjustements ( add 3 ) after the third shift
// Shift 1
bcf (STATUS), 0
rlcf _Bin2AscArray+4, F
rlcf _Bin2AscArray+3, F
rlcf _Bin2AscArray+2, F
rlcf _Bin2AscArray+1, F
rlcf _Bin2AscArray+0, F
// Shift 2
rlcf _Bin2AscArray+4, F
rlcf _Bin2AscArray+3, F
rlcf _Bin2AscArray+2, F
rlcf _Bin2AscArray+1, F
rlcf _Bin2AscArray+0, F
movlw 0x0d
movwf _Bin2AscArray+5

B2B_Loop:
// shifts with adjustement 3-15
bcf (STATUS), 0
rlcf _Bin2AscArray+4, F
rlcf _Bin2AscArray+3, F
rlcf _Bin2AscArray+2, F
rlcf _Bin2AscArray+1, F
rlcf _Bin2AscArray+0, F

PBCD_Digit0:
movlw 0x0f
andwf _Bin2AscArray+2, W
sublw 0x04
btfsc (STATUS), 0
bra PBCD_Digit1
movlw 0x03
addwf _Bin2AscArray+2, F

PBCD_Digit1:
movlw 0xf0
andwf _Bin2AscArray+2, W
sublw 0x40
btfsc (STATUS), 0
bra PBCD_Digit2
movlw 0x30
addwf _Bin2AscArray+2, F
PBCD_Digit2:
movlw 0x0f
andwf _Bin2AscArray+1, W
sublw 0x04
btfsc (STATUS), 0
bra PBCD_Digit3
movlw 0x03
addwf _Bin2AscArray+1, F
PBCD_Digit3:
movlw 0xf0
andwf _Bin2AscArray+1, W
sublw 0x40
btfsc (STATUS), 0
bra PBCD_Digit4
movlw 0x30
addwf _Bin2AscArray+1, F

PBCD_Digit4:
// movlw 0x0f    // this BCD digit never needs adjustement ( add 3))
// andwf _Bin2AscArray+0, W
// sublw 0x04
// btfsc (STATUS), 0
// bra PBCD_Digits_Done
// movlw 0x03
// addwf _Bin2AscArray+0, F

PBCD_Digits_Done:
decfsz _Bin2AscArray+5, F
bra B2B_Loop
// Shift 16
bcf (STATUS), 0
// rlcf _Bin2AscArray+4, F - already empty of significant data
rlcf _Bin2AscArray+3, F
rlcf _Bin2AscArray+2, F
rlcf _Bin2AscArray+1, F
rlcf _Bin2AscArray+0, F
 
// Bin2AscArray status
// Bin2AscArray [0] - Packed BCD digit 4 (decs thous)
// Bin2AscArray [1] - Packed BCD digits 3 and 2 (thous and huns)
// Bin2AscArray [2] - Packed BCD digits 1 and 0 (decs and units)
// Bin2AscArray [3] - 0 - dont't care
// Bin2AscArray [4] - 0 - dont't care
// Bin2AscArray [5] - NULL string terminator (left by the double-dabble loop)

// Expand Packed BCD digits (0x00~0x09)
// and convert to ASCII (0x30~0x39)
PB2A_Digit0:
movf _Bin2AscArray+2, W
andlw 0x0f
iorlw 0x30
movwf _Bin2AscArray+4

PB2A_Digit1:
swapf _Bin2AscArray+2, W
andlw 0x0f
btfss (STATUS), 2
bra PB2A_Digit1_NZ
movlw 0x20
movwf _Bin2AscArray+3
bra PB2A_Digit2
PB2A_Digit1_NZ:
iorlw 0x30
movwf _Bin2AscArray+3

PB2A_Digit2:
movf _Bin2AscArray+1, W
andlw 0x0f
btfss (STATUS), 2
bra PB2A_Digit2_NZ
movlw 0x20
movwf _Bin2AscArray+2
bra PB2A_Digit3
PB2A_Digit2_NZ:
iorlw 0x30
movwf _Bin2AscArray+2

PB2A_Digit3:
swapf _Bin2AscArray+1, W
andlw 0x0f
btfss (STATUS), 2
bra PB2A_Digit3_NZ
movlw 0x20
movwf _Bin2AscArray+1
bra PB2A_Digit4
PB2A_Digit3_NZ:
iorlw 0x30
movwf _Bin2AscArray+1

PB2A_Digit4:
movf _Bin2AscArray+0, W
andlw 0x0f
btfss (STATUS), 2
bra PB2A_Digit4_NZ
movlw 0x20
movwf _Bin2AscArray+0
bra PB2A_Done
PB2A_Digit4_NZ:
iorlw 0x30
movwf _Bin2AscArray+0
PB2A_Done:
// Bin2AscArray status
// Bin2AscArray [0] - ASCII digit 5 (decs thous)
// Bin2AscArray [1] - ASCII digit 3 (thous)
// Bin2AscArray [2] - ASCII digit 2 (hunds)
// Bin2AscArray [3] - ASCII digit 1 (decs)
// Bin2AscArray [4] - ASCII digit 0 (units)
// Bin2AscArray [5] - NULL string terminator (left by the double-dabble loop)
#endasm
}

Usage, simplified version, its like this

........ // converting an unsigned int variable "period" to ASCII
Bin2AscArray[4] = (unsigned char)(period & 0x00ff);
Bin2AscArray[3] = (unsigned char)(period >> 8);
Bin2Asc();
// --- Now just send the string in "Bin2AscArray" to the UART
// using "puts" or alike

Have fun.
 

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
JorgeF
Super Member
  • Total Posts : 3340
  • Reward points : 0
  • Joined: 2011/07/09 11:56:58
  • Location: PT/EU @ Third rock from the Sun
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 20:04:25 (permalink)
0
Oppsss.
 
Forgot to say that on my previous post, the code that expands the Packed BCD and converts it in ASCII replaces the "0" to the left with spaces.
 

Best regards
Jorge
 
I'm here http://picforum.ric323.com too!
And it works better....
eagle1
Super Member
  • Total Posts : 341
  • Reward points : 0
  • Joined: 2014/11/02 03:04:06
  • Location: Saudi Arabia
  • Status: offline
Re: I want to know the difference between capture and compare modes with examples 2018/07/22 22:00:29 (permalink)
0
JorgeF
Hi
 
Back to the question around "printf" and its large code and execution time footprint .
If you just need to convert numbers to ASCII and vice versa, there are specialized functions like "itoa" there are smaller and faster than "printf". If you need do add some text, place the strings or string section in ROM and assemble the final result by sending to the "UART" the text parts and the converted numbers in sequence.
Its a bit more coding but it pays when using an 8bit PIC.
 
 
I leave you here an implementation of it in ASM embedded in a 'C' function.
As 1and0 says, its a "too literal" implementation of the algorhitm as described in the Wikipedia.
I'm aware of it, but its also a "begginers" implementation that can easily be compared to the description.
For advanced versions visit http://www.microchip.com/forums/m853241.aspx
 
 
Usage, simplified version, its like this

........ // converting an unsigned int variable "period" to ASCII
 
Bin2AscArray[4] = (unsigned char)(period & 0x00ff);
Bin2AscArray[3] = (unsigned char)(period >> 8);
Bin2Asc();
// --- Now just send the string in "Bin2AscArray" to the UART
// using "puts" or alike

Have fun.

wow :) that's a lot of assembly. Yes the itoa would be a very good solution, also I tried sprintf.
 
But now I'm trying to implement my LCD1602 library into MPLAB projects folder which contain different projects, and I thought installing the library in this folder is enough, but it isn't.
 
I get this error:
program_main.c:79: error: (141) can't open include file "LCD1602_I2C_PIC.h": No such file or directory
 
I have to include the files in the same project folder and NOT in the folder which contain multiple projects.
 
How to configure the MPLAB for the directory?
Page: << < ..678910.. > >> Showing page 7 of 13
Jump to:
© 2019 APG vNext Commercial Version 4.5