Compare commits
3 Commits
78842c48dc
...
b3efac5a67
Author | SHA1 | Date |
---|---|---|
Tiago Garcia | b3efac5a67 | |
Tiago Garcia | 282dac9853 | |
Tiago Garcia | 9877d543f4 |
Binary file not shown.
|
@ -0,0 +1,16 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
int main() {
|
||||
T3CONbits.TCKPS = 7; // 1:256 prescaler
|
||||
PR3 = 39062; // Fout = 20MHz / (256 * (39062 + 1)) = 2Hz
|
||||
TMR3 = 0; // Reset timer T3 count register
|
||||
T3CONbits.TON = 1; // Enable timer T3 (must be the last command of the timer configuration sequence)
|
||||
|
||||
while (1) {
|
||||
while (IFS0bits.T3IF == 0); // Wait while T3IF is 0
|
||||
IFS0bits.T3IF = 0; // Reset T3IF
|
||||
putChar('.');
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
void _int_(12) isr_T3(void) {
|
||||
putChar('.');
|
||||
IFS0bits.T3IF = 0; // Reset timer T3 interrupt flag
|
||||
}
|
||||
|
||||
int main() {
|
||||
T3CONbits.TCKPS = 7; // 1:256 prescaler
|
||||
PR3 = 39062; // Fout = 20MHz / (256 * (39062 + 1)) = 2Hz
|
||||
TMR3 = 0; // Reset timer T3 count register
|
||||
T3CONbits.TON = 1; // Enable timer T3 (must be the last command of the timer configuration sequence)
|
||||
|
||||
IPC3bits.T3IP = 2; // Interrupt priority (must be in range [1..6])
|
||||
IEC0bits.T3IE = 1; // Enable timer T3 interrupts
|
||||
IFS0bits.T3IF = 0; // Reset timer T3 interrupt flag
|
||||
|
||||
EnableInterrupts(); // Global Interrupt Enable
|
||||
while (1);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
void _int_(12) isr_T3(void) {
|
||||
static int flag = 0;
|
||||
if (flag == 0) putChar('.');
|
||||
flag = !flag;
|
||||
IFS0bits.T3IF = 0; // Reset timer T3 interrupt flag
|
||||
}
|
||||
|
||||
int main() {
|
||||
T3CONbits.TCKPS = 7; // 1:256 prescaler
|
||||
PR3 = 39062; // Fout = 20MHz / (256 * (39062 + 1)) = 2Hz
|
||||
TMR3 = 0; // Reset timer T3 count register
|
||||
T3CONbits.TON = 1; // Enable timer T3 (must be the last command of the timer configuration sequence)
|
||||
|
||||
IPC3bits.T3IP = 2; // Interrupt priority (must be in range [1..6])
|
||||
IEC0bits.T3IE = 1; // Enable timer T3 interrupts
|
||||
IFS0bits.T3IF = 0; // Reset timer T3 interrupt flag
|
||||
|
||||
EnableInterrupts(); // Global Interrupt Enable
|
||||
while (1);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
int counters[] = {0, 0};
|
||||
|
||||
void print() {
|
||||
putChar('\r');
|
||||
printStr("T1: ");
|
||||
printInt10(0[counters]);
|
||||
printStr(" T3: ");
|
||||
printInt10(1[counters]);
|
||||
}
|
||||
|
||||
void _int_(4) isr_T1(void) {
|
||||
counters[0]++;
|
||||
LATD ^= 0x0001;
|
||||
LATE ^= 0x0002;
|
||||
print();
|
||||
IFS0bits.T1IF = 0; // Reset timer T1 interrupt flag
|
||||
}
|
||||
|
||||
void _int_(12) isr_T3(void) {
|
||||
counters[1]++;
|
||||
LATD ^= 0x0004;
|
||||
LATE ^= 0x0008;
|
||||
print();
|
||||
IFS0bits.T3IF = 0; // Reset timer T3 interrupt flag
|
||||
}
|
||||
|
||||
int main() {
|
||||
T1CONbits.TCKPS = 2;
|
||||
PR1 = 62499;
|
||||
TMR1 = 0;
|
||||
T1CONbits.TON = 1;
|
||||
|
||||
T3CONbits.TCKPS = 4;
|
||||
PR3 = 49999;
|
||||
TMR3 = 0;
|
||||
T3CONbits.TON = 1;
|
||||
|
||||
IPC1bits.T1IP = 2; // Interrupt priority (must be in range [1..6])
|
||||
IEC0bits.T1IE = 1; // Enable timer T1 interrupts
|
||||
IFS0bits.T1IF = 0; // Reset timer T1 interrupt flag
|
||||
|
||||
IPC3bits.T3IP = 2; // Interrupt priority (must be in range [1..6])
|
||||
IEC0bits.T3IE = 1; // Enable timer T3 interrupts
|
||||
IFS0bits.T3IF = 0; // Reset timer T3 interrupt flag
|
||||
|
||||
TRISD &= 0xFFFA;
|
||||
LATD &= 0xFFFA;
|
||||
TRISE &= 0xFFF5;
|
||||
LATE &= 0xFFF5;
|
||||
|
||||
EnableInterrupts(); // Global Interrupt Enable
|
||||
while (1);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
Timer 1
|
||||
|
||||
K > 20000000 / (65536 * 5)
|
||||
K > 61.035
|
||||
K = 64
|
||||
|
||||
PR1 = ((20000000 / 64) / 5) - 1 = 62499
|
||||
|
||||
------------------------------------------------
|
||||
|
||||
Timer 3
|
||||
|
||||
K > 20000000 / (65536 * 25)
|
||||
K > 12.207
|
||||
K > 16
|
||||
|
||||
PR3 = ((20000000 / 16) / 25) - 1 = 49999
|
|
@ -0,0 +1,21 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
void delay(unsigned int ms) {
|
||||
resetCoreTimer();
|
||||
while (readCoreTimer() < 20000 * ms);
|
||||
}
|
||||
|
||||
int main() {
|
||||
TRISD |= 0x0100;
|
||||
TRISE &= 0xFFFE;
|
||||
LATE &= 0xFFFE;
|
||||
|
||||
while (1) {
|
||||
while (((PORTD >> 8) & 0x0001) == 1);
|
||||
LATE ^= 0x0001;
|
||||
delay(3000);
|
||||
LATE ^= 0x0001;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
void _int_(8) isr_T2() {
|
||||
static int c = 0;
|
||||
if (++c == 6) {
|
||||
T2CONbits.TON = 0;
|
||||
LATE &= 0xFFFE;
|
||||
c = 0;
|
||||
}
|
||||
IFS0bits.T2IF = 0;
|
||||
}
|
||||
|
||||
void _int_(7) isr_INT1() {
|
||||
LATE |= 0x0001;
|
||||
TMR2 = 0;
|
||||
T2CONbits.TON = 1;
|
||||
IFS0bits.INT1IF = 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
TRISD |= 0x0100;
|
||||
TRISE &= 0xFFFE;
|
||||
LATE &= 0xFFFE;
|
||||
|
||||
IPC1bits.INT1IP = 2;
|
||||
IEC0bits.INT1IE = 1;
|
||||
IFS0bits.INT1IF = 0;
|
||||
INTCONbits.INT1EP = 0;
|
||||
|
||||
IPC2bits.T2IP = 2;
|
||||
IFS0bits.T2IF = 0;
|
||||
|
||||
T2CONbits.TCKPS = 7;
|
||||
PR2 = 39062;
|
||||
IEC0bits.T2IE = 1;
|
||||
|
||||
EnableInterrupts();
|
||||
|
||||
while (1);
|
||||
|
||||
return 0;
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,91 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
#define SAMPLES 4
|
||||
|
||||
volatile int voltage = 0;
|
||||
|
||||
const unsigned int dis7Scodes[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D,
|
||||
0xFD, 0x07, 0x7F, 0x6F, 0x77, 0xFC,
|
||||
0x39, 0x5E, 0xF9, 0xF1};
|
||||
|
||||
typedef enum { HIGH, LOW } display;
|
||||
|
||||
void send2displays(unsigned char value) {
|
||||
static display flag = LOW;
|
||||
value = ((value / 10) << 4) + (value % 10);
|
||||
unsigned char high = dis7Scodes[value >> 4];
|
||||
unsigned char low = dis7Scodes[value & 0x0F];
|
||||
|
||||
if (flag == HIGH) {
|
||||
LATD = (LATD & 0xFF9F) | 0x0040;
|
||||
LATB = (LATB & 0x80FF) | high << 8;
|
||||
flag = LOW;
|
||||
} else {
|
||||
LATD = (LATD & 0xFF9F) | 0x0020;
|
||||
LATB = (LATB & 0x80FF) | low << 8;
|
||||
flag = HIGH;
|
||||
}
|
||||
}
|
||||
|
||||
void _int_(4) isr_T1(void) {
|
||||
AD1CON1bits.ASAM = 1;
|
||||
IFS0bits.T1IF = 0;
|
||||
}
|
||||
|
||||
void _int_(12) isr_T3(void) {
|
||||
send2displays(voltage);
|
||||
IFS0bits.T3IF = 0;
|
||||
}
|
||||
|
||||
void _int_(27) isr_adc(void) {
|
||||
int *p = (int *)(&ADC1BUF0);
|
||||
int media = 0;
|
||||
for (; p <= (int *)(&ADC1BUFF); p++)
|
||||
media += *p;
|
||||
media /= SAMPLES;
|
||||
voltage = (media * 33 + 511) / 1023;
|
||||
IFS1bits.AD1IF = 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
TRISB &= 0x80FF;
|
||||
TRISD &= 0xFF9F;
|
||||
|
||||
TRISBbits.TRISB4 = 1;
|
||||
AD1PCFGbits.PCFG4 = 0;
|
||||
AD1CON1bits.SSRC = 7;
|
||||
AD1CON1bits.CLRASAM = 1;
|
||||
AD1CON3bits.SAMC = 16;
|
||||
AD1CON2bits.SMPI = SAMPLES - 1;
|
||||
AD1CHSbits.CH0SA = 4;
|
||||
AD1CON1bits.ON = 1;
|
||||
|
||||
IPC6bits.AD1IP = 2;
|
||||
IFS1bits.AD1IF = 0;
|
||||
IEC1bits.AD1IE = 1;
|
||||
|
||||
// Configure Timer T1 with 5Hz frequency
|
||||
T1CONbits.TCKPS = 2;
|
||||
PR1 = 62499;
|
||||
TMR1 = 0;
|
||||
T1CONbits.TON = 1;
|
||||
|
||||
// Configure Timer T3 with 100Hz frequency
|
||||
T3CONbits.TCKPS = 2;
|
||||
PR3 = 49999;
|
||||
TMR3 = 0;
|
||||
T3CONbits.TON = 1;
|
||||
|
||||
IPC1bits.T1IP = 2;
|
||||
IFS0bits.T1IF = 0;
|
||||
IEC0bits.T1IE = 1;
|
||||
|
||||
IPC3bits.T3IP = 2;
|
||||
IFS0bits.T3IF = 0;
|
||||
IEC0bits.T3IE = 1;
|
||||
|
||||
EnableInterrupts();
|
||||
while (1);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
int main() {
|
||||
T3CONbits.TCKPS = 2;
|
||||
PR3 = 49999;
|
||||
TMR3 = 0;
|
||||
T3CONbits.TON = 1;
|
||||
|
||||
// tON = 0.25 * (1 / 100) = 2.5ms
|
||||
// fOutPreScaler = 20MHz / 4 = 5MHz
|
||||
// tOutPreScaler = 1 / 5MHz = 200ns
|
||||
// OC1RS = (2.5 * 10^-3) / (200 * 10^-9) = 12500
|
||||
OC1CONbits.OCM = 6;
|
||||
OC1CONbits.OCTSEL = 1;
|
||||
OC1RS = 12500;
|
||||
OC1CONbits.ON = 1;
|
||||
|
||||
while (1);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
void setPWM(unsigned int dutyCycle) {
|
||||
if (dutyCycle < 0 || dutyCycle > 100)
|
||||
return;
|
||||
OC1RS = ((PR3 + 1) * dutyCycle) / 100;
|
||||
}
|
||||
|
||||
int main() {
|
||||
T3CONbits.TCKPS = 2;
|
||||
PR3 = 49999;
|
||||
TMR3 = 0;
|
||||
T3CONbits.TON = 1;
|
||||
|
||||
// tON = 0.25 * (1 / 100) = 2.5ms
|
||||
// fOutPreScaler = 20MHz / 4 = 5MHz
|
||||
// tOutPreScaler = 1 / 5MHz = 200ns
|
||||
// OC1RS = (2.5 * 10^-3) / (200 * 10^-9) = 12500
|
||||
OC1CONbits.OCM = 6;
|
||||
OC1CONbits.OCTSEL = 1;
|
||||
OC1RS = 12500;
|
||||
OC1CONbits.ON = 1;
|
||||
|
||||
while (1) setPWM(80);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
#include <detpic32.h>
|
||||
|
||||
void setPWM(unsigned int dutyCycle) {
|
||||
if (dutyCycle < 0 || dutyCycle > 100)
|
||||
return;
|
||||
OC1RS = ((PR3 + 1) * dutyCycle) / 100;
|
||||
}
|
||||
|
||||
int main() {
|
||||
T3CONbits.TCKPS = 2;
|
||||
PR3 = 49999;
|
||||
TMR3 = 0;
|
||||
T3CONbits.TON = 1;
|
||||
|
||||
// tON = 0.25 * (1 / 100) = 2.5ms
|
||||
// fOutPreScaler = 20MHz / 4 = 5MHz
|
||||
// tOutPreScaler = 1 / 5MHz = 200ns
|
||||
// OC1RS = (2.5 * 10^-3) / (200 * 10^-9) = 12500
|
||||
OC1CONbits.OCM = 6;
|
||||
OC1CONbits.OCTSEL = 1;
|
||||
OC1RS = 12500;
|
||||
OC1CONbits.ON = 1;
|
||||
|
||||
TRISC &= 0xBFFF;
|
||||
|
||||
while (1) {
|
||||
setPWM(10);
|
||||
LATC = (LATC & 0xBFFF) | (PORTD & 0x0001) << 14;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Validate arguments
|
||||
if [ "$#" -ne 1 ]; then
|
||||
echo "Usage: $0 <source_file>"
|
||||
exit
|
||||
fi
|
||||
|
||||
# Parse arguments
|
||||
file=${1##*/}
|
||||
dir=${1%/*}
|
||||
filename="${file%.*}"
|
||||
|
||||
# Go to source directory
|
||||
if [ -d "$dir" ]; then
|
||||
cd "$dir" || exit
|
||||
fi
|
||||
|
||||
# Compile and clean up
|
||||
pcompile "$file" || exit
|
||||
for f in *; do
|
||||
if [[ "$f" =~ ^.+\.(o|elf|map|sym)$ ]]; then
|
||||
rm "$f"
|
||||
fi
|
||||
done
|
||||
|
||||
# Program and open terminal
|
||||
ldpic32 "$filename.hex" || exit
|
||||
pterm || exit
|
||||
|
||||
# Go back to original directory
|
||||
if [ -d "$dir" ]; then
|
||||
cd - || exit
|
||||
fi
|
Loading…
Reference in New Issue