refactoring
This commit is contained in:
parent
d1c3813510
commit
4004ef8780
15
main.c
15
main.c
|
|
@ -34,19 +34,18 @@ void main(void) {
|
||||||
T2CON = 0x04; // Timer2 on, prescaler 1:1
|
T2CON = 0x04; // Timer2 on, prescaler 1:1
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
// Start conversion using shadow register
|
ADCON0bits.GO = 1; // Set GO/DONE bit (bit 1)
|
||||||
uint8_t adcon0 = ADCON0;
|
|
||||||
adcon0 |= (1 << 1); // Set GO/DONE bit (bit 1)
|
|
||||||
ADCON0 = adcon0;
|
|
||||||
|
|
||||||
while(ADCON0bits.GO); // Wait for conversion
|
while(ADCON0bits.GO); // Wait for conversion
|
||||||
|
|
||||||
// Read 10-bit result
|
// Read 10-bit result
|
||||||
unsigned int adc_val = ((ADRESH & 0x03) << 8) | ADRESL;
|
CCPR1L = ((ADRESH & 0x03) << 8) | ADRESL;
|
||||||
|
|
||||||
// Update PWM duty cycle
|
// Update PWM duty cycle
|
||||||
CCPR1L = adc_val >> 2; // Upper 8 bits
|
unsigned int adc_value = ((ADRESH & 0x03) << 8) | ADRESL;
|
||||||
CCP1CONbits.DC1B = adc_val & 0x03; // Lower 2 bits
|
|
||||||
|
// Set PWM duty cycle
|
||||||
|
CCPR1L = adc_value >> 2; // Upper 8 bits
|
||||||
|
CCP1CONbits.DC1B = adc_value & 3; // Lower 2 bits
|
||||||
|
|
||||||
__delay_ms(10);
|
__delay_ms(10);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user