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

PWMs, serial data and PIC16F876

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PWMs, serial data and PIC16F876
PostPosted: Sun Nov 20, 2016 12:45 pm     Reply with quote

compiler: CCS PCM C Compiler, Version 5.064, 33349
PIC: 16F876

Trying to get two PWMs running, one on CCP1 and the other on CCP2 on a PIC 16F876. I am also running the UART on the C6 and C7 pin at 4800 baud. (I know, I know, slow but I am running on unshielded lines for about 200 feet and not wanting troubles. Below is a code snippet of how I am setting up the hardware:
Code:

 if(PWM_MODE)  {         // Set up CCP1 and CCP2
    setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM for UV
    setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM for VISIBLE
   setup_timer_2(T2_DIV_BY_4,249,1); // 1000 Hz
    //enable_interrupts(INT_TIMER2);
    set_pwm1_duty(PWM_OFF_UV); // 0% duty cycle on pin C2
    set_pwm2_duty(PWM_OFF_VIS); // 0% duty cycle on pin C1
  }

set_rtcc(0);
setup_counters( RTCC_INTERNAL, RTCC_DIV_256 );  //15 interrupts/sec

enable_interrupts(INT_RTCC);                    // set up the RTCC counter
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);                      // turn on all interrupts.
setup_port_a(NO_ANALOGS);                     // no analogs at this time.

When I uncomment the enable_interrupts(INT_timer2) line, my serial stream stops.
How do you get PWMs to work with a serial stream? I am using RTCC to set a timer for data trasmission.
My serial port is set up as follows:
Code:

#FUSES XT,NOWDT,NOPROTECT,PUT,NOBROWNOUT,NOLVP,NODEBUG

#use delay(clock=4000000)
#use rs232(baud=4800,xmit=PIN_C6, rcv=PIN_C7,stream=USB_SERIAL,ERRORS)


Thanks all.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 20, 2016 12:59 pm     Reply with quote

Do you have an #int_timer2 interrupt routine ? If so, post it.
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PostPosted: Sun Nov 20, 2016 1:05 pm     Reply with quote

no int_timer2 routine. I copied this portion of code from some other working code and the timer2 enable call was after the CCPX setup calls. That code works. in my efforts to determine where my serial was dying, found that taking this line out saved the serial but then my PWMs do not work.
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PostPosted: Sun Nov 20, 2016 1:18 pm     Reply with quote

As a test, I turned the PWMs on to 50 percent right at the start, before I send any serial data. This caused the sent serial data to go garbled and lockup.
is there a problem with running the PWMs with a UART? Perhaps I need to be running faster than 4MHz to get all this done in time?
Ttelmah



Joined: 11 Mar 2010
Posts: 19236

View user's profile Send private message

PostPosted: Sun Nov 20, 2016 1:29 pm     Reply with quote

beaker404 wrote:
no int_timer2 routine. I copied this portion of code from some other working code and the timer2 enable call was after the CCPX setup calls. That code works. in my efforts to determine where my serial was dying, found that taking this line out saved the serial but then my PWMs do not work.


Point being made by PCM_programmer is you must never enable an interrupt without a handler. Doing so will result in the processor crashing as soon as the interrupt occurs. INT_TIMER2 gets triggered a few hundred machine cycles after you setup timer2. The 'working code' you took this from, must have had an INT_TIMER2 handler.

Just remove the enable for INT_TIMER2. There is no point in enabling it without a handler anyway....
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PostPosted: Sun Nov 20, 2016 1:32 pm     Reply with quote

Point taken, checked, no timer2 routine in the other code, so not sure what is up there, still not sure why the serial gets garbled on this code. So timer2 does not need to be enabled for PWM functions?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 20, 2016 3:07 pm     Reply with quote

Quote:

is there a problem with running the PWMs with a UART? Perhaps I need to
be running faster than 4MHz to get all this done in time?

What devices are your pwm signals driving ? If they are driving external
circuits, describe those circuits or post a link to a schematic. Also describe
the power supply for the circuits and devices driven by the PWM.

The point is, you probably have electrically noisy driver circuits or devices
and this causes noise on your power supply rails. This may then be
affecting your RS-232 drivers or the PIC.

It wouldn't hurt to post a link to a photo of the board.
beaker404



Joined: 24 Jul 2012
Posts: 163

View user's profile Send private message

PostPosted: Sun Nov 20, 2016 3:14 pm     Reply with quote

I think you are right, noisy board, I am driving FETS with the PWM that currently have no load on them.
This board is acting strangely, I put a continuous while(1) loop to stop the code after the PWMs where hard set to fixed values. Sometimes it will run and other times it seems to get to other code in the main() somehow.
This is a surface mount PCB I had to hand build. so tomorrow I will start slowly building a second PCB and testing it as I go.
As for power, 12VDC power supply running to a 7805 with .1uF caps on it.
Ttelmah



Joined: 11 Mar 2010
Posts: 19236

View user's profile Send private message

PostPosted: Sun Nov 20, 2016 3:45 pm     Reply with quote

INT_TIMER2, is _not_ required for PWM functions.

Did the other code have an 'INT_GLOBAL' (that is the only other way to handle the interrupt). Or was possibly the GLOBAL not enabled?.

Without a handler present, if you enable the interrupt and global, the code will crash in erratic ways.

I suspect you have two separate problems (electrical noise, and timer2 going off somewhere it shouldn't...).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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