Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DEVCFG2
- #pragma config FPLLIDIV = DIV_2 // PLL Input Divider (1x Divider)
- #pragma config FPLLMUL = MUL_20 // PLL Multiplier (24x Multiplier)
- #pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider (12x Divider)
- #pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
- #pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 256)
- // DEVCFG1
- #pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
- #pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled)
- #pragma config IESO = ON // Internal/External Switch Over (Enabled)
- #pragma config POSCMOD = HS // Primary Oscillator Configuration (HS osc mode)
- #pragma config OSCIOFNC = ON // CLKO Output Signal Active on the OSCO Pin (Enabled)
- #pragma config FPBDIV = DIV_8 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
- #pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
- #pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
- #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
- // DEVCFG0
- #pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
- #pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (ICE EMUC2/EMUD2 pins shared with PGC2/PGD2)
- #pragma config PWP = OFF // Program Flash Write Protect (Disable)
- #pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
- #pragma config CP = OFF // Code Protect (Protection Disabled)
- #define CONFIG1 (ADC_MODULE_ON | ADC_FORMAT_INTG32 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON)
- #define CONFIG2 (ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON |ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF)
- #define CONFIG3 (ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15)
- #define CONFIGPORT (ENABLE_AN1_ANA)
- #define CONFIGSCAN (SKIP_SCAN_AN0 | SKIP_SCAN_AN2 | SKIP_SCAN_AN3 | SKIP_SCAN_AN4 |SKIP_SCAN_AN5 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 |SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14 |SKIP_SCAN_AN15)
- #define PORT_CLOCK LATBbits.LATB13
- #define PORT_DATA LATBbits.LATB14
- #define bitN(arg,n) (((arg)>>(n))&1)
- #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
- #define SYSCLK 80000000
- #include <p32xxxx.h>
- #include <plib.h> // Include the PIC32 Peripheral Library.
- int n = 0;
- int dato = 0;
- int nbit = 0;
- int readFlag = 0;
- void initializeADC(){
- CloseADC10(); // Generally, you should disable the ADC before setup.
- // Use ground as negative reference for channel A instead of pin AN1 (RB1)
- SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF);
- OpenADC10( CONFIG1, CONFIG2, CONFIG3, CONFIGPORT, CONFIGSCAN);
- // Setup for the ADC10.
- EnableADC10(); // Enables the ADC10.
- }
- long map(long x, long in_min, long in_max, long out_min, long out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
- void main(void) {
- SYSTEMConfigPerformance(SYSCLK);
- initializeADC();
- TRISB = 0x0000;
- TRISD = 0x0000;
- TRISG = 0x0000;
- TRISD = 0xffff;
- TRISDbits.TRISD4 = 1;
- TRISDbits.TRISD5 = 1;
- TRISDbits.TRISD6 = 1;
- TRISDbits.TRISD7 = 1;
- TRISDbits.TRISD8 = 1;
- TRISDbits.TRISD11 = 1;
- TRISBbits.TRISB1 = 1 ;
- TRISBbits.TRISB2 = 1 ;
- int btn_addresses[6] = {4,5,6,7,8,11};
- int oldbtn_states[6] = {PORTDbits.RD4,PORTDbits.RD5,PORTDbits.RD6,PORTDbits.RD7,PORTDbits.RD8,PORTDbits.RD11};
- int data_addresses[3] = {0b000, 0b001, 0b010};
- int amount = 0;
- int i = 0;
- int interval = 0;
- OpenTimer2( T2_ON | T2_SOURCE_INT | T2_PS_1_1, 0x04E2);
- OpenTimer3( T3_ON | T3_SOURCE_INT | T3_PS_1_64, 0x04E2);
- ConfigIntTimer2( T2_INT_OFF | T2_INT_PRIOR_2);
- ConfigIntTimer3( T3_INT_ON | T3_INT_PRIOR_3);
- INTEnableSystemMultiVectoredInt();
- PORT_CLOCK = 1;
- mJTAGPortEnable(0);
- while(1){
- if (oldbtn_states[i] & !bitN(PORTD,btn_addresses[i]))
- {
- amount = ReadADC10(0);
- if(i%2 == 0){
- interval = +1;
- }
- else{
- interval = -1;
- }
- //POTENZIOMETRO boi
- amount = ReadADC10(0);
- int mappato = map(amount, 0 ,1022, 0, 15);
- dato = data_addresses[i/2] | i%2 <<3 | mappato<<4;
- ConfigIntTimer2( T2_INT_ON | T2_INT_PRIOR_2);
- }
- if(readFlag){
- readFlag = 0;
- oldbtn_states[i] = bitN(PORTD,btn_addresses[i]);
- i++;
- i = i % 6;
- }
- }
- // i++;
- // i = i%6;
- return;
- }
- void __ISR(_TIMER_2_VECTOR, ipl2) handlesTimer2Ints(void){
- // **make sure iplx matches the timer?s interrupt priority level
- n++;
- if(n >= 10){
- n = 0;
- PORT_DATA = bitN(dato,nbit);
- nbit++;
- if(nbit == 8){
- nbit = 0;
- PORT_CLOCK = 0;
- PORT_DATA = 0;
- ConfigIntTimer2( T2_INT_OFF | T2_INT_PRIOR_2);
- }
- }
- if(n % 5 == 0)
- PORT_CLOCK = PORT_CLOCK ^1;
- mT2ClearIntFlag();
- // Clears the interrupt flag so that the program returns to the main loop
- } // END Timer2 ISR
- void __ISR(_TIMER_3_VECTOR, ipl3) handlesTimer3Ints(void){
- // **make sure iplx matches the timer?s interrupt priority level
- readFlag = 1;
- mT3ClearIntFlag();
- // Clears the interrupt flag so that the program returns to the main loop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement