├── Simulation ├── proteus ├── DispicUart.pdsprj ├── DspicADC.pdsprj └── dspicPWM.pdsprj ├── dsPIC33FJ32MC204.pdf ├── ADC ├── ADC_MikroC.c └── ADC.c ├── UART ├── UART_mikroC.c ├── UART_Interrupts.c ├── UART_SendReceive.c └── UART.h ├── Timer ├── Delay.c └── Timer.c ├── SPI ├── main.c └── SPI.h ├── PWM.c └── DspicMotorControl.c /Simulation/proteus: -------------------------------------------------------------------------------- 1 | This file contains the proteus design for each hardware simulation 2 | -------------------------------------------------------------------------------- /dsPIC33FJ32MC204.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NazimBL/DSPIC33F/HEAD/dsPIC33FJ32MC204.pdf -------------------------------------------------------------------------------- /Simulation/DispicUart.pdsprj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NazimBL/DSPIC33F/HEAD/Simulation/DispicUart.pdsprj -------------------------------------------------------------------------------- /Simulation/DspicADC.pdsprj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NazimBL/DSPIC33F/HEAD/Simulation/DspicADC.pdsprj -------------------------------------------------------------------------------- /Simulation/dspicPWM.pdsprj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NazimBL/DSPIC33F/HEAD/Simulation/dspicPWM.pdsprj -------------------------------------------------------------------------------- /ADC/ADC_MikroC.c: -------------------------------------------------------------------------------- 1 | unsigned adcRes; 2 | 3 | void main() { 4 | 5 | //this program turns on an LED (Output) when the ADC input (Potentiometer) is over 500 6 | TRISA= 1; 7 | TRISB.B0=0; 8 | //dnt forget to check ADC library from library manager 9 | ADC1_Init_Advanced(_ADC_12bit, _ADC_INTERNAL_REF); 10 | 11 | while (1) { 12 | 13 | adcRes = ADC1_Get_Sample(1); 14 | if(adcRes>500)LATB.B0=1; 15 | else LATB.B0=0; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /UART/UART_mikroC.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Nazim BL 3 | * MikroC PRO For ARM 4 | * Config : 72 Mhz (8 Mhz Quartz, PLLx9) 5 | * don't forget to add UART library from library manager 6 | */ 7 | 8 | void main() { 9 | 10 | UART1_Init_Advanced(19200,_UART_8BIT_NOPARITY,_UART_ONE_STOPBIT,_UART_LOW_SPEED); 11 | Delay_ms(100); 12 | UART_Write_Text("Start"); 13 | UART_Write(10); 14 | UART_Write(13); 15 | 16 | //set Port A output, high 17 | TRISA = 0; 18 | LATA = 1; 19 | 20 | while(1){ 21 | 22 | UART_Write('Z'); 23 | UART_Write(10); 24 | UART_Write(13); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /UART/UART_Interrupts.c: -------------------------------------------------------------------------------- 1 | //UART Transmit ISR 2 | void __attribute__((__interrupt__, no_auto_psv)) _U1TXInterrupt(void) 3 | { 4 | IFS0bits.U1TXIF = 0; // clear TX interrupt flag 5 | U1TXREG = ReceivedChar; // Transmit one character 6 | } 7 | 8 | //UART Receive ISR 9 | void __attribute__((interrupt, auto_psv)) _U1RXInterrupt( void ) 10 | { 11 | 12 | if(U1STAbits.FERR == 1) 13 | { 14 | ReceivedChar='0'; 15 | } 16 | 17 | if ( U1STAbits.OERR == 1 ) 18 | { 19 | U1STAbits.OERR = 0; 20 | } 21 | 22 | ReceivedChar = (char)U1RXREG; 23 | IFS0bits.U1RXIF = 0; 24 | } 25 | -------------------------------------------------------------------------------- /Timer/Delay.c: -------------------------------------------------------------------------------- 1 | #include "xc.h" 2 | 3 | _FOSCSEL(FNOSC_FRC); 4 | _FOSC(FCKSM_CSECMD & OSCIOFNC_OFF); 5 | 6 | //define operating frequency and LED pin on B10 7 | #define FCY 40000000 8 | #define LED LATBbits.LATB10 9 | 10 | void SetupOSC(); 11 | void myDelay(); 12 | 13 | void main(void) { 14 | 15 | SetupOSC(); 16 | TRISBbits.TRISB10=0; 17 | 18 | while (1) // repeat continuously 19 | { 20 | LED=~LED; 21 | myDelay(); 22 | } 23 | } 24 | 25 | void myDelay(){ 26 | 27 | T3CONbits.TON = 0; // Disable Timer 28 | T3CONbits.TCS = 0; // Select internal instruction cycle clock 29 | T3CONbits.TGATE = 0; // Disable Gated Timer mode 30 | T3CONbits.TCKPS = 0b00; // Select 1:1 Prescaler 31 | TMR3 = 0x00; // Clear timer register 32 | PR3 = 56000; // Load the period value 33 | //IPC0bits.T3IP = 0x01; // Set Timer1 Interrupt Priority Level 34 | IFS0bits.T3IF = 0; // Clear Timer1 Interrupt Flag 35 | IEC0bits.T3IE = 1; // Enable Timer1 interrupt 36 | T3CONbits.TON = 1; 37 | while(IFS0bits.T3IF == 0); 38 | IFS0bits.T3IF = 0; 39 | } 40 | -------------------------------------------------------------------------------- /Timer/Timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * MPLAB X IDE 3 | * Author: Nazim BL 4 | * Created on 1 juillet 2018, 10:10 5 | */ 6 | 7 | #include "xc.h" 8 | 9 | _FOSCSEL(FNOSC_FRC); 10 | _FOSC(FCKSM_CSECMD & OSCIOFNC_OFF); 11 | 12 | #define FCY 40000000 13 | #define LED LATBbits.LATB10 14 | 15 | void SetupOSC(); 16 | void Timer_Setup(); 17 | 18 | void __attribute__((__interrupt__, no_auto_psv)) _T3Interrupt(void) 19 | { 20 | 21 | IFS0bits.T3IF = 0; 22 | LED=!LED; 23 | //ADC_Read(); to read adc input at a set frequency 24 | } 25 | 26 | void main(void) { 27 | 28 | SetupOSC(); 29 | Timer_Setup(); 30 | 31 | //LED 32 | TRISBbits.TRISB10=0; 33 | LED=0; 34 | 35 | while (1); 36 | } 37 | 38 | void Timer_Setup(){ 39 | 40 | T3CONbits.TON = 0; // Disable Timer 41 | T3CONbits.TCS = 0; // Select internal instruction cycle clock 42 | T3CONbits.TGATE = 0; // Disable Gated Timer mode 43 | T3CONbits.TCKPS = 0b00; // Select 1:1 Prescaler 44 | TMR3 = 0x00; // Clear timer register 45 | PR3 = 56000; // Load the period value 46 | //IPC0bits.T3IP = 0x01; // Set Timer1 Interrupt Priority Level 47 | IFS0bits.T3IF = 0; // Clear Timer1 Interrupt Flag 48 | IEC0bits.T3IE = 1; // Enable Timer1 interrupt 49 | T3CONbits.TON = 1; 50 | } 51 | 52 | 53 | -------------------------------------------------------------------------------- /UART/UART_SendReceive.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Nazim 3 | * Created on 1 juillet 2018, 10:10 4 | */ 5 | 6 | #include "xc.h" 7 | #include "UART.h" 8 | 9 | _FOSCSEL(FNOSC_FRC); 10 | _FOSC(FCKSM_CSECMD & OSCIOFNC_OFF); 11 | 12 | #define FCY 40000000 13 | #define LED LATBbits.LATB10 14 | 15 | void SetupOSC(); 16 | char ReceivedChar='x'; 17 | char received[4]; 18 | 19 | void main() { 20 | 21 | SetupOSC(); 22 | UartInit(1200); 23 | //LED 24 | TRISBbits.TRISB10=0; 25 | LED=0; 26 | 27 | while (1) 28 | { 29 | 30 | // if(ReceivedChar=='1')LED=1; //this was to test reception 31 | //else LED=0; 32 | //LED=~LED; 33 | 34 | if(U1STAbits.URXDA==1){ 35 | ReceivedChar=(char)U1RXREG; 36 | UART_Write(ReceivedChar); 37 | ReceivedChar=(char)U1RXREG; 38 | UART_Write(ReceivedChar); 39 | ReceivedChar=(char)U1RXREG; 40 | UART_Write(ReceivedChar); 41 | UART_Write('\r'); 42 | UART_Write('\n'); 43 | } 44 | UART_Write('1'); 45 | UART_Write('2'); 46 | UART_Write('3'); 47 | 48 | } 49 | 50 | void SetupOSC(){ 51 | 52 | //FCY = 40 Mhz 53 | PLLFBD = 41;// M = 43 54 | CLKDIVbits.PLLPOST = 0; // N1 = 2 55 | CLKDIVbits.PLLPRE = 0; // N2 = 2 56 | 57 | __builtin_write_OSCCONH(0x03); 58 | __builtin_write_OSCCONL(0x01); 59 | // Wait for clock switch to occur 60 | while (OSCCONbits.COSC != 0b011); 61 | while(OSCCONbits.LOCK !=1); // Wait for PLL to lock 62 | } 63 | } 64 | 65 | 66 | -------------------------------------------------------------------------------- /ADC/ADC.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Nazim 3 | * MPLABX IDE 4 | * Created on 18 juin 2018, 11:58 5 | */ 6 | #include 7 | #include "xc.h" 8 | #include 9 | 10 | _FOSCSEL(FNOSC_FRCPLL); // Start with FRC will switch to Primary (XT, HS, EC) Oscillator with PLL 11 | _FOSC(FCKSM_CSDCMD & POSCMD_NONE); // Clock Switching disabled 12 | _FBS (BSS_NO_FLASH & BWRP_WRPROTECT_OFF); 13 | _FWDT (FWDTEN_OFF); 14 | _FGS (GSS_OFF & GCP_OFF & GWRP_OFF); 15 | _FPOR (PWMPIN_ON & HPOL_ON & LPOL_ON & FPWRT_PWR128); 16 | _FICD (ICS_PGD1 & JTAGEN_OFF); 17 | 18 | #define ON_LED LATAbits.LATA0 19 | #define FCY 40000000 20 | 21 | int ADCValue; 22 | 23 | void ADC_Init(){ 24 | 25 | TRISA = 0xFFFF; //Set as Input Port 26 | ADPCFG = 0x0000; 27 | AD1CON1=0x0004; 28 | AD1CHS0=0x0001; 29 | AD1CSSL=0; 30 | AD1CON3=0x0002; 31 | AD1CON2=0; 32 | AD1CON1bits.ADON =1; 33 | } 34 | 35 | int readADC() 36 | { 37 | AD1CON1bits.SAMP=0; 38 | while (!AD1CON1bits.DONE); // wait to complete the conversion 39 | return ADCBUF0; 40 | } 41 | 42 | void OSC_Init(){ 43 | 44 | PLLFBD = 33; // M=45 45 | CLKDIVbits.PLLPOST = 0; // N1=2 46 | CLKDIVbits.PLLPRE = 0; // N2=2 47 | // OSCCONbits.OSWEN=1; 48 | while (OSCCONbits.COSC != 0b001); 49 | while(OSCCONbits.LOCK!=1); 50 | } 51 | 52 | int main(void) { 53 | 54 | OSC_Init(); 55 | ADC_Init(); 56 | TRISAbits.TRISA0=0; 57 | 58 | while(1) 59 | { 60 | ADCValue=readADC(); 61 | if(ADCValue>500)ON_LED=1; 62 | else ON_LED=0; 63 | } 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /SPI/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Author: Nazim 3 | * Created on 18 juin 2018, 11:58 4 | */ 5 | 6 | #include 7 | #include "xc.h" 8 | #include "SPI.h" 9 | 10 | _FOSCSEL(FNOSC_FRCPLL); // Start with FRC will switch to Primary (XT, HS, EC) Oscillator with PLL 11 | _FOSC(FCKSM_CSDCMD & POSCMD_NONE); // Clock Switching disabled 12 | _FBS (BSS_NO_FLASH & BWRP_WRPROTECT_OFF); 13 | _FWDT (FWDTEN_OFF); 14 | _FGS (GSS_OFF & GCP_OFF & GWRP_OFF); 15 | _FPOR (PWMPIN_ON & HPOL_ON & LPOL_ON & FPWRT_PWR128); 16 | _FICD (ICS_PGD1 & JTAGEN_OFF); 17 | 18 | #define ON_LED LATAbits.LATA0 19 | #define FCY 40000000 20 | 21 | char master=0; 22 | char data='z'; 23 | 24 | //SPI ISR, asynchronous read when data buffer is full 25 | void __attribute__((__interrupt__,__auto_psv__)) _SPI1Interrupt(void) 26 | { 27 | 28 | if (SPI1STATbits.SPIROV) 29 | { 30 | //----- OVERFLOW ERROR ----- 31 | data = SPI1BUF; 32 | SPI1STATbits.SPIROV = 0; 33 | IFS0bits.SPI1IF = 0; 34 | return; 35 | } 36 | while(!SPI1STATbits.SPIRBF); 37 | data = SPI1BUF; 38 | IFS0bits.SPI1IF = 0; //Clear the Interrupt Flag 39 | } 40 | 41 | void OSC_Init(){ 42 | 43 | PLLFBD = 33; // M=45 44 | CLKDIVbits.PLLPOST = 0; // N1=2 45 | CLKDIVbits.PLLPRE = 0; // N2=2 46 | // OSCCONbits.OSWEN=1; 47 | while (OSCCONbits.COSC != 0b001); 48 | while(OSCCONbits.LOCK!=1); 49 | } 50 | 51 | void main() { 52 | 53 | OSC_Init(); 54 | SPI_Init(master); 55 | TRISAbits.TRISA0=0; 56 | 57 | while(1) 58 | { 59 | // decomment to read synchronously 60 | //if(IFS0bits.SPI1IF)data=SPI_Read(); //Polling 61 | //data='l'; 62 | if(data==0x1E)ON_LED=1; 63 | else ON_LED=0; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /UART/UART.h: -------------------------------------------------------------------------------- 1 | #ifndef XC_HEADER_TEMPLATE_H 2 | #define XC_HEADER_TEMPLATE_H 3 | #define FCY 40000000 4 | 5 | #include 6 | 7 | int i=0; 8 | void PPS(); 9 | 10 | void UartInit(unsigned int baud){ 11 | 12 | AD1PCFGL = 0xFFFF; 13 | //AD1CON1bits.ADON = 0; 14 | //RX,TX 15 | TRISBbits.TRISB0=1; 16 | TRISBbits.TRISB2=0; 17 | 18 | PPS();//Peripheral Pin Select 19 | 20 | U1MODEbits.STSEL = 0; // 1 Stop bit 21 | U1MODEbits.PDSEL = 0; // No Parity, 8 data bits 22 | U1MODEbits.ABAUD = 0; // Auto-Baud Disabled 23 | U1MODEbits.BRGH = 0; // Low Speed mode 24 | U1MODEbits.UEN = 0; 25 | U1STAbits.ADDEN = 0; 26 | U1BRG =((FCY/baud)/16)-1 ; // BAUD Rate Setting for 9600 27 | U1STAbits.UTXISEL0 = 0; // Interrupt after one TX Character is transmitted 28 | U1STAbits.UTXISEL1 = 0; 29 | U1STAbits.URXISEL = 0; 30 | IEC0bits.U1TXIE = 1; // Enable UART TX Interrupt 31 | IEC0bits.U1RXIE = 1; 32 | U1MODEbits.UARTEN = 1; // Enable UART 33 | U1STAbits.UTXEN = 1; // Enable UART TX 34 | 35 | //wait at least 104 usec (1/9600) before sending first char 36 | for(i = 0; i < 4160; i++)Nop(); 37 | } 38 | 39 | void UART_Write(char send){ 40 | 41 | while(U1STAbits.UTXBF== 1); 42 | U1TXREG = send; 43 | } 44 | 45 | char UART_Read(){ 46 | return (char)U1RXREG; 47 | } 48 | 49 | void PPS(){ 50 | RPINR18bits.U1RXR = 0; // rx to RP0 51 | //RPINR18bits.U1CTSR = 1;// Assign U1CTS To Pin RP1 52 | RPOR1bits.RP2R = 3;// Assign U1Tx To Pin RP2 53 | //RPOR1bits.RP3R = 4;// Assign U1RTS To Pin RP3 54 | } 55 | 56 | #ifdef __cplusplus 57 | extern "C" { 58 | #endif /* __cplusplus */ 59 | #ifdef __cplusplus 60 | } 61 | #endif /* __cplusplus */ 62 | #endif /* XC_HEADER_TEMPLATE_H */ 63 | 64 | -------------------------------------------------------------------------------- /SPI/SPI.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void SPI_Init(char master){ 4 | 5 | //DIN 6 | RPINR20bits.SDI1R0=0; 7 | //DOUT 8 | RPOR1bits.RP2R=7; 9 | SPI1CON1bits.MSTEN = master; // 0=Slave, 1=Master 10 | 11 | if(master==1){ 12 | //clock out 13 | RPOR1bits.RP3R=8; 14 | } 15 | else { 16 | //clk in nd cs 17 | RPINR20bits.SCK1R0=1; 18 | RPINR21bits.SS1R0=4; 19 | } 20 | 21 | //rp0 = din 22 | //rp1 =salve clk 23 | //rp2 dout 24 | //rp3 =master clock 25 | 26 | TRISBbits.TRISB0=1; 27 | TRISBbits.TRISB2=0; 28 | if(master==1)TRISBbits.TRISB3=0; 29 | else TRISBbits.TRISB1=1; 30 | 31 | IFS0bits.SPI1IF = 0; // Clear the Interrupt flag 32 | IEC0bits.SPI1IE = 0; // Disable the interrupt 33 | 34 | // SPI1CON1 Register Settings 35 | SPI1CON1bits.DISSCK = 0; // Internal serial clock is enabled 36 | SPI1CON1bits.DISSDO = 0; // SDOx pin is controlled by the module 37 | SPI1CON1bits.MODE16 = 0; // Communication is word-wide (16 bits) 38 | SPI1CON1bits.PPRE = 2; //Pre-scaler 39 | SPI1CON1bits.SPRE = 8;//Pre-scaler 2 40 | // Input data is sampled at the middle of data output time 41 | SPI1CON1bits.CKE = 0; // Serial output data changes on transition from 42 | // Idle clock state to active clock state 43 | SPI1CON1bits.CKP = 0; // Idle state for clock is a low level; 44 | SPI1CON1bits.SMP = 0; 45 | // active state is a high level 46 | SPI1STATbits.SPIEN = 1; // Enable SPI module 47 | // Interrupt Controller Settings 48 | IFS0bits.SPI1IF = 0; // Clear the Interrupt flag 49 | IEC0bits.SPI1IE = 1; // Enable the interrupt 50 | } 51 | 52 | char SPI_Writ(char w){ 53 | 54 | SPI1BUF=w; 55 | while (SPI1STATbits.SPITBF); 56 | return SPI1BUF; 57 | } 58 | 59 | char SPI_Read() 60 | { 61 | //while (SPI1STATbits.SPITBF); 62 | while(!SPI1STATbits.SPIRBF); 63 | return SPI1BUF; 64 | } 65 | -------------------------------------------------------------------------------- /PWM.c: -------------------------------------------------------------------------------- 1 | /* 2 | * MPLAB X IDE 3 | * Author: Nazim 4 | * Created on 18 juin 2018, 11:58 5 | */ 6 | 7 | #include 8 | #include "xc.h" 9 | #include 10 | 11 | _FOSCSEL(FNOSC_FRCPLL); // Start with FRC will switch to Primary (XT, HS, EC) Oscillator with PLL 12 | _FOSC(FCKSM_CSDCMD & POSCMD_NONE); // Clock Switching disabled 13 | _FBS (BSS_NO_FLASH & BWRP_WRPROTECT_OFF); 14 | _FWDT (FWDTEN_OFF); 15 | _FGS (GSS_OFF & GCP_OFF & GWRP_OFF); 16 | _FPOR (PWMPIN_ON & HPOL_ON & LPOL_ON & FPWRT_PWR128); 17 | _FICD (ICS_PGD1 & JTAGEN_OFF); 18 | 19 | #define FCY 40000000 20 | 21 | void PWM_Init() 22 | { 23 | TRISB = 0x00; // make sure PWM pins are set to be outputs 24 | PORTB = 0x00; // clear the outputs 25 | 26 | PTCONbits.PTOPS = 1; // PWM timer post-scale 27 | PTCONbits.PTCKPS = 0; // PWM timer pre-scale 28 | PTCONbits.PTMOD = 2; // PWM operates in Up-down Mode continuously 29 | 30 | PTMR = 0; // PWM counter value, start at 0 31 | 32 | PTPER = 19999; // PWM Timebase period 33 | 34 | PWMCON1bits.PMOD3 = 0; // PWM in complimentary mode 35 | PWMCON1bits.PMOD2 = 0; // PWM in complimentary mode 36 | PWMCON1bits.PMOD1 = 0; // PWM in complimentary mode 37 | PWMCON1bits.PEN3H = 1; // PWM High pin is enabled 38 | PWMCON1bits.PEN2H = 1; // PWM High pin is enabled 39 | PWMCON1bits.PEN1H = 1; // PWM High pin is enabled 40 | PWMCON1bits.PEN3L = 1; // PWM Low pin enabled (direction control later?) 41 | PWMCON1bits.PEN2L = 1; // PWM Low pin enabled (direction control later?) 42 | PWMCON1bits.PEN1L = 1; // PWM Low pin enabled (direction control later?) 43 | 44 | DTCON1bits.DTAPS = 0; //DeadTime pre-scaler 45 | DTCON1bits.DTA = 59; //DeadTime value for 4 us. 46 | 47 | PDC1 = 19999; // PWM#1 Duty Cycle register (11-bit) 48 | PDC2 = 19999; // PWM#2 Duty Cycle register (11-bit) 49 | PDC3 = 19999; // PWM#3 Duty Cycle register (11-bit) 50 | 51 | PTCONbits.PTEN = 1; // Enable PWM Timerbase! 52 | } 53 | 54 | void OSC_Init(){ 55 | 56 | PLLFBD = 33; // M=45 57 | CLKDIVbits.PLLPOST = 0; // N1=2 58 | CLKDIVbits.PLLPRE = 0; // N2=2 59 | // OSCCONbits.OSWEN=1; 60 | while (OSCCONbits.COSC != 0b001); 61 | while(OSCCONbits.LOCK!=1); 62 | } 63 | 64 | void main(void) { 65 | 66 | OSC_Init(); 67 | PWM_Init(); 68 | 69 | while(1); 70 | } 71 | -------------------------------------------------------------------------------- /DspicMotorControl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: MPLAB X IDE 3 | * Author: Nazim BL 4 | * Created on 1 juillet 2018, 10:10 5 | */ 6 | 7 | #include "xc.h" 8 | 9 | _FOSCSEL(FNOSC_FRC); 10 | _FOSC(FCKSM_CSECMD & OSCIOFNC_OFF); 11 | 12 | #define FCY 40000000 13 | #define LED LATBbits.LATB10 14 | 15 | void SetupOSC(); 16 | void Buck_Boost(); 17 | void ADC_Setup(); 18 | void Timer_Setup(); 19 | void myDelay(); 20 | 21 | int ADCValue=0; 22 | char temp; 23 | float d=1; 24 | int p1=100,p2=100; 25 | void PWM_update(int pwm1,int pwm2); 26 | 27 | int main(void) { 28 | 29 | SetupOSC(); 30 | ADC_Setup(); 31 | PWM_Init(); 32 | //LED 33 | TRISBbits.TRISB10=0; 34 | LED=0; 35 | 36 | while (1) 37 | { 38 | Buck_Boost(); 39 | //PWM_update(800,1600); test 40 | } 41 | return 0; 42 | } 43 | 44 | void Buck_Boost(){ 45 | //read adc input 46 | AD1CON1bits.SAMP = 1; 47 | while (!AD1CON1bits.DONE);// conversion done? 48 | ADCValue = ADC1BUF0; 49 | if(ADCValue>2500)LED=1; 50 | else LED=0; 51 | 52 | d=(float)ADCValue/4095; 53 | //d=0.65; 54 | d=d*2; 55 | if(d>=1){ 56 | d=d-1; 57 | if(d*800<600)PWM_update((int)(d*800),800); 58 | else PWM_update(600,800); 59 | 60 | }else PWM_update(0,(int)(d*800)); 61 | } 62 | 63 | void PWM_Init() 64 | { 65 | TRISBbits.TRISB12=0; 66 | TRISBbits.TRISB14=0; 67 | // RB14 and Rb12 68 | PORTB = 0x00; // clear the outputs 69 | 70 | PTCONbits.PTOPS = 1; // PWM timer post-scale 71 | PTCONbits.PTCKPS = 0; // PWM timer pre-scale 72 | PTCONbits.PTMOD = 2; // PWM operates in Up-down Mode continuously 73 | //--> interupt each time wwe get to zero 74 | PTMR = 0; // PWM counter value, start at 0 75 | PTPER = 799; // PWM Timebase period 76 | 77 | PWMCON1bits.PMOD2 = 0; // PWM in complimentary mode 78 | PWMCON1bits.PMOD1 = 0; // PWM in complimentary mode 79 | PWMCON1bits.PEN2H = 1; // PWM High pin is enabled 80 | PWMCON1bits.PEN1H = 1; // PWM High pin is enabled 81 | 82 | DTCON1bits.DTAPS = 0; //DeadTime pre-scaler = Tcy 83 | DTCON1bits.DTA = 0; //DeadTime value for 4 us. 84 | 85 | PDC1 = p1; // PWM#1 Duty Cycle register (11-bit) 86 | PDC2 = p2; // PWM#2 Duty Cycle register (11-bit) 87 | 88 | PTCONbits.PTEN = 1; // Enable PWM Timerbase! 89 | } 90 | 91 | void PWM_update(int pwm1,int pwm2){ 92 | PTCONbits.PTEN=0; 93 | PDC1 = pwm1; 94 | PDC2 = pwm2; 95 | PTCONbits.PTEN=1; 96 | } 97 | 98 | void SetupOSC(){ 99 | //FCY = 40 Mhz 100 | PLLFBD = 41;// M = 43 101 | CLKDIVbits.PLLPOST = 0; // N1 = 2 102 | CLKDIVbits.PLLPRE = 0; // N2 = 2 103 | 104 | __builtin_write_OSCCONH(0x03); 105 | __builtin_write_OSCCONL(0x01); 106 | // Wait for clock switch to occur 107 | while (OSCCONbits.COSC != 0b011); 108 | while(OSCCONbits.LOCK !=1); // Wait for PLL to lock 109 | } 110 | 111 | //read each 5khz 112 | void ADC_Setup(){ 113 | 114 | TRISAbits.TRISA0=1; 115 | AD1PCFGL = 0xFFFE;// all PORTA = Digital; RA0 = analog 116 | //10 bit //AD1CON1 = 0x00E0; 117 | //12 bit 118 | AD1CON1 = 0x04E0; 119 | AD1CHS0= 0x0000; // Connect RA0/AN0 as CH0 input .. 120 | AD1CSSL = 0; 121 | AD1CON3 = 0x1F02; // Tsampling = 2 Tad 122 | AD1CON2 = 0; 123 | AD1CON1bits.ADON = 1; // turn ADC ON 124 | } 125 | 126 | --------------------------------------------------------------------------------