Skip to content

Commit b8742cb

Browse files
committed
MPAE-10376: Initial Commit
1 parent c046a76 commit b8742cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+5663
-18306
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Note: Value of the resistor depends on the desired brightness, wavelength, and o
5454

5555
Note: To Reverse Direction, Swap "A" and "B" lines on the encoder.
5656

57-
### Encoder Wiring and Setup
57+
### Encoder Wiring and Demo Setup
5858

5959
Depending on the encoder, the specific circuit required for proper operation will vary. The manufacturer of the device will specify the circuits required to interface with the device.
6060

@@ -156,5 +156,13 @@ To prevent invalid reads, this example uses TMR1 and TMR3 in 16-bit read mode to
156156

157157
To find the net direction of rotation, subtract the net change of one of the timers from the other. The sign of this value indicates whether the encoder is turning clockwise or counter-clockwise. The magnitude of this value represents (approximately) how far it has rotated.
158158

159+
### CLC Implementation
160+
161+
![CLC1 Image](./images/CLC1.png)
162+
*Figure 11 - CLC 1 Implementation*
163+
164+
![CLC1 Image](./images/CLC2.png)
165+
*Figure 12 - CLC 2 Implementation*
166+
159167
## Summary
160168
In this example, a quadrature decoder was almost entirely implemented in hardware on the PIC18F16Q40 device. This implementation minimizes the amount of software required, freeing up the CPU to perform other tasks. This code example can be easily tailored for a specific application. The only requirements to implement the quadrature decoder (with no physical outputs or indicators) are 2 CLCs, TMR1, TMR2, and TMR3.

images/clc1.png

55.7 KB
Loading

images/clc2.png

55.7 KB
Loading

pic18f16q40-quadrature-decoder.X/config.mc3

Lines changed: 0 additions & 14312 deletions
This file was deleted.

pic18f16q40-quadrature-decoder.X/dist/free/production/pic18f16q40-quadrature-decoder.X.production.hex

Lines changed: 336 additions & 305 deletions
Large diffs are not rendered by default.

pic18f16q40-quadrature-decoder.X/encoder.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <stdint.h>
44
#include <stdbool.h>
55

6-
#include "mcc_generated_files/mcc.h"
6+
#include "mcc_generated_files/system/system.h"
77

88
//Net Encoder Change (in Pulses)
99
static volatile int16_t encoder;
@@ -17,6 +17,20 @@ void initEncoders(void)
1717
newEncoder = false;
1818
}
1919

