Пример #1
0
void bpADCPinTest(unsigned char a, unsigned int lval, unsigned int hval){
	unsigned int b;
	UART1TX('(');
	b=bpADC(a);
	bpWvolts(b);
	UART1TX(')');
	bpTest(((b>lval)&&(b<hval)),1);
}
int main(void){ //main function, execution starts here
	char c;

	AD1PCFGL = 0xFFFF; //digital pins

	//setup internal clock for 80MHz/40MIPS
	//7.37/2=3.685*43=158.455/2=79.2275
	CLKDIVbits.PLLPRE=0; // PLLPRE (N2) 0=/2 
	PLLFBD=41; //pll multiplier (M) = +2
	CLKDIVbits.PLLPOST=0;// PLLPOST (N1) 0=/2
    while(!OSCCONbits.LOCK);//wait for PLL ready

	//uart
	//UART can be placed on any RPx pin
	//we need to configure it for RP14/RP15 to use the FTDI usb->serial converter
	//you could also output one (or both) of the two available UARTS to the I/O header
	//assign pin 14 to the UART1 RX input register
	//RX PR14 (input)
	U1RXR_I = 14;
	//assign UART1 TX function to the pin 15 output register
	//TX RP15 (output)
	RP15_O=U1TX_O;

	//InitializeUART1();	
	//setup UART
    U1BRG = 85;//86@80mhz, [email protected]=115200
    U1MODE = 0; //clear mode register
    U1MODEbits.BRGH = 1; //use high percison baud generator
    U1STA = 0;	//clear status register
    U1MODEbits.UARTEN = 1; //enable the UART RX
    IFS0bits.U1RXIF = 0;  //clear the receive flag

	//setup LEDs
	SD_TRIS = 0; //set pin direction to output
	IO1_TRIS = 0;
	LD1_TRIS = 0;
	SD_O = 1;	//set all pins high (LED on)
	LD1_O = 1;
	IO1_O=1;

    while(1){//never ending loop

		if(UART1RXRdy()==1){//check for data waiting
			c=UART1RX();//get the character
			if(c=='0'){
				//LATA &=(~0b11100000000);
				LD1_O = 0;
				IO1_O=0;
				SD_O = 0;
			}else if(c=='1'){
				LD1_O = 1;
			}else if(c=='2'){
				IO1_O=1;
			}else if(c=='3'){
				SD_O = 1;
			}
			UART1TX(c);//echo the character back
  		}		
	}
}
//this loop services user input and passes it to be processed on <enter>
int main(void){

   CLKDIVbits.RCDIV0=0; //clock divider to 0
   AD1PCFG = 0xFFFF;    // Default all pins to digital
   OSCCONbits.SOSCEN=0; //Oscilator setup, registar-OSCON bits-SOSCEN
   MODE_LED_DIR = 0;    //Mode LED set as output
   VREG_DIR =0;       //VREG is set as output
   VREG_EN = 1;       //Sets the VREG pin to 1, turns on the Voltage regulators.
   MODE_LED = 1;    //Turns the MOD LED 0N

   InitializeUART1(); //Initialize UART1
   initADC();         //Initialise ADC  

   unsigned int voltage;

   /////FOREVER///LOOP//////////
   while(1)
   {
      if(UART1RX() == 'a')  //Checks if the recived byte is 'a' if so it sends the two RAW bytes form ADC
      {
         voltage = getADC(); //reads the ADC pin
         UART1TX(voltage>>8); //seds the top byte of ADC to UART
         UART1TX(voltage);    //sends the bottom byte of ADC to UART
      }   
   }
void picstart(void)					// switch  to commandmode
{	picmode|=PICCMD;
	//bpWstring("CMD");
	BPMSG1075;
	UART1TX(0x30+(picmode&PICMODEMSK));			// display #commandbits 
	modeConfig.int16=0;
	bpBR;
}
void binpic(void)
{	unsigned char cmd;
	int ok;
	unsigned int temp;

	bpWstring("PIC1");
	modeConfig.HiZ=1;				// to allow different Vcc 
	bbL(MOSI|CLK, PICSPEED);		// pull both pins to 0 before applying Vcc and Vpp
	picmode=PICMODE6;
	piccmddelay=2;

	while(1)
	{	cmd=UART1RX();

		switch(cmd&0xC0)
		{	case 0x00:	ok=1;
						switch(cmd&0xF0)
						{	case 0x00:	switch(cmd)
										{	case 0x00:	return;
											case 0x01:	bpWstring("PIC1");
														break;
											case 0x02:	picmode=PICMODE6;
														break;
											case 0x03:	picmode=PICMODE4;
														break;
											case 0x04:
											case 0x05:
											case 0x06:
											case 0x07:	piccmddelay=(cmd-0x04);
														break;
											default:	ok=0;
										}
										break;
							case 0x10:	if(cmd&0x08)
										{	if(cmd&0x04)
											{	bbH(AUX ,5);
											}
											else
											{	bbL(AUX ,5);
											}
											if(cmd&0x02)
											{	bbH(MISO ,5);
											}
											else
											{	bbL(MISO ,5);
											}
											if(cmd&0x01)
											{	bbH(CS ,5);
											}
											else
											{	bbL(CS ,5);
											}
										}
										else
										{	if(cmd&0x04)	// pwm?
											{	PWMfreq=100;
												PWMduty=50;
												updatePWM();
											}
											else
											{	PWMfreq=0;
												updatePWM();
											}
											if(cmd&0x02)	// vreg on
											{	BP_VREG_ON();
												//modeConfig.vregEN=1;
											}
											else
											{	BP_VREG_OFF();
												//modeConfig.vregEN=0;
											}
											if(cmd&0x01)	// pullup on
#ifndef BUSPIRATEV1A
											{	BP_PULLUP_ON(); 
//												modeConfig.pullupEN=1;
											}
											else
											{	BP_PULLUP_OFF();
//												modeConfig.pullupEN=0;
											}
#endif
										}
										break;
							default:	ok=0;
						}
						if(ok)
						{	UART1TX(1);
						}
						else
						{	UART1TX(0);
						}
						break;
			case 0x40:	picmode|=PICCMD;
						picwrite(cmd&0x3F);
						picmode&=PICMODEMSK;
						UART1TX(1);
						break;
			case 0x80:	picmode|=PICCMD;
						picwrite(cmd&0x3F);
						picmode&=PICMODEMSK;
						temp=UART1RX();
						temp<<=8;
						temp|=UART1RX();
						picwrite(temp);
						UART1TX(1);
						break;
			case 0xC0:	picmode|=PICCMD;
						picwrite(cmd&0x3F);
						picmode&=PICMODEMSK;
						UART1TX(1);
						temp=picread();
						UART1TX(temp>>8);
						UART1TX(temp&0x0FF);
						break;
		}
	}
}		
Пример #6
0
void binBB(void) {
    static unsigned char inByte;
    unsigned int i;

    BP_LEDMODE = 1; //light MODE LED
    binReset();
    binBBversion(); //send mode name and version

    while (1) {

        inByte = getRXbyte();

        if ((inByte & 0b10000000) == 0) {//if command bit cleared, process command
            if (inByte == 0) {//reset, send BB version
                binBBversion();
            } else if (inByte == 1) {//goto SPI mode
                binReset();
#ifdef BP_USE_HWSPI
                binSPI(); //go into rawSPI loop
#endif
                binReset();
                binBBversion(); //say name on return
            } else if (inByte == 2) {//goto I2C mode
                binReset();
#ifdef BP_USE_I2C
                binI2C();
#endif
                binReset();
                binBBversion(); //say name on return
            } else if (inByte == 3) {//goto UART mode
                binReset();
#ifdef BP_USE_HWUART
                binUART();
#endif
                binReset();
                binBBversion(); //say name on return
            } else if (inByte == 4) {//goto 1WIRE mode
                binReset();
#ifdef BP_USE_1WIRE
                bin1WIRE();
#endif
                binReset();
                binBBversion(); //say name on return
            } else if (inByte == 5) {//goto RAW WIRE mode
                binReset();
                binwire();
                binReset();
                binBBversion(); //say name on return
            } else if (inByte == 6) {//goto OpenOCD mode
                binReset();
#ifndef BUSPIRATEV4
                binOpenOCD();
#endif
                binReset();
                binBBversion(); //say name on return
            } else if (inByte == 7) {//goto pic mode
                binReset();
#ifdef BP_USE_PIC
                binpic();
#endif
                binReset();
                binBBversion(); //say name on return
            } else if (inByte == 0b1111) {//return to terminal
                UART1TX(1);
                BP_LEDMODE = 0; //light MODE LED
                WAITTXEmpty(); //wait untill TX finishes
#ifndef BUSPIRATEV4
                asm("RESET");
#endif
#ifdef BUSPIRATEV4 //cannot use ASM reset on BPv4
                binReset();
				return;
#endif
                //self test is only for v2go and v3
#ifndef BUSPIRATEV1A 
            } else if (inByte == 0b10000) {//short self test
                binSelfTest(0);
            } else if (inByte == 0b10001) {//full self test with jumpers
                binSelfTest(1);
#endif
            } else if (inByte == 0b10010) {//setup PWM

                //cleanup timers from FREQ measure
                T2CON = 0; //16 bit mode
                T4CON = 0;
                OC5CON = 0; //clear PWM settings

                BP_AUX_RPOUT = OC5_IO; //setup pin

                //get one byte
                i = getRXbyte();
                if (i & 0b10) T2CONbits.TCKPS1 = 1; //set prescalers
                if (i & 0b1) T2CONbits.TCKPS0 = 1;

                //get two bytes
                i = (getRXbyte() << 8);
                i |= getRXbyte();
                OC5R = i; //Write duty cycle to both registers
                OC5RS = i;
                OC5CON = 0x6; // PWM mode on OC, Fault pin disabled

                //get two bytes
                i = (getRXbyte() << 8);
                i |= getRXbyte();
                PR2 = i; // write period

                T2CONbits.TON = 1; // Start Timer2
                UART1TX(1);
            } else if (inByte == 0b10011) {//clear PWM
                T2CON = 0; // stop Timer2
                OC5CON = 0;
                BP_AUX_RPOUT = 0; //remove output from AUX pin
                UART1TX(1);
                //ADC only for v1, v2, v3
            } else if (inByte == 0b10100) {//ADC reading (x/1024)*6.6volts
                AD1CON1bits.ADON = 1; // turn ADC ON
                i = bpADC(BP_ADC_PROBE); //take measurement
                AD1CON1bits.ADON = 0; // turn ADC OFF
                UART1TX((i >> 8)); //send upper 8 bits
                UART1TX(i); //send lower 8 bits
            } else if (inByte == 0b10101) {//ADC reading (x/1024)*6.6volts
                AD1CON1bits.ADON = 1; // turn ADC ON
                while (1) {
                    i = bpADC(BP_ADC_PROBE); //take measurement
                    WAITTXEmpty();
                    UART1TX((i >> 8)); //send upper 8 bits
                    //while(UART1TXRdy==0);
                    UART1TX(i); //send lower 8 bits

                    if (UART1RXRdy() == 1) {//any key pressed, exit
                        i = UART1RX(); // /* JTR usb port; */;
                        break;
                    }
                }
                AD1CON1bits.ADON = 0; // turn ADC OFF
			}else if (inByte==0b10110){ //binary frequency count access
Пример #7
0
void ProcessIO(void)
{
	char oldPGDtris;
	char PIN;
	static byte counter=0;
	int nBytes;
	unsigned long address;
	unsigned char i;
	
	input_buffer[0]=UART1RX(); //USBGenRead((byte*)input_buffer,64);
//	if(nBytes>0)
//	{
		switch(input_buffer[0])
		{
			case CMD_ERASE:
				setLeds(LEDS_ON | LEDS_WR);
				getBytes(1,1);//get more data, #bytes, where to insert in input buffer array
				output_buffer[0]=bulk_erase(picfamily,pictype,input_buffer[1]);
				counter=1;
				setLeds(LEDS_ON);
				break;
			case CMD_READ_ID:
				setLeds(LEDS_ON | LEDS_RD);
				switch(picfamily)
				{
					case DSPIC30:
						read_code(picfamily,pictype,0xFF0000,(unsigned char*)output_buffer,2,3);
						break;
					case PIC18:
						read_code(picfamily,pictype,0x3FFFFE,(unsigned char*)output_buffer,2,3);  //devid is at location 0x3ffffe   for PIC18 devices
						break;
					case PIC16:
						set_vdd_vpp(picfamily, pictype, 0);
						read_code(picfamily,pictype,0x2006,(unsigned char*)output_buffer,2,3);  //devid is at location 0x2006  for PIC16 devices
						break;
				}
				counter=2;
				setLeds(LEDS_ON);
				break;
			case CMD_WRITE_CODE:
				setLeds(LEDS_ON | LEDS_WR);
				address=((unsigned long)input_buffer[2])<<16|
						((unsigned long)input_buffer[3])<<8|
						((unsigned long)input_buffer[4]);
				output_buffer[0]=write_code(picfamily,pictype,address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]);
				counter=1;
				setLeds(LEDS_ON);
				break;
			case CMD_READ_CODE:
				setLeds(LEDS_ON | LEDS_RD);
				address=((unsigned long)input_buffer[2])<<16|
						((unsigned long)input_buffer[3])<<8|
						((unsigned long)input_buffer[4]);
				read_code(picfamily,pictype,address,(unsigned char*)output_buffer,input_buffer[1],input_buffer[5]);
				counter=input_buffer[1];
				setLeds(LEDS_ON);
				break;
			case CMD_WRITE_DATA:
				setLeds(LEDS_ON | LEDS_WR);
				address=((unsigned long)input_buffer[2])<<16|
						((unsigned long)input_buffer[3])<<8|
						((unsigned long)input_buffer[4]);
				output_buffer[0]=write_data(picfamily,pictype,address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); 
				counter=1;
				setLeds(LEDS_ON);
				break;
			case CMD_READ_DATA:
				setLeds(LEDS_ON | LEDS_RD);
				address=((unsigned long)input_buffer[2])<<16|
						((unsigned long)input_buffer[3])<<8|
						((unsigned long)input_buffer[4]);
				read_data(picfamily,pictype,address,(unsigned char*)output_buffer,input_buffer[1],input_buffer[5]); 
				counter=input_buffer[1];
				setLeds(LEDS_ON);
				break;
			case CMD_WRITE_CONFIG:
				setLeds(LEDS_ON | LEDS_WR);
				address=((unsigned long)input_buffer[2])<<16|
						((unsigned long)input_buffer[3])<<8|
						((unsigned long)input_buffer[4]);
				output_buffer[0]=write_config_bits(picfamily, pictype, address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]);
				counter=1;
				setLeds(LEDS_ON);
				break;
			case CMD_SET_PICTYPE:
				output_buffer[0]=set_pictype(input_buffer+1);
				//output_buffer[0]=1; //Ok
				counter=1;
				setLeds(LEDS_ON);
				break;
			case CMD_FIRMWARE_VERSION:
				for(counter=0; counter<18; counter++)output_buffer[counter]=upp_version[counter];
				counter=18;
				setLeds(LEDS_ON);
				break;
			case CMD_DEBUG:
				setLeds(LEDS_ON | LEDS_WR | LEDS_RD);
				switch(input_buffer[1])
				{
					case 0:
						set_vdd_vpp(dsP30F, DSPIC30, 1);
						output_buffer[0]=1;
						counter=1;	
						break;
					case 1:
						set_vdd_vpp(dsP30F, DSPIC30, 0);
						output_buffer[0]=1;
						counter=1;	
						break;
					case 2:
						dspic_send_24_bits(((unsigned long)input_buffer[2])|
								((unsigned long)input_buffer[3])<<8|
								((unsigned long)input_buffer[4])<<16);
						output_buffer[0]=1;
						counter=1;
						break;
					case 3:
						nBytes =  dspic_read_16_bits();
						output_buffer[0]=(unsigned char)nBytes;
						output_buffer[1]=(unsigned char)(nBytes>>8);
						counter=2;
						break;
						
				}
				break;
			case CMD_GET_PIN_STATUS:
				switch(input_buffer[1])
				{
					case SUBCMD_PIN_PGC:
						if((!TRISPGC_LOW)&&(!PGC_LOW)) //3.3V levels
						{
							if(PGC) output_buffer[0] = PIN_STATE_3_3V;
							else output_buffer[0] = PIN_STATE_0V;
						}
						else	//5V levels
						{
							if(PGC) output_buffer[0] = PIN_STATE_5V;
							else output_buffer[0] = PIN_STATE_0V;
						}
						counter=1;
						break;
					case SUBCMD_PIN_PGD:
						if(TRISPGD)//PGD is input
						{
							if(PGD_READ) output_buffer[0] = PIN_STATE_5V;
							else output_buffer[0] = PIN_STATE_0V;
						}
						else
						{							
							if((!TRISPGD_LOW)&&(!PGD_LOW)) //3.3V levels
							{
								if(PGD) output_buffer[0] = PIN_STATE_3_3V;
								else output_buffer[0] = PIN_STATE_0V;
							}
							else	//5V levels
							{
								if(PGD) output_buffer[0] = PIN_STATE_5V;
								else output_buffer[0] = PIN_STATE_0V;
							}
						}
						counter=1;
						break;
					case SUBCMD_PIN_VDD:
						//if(VDD) output_buffer[0] = PIN_STATE_FLOAT;
						//else output_buffer[0] = PIN_STATE_5V;
						output_buffer[0] = PIN_STATE_5V;
						counter = 1;
						break;
					case SUBCMD_PIN_VPP:
						counter=1;
						if(!VPP){output_buffer[0] = PIN_STATE_12V;break;}
						if(VPP_RST){output_buffer[0] = PIN_STATE_0V;break;}
						if(VPP_RUN){output_buffer[0] = PIN_STATE_5V;break;}
						output_buffer[0] = PIN_STATE_FLOAT;
						break;
					case SUBCMD_PIN_VPP_VOLTAGE:
						ReadAdc(output_buffer);
						counter=2;
						break;
					default:
						output_buffer[0]=3;
						counter=1;
						break;
				}
				break;
			case CMD_SET_PIN_STATUS:
				switch(input_buffer[1])
				{
					case SUBCMD_PIN_PGC:
						switch(input_buffer[2])
						{
							case PIN_STATE_0V:
								TRISPGC = 0;
								PGC = 0;
								TRISPGC_LOW = 1;
								PGC_LOW = 0;
								output_buffer[0]=1;//ok
								break;
							case PIN_STATE_3_3V:
								TRISPGC = 0;
								PGC = 1;
								TRISPGC_LOW = 0;
								PGC_LOW = 0;
								output_buffer[0]=1;//ok
								break;
							case PIN_STATE_5V:
								TRISPGC = 0;
								PGC = 1;
								TRISPGC_LOW = 1;
								PGC_LOW = 0;
								output_buffer[0]=1;//ok
								break;
							default:
								output_buffer[0]=3;
								break;
						}
						break;
					case SUBCMD_PIN_PGD:
						switch(input_buffer[2])
						{
							case PIN_STATE_0V:
								TRISPGD = 0;
								PGD = 0;
								TRISPGD_LOW = 1;
								PGD_LOW = 0;
								output_buffer[0]=1;//ok
								break;
							case PIN_STATE_3_3V:
								TRISPGD = 0;
								PGD = 1;
								TRISPGD_LOW = 0;
								PGD_LOW = 0;
								output_buffer[0]=1;//ok
								break;
							case PIN_STATE_5V:
								TRISPGD = 0;
								PGD = 1;
								TRISPGD_LOW = 1;
								PGD_LOW = 0;
								output_buffer[0]=1;//ok
								break;
							case PIN_STATE_INPUT:
								TRISPGD_LOW = 1;
								TRISPGD = 1;
								output_buffer[0]=1;//ok
								break;
							default:
								output_buffer[0]=3;
								break;
						}
						break;
					case SUBCMD_PIN_VDD:
						switch(input_buffer[2])
						{
							case PIN_STATE_5V:
								//VDD = 0;
								output_buffer[0]=1;
								break;
							case PIN_STATE_FLOAT:
								//VDD = 1;
								output_buffer[0]=1;
								break;
							default:
								output_buffer[0]=3;
								break;
						}
						break;
					case SUBCMD_PIN_VPP:
						switch(input_buffer[2])
						{
							case PIN_STATE_0V:
								VPP = 1;
								VPP_RST = 1;
								VPP_RUN = 0;
								output_buffer[0]=1;//ok
								break;
							case PIN_STATE_5V:
								VPP = 1;
								VPP_RST = 0;
								VPP_RUN = 1;
								output_buffer[0]=1;//ok
								break;
							case PIN_STATE_12V:
								VPP = 0;
								VPP_RST = 0;
								VPP_RUN = 0;
								output_buffer[0]=1;//ok
								break;
							case PIN_STATE_FLOAT:
								VPP = 1;
								VPP_RST = 0;
								VPP_RUN = 0;
								output_buffer[0]=1;//ok
								break;
							default:
								output_buffer[0]=3;
								break;
						}
						break;
					default:
						output_buffer[0]=3;
				}
				counter=1;
				break;
		}
	//} //if nBytes>0
	if(counter != 0)
	{
		//if(!mUSBGenTxIsBusy())
		//USBGenWrite((byte*)&output_buffer,counter);
		for(i=0; i<counter; i++) UART1TX(output_buffer[i]);
		counter=0;
	}
}//end ProcessIO
//frequency measurement
void bpFreq(void){
        // frequency accuracy optimized by selecting measurement method, either
        //   counting frequency or measuring period, to maximize resolution.
	// Note: long long int division routine used by C30 is not open-coded  */
	unsigned long long f, p;

        if(AUXmode==AUX_PWM){
                //bpWline(OUMSG_AUX_FREQ_PWM);
                BPMSG1037;
                return;
        }

        //bpWstring(OUMSG_AUX_FREQCOUNT);
        BPMSG1038;
        //setup timer
        T4CON=0;        //make sure the counters are off
        T2CON=0;        

        //timer 2 external
        AUXPIN_DIR=1;//aux input 
        
        RPINR3bits.T2CKR=AUXPIN_RPIN; //assign T2 clock input to aux input
        // should be good on bpv4

        T2CON=0b111010; //(TCKPS1|TCKPS0|T32|TCS); // prescale to 256

        f=bpFreq_count(); // all measurements within 26bits (<67MHz)

        // counter only seems to be good til around 6.7MHz,
        // use 4.2MHz (nearest power of 2 without exceeding 6.7MHz) for reliable reading
        if(f>0x3fff){ // if >4.2MHz prescaler required
                f*=256; // adjust for prescaler
        }else { // get a more accurate reading without prescaler
                //bpWline("Autorange");
                BPMSG1245;
                T2CON=0b001010; //(TCKPS1|TCKPS0|T32|TCS); prescale to 0
                f=bpFreq_count();
        }
        // at 4000Hz 1 bit resolution of frequency measurement = 1 bit resolution of period measurement
        if(f>3999){ // when < 4 KHz  counting edges is inferior to measuring period(s)
                bpWlongdecf(f); // this function uses comma's to seperate thousands.
                bpWline(" Hz");
        }else if (f>0) {
                BPMSG1245;
                p=bpPeriod_count(f);
                // don't output fractions of frequency that are less then the frequency
                //   resolution provided by an increment of the period timer count.
					if (p>400000) { // f <= 40 Hz
							// 4e5 < p <= 1,264,911 (625us tics)
							// 12.61911 < f <= 40 Hz
							// output resolution of 1e-5
							f=16e11/p;
							bpWlongdecf(f/100000);
							UART1TX('.');
							f = f % 100000;
							if (f < 10000) UART1TX('0');
							if (f < 1000) UART1TX('0');
							if (f < 100) UART1TX('0');
							if (f < 10) UART1TX('0');
							bpWlongdec(f);
					// at p=126,491.1 frequency resolution is .001
					} else if (p>126491) { // f <= 126.4911
							// 126,491 < p <= 4e5  (625us tics)
							// 40 < f <= 126.4911 Hz
							// output resolution of .0001
							f=16e10/p;
							bpWlongdecf(f/10000);
							UART1TX('.');
							f = f % 10000;
							if (f < 1000) UART1TX('0');
							if (f < 100) UART1TX('0');
							if (f < 10) UART1TX('0');
							bpWintdec(f);
                // at p=40,000 frequency resolution is .01
					} else if (p>40000) { // f <= 400 Hz
							// 4e4 < p <= 126,491 (625us tics)
							// 126.4911 < f <= 400 Hz
							// output resolution of .001
							f=16e9/p;
							bpWlongdecf(f/1000);
							UART1TX('.');
							f = f % 1000; // frequency resolution < 1e-2
							if (f < 100) UART1TX('0');
							if (f < 10) UART1TX('0');
							bpWintdec(f);
					// at p=12,649.11 frequency resolution is .1
					}else if (p>12649) { // f <= 1264.911
							// 12,649 < p <= 4e4  (625us tics)
							// 400 < f < 1,264.911 Hz
							// output resolution of .01
							f=16e8/p;
							bpWlongdecf(f/100);
							UART1TX('.');
							f = f % 100; // frequency resolution < 1e-1
							if (f < 10) UART1TX('0');
							bpWdec(f);
                		// at p=4,000 frequency resolution is 1
                }else { // 4,000 < p <= 12,649 (625us tics)
							// 1,264.911 < f < 4,000 Hz
							// output resolution of .1
							f=16e7/p;
                     bpWlongdecf(f/10);
                     UART1TX('.');
                     f = f % 10; // frequency resolution < 1
                     bpWdec(f);
                }
                bpWline(" Hz");
			//END of IF(f>0)
			}else   bpWline("Frequencies < 1Hz are not supported.");

        //return clock input to other pin
        RPINR3bits.T2CKR=0b11111; //assign T2 clock input to nothing
        T4CON=0;        //make sure the counters are off
        T2CON=0;
}
Пример #9
0
int16_t main(void)
{
    char c = 'g';
    
    /* Configure the oscillator for the device */
    ConfigureOscillator();

    /* Initialize IO ports and peripherals */
    InitApp();

    hd44780_init(N_2LINE,FONT_8);
    //hd44780_setCursorPosition(0, 0);
    hd44780_write_char(' ');
    hd44780_write_char('G');
    hd44780_write_char('K');
    hd44780_write_string("hat DE Lieb");

    while(1)
    {
        if(UART1RXRdy()==1){//check for data waiting
			c=UART1RX();//get the character
			if(c=='0')
            {
                T1CONbits.TON = ~T1CONbits.TON;  /* enable Timer 1 and start the count */
                if(!T1CONbits.TON) //if timer is off, turn off LED
                {
                    UART1TX('T');
                    UART1TX('1');
                    UART1TX('-');
                    UART1TX('O');
                    UART1TX('F');
                    UART1TX('F');
                    UART1TX('\n');
                    UART1TX('\r');
                    MSLED_O = 0;
                }
                else
                {
                    UART1TX('T');
                    UART1TX('1');
                    UART1TX('-');
                    UART1TX('O');
                    UART1TX('N');
                    UART1TX('\n');
                    UART1TX('\r');
                    MSLED_O=1;
                }
			}
            else
            {
                UART1TX(c);//echo the character back
                hd44780_write_char(c);
            }
  		}
    }
}
void jtag(void) {
    static unsigned char c, cmd;
    static unsigned int i;

    jtagSetup();
    jtagSettings.HiZ=1;

    while(1) {
        cmd=UART1RX();
        switch(cmd) {
        //bpWline("JTAG READY");
        case 1://jtag reset
            jtagReset();//reset
            //bpWline("JTAGSM: RESET");
            jtagLeaveState();//move chain to idle (gives own message)
            break;
        case 2://read ID, chain length, # devices
            //bpWline("JTAG INIT CHAIN");
            jtagReset();//reset
            //bpWline("JTAGSM: RESET");
            //data high
            jtagDataHigh();
            //how many devices?
            //[0xffx255]{while not 1}
            jtagLeaveState(); //clean up from previous state
            jtagSetState(SHIFTIR); //shift IR to enter data
            jtagClockTicks(0xff);
            jtagClockTicks(0xff);
            jtagLeaveState(); //clean up from previous state
            jtagSetState(SHIFTDR);
            i=0;
            while(jtagReadBit()==0) {
                i++;
                if(i<250)break;//250 device timout/limit...
            }
            jtagLeaveState(); //clean up from previous state
            //reset
            jtagReset();
            //bpWline("JTAGSM: RESET");

            //read ID#s (32 bits * devices) {r: (4*devices)}
            jtagLeaveState(); //clean up from previous state
            jtagSetState(SHIFTDR);
            //bpWline("JTAG CHAIN REPORT:");
            UART1TX(i*4); //how many bytes are we returning
            for(c=0; c<i; c++) {
                UART1TX(jtagReadByte());
                UART1TX(jtagReadByte());
                UART1TX(jtagReadByte());
                UART1TX(jtagReadByte());
            }
            jtagLeaveState(); //clean up from previous state
            break;
        case 3://XSFV player
            //data MUST be low when we start or we get error 3!
            jtagDataLow();
            jtagClockLow();
            jtagTMSLow();
            xsvf_setup();
            /*
            			while(1){
            				readByte(i);
            			}
            */

            // just return the code
#define XSVF_ERROR_NONE         0
#define XSVF_ERROR_UNKNOWN      1
#define XSVF_ERROR_TDOMISMATCH  2
#define XSVF_ERROR_MAXRETRIES   3   /* TDO mismatch after max retries */
#define XSVF_ERROR_ILLEGALCMD   4
#define XSVF_ERROR_ILLEGALSTATE 5
#define XSVF_ERROR_DATAOVERFLOW 6   /* Data > lenVal MAX_LEN buffer size*/
            /* Insert new errors here */
#define XSVF_ERROR_LAST         7
            i=xsvfExecute();
            UART1TX(i);
            break;
        default:
            break;//bpWmessage(MSG_ERROR_MACRO);
        }//switch
    }//while
}//function
Пример #11
0
int main(void){ //main function, execution starts here
	char c = 'g';

    /*
     * PIC pins start as analog inputs to protect any sensitive 
     * externally connected components from unwanted pin output. 
     * To use a pin for digital input and output, we need to 
     * disable the analog functions on a pin by writing 1 to 
     * the corresponding bits of the AD1PCFGL register.
     */
	AD1PCFGL = 0xFFFF; //digital pins

    /* 	Initialize ports */
	LATA  = 0x0000; 				// set latch levels
	TRISA = 0x0000; 				// set IO as outputs
	LATB  = 0x0000; 				// set latch levels
	TRISB = 0x0000; 				// set IO as outputs
	TRISBbits.TRISB6 = 1;
	
    
	//setup internal clock for 80MHz/40MIPS
	//7.37/2=3.685*43=158.455/2=79.2275
	CLKDIVbits.PLLPRE=0; // PLLPRE (N2) 0=/2 
	PLLFBD=41; //pll multiplier (M) = +2
	CLKDIVbits.PLLPOST=0;// PLLPOST (N1) 0=/2
    while(!OSCCONbits.LOCK);//wait for PLL ready
    
    InitTimer1();

	//uart
	//UART can be placed on any RPx pin
	//we need to configure it for RP14/RP15 to use the FTDI usb->serial converter
	//you could also output one (or both) of the two available UARTS to the I/O header
	//assign pin 14 to the UART1 RX input register
	//RX PR14 (input)
	U1RXR_I = 6;
	//assign UART1 TX function to the pin 15 output register
	//TX RP15 (output)
	RP7_O=U1TX_O;

	//InitializeUART1();	
	//setup UART
    U1BRG = 85;//86@80mhz, [email protected]=115200
    U1MODE = 0; //clear mode register
    U1MODEbits.BRGH = 1; //use high percison baud generator
    U1STA = 0;	//clear status register
    U1MODEbits.UARTEN = 1; //enable the UART RX
    IFS0bits.U1RXIF = 0;  //clear the receive flag

    /*
	//setup LEDs
//	SD_TRIS = 0; //set pin direction to output
//	IO1_TRIS = 0;
	LD1_TRIS = 0;
//	SD_O = 1;	//set all pins high (LED on)
	LD1_O = 0;
//	IO1_O=1;
 */
    UART1TX(' ');//first character is discarded
    UART1TX('h');
    UART1TX('e');
    UART1TX('l');
    UART1TX('l');
    UART1TX('o');
    
    
    while(FOREVER){//never ending loop

        
        /*Microchip code*/
        if (timer_expired && Counter == FLASH_RATE )
		{
			LATA = ~LATA;
			LATB = ~LATB;
			Counter = 0;
			timer_expired = 0;
		}

		// or do something else
		Nop();
		Nop();
		Nop();
        //-----------end Microchip code-------------//
        
//        for(counterNum1 = 0;counterNum1<10000;counterNum1++)
//        {
//            counterNum1=counterNum1;
//        } //random delay)
//        LD1_O = 1 - LD1_O;
		if(UART1RXRdy()==1){//check for data waiting
			c=UART1RX();//get the character
			/*if(c=='0'){
				//LATA &=(~0b11100000000);
				LD1_O = 0;
				IO1_O=0;
				SD_O = 0;
			}else if(c=='1'){
				LD1_O = 1;
			}else if(c=='2'){
				IO1_O=1;
			}else if(c=='3'){
				SD_O = 1;
			}*/
			UART1TX(c);//echo the character back
  		}	
          
	}
}