Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

desperately seeking help using the PIC18F452 with the CCS C

Status
Not open for further replies.

amph1bius

New Member
I'm desperately seeking help using the PIC18F452 with the CCS C Compiler.

Heres my code:

Code:
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#bit TMR2IF=0xF9E.1

void main(void)
{

 output_high(PIN_A0);
 SET_TRIS_C(0b00000000);
 SETUP_TIMER_2(T2_DIV_BY_1,128,1);
 SET_PWM1_DUTY(0);
 SETUP_CCP1(CCP_PWM); // PUT THE CCP1 PORT INTO PWM MODE
 

TMR2IF=0;
while (TRUE) {
set_pwm1_duty(120); 
while (TMR2IF==0);

}
}

I have the CCP1 port hooked into an NPN transistor which should control the motor, however, it only turns on the motor for a random amount of time, and there is a random amount of delay until it repeats again. I have a 270ohm resistor on CCP1 and a pulldown of 10k.

I've tried everything but I can't get it to run continuously.

I have the MCLR pin tied to high through a 10k resistor. I've also tried directly tieing it to high, but no luck.

Please help, thank you!!
 
Re: desperately seeking help using the PIC18F452 with the CC

Code:
 output_high(PIN_A0);
 SET_TRIS_C(0b00000000);
 SETUP_TIMER_2(T2_DIV_BY_1,128,1);
 SET_PWM1_DUTY(0);
 SETUP_CCP1(CCP_PWM); // PUT THE CCP1 PORT INTO PWM MODE

it's canned, proprietary functions like these that make me really wonder why anyone uses some of these C compilers... whatever happened to accessing registers yourself, using the good old fashioned '=' operator, or maybe even some bitwise operators? it's no wonder it's impossible to port code from one PIC C compiler to another.

sorry I'm not really helping with your problem, I don't use CCS as you can tell, but I would highly suggest you take a look at some of the other compilers, especially BoostC. at least when you are using standard C operators and functions, and not compiler-specific, arbitrary, hidden routines people with knowledge of C but without knowledge of your specific compiler will be able to provide a little assistance.
 
Never used BoostC. HiTech's PICC18 and Microchip's own MCC18 are good. MCC18 has a free version, though not fully optimized like their pay version.

CCS strongly encourages proprietary functions, and sadly forgot to implement all of ANSI C functionality along the way. Including some very important stuff. IMHO there's no reason whatsoever to use it once free MCC18 came out.

I share evandude's sentiments on proprietary functions. Direct code is SO much more practical.

I'm not sure what you're trying to do. Rewriting the PWM period is unnecessary and can introduce glitches. This code is bad:

Code:
TMR2IF=0;
while (TRUE) {
set_pwm1_duty(120);
while (TMR2IF==0);
}

TMR2IF is not cleared within the loop, so it will just execute set_pwm1_duty in a continuous loop. You probably want TMR2IF=0 at the top of the while loop, but then again I don't see any reason for the loop at all. Just
Code:
set_pwm1_duty(120);
while(TRUE);

should be able to replace all the code I quoted above..

I would first suspect your system is being reset after getting screwed by electrical motor noise. Make sure you have a ceramic cap right on the Vdd to Vcc pins. Put a cap on the motor's supply. Try to isolate the motor's supply from the chip's supply (like powering it off the +5v reg may be a problem for noise).
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top