Untitled

 avatar
unknown
plain_text
a year ago
2.0 kB
5
Indexable
// PIC24FJ128GA010 Configuration Bit Settings

// For more on Configuration Bits

// consult your device data sheet

// CONFIG2

#pragma config POSCMOD = XT // XT Oscillator mode selected

#pragma config OSCIOFNC = ON // OSC2/CLKO/RC15 as port I/O (RC15)

#pragma config FCKSM = CSDCMD // Clock Switching and Monitor disabled

#pragma config FNOSC = PRI // Primary Oscillator (XT, HS, EC)

#pragma config IESO = ON // Int Ext Switch Over Mode enabled

// CONFIG1

#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)

#pragma config FWPSA = PR128 // WDT Prescaler (1:128)

#pragma config WINDIS = ON // Watchdog Timer Window Mode disabled

#pragma config FWDTEN = OFF // Watchdog Timer disabled

#pragma config ICS = PGx2 // Emulator/debugger uses EMUC2/EMUD2

#pragma config GWRP = OFF // Writes to program memory allowed

#pragma config GCP = OFF // Code protection is disabled

#pragma config JTAGEN = OFF // JTAG port is disabled

// #pragma config statements should precede project file includes.

// Use project enums instead of #define for ON and OFF.

#include <math.h>

#include <xc.h>

#include <stdlib.h>

#include <time.h> // Dodaj bibliotekę time.h

#include "adc.h"

int main(void) {
    // Domyślna konfiguracja ADC
    ADC_SetConfiguration(ADC_CONFIGURATION_DEFAULT);
    // ADC ma czytać potencjometr
    ADC_ChannelEnable(ADC_CHANNEL_POTENTIOMETER);
    // Output LEDów PORT A
    TRISA = 0x0000;

    unsigned int value;

    while (1) {
        // Czytanie 10-bit wartości z potencjometru
        value = ADC_Read10bit(ADC_CHANNEL_POTENTIOMETER);
        
        // Błąd?
        if (value == 0xFFFF) {
            // W razie czego przechodzimy do kolejnej iteracji
            continue;
        }
        
        // Normalizacja zakresu do 0-255
        unsigned char normalizedValue = value >> 2;
        // Output na porcie A (LEDy)
        LATA = normalizedValue;
    }

    return 0;
}
Editor is loading...
Leave a Comment