20+
uint16_t getAsyncTimer1Value(void)
21+
{
22+
uint16_t t = TMR1L;
23+
t |= (TMR1H << 8);
24+
return t;
25+
}
26+
27+
uint16_t getAsyncTimer3Value(void)
28+
{
29+
uint16_t t = TMR3L;
30+
t |= (TMR3H << 8);
31+
return t;
32+
}
33+
2034
void readTMRs_ISR(void)
2135
{
2236
static volatile uint16_t old_TMR1 = 0;
@@ -25,8 +39,8 @@ void readTMRs_ISR(void)
2539
uint16_t new_TMR1, new_TMR3, net_TMR1, net_TMR3;
2640

2741
//Get Current Values
28-
new_TMR1 = TMR1_ReadTimer();
29-
new_TMR3 = TMR3_ReadTimer();
42+
new_TMR1 = getAsyncTimer1Value();
43+
new_TMR3 = getAsyncTimer3Value();
3044

3145
//Zero out the net change
3246
net_TMR1 = 0;

pic18f16q40-quadrature-decoder.X/encoder.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,21 @@ extern "C" {
4646
* readTMRs_ISR.
4747
* */
4848
int16_t getNewEncoderValue(void);
49-
49+
50+
/**
51+
* <B><FONT COLOR=BLUE>uint16_t</FONT> getAsyncTimer1Value(<FONT COLOR=BLUE>void</FONT>)</B>
52+
*
53+
* This function returns the TMR1 count without stopping the timer.
54+
* */
55+
uint16_t getAsyncTimer1Value(void);
56+
57+
/**
58+
* <B><FONT COLOR=BLUE>uint16_t</FONT> getAsyncTimer3Value(<FONT COLOR=BLUE>void</FONT>)</B>
59+
*
60+
* This function returns the TMR3 count without stopping the timer.
61+
* */
62+
uint16_t getAsyncTimer3Value(void);
63+
5064
#ifdef __cplusplus
5165
}
5266
#endif

pic18f16q40-quadrature-decoder.X/main.c

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,50 @@
1-
/**
2-
Generated Main Source File
3-
4-
Company:
5-
Microchip Technology Inc.
1+
/*
2+
Copyright (c) [2012-2020] Microchip Technology Inc.
63
7-
File Name:
8-
main.c
4+
All rights reserved.
95
10-
Summary:
11-
This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs
6+
You are permitted to use the accompanying software and its derivatives
7+
with Microchip products. See the Microchip license agreement accompanying
8+
this software, if any, for additional info regarding your rights and
9+
obligations.
1210
13-
Description:
14-
This header file provides implementations for driver APIs for all modules selected in the GUI.
15-
Generation Information :
16-
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.6
17-
Device : PIC18F16Q40
18-
Driver Version : 2.00
19-
*/
11+
MICROCHIP SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
12+
WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
13+
LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT
14+
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP OR ITS
15+
LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT
16+
LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE
17+
THEORY FOR ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT
18+
LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES,
19+
OR OTHER SIMILAR COSTS.
2020
21-
/*
22-
(c) 2018 Microchip Technology Inc. and its subsidiaries.
23-
24-
Subject to your compliance with these terms, you may use Microchip software and any
25-
derivatives exclusively with Microchip products. It is your responsibility to comply with third party
26-
license terms applicable to your use of third party software (including open source software) that
27-
may accompany Microchip software.
21+
To the fullest extend allowed by law, Microchip and its licensors
22+
liability will not exceed the amount of fees, if any, that you paid
23+
directly to Microchip to use this software.
2824
29-
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
30-
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
31-
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
32-
FOR A PARTICULAR PURPOSE.
33-
34-
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
35-
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
36-
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
37-
HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
38-
THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
39-
CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
40-
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
41-
SOFTWARE.
25+
THIRD PARTY SOFTWARE: Notwithstanding anything to the contrary, any
26+
third party software accompanying this software is subject to the terms
27+
and conditions of the third party's license agreement. To the extent
28+
required by third party licenses covering such third party software,
29+
the terms of such license will apply in lieu of the terms provided in
30+
this notice or applicable license. To the extent the terms of such
31+
third party licenses prohibit any of the restrictions described here,
32+
such restrictions will not apply to such third party software.
4233
*/
34+
#include "mcc_generated_files/system/system.h"
4335

44-
#include "mcc_generated_files/mcc.h"
4536
#include "encoder.h"
4637
#include "constants.h"
4738
#include "volume.h"
4839

40+
void __interrupt(irq(U1TX),base(8)) UART_TX_HOTFIX_ISR()
41+
{
42+
UART1_Transmit_ISR();
43+
}
44+
45+
#define TIMER1_16BIT_ENABLE() do { TMR1CONbits.RD16 = 0b1; } while (0)
46+
#define TIMER3_16BIT_ENABLE() do { TMR3CONbits.RD16 = 0b1; } while (0)
47+
4948
void main(void)
5049
{
5150
// Initialize the device
@@ -56,15 +55,21 @@ void main(void)
5655
initVolumeControl();
5756

5857
//Start Pulse Counters
59-
TMR1_StartTimer();
60-
TMR3_StartTimer();
58+
TIMER1_16BIT_ENABLE();
59+
Timer1_Write(0x0000);
60+
Timer1_Start();
61+
62+
TIMER3_16BIT_ENABLE();
63+
Timer3_Write(0x0000);
64+
Timer3_Start();
6165

6266
//Start Polling Timer
63-
TMR2_SetInterruptHandler(&readTMRs_ISR);
64-
TMR2_StartTimer();
67+
Timer2_OverflowCallbackRegister(&readTMRs_ISR);
68+
Timer2_Start();
6569

6670
// Enable the Global Interrupts
67-
INTERRUPT_GlobalInterruptEnable();
71+
INTERRUPT_GlobalInterruptHighEnable();
72+
INTERRUPT_GlobalInterruptLowEnable();
6873

6974
//Sum of all encoder values in the period
7075
int encoderCounter = 0;
@@ -109,7 +114,7 @@ void main(void)
109114
//Prevent Sleep while transmitting
110115
if (UART1_is_tx_done())
111116
{
112-
Sleep();
117+
//Sleep();
113118
asm("NOP");
114119
}
115120

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
CLC1 Generated Driver API Header File
3+
4+
@Company
5+
Microchip Technology Inc.
6+
7+
@File Name
8+
clc1.h
9+
10+
@Summary
11+
This is the generated header file for the CLC1 driver.
12+
13+
@Description
14+
This header file provides APIs for driver for CLC1 driver.
15+
Generation Information :
16+
Driver Version : 1.0.0
17+
The generated drivers are tested against the following:
18+
Compiler : XC8 v2.20
19+
MPLAB : MPLABX v5.40
20+
*/
21+
22+
/*
23+
Copyright (c) [2012-2020] Microchip Technology Inc.
24+
25+
All rights reserved.
26+
27+
You are permitted to use the accompanying software and its derivatives
28+
with Microchip products. See the Microchip license agreement accompanying
29+
this software, if any, for additional info regarding your rights and
30+
obligations.
31+
32+
MICROCHIP SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
33+
WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
34+
LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT
35+
AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP OR ITS
36+
LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT
37+
LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE
38+
THEORY FOR ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT
39+
LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES,
40+
OR OTHER SIMILAR COSTS.
41+
42+
To the fullest extend allowed by law, Microchip and its licensors
43+
liability will not exceed the amount of fees, if any, that you paid
44+
directly to Microchip to use this software.
45+
46+
THIRD PARTY SOFTWARE: Notwithstanding anything to the contrary, any
47+
third party software accompanying this software is subject to the terms
48+
and conditions of the third party's license agreement. To the extent
49+
required by third party licenses covering such third party software,
50+
the terms of such license will apply in lieu of the terms provided in
51+
this notice or applicable license. To the extent the terms of such
52+
third party licenses prohibit any of the restrictions described here,
53+
such restrictions will not apply to such third party software.
54+
*/
55+
56+
#ifndef CLC1_H
57+
#define CLC1_H
58+
59+
/**
60+
Section: Included Files
61+
*/
62+
63+
#include <xc.h>
64+
#include <stdint.h>
65+
#include <stdbool.h>
66+
67+
/**
68+
Section: CLC1 APIs
69+
*/
70+
71+
/**
72+
@Summary
73+
Initializes the CLC1
74+
75+
@Description
76+
This routine configures the CLC1 specific control registers
77+
78+
@Preconditions
79+
None
80+
81+
@Returns
82+
None
83+
84+
@Param
85+
None
86+
87+
@Comment
88+
89+
@Example
90+
<code>
91+
CLC1_Initialize();
92+
</code>
93+
*/
94+
void CLC1_Initialize(void);
95+
96+
/**
97+
@Summary
98+
Returns output pin status of the CLC module.
99+
100+
@Description
101+
This routine returns output pin status of the CLC module.
102+
103+
@Param
104+
None.
105+
106+
@Returns
107+
Output pin status
108+
109+
@Example
110+
<code>
111+
bool outputStatus;
112+
outputStatus = CLC1_OutputStatusGet();
113+
</code>
114+
*/
115+
116+
bool CLC1_OutputStatusGet(void);
117+
118+
#endif // CLC1_H
119+
/**
120+
End of File
121+
*/
122+

0 commit comments

Comments
 (0)