| 333 |
Kevin |
1 |
#include "DEFINES.h"
|
|
|
2 |
#include "INTERRUPTS.h"
|
|
|
3 |
#include "PWM.h"
|
|
|
4 |
#include "IOC.h"
|
|
|
5 |
#include "EUSART.h"
|
|
|
6 |
|
|
|
7 |
// <editor-fold defaultstate="collapsed" desc="Configuration Registers">
|
|
|
8 |
// CONFIG1
|
|
|
9 |
#pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins)
|
|
|
10 |
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
|
|
|
11 |
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
|
|
|
12 |
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
|
|
|
13 |
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
|
|
|
14 |
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
|
|
|
15 |
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
|
|
|
16 |
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
|
|
|
17 |
#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)
|
|
|
18 |
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
|
|
|
19 |
|
|
|
20 |
// CONFIG2
|
|
|
21 |
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
|
|
|
22 |
#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
|
|
|
23 |
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
|
|
|
24 |
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
|
|
|
25 |
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
|
|
|
26 |
// </editor-fold>
|
|
|
27 |
|
|
|
28 |
int main() {
|
|
|
29 |
// Initialize oscillator to external crystal (20Mhz)
|
|
|
30 |
OSCCONbits.SCS = 0b00;
|
|
|
31 |
OSCCONbits.SPLLEN = 0b0;
|
|
|
32 |
OSCCONbits.IRCF = 0b1111;
|
|
|
33 |
|
|
|
34 |
// Set all pins to digital I/O
|
|
|
35 |
ANSELA = 0x00;
|
|
|
36 |
// Disable pull-ups
|
|
|
37 |
OPTION_REGbits.nWPUEN = 1;
|
|
|
38 |
WPUA = 0x0;
|
|
|
39 |
|
|
|
40 |
// Configure alternate pin function register
|
|
|
41 |
APFCONbits.RXDTSEL = 0; // RX on RA1
|
|
|
42 |
APFCONbits.TXCKSEL = 0; // TX on RA0
|
|
|
43 |
APFCONbits.CCP1SEL = 0; // CCP1 on RA2
|
|
|
44 |
|
|
|
45 |
// Delay for a bit to ensure oscillator has started
|
|
|
46 |
__delay_ms(10);
|
|
|
47 |
|
|
|
48 |
// Configure and enable interrupts
|
|
|
49 |
Interrupt_Enable();
|
|
|
50 |
|
|
|
51 |
// Configure and enable peripherals
|
|
|
52 |
PWM_Init();
|
|
|
53 |
IOC_Init();
|
|
|
54 |
UART_Init();
|
|
|
55 |
|
|
|
56 |
uint8_t recvBuffer[32];
|
|
|
57 |
uint8_t txOk[] = "Ok!\n";
|
|
|
58 |
uint8_t txError[] = "Error!\n";
|
|
|
59 |
|
|
|
60 |
/* Protocol format as follows:
|
|
|
61 |
* Byte 0 = OPCODE
|
|
|
62 |
* 0x1 = Set frequency
|
|
|
63 |
* Bytes 1-4 = 32 bit unsigned value
|
|
|
64 |
* 0x2 = Set duty cycle
|
|
|
65 |
* Byte 1 = 8 bit unsigned value (high value)
|
|
|
66 |
* Byte 2 = 8 bit unsigned value (low value)
|
|
|
67 |
* 0x3 = Set pattern
|
|
|
68 |
* Bytes 1-2 = 16 bit pattern (transmits MSB first)
|
|
|
69 |
* Everything else = nop
|
|
|
70 |
*/
|
|
|
71 |
|
|
|
72 |
while(1) {
|
|
|
73 |
uint8_t recvBytes = UART_Read(recvBuffer);
|
|
|
74 |
if (recvBytes != 0) {
|
|
|
75 |
// Process op-code for setting frequency
|
|
|
76 |
if (recvBuffer[0] == 0x1 && recvBytes == 5) {
|
|
|
77 |
uint32_t byte0 = recvBuffer[1];
|
|
|
78 |
byte0 <<= 24;
|
|
|
79 |
uint32_t byte1 = recvBuffer[2];
|
|
|
80 |
byte1 <<= 16;
|
|
|
81 |
uint32_t byte2 = recvBuffer[3];
|
|
|
82 |
byte2 <<= 8;
|
|
|
83 |
uint32_t freq = 0;
|
|
|
84 |
freq |= byte0;
|
|
|
85 |
freq |= byte1;
|
|
|
86 |
freq |= byte2;
|
|
|
87 |
freq |= recvBuffer[4];
|
|
|
88 |
// Ensure that received value falls within working bounds
|
|
|
89 |
if (freq >= 20000 && freq <= 200000) {
|
|
|
90 |
Set_PWM_Frequency(freq);
|
|
|
91 |
UART_Write(txOk, 4);
|
|
|
92 |
} else {
|
|
|
93 |
UART_Write(txError, 7);
|
|
|
94 |
}
|
|
|
95 |
UART_Reset_RX();
|
|
|
96 |
} else if (recvBuffer[0] == 0x2 && recvBytes == 3) {
|
|
|
97 |
// Ensure that received value falls within working bounds
|
|
|
98 |
if (recvBuffer[1] <= 100 && recvBuffer[2] <= 100) {
|
|
|
99 |
Set_PWM_Duty_Cycle(recvBuffer[1], recvBuffer[2]);
|
|
|
100 |
UART_Write(txOk, 4);
|
|
|
101 |
} else {
|
|
|
102 |
UART_Write(txError, 7);
|
|
|
103 |
}
|
|
|
104 |
UART_Reset_RX();
|
|
|
105 |
} else if (recvBuffer[0] == 0x3 && recvBytes == 3) {
|
|
|
106 |
uint16_t byte0 = recvBuffer[1];
|
|
|
107 |
byte0 <<= 8;
|
|
|
108 |
uint16_t pattern = 0;
|
|
|
109 |
pattern |= byte0;
|
|
|
110 |
pattern |= recvBuffer[2];
|
|
|
111 |
Set_PWM_Pattern(pattern);
|
|
|
112 |
UART_Reset_RX();
|
|
|
113 |
UART_Write(txOk, 4);
|
|
|
114 |
} else if (recvBuffer[0] == 0x0 || recvBuffer[0] > 0x03 || recvBytes > 5) {
|
|
|
115 |
UART_Reset_RX();
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
}
|