View previous topic :: View next topic |
Author |
Message |
beaker404
Joined: 24 Jul 2012 Posts: 163
|
PWMs, serial data and PIC16F876 |
Posted: Sun Nov 20, 2016 12:45 pm |
|
|
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
|
|
Posted: Sun Nov 20, 2016 12:59 pm |
|
|
Do you have an #int_timer2 interrupt routine ? If so, post it. |
|
|
beaker404
Joined: 24 Jul 2012 Posts: 163
|
|
Posted: Sun Nov 20, 2016 1:05 pm |
|
|
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
|
|
Posted: Sun Nov 20, 2016 1:18 pm |
|
|
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: 19496
|
|
Posted: Sun Nov 20, 2016 1:29 pm |
|
|
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
|
|
Posted: Sun Nov 20, 2016 1:32 pm |
|
|
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
|
|
Posted: Sun Nov 20, 2016 3:07 pm |
|
|
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
|
|
Posted: Sun Nov 20, 2016 3:14 pm |
|
|
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: 19496
|
|
Posted: Sun Nov 20, 2016 3:45 pm |
|
|
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...). |
|
|
|