Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 17th January 2006, 06:25 AM   (permalink)
Default desperately seeking help using the PIC18F452 with the CCS C

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!!
amph1bius is offline   Reply With Quote
Old 17th January 2006, 01:09 PM   (permalink)
Default 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.
__________________
EEgeek.net
evandude is offline   Reply With Quote
Old 18th January 2006, 03:07 AM   (permalink)
Default

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).
__________________
I thought what I'd do was I'd pretend I was one of those deaf-mutes.
Oznog is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes




All times are GMT. The time now is 05:49 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.