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

ECCP1 setup for PIC18F4685 [SOLVED]
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sun Jun 17, 2018 12:33 pm     Reply with quote

That's down to thought.

For example. Imagine you are reading an ADC. 0 to 5v for 0-1024 (though ypu never get the 1024).
Floating point answer. adc_val*5.0/1024.
Problem this involves converting the adc_val to float, then floating point maths. Result several hundred mSec to do this.

Integer version.
(adv_val*64)/131
Done in an int16, gives 0 to 500, which can then be printed using %4.2LW, to show 0.00 to 5.00.
Know not only does this take perhaps less thab 1/10th the time, but the actual printout is also this much quicker for each digit!...

Floats are relatively inaccurate, and very slow. Their 'simplicity' has huge costs in processing terms. For any application starting with numbers in a fixed range, it is far better to use integers if you can.
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Sun Jun 17, 2018 1:01 pm     Reply with quote

PCM programmer,
Thank you for clearing the setup_ccp1 for me. I could not got my project to work without your help, I really appreciate all your time and help.

Ttelmah,
Thank you for your input. for my own information:
converting:
Code:

float per=0.50;

to
Code:

int8 per=50;

then convert:
Code:

dty = pr2 * per;

to
Code:

dty = (pr2 * per) /100;

finally converting:
Code:

printf(lcd_putc,"\fFrequency: %lu\n", val);
printf(lcd_putc,"DutyCycle: %0.2f", per);

to:
Code:

printf(lcd_putc,"\fFrequency: %lu\n", val);
printf(lcd_putc,"DutyCycle: %u", per);


and maybe move the:
Code:

delay_ms(10);

to the bottom of each of the if statements, will result into more efficient code?
FYI: for this project, that is it I am not planning to add more hardware, but I would like to learn and comment my code for future projects.

also: what is you thought about my switch statement? The default bother me and I am not sure how can I improve it?

Thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 17, 2018 5:01 pm     Reply with quote

Sam_40 wrote:

and maybe move the:

delay_ms(10);

to the bottom of each of the if statements, will result into more efficient code?

To remove the delay_ms(10) statement, try using the Timer interrupt
version of the button code, at this link:
http://www.ccsinfo.com/forum/viewtopic.php?t=31849&start=4

Based on my testing with the MPLAB vs. 8.92 simulator stopwatch program,
it will take a total of about 1.25 ms to process the #int_timer0 interrupt
and call the button() function 4 times (once for each button). This is with
your oscillator frequency of 1 MHz.
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Mon Jun 18, 2018 3:39 pm     Reply with quote

PCM programmer,
Your testing with the simulator, did you use the float variable or did you use the integer version that I wrote?

Also what is your thought about my switch statement?
Code:

void callT2(int8 frq)
{
switch (frq)
         {
         case 1:
            setup_timer_2(T2_DIV_BY_16, 255, 1);/*61Hz*/
            pr2=256;
            val=61;
            break;
         // Other cases
         default:
            setup_timer_2(T2_DIV_BY_16, 38, 1); /*400Hz*/
            pr2=39;
            val=400;
            break;       
        }
}

Its working fine but for some reason my default: doesn't look right? IDK why?

Also I been reading the datasheet about setting timer 0. I can't find the formula to figure how to set timer 0 if I want it to interrupt every 10mS for external 1MHz OSC?


Finally, The IGBT datasheet shows the MAX t-off and t-fall is 2uS however in the circuit I have (measuring with oscilloscope) from the PIC output falling edge to the IGBT falling edge is about 2.3uS. So I think 4uS is Okay for the dead band? I thought about using smaller than 1mHz or bigger but 1MHz seems to be the best oscillator for this project? I originally needed 30Hz-400Hz but I had to go with 60-400Hz as smaller OSC will yield more dead band time. What is your thoughts?

Thank you


Last edited by Sam_40 on Tue Jun 26, 2018 7:37 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 18, 2018 5:00 pm     Reply with quote

Quote:
did you use the float variable or did you use the integer version that I wrote?

Did you look at the interrupt routine in the link that I posted ?
There is no floating point involved. It just reads the 4 switches every 10 ms
and records if any of them were pressed. This is the routine:
Code:
#int_rtcc
void rtcc_isr(void)
{
set_timer0(T0_PRELOAD); // Put a breakpoint here

if(button(PIN_B0, 0, 50, 10, B0, 1))   
   B0_pressed = TRUE; 

if(button(PIN_B1, 0, 50, 10, B1, 1))   
   B1_pressed = TRUE; 

if(button(PIN_B2, 0, 50, 10, B2, 1))   
   B2_pressed = TRUE; 
   
if(button(PIN_B3, 0, 50, 10, B3, 1))   
   B3_pressed = TRUE;

delay_cycles(1);   // Add this line so I can put a breakpoint on it
}


