// 20Hz ISR void initTimer3Interrupt(void) { OpenTimer3(T3_ON | T3_PS_1_256, 15625); mT3SetIntPriority(1); mT3ClearIntFlag(); mT3IntEnable(1); }
//determines the amount of time before the timer interrupts void Timer3Init(int Time) { int frequency = 0; //the frequency is calculated as follows : 10000000 / Prescaler / Hertz //Hertz is calculates as follows : 1 / (((msec per div) / 40) * 10^-3)) if (Time == 0) { frequency = 50; // 10000000/1/200000 = 50 } else if (Time == 1) { frequency = 125; // 10000000/1/80000 = 125 } else if (Time == 2) { frequency = 250; // 10000000/1/40000 = 250 } else if (Time == 3) { frequency = 500; // 10000000/1/20000 = 500 } else if (Time == 4) { frequency = 1250; // 10000000/1/8000 = 1250 } OpenTimer3(T3_ON | T3_IDLE_CON | T3_SOURCE_INT | T3_PS_1_1 | T3_GATE_OFF, frequency); mT3SetIntPriority(1); mT3IntEnable(1); }
// h/w clock ASK emulator - initialise data pointers for ISR and start timer 2 // args: clock pulsewidth (NOT period!), data as array of 0/1, repeat void write_ASK_HW_clock(unsigned int pulse, BYTE *data, unsigned int tpb, unsigned int repeat) { // convert to ticks pulse= CONVERT_TO_TICKS(pulse); // we only need to tick once per bit pulse *= (unsigned long) tpb; // point globals at data for ISR EMU_ThisBit= *data; EMU_Data= data + 1; EMU_Reset_Data= data; EMU_DataBitRate= 1; EMU_SubCarrier_T0= 1; EMU_Repeat= repeat; // if we're manchester or bi-phase encoding, we want to clock twice as fast so we can toggle on half-bit if(RFIDlerConfig.Manchester || RFIDlerConfig.BiPhase) { pulse /= 2; EMU_SubCarrier_T0 *= 2; EMU_DataBitRate *= 2; } // make sure no clock is running stop_HW_clock(); // set mode EmulationMode= MOD_MODE_ASK_OOK; // make sure nobody's using our timer CloseTimer3(); // tri-state on, clock low READER_CLOCK_ENABLE_ON(); CLOCK_COIL= LOW; // emulator mode COIL_MODE_EMULATOR(); // this is also a semaphore so the rest of the code knows we're running mLED_Emulate_On(); // start timer - ISR will send data OpenTimer3( T3_ON | T3_PS_1_1 | T3_SOURCE_INT, pulse - 1L); mT3SetIntPriority(6); mT3ClearIntFlag(); mT3IntEnable(TRUE); }
void initPIC() { SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL); initLEDs(); initSerialNU32v2(); // Setup and turn off electromagnets EMAG1 = 0; EMAG2 = 0; TRISEbits.TRISE7 = 0; TRISCbits.TRISC1 = 0; // Direction Output DIR = 1; TRISAbits.TRISA9 = 0; setup_counters(); CloseADC10(); #define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON #define PARAM2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_16 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF #define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31 #define PARAM4 ENABLE_AN0_ANA | ENABLE_AN1_ANA | ENABLE_AN2_ANA | ENABLE_AN3_ANA | ENABLE_AN5_ANA | ENABLE_AN15_ANA OpenADC10(PARAM1, PARAM2, PARAM3, PARAM4, 0); EnableADC10(); // 20kHz PWM signal, duty from 0-1000, pin D3 OpenTimer2(T2_ON | T2_PS_1_4, MAX_DUTY); OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); SetDCOC4PWM(0); // 200 Hz ISR OpenTimer3(T3_ON | T3_PS_1_256, (6250/4 - 1)); //OpenTimer3(T3_ON | T3_PS_1_256, (62500 - 1)); mT3SetIntPriority(1); mT3ClearIntFlag(); mT3IntEnable(1); INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); INTEnableSystemMultiVectoredInt(); }
// h/w clock PSK emulator - initialise data pointers for ISR and start timer 2 // args: clock period, data as array of 0/1, fc per bit, sub-carrier ticks, repeat void write_PSK1_HW_clock(unsigned int period, BYTE *data, unsigned int fcpb, unsigned int c0, unsigned int repeat) { // convert to ticks period= CONVERT_TO_TICKS(period); // for psk we want a full clock cycle, not a tick period *= 2; // point globals at data for ISR EMU_ThisBit= *data; EMU_Data= data + 1; EMU_Reset_Data= data; EMU_DataBitRate= fcpb; EMU_SubCarrier_T0= c0; EMU_Repeat= repeat; // set mode EmulationMode= MOD_MODE_PSK1; // make sure no clock is running stop_HW_clock(); // make sure nobody's using our timers CloseTimer3(); // tri-state on, clock low READER_CLOCK_ENABLE_ON(); CLOCK_COIL= LOW; // emulator mode COIL_MODE_EMULATOR(); // this is also a semaphore so the rest of the code knows we're running mLED_Emulate_On(); // start timer - ISR will send data OpenTimer3( T3_ON | T3_PS_1_1 | T3_SOURCE_INT, period / 2L - 1L); mT3SetIntPriority(6); mT3ClearIntFlag(); mT3IntEnable(TRUE); }
// h/w clock FSK emulator - initialise data pointers for ISR and start timer 2 // args: clock pulsewidth (NOT period!), data as array of 0/1, T0 sub-carrier ticks, T1 sub-carrier ticks, repeat void write_FSK_HW_clock(unsigned long pulse, BYTE *data, unsigned int tpb, unsigned int t0, unsigned int t1, unsigned int repeat) { // convert to ticks pulse= CONVERT_TO_TICKS(pulse); // point globals at data for ISR EMU_ThisBit= *data; EMU_Data= data + 1; EMU_Reset_Data= data; EMU_Repeat= repeat; EMU_DataBitRate= tpb; EMU_SubCarrier_T0= t0; EMU_SubCarrier_T1= t1; // set mode EmulationMode= MOD_MODE_FSK; // make sure no clock is running stop_HW_clock(); // make sure nobody's using our timer CloseTimer3(); // emulator mode COIL_MODE_EMULATOR(); // tri-state on, clock low READER_CLOCK_ENABLE_ON(); CLOCK_COIL= LOW; // this is also a semaphore so the rest of the code knows we're running mLED_Emulate_On(); // start timer - ISR will send data OpenTimer3( T3_ON | T3_PS_1_1 | T3_SOURCE_INT, pulse - 1L); mT3SetIntPriority(6); mT3ClearIntFlag(); mT3IntEnable(TRUE); }
int main(void) { timesec=0; // set PIC32 to max computing power SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL); INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); INTEnableSystemMultiVectoredInt(); initLEDs(); LED0 = 1; LED1 = 1; initSerialNU32v2(); setup_counters(); CloseADC10(); #define PARAM1 ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON #define PARAM2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_16 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF #define PARAM3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31 #define PARAM4 ENABLE_AN0_ANA | ENABLE_AN1_ANA | ENABLE_AN2_ANA | ENABLE_AN3_ANA | ENABLE_AN5_ANA | ENABLE_AN15_ANA OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4,0); EnableADC10(); // Setup and turn off electromagnets EMAG1 = 0; EMAG2 = 0; TRISEbits.TRISE7 = 0; TRISCbits.TRISC1 = 0; //Direction Output DIR = 1; TRISAbits.TRISA9 = 0; //g-select Outputs GSEL1 = 0; GSEL2 = 0; TRISEbits.TRISE2 = 0; TRISCbits.TRISC13= 0; //0g Inputs TRISAbits.TRISA0 = 1; TRISAbits.TRISA4 = 1; // 20kHz PWM signal, duty from 0-1000, pin D3 OpenTimer2(T2_ON | T2_PS_1_4, 1000); OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); HBridgeDuty = 0; SetDCOC4PWM(HBridgeDuty); // 20Hz ISR OpenTimer3(T3_ON | T3_PS_1_256, 15625); mT3SetIntPriority(1); mT3ClearIntFlag(); mT3IntEnable(1); while(1) { if(start){ EMAG2=0; SetDCOC4PWM(100); delaysec(delay1); SetDCOC4PWM(1000); delaysec(delay2); SetDCOC4PWM(500); delaysec(delay3); EMAG2=1; SetDCOC4PWM(0); // EMAG1=0; // SetDCOC4PWM(900); // DIR = 0; // delaysec(delay1); // SetDCOC4PWM(0); // delaysec(delay2); // SetDCOC4PWM(700); // delaysec(delay1); // SetDCOC4PWM(1000); // EMAG1=1; start=0; } } }
/************************************************************************************************** Initialise the video components ***************************************************************************************************/ void initVideo(void) { AutoLineWrap = 1; // test if there is a monitor plugged into the VGA connector CNPUASET = (1 << 4); // set a pullup on the video output pin uSec(300); // allow it to settle //vga = !PORTAbits.RA4; // the pin will be pulled low if the monitor is there CNPUACLR = (1 << 4); //////////////////////////// // setup SPI2 which is the video generator. the output of this module is a stream of bits which are the pixels in a horiz scan line //if(vga) PPSOutput(3, RPA4, SDO2); // B5 is the video out for VGA [color]] //else // PPSOutput(3, RPB2, SDO2); // B2 is the video out for composite PPSInput(4, SS2, RPB9); // B9 is the framing input [horizontal synch] #define P_VIDEO_SPI 2 // the SPI peripheral used for video #define P_SPI_INPUT SPI2BUF // input buffer for the SPI peripheral #define P_SPI_INTERRUPT _SPI2_TX_IRQ // interrupt used by the SPI peripheral when it needs more data //////////////////////////// // the horizontal sync uses Timer 3 and Output Compare 3 to generate the pulse and interrupt level 7 PPSOutput(4, RPB14, OC3); // B14 is the horizontal sync output (ie, the output from OC3) #define P_VID_OC_OPEN OpenOC3 // the function used to open the output compare #define P_VID_OC_REG OC3R // the output compare register //////////////////////////// // the vertical sync uses B13 TRISBCLR = (1<<13); // Vert sync output used by VGA #define P_VERT_SET_HI LATBSET = (1 << 13) // set vert sync hi #define P_VERT_SET_LO LATBCLR = (1 << 13) // set vert sync lo // calculate the paramaters for each video mode and setup Timer 3 to generate the horiz synch and interrupt on its leading edge //if(vga) { // set up for VGA output HRes = VGA_HRES; // HRes is the number of horiz pixels to use HBuf = VGA_HBUFF * 32; // HBuf is the horiz buffer size (in pixels) ConfigBuffers(Option[O_LINES24]); // setup the buffer pointers and VBuf, VRes VC[0] = VGA_VSYNC_N; // setup the table used to count lines VC[1] = VGA_POSTEQ_N; P_VID_OC_OPEN(OC_ON | OC_TIMER3_SRC | OC_CONTINUE_PULSE, 0, VGA_HSYNC_T); // enable the output compare which is used to time the width of the horiz sync pulse OpenTimer3( T3_ON | T3_PS_1_1 | T3_SOURCE_INT, VGA_LINE_T-1); // enable timer 3 and set to the horizontal scanning frequency //} /*else if(Option[O_PAL]) { // this is for the PAL composite output and is the same as VGA with timing differences VBuf = VRes = PAL_VRES; HRes = PAL_HRES; HBuf = PAL_HBUFF * 32; // HBuf is the horiz buffer size (in pixels) ConfigBuffers(0); SvSyncT = PAL_LINE_T - C_HSYNC_T; VC[0] = C_VSYNC_N; VC[1] = PAL_POSTEQ_N; VC[2] = PAL_VRES; VC[3] = PAL_PREEQ_N; P_VID_OC_OPEN(OC_ON | OC_TIMER3_SRC | OC_CONTINUE_PULSE, 0, C_HSYNC_T); // enable the output compare which is used to time the width of the horiz sync pulse OpenTimer3(T3_ON | T3_PS_1_1 | T3_SOURCE_INT, PAL_LINE_T-1); // enable timer 3 and set to the horizontal scanning frequency } */ /* else { // this is for the NTSC composite output and is similar again VBuf = VRes = NTSC_VRES; HRes = NTSC_HRES; HBuf = NTSC_HBUFF * 32; // HBuf is the horiz buffer size (in pixels) ConfigBuffers(0); SvSyncT = NTSC_LINE_T - C_HSYNC_T; VC[0] = C_VSYNC_N; VC[1] = NTSC_POSTEQ_N; VC[2] = NTSC_VRES; VC[3] = NTSC_PREEQ_N; P_VID_OC_OPEN(OC_ON | OC_TIMER3_SRC | OC_CONTINUE_PULSE, 0, C_HSYNC_T); // enable the output compare which is used to time the width of the horiz sync pulse OpenTimer3(T3_ON | T3_PS_1_1 | T3_SOURCE_INT, NTSC_LINE_T-1); // enable timer 3 and set to the horizontal scanning frequency } */ // set priority level 7 for the timer 3 interrupt (horiz synch) and enable it mT3SetIntPriority(7); mT3IntEnable(1); // initialise the state machine and set the count so that the first interrupt will increment the state VState = SV_PREEQ; VCount = 1; // setup the SPI channel then DMA channel which will copy the memory bitmap buffer to the SPI channel //if(vga) { // open the SPI in framing mode. Note that SPI_OPEN_DISSDI will disable the input (which we do not need) SpiChnOpen(P_VIDEO_SPI, SPICON_ON | SPICON_MSTEN | SPICON_MODE32 | SPICON_FRMEN | SPICON_FRMSYNC | SPICON_FRMPOL | SPI_OPEN_DISSDI, VGA_PIX_T); SPI2CON2 = (1<<9) | (1<<8); // instruct the SPI module to ignore any errors that might occur DmaChnOpen(1, 1, DMA_OPEN_DEFAULT); // setup DMA 1 to send data to SPI channel 2 DmaChnSetEventControl(1, DMA_EV_START_IRQ_EN | DMA_EV_START_IRQ(P_SPI_INTERRUPT)); DmaChnSetTxfer(1, (void*)VideoBuf, (void *)&P_SPI_INPUT, HBuf/8, 4, 4); //} /* else { SpiChnOpen(P_VIDEO_SPI, SPICON_ON | SPICON_MSTEN | SPICON_MODE32 | SPICON_FRMEN | SPICON_FRMSYNC | SPICON_FRMPOL | SPI_OPEN_DISSDI, C_PIX_T); SPI2CON2 = (1<<9) | (1<<8); // instruct the SPI module to ignore any errors that might occur DmaChnOpen(1, 1, DMA_OPEN_DEFAULT); // setup DMA 1 is the blank padding at the start of a scan line DmaChnSetEventControl(1, DMA_EV_START_IRQ_EN | DMA_EV_START_IRQ(P_SPI_INTERRUPT)); DmaChnSetTxfer(1, (void*)zero, (void *)&P_SPI_INPUT, C_BLANKPAD, 4, 4); DmaChnOpen( 0, 0, DMA_OPEN_DEFAULT); // setup DMA 0 to pump the data from the graphics buffer to the SPI peripheral DmaChnSetEventControl(0, DMA_EV_START_IRQ_EN | DMA_EV_START_IRQ(P_SPI_INTERRUPT)); DmaChnSetTxfer(0, (void*)VideoBuf, (void *)&P_SPI_INPUT, HBuf/8 + 6, 4, 4); DmaChnSetControlFlags(0, DMA_CTL_CHAIN_EN | DMA_CTL_CHAIN_DIR); // chain DMA 0 so that it will start on completion of the DMA 1 transfer } */ }