Dinuwilson
Advanced Member level 4
- Joined
- Aug 20, 2011
- Messages
- 100
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,298
- Activity points
- 1,985
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 #include "common.h" // Common file to define all #defines #include "Ext_mcal_InOutConfig.h" #include "Ext_mcal_TmrConfig.h" #include "Ext_mcal_Irq.h" /* Configuration Bits*/ /****************************************************************************************************/ /* FBS */ #pragma config BWRP = WRPROTECT_OFF /* Boot Segment Write Protect (Boot Segment may be written) */ #pragma config BSS = LARGE_FLASH_STD /* Boot Segment Program Flash Code Protection (Standard Security, Large-sized Boot Flash) */ /* FGS */ #pragma config GWRP = OFF /* General Code Segment Write Protect (User program memory is not write-protected) */ #pragma config GSS = OFF /* General Segment Code Protection (User program memory is not code-protected) */ /* FOSCSEL */ #pragma config FNOSC = PRI /* Oscillator Mode (Primary Oscillator (XT, HS, EC)) */ #pragma config IESO = OFF /* Internal External Switch Over Mode (Start-up device with user-selected oscillator source) */ /* FOSC */ #pragma config POSCMD = HS /* Primary Oscillator Source (HS Oscillator Mode) */ #pragma config OSCIOFNC = OFF /* OSC2 Pin Function (OSC2 pin has clock out function) */ #pragma config IOL1WAY = ON /* Peripheral Pin Select Configuration (Allow Only One Re-configuration) */ #pragma config FCKSM = CSDCMD /* Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled) */ /* FWDT */ #pragma config WDTPOST = PS32768 /* Watchdog Timer Postscaler (1:32,768) */ #pragma config WDTPRE = PR128 /* WDT Prescaler (1:128) */ #pragma config WINDIS = OFF /* Watchdog Timer Window (Watchdog Timer in Non-Window mode) */ #pragma config FWDTEN = OFF /* Watchdog Timer Enable (Watchdog timer enabled/disabled by user software) */ /* FPOR */ #pragma config FPWRT = PWR128 /* POR Timer Value (128ms) */ #pragma config ALTI2C = OFF /* Alternate I2C pins (I2C mapped to SDA1/SCL1 pins) */ #pragma config LPOL = ON /* Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-high output polarity) */ #pragma config HPOL = ON /* Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-high output polarity) */ #pragma config PWMPIN = ON /* Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PORT register at device Reset) */ /* FICD */ #pragma config ICS = PGD2 /* Comm Channel Select (Communicate on PGC2/EMUC2 and PGD2/EMUD2) */ #pragma config JTAGEN = OFF /* JTAG Port Enable (JTAG is Disabled) */ /****************************************************************************************************/ void main(void) { /* The following code example will enable Timer1 interrupts, load the Timer1 Period register and start Timer1 using an asynchronous external clock and a 1:8 prescaler setting. When a Timer1 period match interrupt occurs, the interrupt service routine must clear the Timer1 interrupt status flag in software. */ //Ext_mcal_AllPortClr(); Ext_mcal_PORTAConfig(0x0000); Ext_mcal_TMR1Config(); Ext_mcal_IrqTmr1Config(); TMR1_CLR(0x0000); TMR1_STRT(); IRQ_TMR1_ENABLE(); while(1) { } } /* Example code for Timer1 ISR*/ void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void) { if(IFS0bits.T1IF == 0x01) { IFS0bits.T1IF = 0; //Reset Timer1 interrupt flag and Return from ISR Ext_mcal_PORTASetVal(SET_BIT(0)); //porta bit 1 on for testing } }
I am working with 'dsPIC33FJ12MC202' ....
#include "common.h"
#include "Ext_mcal_InOutConfig.h"
#include "Ext_mcal_TmrConfig.h"
#include "Ext_mcal_Irq.h"
#include <xc.h>
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 /* * File: common.h * * * */ #ifndef _COMMON_H_ #define _COMMON_H_ #include <stdio.h> #include <stdlib.h> #include <xc.h> #define IF_DIAG_TEST 0 #define U8_T unsigned char #define S8_T signed char #define U16_T unsigned int #define S16_T signed int #define U32_T unsigned long int #define S32_T signed long int #define LOCAL static #define EXTERN #define SET_BIT(pos) ((U16_T)(0x0001<<pos)) #endif /* COMMON_H */
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /* * File: Ext_mcal_InOutConfig.h * * * */ #ifndef _EXT_MCAL_INOUTCONFIG_H_ #define _EXT_MCAL_INOUTCONFIG_H_ #include "common.h" extern void Ext_mcal_PORTBConfig(U16_T config_val_par); extern void Ext_mcal_PORTAConfig(U16_T config_val_par); extern void Ext_mcal_PORTBSetVal(U16_T set_val_par); extern void Ext_mcal_PORTASetVal(U16_T set_val_par); extern void Ext_mcal_AllPortClr(void);
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* * File: Ext_mcal_Irq.h * * * */ #ifndef _EXT_MCAL_IRQ_H_ #define _EXT_MCAL_IRQ_H_ #include "common.h" #define IRQ_TMR1_ENABLE() IEC0 |= (U16_T)0x0008 #define IRQ_TMR1_DSABLE() IEC0 &= (U16_T)0xFFF7 #endif /* _EXT_MCAL_IRQ_H_ */
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 /* * File: Ext_mcal_TmrConfig.h * * * */ #ifndef _EXT_MCAL_TMRCONFIG_H_ #define _EXT_MCAL_TMRCONFIG_H_ #include "common.h" #define TMR1_CLR(val) (TMR1 = (U16_T)(val)) #define TMR1_STRT() (T1CON |= (U16_T)(0x8000)) /* T1CON <1> SET*/ #define TMR1_STOP() (T1CON &= (U16_T)(0x7FFF)) /* T1CON <1> CLR*/ #define TMR1_PRSCL (U16_T)(0x0030) /* T1CON <5-4> */ #define TMR1_Flg_Clr() IFS0 &= 0xFFF7 extern void Ext_mcal_TMR1Config(void); extern void Ext_mcal_TMR2Config(void); extern void Ext_mcal_TMR3Config(void); extern void Ext_mcal_IrqTmr1Config(void); #endif /* _EXT_MCAL_TMRCONFIG_H_ */
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // Ext_mcal_InOutConfig.c #include "Ext_mcal_InOutConfig.h" /*************************************************************/ EXTERN void Ext_mcal_PORTBConfig(U16_T config_val_par) { TRISB |= config_val_par; //LATB = (U16_T)0x0000; } /*************************************************************/ EXTERN void Ext_mcal_PORTAConfig(U16_T config_val_par) { TRISA = config_val_par; LATA = (U16_T)0x0000; } /*************************************************************/ EXTERN void Ext_mcal_PORTBSetVal(U16_T set_val_par) { LATB |= set_val_par; } /*************************************************************/ EXTERN void Ext_mcal_PORTASetVal(U16_T set_val_par) { LATA |= set_val_par; } /*************************************************************/ EXTERN void Ext_mcal_AllPortClr(void) { TRISA = (U16_T)0x0000; TRISB = (U16_T)0x0000; LATA = (U16_T)0x0000; LATB = (U16_T)0x0000; }
Code C - [expand] 1 2 3 // Ext_mcal_Irq.c #include "Ext_mcal_Irq.h"
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 // Ext_mcal_TmrConfig.c #include "Ext_mcal_TmrConfig.h" EXTERN void Ext_mcal_TMR1Config(void) { T1CON = (U16_T)((0x0000)|(TMR1_PRSCL)); TMR1_CLR(0x0000); PR1 = (U16_T)(0xFFFF); TMR1_Flg_Clr(); } EXTERN void Ext_mcal_TMR2Config(void) { /* Yet to add */ } EXTERN void Ext_mcal_TMR3Config(void) { /* Yet to add */ } EXTERN void Ext_mcal_IrqTmr1Config(void) { SR |= (U16_T)0x0060; /* CPU Prio 3 */ IEC0 &= (U16_T)0xFFF7; /* Disable Interrupt */ INTCON1 = (U16_T)0x8000; INTCON2 = (U16_T)0x0000; IFS0 = (U16_T)0x0000; IPC0 = (U16_T)0x4000; /* TMR1 Prio 4 */ INTTREG = (U16_T)0x0000; }
No, I am using 'dsPIC33FJ12MC202' only
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 #include "common.h" // Common file to define all #defines #include "Ext_mcal_InOutConfig.h" #include "Ext_mcal_TmrConfig.h" #include "Ext_mcal_Irq.h" /* Configuration Bits*/ /****************************************************************************************************/ /* FBS */ #pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment may be written) #pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment) /* FGS */ #pragma config GWRP = OFF /* General Code Segment Write Protect (User program memory is not write-protected) */ #pragma config GSS = OFF /* General Segment Code Protection (User program memory is not code-protected) */ /* FOSCSEL */ #pragma config FNOSC = PRI /* Oscillator Mode (Primary Oscillator (XT, HS, EC)) */ #pragma config IESO = OFF /* Internal External Switch Over Mode (Start-up device with user-selected oscillator source) */ /* FOSC */ #pragma config POSCMD = HS /* Primary Oscillator Source (HS Oscillator Mode) */ #pragma config OSCIOFNC = OFF /* OSC2 Pin Function (OSC2 pin has clock out function) */ #pragma config IOL1WAY = ON /* Peripheral Pin Select Configuration (Allow Only One Re-configuration) */ #pragma config FCKSM = CSDCMD /* Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled) */ /* FWDT */ #pragma config WDTPOST = PS32768 /* Watchdog Timer Postscaler (1:32,768) */ #pragma config WDTPRE = PR128 /* WDT Prescaler (1:128) */ #pragma config WINDIS = OFF /* Watchdog Timer Window (Watchdog Timer in Non-Window mode) */ #pragma config FWDTEN = OFF /* Watchdog Timer Enable (Watchdog timer enabled/disabled by user software) */ /* FPOR */ #pragma config FPWRT = PWR128 /* POR Timer Value (128ms) */ #pragma config ALTI2C = OFF /* Alternate I2C pins (I2C mapped to SDA1/SCL1 pins) */ #pragma config LPOL = ON /* Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-high output polarity) */ #pragma config HPOL = ON /* Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-high output polarity) */ #pragma config PWMPIN = ON /* Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PORT register at device Reset) */ /* FICD */ #pragma config ICS = PGD2 /* Comm Channel Select (Communicate on PGC2/EMUC2 and PGD2/EMUD2) */ #pragma config JTAGEN = OFF /* JTAG Port Enable (JTAG is Disabled) */ /****************************************************************************************************/ int main(void) { /* The following code example will enable Timer1 interrupts, load the Timer1 Period register and start Timer1 using an asynchronous external clock and a 1:8 prescaler setting. When a Timer1 period match interrupt occurs, the interrupt service routine must clear the Timer1 interrupt status flag in software. */ volatile int temp; //Ext_mcal_AllPortClr(); AD1PCFGL = 0xFF; // Sets All Pins to Digital, Rather than Analog Inputs Ext_mcal_PORTAConfig(0x0000); Ext_mcal_TMR1Config(); Ext_mcal_IrqTmr1Config(); TMR1_CLR(0x0000); TMR1_STRT(); IRQ_TMR1_ENABLE(); while(1) { temp++; } } /* Example code for Timer1 ISR*/ void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void) { if(IFS0bits.T1IF == 0x01) { IFS0bits.T1IF = 0; //Reset Timer1 interrupt flag and Return from ISR LATAbits.LATA0 = ~PORTAbits.RA0; // Flips the State of RA0 //Ext_mcal_PORTASetVal(SET_BIT(0)); //porta bit 1 on for testing } }
#pragma config BSS = LARGE_FLASH_STD /* Boot Segment Program Flash Code Protection (Standard Security, Large-sized Boot Flash) */
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void)
AD1PCFGL = 0xFF; // Sets All Pins to Digital, Rather than Analog Inputs
volatile int temp;
while(1)
{
temp++;
}
LATAbits.LATA0 = ~PORTAbits.RA0; // Flips the State of RA0
#define _ISR_PSV __attribute__((__interrupt__, __auto_psv__))
void _ISR_PSV _T1Interrupt()
{
LATAbits.LATA0 = ~LATAbits.LATA0; //toggle/flip RA0 pin
IFS0bits.T1IF = 0; //clear T1 interrupt flag
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?