|
|
View previous topic :: View next topic |
Author |
Message |
Jerry I
Joined: 14 Sep 2003 Posts: 96 Location: Toronto, Ontario, Canada
|
Using CLC with PIC16F1619 (Solved) Noise problem found ?? |
Posted: Tue Mar 08, 2016 8:47 pm |
|
|
I am having a lot of trouble trying to understand using CLC in the PIC16F1619.
I got it working in the PIC 16F1509, but the PIC16F1619 is another animal. Not really the same.
I am trying to use 2 CLC's CLC1, and CLC2.
Trying to configure the inputs using CCS's functions. I have read the data sheet, I find it very confusing. This chip also uses pin_select also, which is confusing me even more. I have tried many versions, none have worked,
Required
CLCIN0 = PIN_A3
CLCIN1 = PIN_C7
CLCIN2 = PIN_C3
CLCIN3 = PIN_C4
CLC1OUT = PIN_RC5 -> T3CKI Output of CLC1 feeding T3CKI
CLC2OUT = PIN_RC0 -> T5CKI Output of CLC2 feeding T5CKI
I looked at MPLAB-X Code Configurator for help, Posted code at the end.
I have attached the screen from Code Configurator to show CLC Function required.
I have had no success using the code from Code Configurator or trying CCS functions.
I probably have the two parts of code conflicting. I would be fine if it would work with just CCS's Functions.
I looked for examples other than the manual, but I could not find any.
Microchips MPLAB-X Code configurator is no better with examples using CLC
I have found that this forum has always helped me, and I appreciate it very much.
Thanks again,
Jerry
[img]http://postimg.org/image/yyirm65fz/[/img]
Code: |
#device PIC16F1619
//#IFDEF PIC_16F1509
#include <16F1619.h>
#fuses intrc_io, nowdt, noprotect, nolvp, put
#fuses nomclr, nodebug
#device *=16
#device adc=8
//#ENDIF
// #include "i2cslave.h"
#include "CfgCLC.h"
#define CLOCK16MHZ
#define TEST_BIT rc1
#define TEST_BIT2 rc2
#case
#ifdef CLOCK16MHZ
#use delay(clock=16000000, internal) //one instruction=0.25us
#endif
#define BAUD1_SPEED 9600
#define BAUD2_SPEED 19200
#define BAUD3_SPEED 38400
#pin_select U1TX = PIN_B7
#pin_select U1RX = PIN_B5
#pin_select T3CKI = PIN_C5 ////////////////////////////////
#pin_select T5CKI = PIN_C0
#pin_select CLC1OUT = PIN_C5 // DO NOT KNOW IF THIS SECTION IS REQUIRED
#pin_select CLC2OUT = PIN_C0
#pin_select CLCIN0 = PIN_A3
#pin_select CLCIN1 = PIN_C7
#pin_select CLCIN2 = PIN_C3
#pin_select CLCIN3 = PIN_C4 /////////////////////////////////
#use rs232(baud=BAUD2_SPEED, ERRORS, UART1)
signed int16
DnCount, // Holds accumulated 'up' pulses
UpCount, // Holds accumulated 'down' pulses
prevDnCount, // Previous Holds accumulated 'up' pulses
prevUpCount, // Previous Holds accumulated 'down' pulses
mvelocity, // Measured motor velocity
prev_mvelocity // Holds prev UP/DOWN Counts
;
signed int32
travel_to,
position, // Commanded position.
mposition; // Actual measured position.
#int_timer2 // Quadrature Encoder read counters
void timer2_isr(void)
{
mvelocity = DnCount;
mvelocity -= UpCount;
prev_mvelocity = mvelocity;
// UpCount = tmr0cnt + get_timer0(); // Optional to use Timer0 & 1 with Timer0 Interupt Enabled
// DnCount = get_timer1();
UpCount = get_timer3();
DnCount = get_timer5();
// Add new count values into result.
mvelocity += UpCount;
mvelocity -= DnCount;
// Add measured velocity to measured position to get new motor
// measured position.
mposition += (signed int32)(mvelocity * 256);
}
void main()
{
signed int16 cnt = 0;
setup_adc(ADC_OFF);
TRISA = 0b00001001;
port_a_pullups(0x24);
TRISB = 0b11110111; //
TRISC = 0b11011000; //
//! clc1_setup_input(1, CLC_INPUT_CLCIN0); ///////////////////////////////////
//! clc1_setup_input(4, CLC_INPUT_CLCIN1);
//! CCS Functions Not sure if I am using them properly
//! clc2_setup_input(1, CLC_INPUT_CLCIN2);
//! clc2_setup_input(4, CLC_INPUT_CLCIN3); //////////////////////////////////
setup_timer_3(T1_EXTERNAL|T1_DIV_BY_1|T1_EXTERNAL_SYNC); // 1 ms refresh rate
setup_timer_5(T1_EXTERNAL|T1_DIV_BY_1|T1_EXTERNAL_SYNC); // 1 ms refresh rate
set_timer3(0L);
set_timer5(0L);
cfgCLC_Dual();
printf("Quaderature Encoder Test \r\n");
while(TRUE)
{
printf("UpCount = %ld DnCount = %ld MVelocity = %ld\r\n\r\n", UpCount, DnCount, mvelocity);
delay_ms(2000);
}
}
|
cfgCLC.h
Code: |
#byte CLC1GLS0 = getenv("SFR:CLC1GLS0")
#byte CLC1GLS1 = getenv("SFR:CLC1GLS1")
#byte CLC1GLS2 = getenv("SFR:CLC1GLS2")
#byte CLC1GLS3 = getenv("SFR:CLC1GLS3")
#byte CLC1SEL0 = getenv("SFR:CLC1SEL0")
#byte CLC1SEL1 = getenv("SFR:CLC1SEL1")
#byte CLC1SEL2 = getenv("SFR:CLC1SEL2")
#byte CLC1SEL3 = getenv("SFR:CLC1SEL3")
#byte CLC1POL = getenv("SFR:CLC1POL")
#byte CLC1CON = getenv("SFR:CLC1CON")
#byte CLCDATA = getenv("SFR:CLCDATA")
#byte CLC2GLS0 = getenv("SFR:CLC2GLS0")
#byte CLC2GLS1 = getenv("SFR:CLC2GLS1")
#byte CLC2GLS2 = getenv("SFR:CLC2GLS2")
#byte CLC2GLS3 = getenv("SFR:CLC2GLS3")
#byte CLC2SEL0 = getenv("SFR:CLC2SEL0")
#byte CLC2SEL1 = getenv("SFR:CLC2SEL1")
#byte CLC2SEL2 = getenv("SFR:CLC2SEL2")
#byte CLC2SEL3 = getenv("SFR:CLC2SEL3")
#byte CLC2POL = getenv("SFR:CLC2POL")
#byte CLC2CON = getenv("SFR:CLC2CON")
#bit MLC21OUT = CLCDATA.0
#bit MLC22OUT = CLCDATA.1
#bit MLC23OUT = CLCDATA.2
#bit MLC24OUT = CLCDATA.3
//#bit MLC22OUT = CLC2CON.5
#byte TRISA = getenv("SFR:TRISA")
#byte TRISB = getenv("SFR:TRISB")
#byte TRISC = getenv("SFR:TRISC")
// ------ TRISC Bits -----------------------------------
#bit trisc7 = TRISC.7
#bit trisc6 = TRISC.6
#bit trisc5 = TRISC.5
#bit trisc4 = TRISC.4
#bit trisc3 = TRISC.3
#bit trisc2 = TRISC.2
#bit trisc1 = TRISC.1
#bit trisc0 = TRISC.0
// ------ TRISB Bits ----------------------------------
#bit trisb7 = TRISB.7
#bit trisb6 = TRISB.6
#bit trisb5 = TRISB.5
#bit trisb4 = TRISB.4
#bit trisb3 = TRISB.3
#bit trisb2 = TRISB.2
#bit trisb1 = TRISB.1
#bit trisb0 = TRISB.0
// ------- TRISA Bits -----------------------------------
#bit trisa6 = TRISA.6
#bit trisa5 = TRISA.5
#bit trisa4 = TRISA.4
#bit trisa3 = TRISA.3
#bit trisa2 = TRISA.2
#bit trisa1 = TRISA.1
#bit trisa0 = TRISA.0
#byte PORTA = getenv("SFR:PORTA")
#byte PORTB = getenv("SFR:PORTB")
#byte PORTC = getenv("SFR:PORTC")
// ------- PORTC Bits -----------------------------------
#bit portc7 = PORTC.7
#bit portc6 = PORTC.6
#bit portc5 = PORTC.5
#bit portc4 = PORTC.4
#bit portc3 = PORTC.3
#bit portc2 = PORTC.2
#bit portc1 = PORTC.1
#bit portc0 = PORTC.0
#bit rc7 = PORTC.7
#bit rc6 = PORTC.6
#bit rc5 = PORTC.5
#bit rc4 = PORTC.4
#bit rc3 = PORTC.3
#bit rc2 = PORTC.2
#bit rc1 = PORTC.1
#bit rc0 = PORTC.0
// ------- PORTB Bits -----------------------------------
#bit portb7 = PORTB.7
#bit portb6 = PORTB.6
#bit portb5 = PORTB.5
#bit portb4 = PORTB.4
#bit portb3 = PORTB.3
#bit portb2 = PORTB.2
#bit portb1 = PORTB.1
#bit portb0 = PORTB.0
#bit rb7 = PORTB.7
#bit rb6 = PORTB.6
#bit rb5 = PORTB.5
#bit rb4 = PORTB.4
#bit rb3 = PORTB.3
#bit rb2 = PORTB.2
#bit rb1 = PORTB.1
#bit rb0 = PORTB.0
// ------- PORTA Bits -----------------------------------
#bit porta6 = PORTA.6
#bit porta5 = PORTA.5
#bit porta4 = PORTA.4
#bit porta3 = PORTA.3
#bit porta2 = PORTA.2
#bit porta1 = PORTA.1
#bit porta0 = PORTA.0
#bit ra6 = PORTA.6
#bit ra5 = PORTA.5
#bit ra4 = PORTA.4
#bit ra3 = PORTA.3
#bit ra2 = PORTA.2
#bit ra1 = PORTA.1
#bit ra0 = PORTA.0
#byte LATA = getenv("SFR:LATA")
#byte LATB = getenv("SFR:LATB")
#byte LATC = getenv("SFR:LATC")
// ------- LATC Bits -----------------------------------
#bit latc7 = LATC.7
#bit latc6 = LATC.6
#bit latc5 = LATC.5
#bit latc4 = LATC.4
#bit latc3 = LATC.3
#bit latc2 = LATC.2
#bit latc1 = LATC.1
#bit latc0 = LATC.0
// ------- LATB Bits -----------------------------------
#bit latb7 = LATB.7
#bit latb6 = LATB.6
#bit latb5 = LATB.5
#bit latb4 = LATB.4
#bit latb3 = LATB.3
#bit latb2 = LATB.2
#bit latb1 = LATB.1
#bit latb0 = LATB.0
// ------- LATA Bits -----------------------------------
#bit lata6 = LATA.6
#bit lata5 = LATA.5
#bit lata4 = LATA.4
#bit lata3 = LATA.3
#bit lata2 = LATA.2
#bit lata1 = LATA.1
#bit lata0 = LATA.0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function cfgCLC_Dual(void) Dual Encoders Using CHA & CHB
//
// Dual Encoders A&B Using CLC1 CHA=RA3 CHB=RC7, Output RC5 -> T3CKI
// Dual Encoders A&B Using CLC2 CHA=RC3 CHB=RC4, Output RC0 -> T5CKI
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void cfgCLC_Dual(void) // Timer3 & Timer 5
{
//! setup_clc1(CLC_ENABLED | CLC_OUTPUT | CLC_MODE_1_INPUT_D_FLIP_FLOP);
//! clc1_setup_input(1, CLC_INPUT_CLCIN0);
//! clc1_setup_input(2, CLC_INPUT_CLCIN1);
setup_clc1(CLC_ENABLED | CLC_OUTPUT | CLC_MODE_1_INPUT_D_FLIP_FLOP); // REQUIRED ???
CLC1GLS0 = 0x80; //////////////////////////////////////////
CLC1GLS1 = 0x02;
CLC1GLS2 = 0x02; CODE FROM MPLAB-X
CLC1GLS3 = 0x00;
CLC1SEL0 = 0x00; CODE CONFIGURATOR
CLC1SEL1 = 0x00;
CLC1SEL2 = 0x01;
CLC1SEL3 = 0x01;
CLC1POL = 0x04;
CLC1CON = 0x84; //////////////////////////////////////////
setup_clc2(CLC_ENABLED | CLC_OUTPUT | CLC_MODE_1_INPUT_D_FLIP_FLOP);
CLC2GLS0 = 0x02;
CLC2GLS1 = 0x80;
CLC2GLS2 = 0x80;
CLC2GLS3 = 0x00;
CLC2SEL0 = 0x02;
CLC2SEL1 = 0x02;
CLC2SEL2 = 0x03;
CLC2SEL3 = 0x03;
CLC2POL = 0x04;
CLC2CON = 0x84;
}
|
Last edited by Jerry I on Thu Mar 10, 2016 7:19 pm; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Wed Mar 09, 2016 6:15 am |
|
|
OK, I haven't used that PIC yet BUT....
you need to step back and cut a SIMPLE program.
Please get rid of the ISRs, unused variables, functions, non essential headers, etc.
You should only have basic setup of PIC(fuses and clock), the pin selects for your I/O and a simple 'main()', something like a CLC output which has LED 'follows a CLC input with pushbutton'.
Consider this to be the CLC similar to the 1Hz LED program.
Cut this code, compile, run and report back posting your program. It is a LOT easier to debug without all the 'extra stuff' in your current program AND it allows others to 'cut/paste/run' on their systems.
Jay |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Mar 09, 2016 7:10 am |
|
|
Jay's right.
I took one glance at the pages of code and gave up.
Work on the KISS principle.
Mike |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Mar 09, 2016 7:48 am |
|
|
Setting TRIS for a port is a fools errand in CCS -C UNLESS you declare #use fast_io(). |
|
|
Jerry I
Joined: 14 Sep 2003 Posts: 96 Location: Toronto, Ontario, Canada
|
|
Posted: Thu Mar 10, 2016 7:17 pm |
|
|
I took some advice and created a pass thru with just a AND Gate function. I had 2 problems, broken wire on board, and also my TRIS was set for inputs. I corrected the problems and appears to work fine.
I re-coded for my application and is also working. One thing I noticed, I have Quadrature signals A&B feeding the CLC. When rotating CW Upcount increases, CCW DNcount increases which is correct.
The output of the CLC1out feeds T3CKI and CLC2out feeds T5CKI inputs. This is an internal connection using #pin_select as below.
Code: |
#pin_select T3CKI = PIN_C5
#pin_select T5CKI = PIN_C0
#pin_select CLC1OUT = PIN_C5
#pin_select CLC2OUT = PIN_C0
|
No physical wire added for connections.
When the motor is rotating CW UPcount increases which is correct but if I power on a soldering iron, or my PS internal cooling fan turns on. The DNcount will value will change. The scope did not show me any spikes, I may have to try to trigger and capture the event. I do have decoupling cap. The board is just a hand wired prototype, not the best.
Any ideas.
Thanks again in advance,
Jerry I |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Thu Mar 10, 2016 7:46 pm |
|
|
Could be EMI radiated through the PIC power supply. The usual stuff to do is caps, chokes, ferrites to suppress the EMI. Switching power supplies are 'noisy' compared to linears so you do have to be diligent about 'cleaning' up the power. It should be at least 5X motor stall current BTW.
You talk about a motor and a PIC so you need REAL good decoupling of the motor power supply from the PICs power ! If not, then PIC's VDD could drop just enough to cause an internal 'glitch'. Be sure to put bypass caps AT the PIC power pins. Also be sure both ground and motor supply are capable of the motor currents and that the PIC is NOT between the power supply and motor. Excessive ground current fluctuations can sometimes be 'interesting'.
Without seeing a schematic or picture of hardware it's hard to say what else to do.
Jay |
|
|
|
|
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
|