root / Version 0.5 / Panel_reader_controller.X / main.c @ eae0761b
Historique | Voir | Annoter | Télécharger (4,033 ko)
1 |
/*
|
---|---|
2 |
* File: main.c
|
3 |
* Author: Enzo Niro
|
4 |
*
|
5 |
* Created on August 28, 2024, 2:39 PM
|
6 |
*/
|
7 |
|
8 |
|
9 |
#define F_CPU 10000000UL |
10 |
|
11 |
#include <xc.h> |
12 |
#include <stdint.h> |
13 |
#include <stdbool.h> |
14 |
#include <util/delay.h> |
15 |
|
16 |
#include "defValues.h" |
17 |
#include "configuration.h" |
18 |
#include "pins.h" |
19 |
|
20 |
|
21 |
|
22 |
/*
|
23 |
TODO list :
|
24 |
* Test PWM
|
25 |
* ADC for battery
|
26 |
* Add USART0 & USART1 (implement TX and RX method)
|
27 |
|
28 |
*/
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
////////////////////////////////////////////
|
34 |
//Prototypes & Macros
|
35 |
void dimmerSetup(void); //start dimmer |
36 |
void adcInit(void); //Init ADC cfg |
37 |
|
38 |
#define ADC_START_CONV ADC0.COMMAND |= 0x01 //Start conversion |
39 |
#define ADC_READ ADC0.SAMPLE //Get sample value |
40 |
#define BLUETOOTH_CONNECTED ((PORTA.IN & STAT2_PIN) >> 4) //Get Bluetooth connected status |
41 |
////////////////////////////////////////////
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
int main(void) { |
50 |
//PORTA.DIR = 0xFF;
|
51 |
//PORTA.OUT = 0xFF;
|
52 |
//PORTA.PIN4CTRL = 0x08 | 0x03; //PULLUP enable and sense on low level...
|
53 |
//_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, 0x01); //switch to 20 MHz internal oscillator
|
54 |
//_PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, 0x19); //switch to 20 MHz internal oscillator
|
55 |
//SLPCTRL.CTRLA = (0x02 << 1); //standby sleep mode
|
56 |
//SLPCTRL.CTRLA |= ENABLE_BIT;
|
57 |
|
58 |
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, ENABLE_BIT); //Enable clock prescaler (and divide by 2)
|
59 |
uint16_t counter = 0;
|
60 |
|
61 |
_delay_ms(1000);
|
62 |
|
63 |
////////////////////////////////////////////////
|
64 |
//Basic pins setup
|
65 |
PORTA.OUT = 0x80;
|
66 |
PORTA.DIR = 0x80;
|
67 |
PORTB.OUT = 0x00;
|
68 |
PORTB.DIR = PWM_DIMMER_PIN | BOOT_BL_PIN; |
69 |
////////////////////////////////////////////////
|
70 |
|
71 |
|
72 |
////////////////////////////////////////////////
|
73 |
//USART Setup
|
74 |
#ifdef USE_TWO_SERIAL_PORTS
|
75 |
PORTA.DIR = TX2_PIN; //Only this pin is output, others are input
|
76 |
PORTB.DIR |= TX1_PIN; //Add TX1 pin as output on PORTB
|
77 |
//TODO add a method to configure serial port here
|
78 |
#endif
|
79 |
////////////////////////////////////////////////
|
80 |
|
81 |
|
82 |
////////////////////////////////////////////////
|
83 |
//Start ADC
|
84 |
//adcInit();
|
85 |
|
86 |
|
87 |
/////////////////////////
|
88 |
//Boot Bluetooth module
|
89 |
PORTB.OUT |= BOOT_BL_PIN; |
90 |
|
91 |
|
92 |
/////////////////////////
|
93 |
//Start PWM
|
94 |
dimmerSetup(); |
95 |
|
96 |
PORTA.OUT = 0x00;
|
97 |
_delay_ms(500);
|
98 |
PORTA.OUT = 0x80;
|
99 |
_delay_ms(500);
|
100 |
|
101 |
|
102 |
|
103 |
while(1) |
104 |
{ |
105 |
/*
|
106 |
if(!BLUETOOTH_CONNECTED) //Check STAT2 pin if not connected
|
107 |
{
|
108 |
//Now we accept setup mode
|
109 |
}
|
110 |
*/
|
111 |
|
112 |
/*if((PORTA.IN & 0x10) == 0x00) //button pushed
|
113 |
{
|
114 |
//wait a bit
|
115 |
_delay_ms(500);
|
116 |
_delay_ms(500);
|
117 |
SLPCTRL.CTRLA = (0x01 << 1); //standby sleep mode
|
118 |
SLPCTRL.CTRLA |= ENABLE_BIT;
|
119 |
}
|
120 |
|
121 |
PORTA.OUT ^= 0x20;
|
122 |
counter++;
|
123 |
_delay_ms(500);*/
|
124 |
|
125 |
} |
126 |
|
127 |
return 0; |
128 |
} |
129 |
|
130 |
|
131 |
|
132 |
void adcInit(void) |
133 |
{ |
134 |
ADC0.CTRLA = ENABLE_BIT; //Enable register
|
135 |
ADC0.COMMAND = (ADC_SINGLE_12BIT << 4); //Just use 12 bit resolution (no differential mode) |
136 |
ADC0.CTRLB = ADC_PRESCALER; //divide clock for conversion
|
137 |
ADC0.CTRLC = (ADC_TIMEBASE << 3) | ADC_REF; //Set analog reference |
138 |
ADC0.PGACTRL = (ADC_PGA_GAIN << 5) | (ADC_PGA_BIAS << 3) | (ADC_PGA_SMD << 1) | ENABLE_BIT; //set PGA cfg |
139 |
ADC0.CTRLE = 0x80; //Set sample duration (SAMPDUR -> p.407) |
140 |
ADC0.MUXPOS = (ADC_VIA_PGA << 6) | (ADC_MUX_AIN12); //PC0 connected to AIN12 |
141 |
ADC0.MUXNEG = (ADC_VIA_DIRECT << 6) | (ADC_MUXN_GND); //Set reference in common mode |
142 |
} |
143 |
|
144 |
uint16_t adcGetSample(void)
|
145 |
{ |
146 |
ADC_START_CONV; //as for a conversion
|
147 |
_delay_ms(1); //wait a bit |
148 |
while(!ADC0.STATUS); //Wait when conversion is now complete |
149 |
return ADC_READ; //get stored value |
150 |
} |
151 |
|
152 |
|
153 |
void dimmerSetup(void) |
154 |
{ |
155 |
TCA0.SINGLE.CTRLA = (PWM_DIV << 1) | ENABLE_BIT; //enable register set PWM frequency (datasheet p.209) |
156 |
TCA0.SINGLE.CTRLB = 0x13; //set output frequency in Single-slope PWM |
157 |
TCA0.SINGLE.CTRLD = ENABLE_BIT; //enable split mode
|
158 |
TCA0.SINGLE.CMP0 = PWM_RATE; //set duty cycle
|
159 |
} |