Quote:
what is your thought about my switch statement?

I don't have any objections to it, except that I don't like block comments
on individual lines. If you do that, it prevents using them to comment out
sections of code temporarily for debugging, since nesting is not allowed.

Quote:
for some reason my default: doesn't look right?

The default case is there to handle any switch values that are out of range.
So if 400 Hz is what you want it to do if you accidentally give it an incorrect
switch value, then it's fine with me.

Quote:

I can't find the formula to figure how to set timer 0 if I want it to interrupt
every 10mS for external 1MHz OSC?

This should do it:
Code:
#define T0_PRELOAD (65536-2500)   // Timer0 in 16-bit mode

// Setup Timer0 for a 10 ms interrupt rate.
setup_timer_0(T0_INTERNAL | T0_DIV_1);
set_timer0(T0_PRELOAD);


Quote:
So I think 4uS is Okay for the dead band?

I don't know. I will have to look at it later.
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Mon Jun 18, 2018 9:18 pm     Reply with quote

PCM programmer,
Quote:
Did you look at the interrupt routine in the link that I posted ?
There is no floating point involved. It just reads the 4 switches every 10 ms
and records if any of them were pressed.

I thought you ran my code.

Quote:
I don't like block comments
on individual lines. If you do that, it prevents using them to comment out
sections of code temporarily for debugging, since nesting is not allowed.

Duly noted, Thank you for point that out.


Quote:
#define T0_PRELOAD (65536-2500) // Timer0 in 16-bit mode

// Setup Timer0 for a 10 ms interrupt rate.
setup_timer_0(T0_INTERNAL | T0_DIV_1);
set_timer0(T0_PRELOAD);

Would you please explain how you come up with these numbers? I only got the 65536 for 16 bits to over flow. Why did you use 16 bits VS 8?


Quote:
I don't know. I will have to look at it later.

You are very resourceful and way more knowledgeable than I, Please let me know whenever you find out.

I convert the float to integer, I also used your last code and this is my main, would you please take a look and see if I can improve it more, everything seems to work OK:
Code:

void main()

   // Setup Timer0 for a 10 ms interrupt rate.
   setup_timer_0(T0_INTERNAL | T0_DIV_1);
   set_timer0(T0_PRELOAD);
   
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
   frq = 1;
   dty=128;
   per=50;
   set_tris_b(0X0f);
   setup_eccp1(CCP_PWM_HALF_BRIDGE | CCP_PWM_H_H);
   callT2(frq);
   ECCP1DEL_bit0 = 1; /*Set the bit0 for 1 instruction cycle (4uS)*/
   set_eccp1_duty(dty);
   delay_ms(500); /*Delay for the LCD to start*/
   lcd_init();
   // The loop goes here
}

Thank you


Last edited by Sam_40 on Tue Jun 26, 2018 7:39 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 19, 2018 9:35 am     Reply with quote

Quote:
#define T0_PRELOAD (65536-2500) // Timer0 in 16-bit mode

// Setup Timer0 for a 10 ms interrupt rate.
setup_timer_0(T0_INTERNAL | T0_DIV_1);
set_timer0(T0_PRELOAD);

Would you please explain how you come up with these numbers? I only
got the 65536 for 16 bits to over flow. Why did you use 16 bits VS 8?

I got lazy. I used a PIC Timer0 calculator (searched for with Google).
It gave 63036 as the preload. It's not that hard to figure out, but I just
got really, really lazy. One of the reasons I got lazy is because you kept
bombardng me with questions, and I wanted to go do something that I
wanted to do, instead of answering questions all afternoon. I wanted to
answer as many of your questions as quickly as possible and then bail.

Timer0 counts up. With a 1 MHz oscillator, each instruction cycle takes
4 usec. With a T0 divisor of 1, each T0 clock also takes 4 usec.
So, if you want a 10 ms (10000 usec) interrupt interval, 10000 / 4 = 2500.
Since Timer0 interrupts when it rolls over from 65535 to 0, you subtract
2500 from 65536 to get the preload. This is shown in the #define statement.

I used 16-bit mode because the online calculator used it, but also because
it allows me a granularity of only 4 usec. I can get the 10 ms result exactly.
Sam_40



Joined: 07 Jan 2015
Posts: 127

View user's profile Send private message

PostPosted: Tue Jun 19, 2018 8:20 pm     Reply with quote

Thank you for clearing my question about Timer0.
I am so sorry for asking so many questions, I really appreciate all of your time, I have learned a lot from you. Everything is working OK.

Thank you,
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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