refactoring

This commit is contained in:
sfokin 2025-03-07 18:16:06 +03:00
parent d1c3813510
commit 4004ef8780

15
main.c
View File

@ -34,19 +34,18 @@ void main(void) {
T2CON = 0x04; // Timer2 on, prescaler 1:1
while(1) {
// Start conversion using shadow register
uint8_t adcon0 = ADCON0;
adcon0 |= (1 << 1); // Set GO/DONE bit (bit 1)
ADCON0 = adcon0;
ADCON0bits.GO = 1; // Set GO/DONE bit (bit 1)
while(ADCON0bits.GO); // Wait for conversion
// Read 10-bit result
unsigned int adc_val = ((ADRESH & 0x03) << 8) | ADRESL;
CCPR1L = ((ADRESH & 0x03) << 8) | ADRESL;
// Update PWM duty cycle
CCPR1L = adc_val >> 2; // Upper 8 bits
CCP1CONbits.DC1B = adc_val & 0x03; // Lower 2 bits
unsigned int adc_value = ((ADRESH & 0x03) << 8) | ADRESL;
// Set PWM duty cycle
CCPR1L = adc_value >> 2; // Upper 8 bits
CCP1CONbits.DC1B = adc_value & 3; // Lower 2 bits
__delay_ms(10);
}