Currently viewing the tag: "dspic"

Buttons are cool, so are menu’s. So with the help of a button, a few leds and a dspic I have a menu system (also note that this is blocking, the idea is that its used to start / stop the robot, so the robot does not move while the menu is active), first lets look at the switch circuit and then at the dspic set up (c30 compiler, using a dspic33fj128gp804):

Schematic for a switch for a dspic

Schematic for a switch for a dspic

/* Setup the IO Pin Assignment */
__builtin_write_OSCCONL(OSCCON & ~(1<<6));

	RPINR7bits.IC1R = 5;	// Switch Input Capture

__builtin_write_OSCCONL(OSCCON | (1<<6));

/* Switch Input Capture Setup */
IC1CONbits.ICM = 0x02;	// Capture on falling edge
_IC1IF = 0;
_IC1IE = 1;

/* CPU Time Clock */
T1CONbits.TCKPS = 0x02;	// 40.5504Mhz / 64
TMR1 = 0x00;
PR1 = 634*4;			// Int at 1.000631313ms*4
_T1IF = 0;
_T1IE = 1;
T1CONbits.TON = 1;

The code is fairly simple: first attach the switch input to the input capture (ic1) peripheral, set up ic1 to trigger an interrupt on the falling edge and then there is the set up for my main timer loop, this occurs at ~250Hz, and yes I am using a weird value crystal.

Next lets look at the global variables (well global to the scope of the interrupts)


// Switch input vars
int sw_counter = 0;
int sw_on = 0;

// Menu vars
int menu_counter = 0;
int menu_on = 0;
int menu_value = 0;

Next the interrupt for the input capture routine

void __attribute__((__interrupt__)) _NOPSV _IC1Interrupt(void)
{
	_IC1IF = 0; 		// Clear the interrupt
	_IC1IE = 0;			// Disable the interrupt
	_RED = 1;   		// Show the user the button pressed
	sw_on = 1; 		// Enable the debounce counter

	// If the menu is on already, increment the menu value
	// Otherwise turn the menu on and set the menu value to 
	//default
	if (menu_on)
	{
		menu_val ++;
		menu_counter = 0;
	}
	else
	{
		menu_on = 1;
		menu_val = 0;
	}
}

And then the interrupt for the timer:

void __attribute__((__interrupt__)) _NOPSV _T1Interrupt(void)
{
	// Handle switch
	if (sw_on)				// If a switch 'event' is ongoing
	{
		sw_counter ++;
		if (sw_counter > 50)  	// 50x 1/250 = 200ms debounce
		{
			sw_on = 0;		// Switch no longer on
			sw_counter = 0;	// Counter to zero
			_IC1IF = 0;		// Clear the bounces
			_IC1IE = 1;		// Enable more switches
			_RED = 0;		// Led off
		}
	}

	// Menu
	if (menu_on)			// If menu is on
	{
		menu_counter ++;	// Increment counter

		// Leave menu active for 1s after last click
		if (menu_counter > 250)
		{
			menu_on = 0;	// Menu off
			menu_counter = 0; // Reset

			// Menu complete - do stuff
			switch (menu_val)
			{
				case 0:
					_RED = 0;
					Nop();	// Required if RED and GREEN
							// On same port
					_GREEN = 1;
					break;
				case 1:
					_RED = 1;
					Nop();	// Required if RED and GREEN
							// On same port
					_GREEN = 0;
					break;
				default:
					break;
			}
		}
	}
}

And thats it, google _NOPSV to find the macro for that (sometimes required to compile using c30). Also _RED and _GREEN are port bits, so something like #define _RED _RB1 to make _RED = 1 turn Port B pin 1 on. All quite useful, maybe...

Tagged with:
 

CRC‘s are probably the most time consuming thing you can do in a data stream, but they have their uses – and not just for checking that data is correct, but more on that in a later post.

I found that the microchip documentation extremely useful, and I suspect you will to: Section 36 PCRC. But the biggest shout out should go to billysdomain in the Microchip Forums who actually got it working, the bits which make it work are his code, lets begin:

int CalculateCRC(DataPL *DataPacket)
{
     unsigned int j, length = (DATAPL_LEN*2)-2;
     unsigned char *ptr, *data;
     ptr = (unsigned char *)&CRCDAT;

     CRCCON = 0x0F;      // Configure the polynomial length (PLEN)
     _CRCIF = 0;         // Clear the CRC flag
     _CRCIE = 1;         // Enable the int

     CRCXOR = 0x1021;    // Polynomial - crc-16
     CRCWDAT = 0x0000;   // Initialize CRCWDAT with 0

     data = (*DataPacket).CHARS;

     length >>= 1;
     for(j = 0; j < length; j++)
     {
          //Must use pairs of bytes to get the correct result
          *ptr = *data++;  //write data into FIFO
          *ptr = *data++;  //write data into FIFO

          if (CRCCONbits.CRCFUL)  //check if FIFO is full
          {
               CRCCONbits.CRCGO = 1;         //start CRC engine
               while (!CRCCONbits.CRCMPT);   //check if FIFO is empty
               CRCCONbits.CRCGO = 0;         //stop CRC engine
          }
     }

     CRCDAT = 0x0000;
     CRCCONbits.CRCGO = 1;  // (Re)Start the calculation

     comms_datapacket = DataPacket; // Save a reference
     // With a bit of luck CRC finishes before we send
     // the data via SPI...

     return 1;
}

//Use the software CRC calculator
static const unsigned int CRC16Table[256] = {
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
};

unsigned int ChecksumCalcS(unsigned char *data, unsigned int len)
{
     unsigned int crc = 0;
     unsigned int i;
     for(i = 0; i < len; i++)
     {
          crc = (crc << 8) ^ CRC16Table[((crc >> 8) ^ *data++)];
     }
     return crc;
}

void __attribute__((__interrupt__)) _NOPSV _CRCInterrupt(void)
{
     unsigned short hardware, software;

     _CRCIF = 0;       // Clear the interrupt

     while(!CRCCONbits.CRCMPT);    // Wait for CRC shifter to clear FIFO
     (*comms_datapacket).CRC = CRCWDAT;

     hardware = CRCWDAT;
     software = ChecksumCalcS((*comms_datapacket).CHARS, 
                                                               (DATAPL_LEN*2)-2);

     CRCWDAT = 0;
}

Ok, a lot of things to note ;)

  • This code will not work on its own
  • Putting a breakpoint on the last line of code will allow a software / hardware comparison
  • _NOPSV is a macro (google it)
  • The DataPL datatype is a union of an int array, char array and a large bitfield which allows me to have loads of small (or large) data types of non-standard size.
  • The interrupt is not required if working with more than 8 bytes of data as you need to wait for the buffer to clear before filling it – so might as well wait for it all to happen
  • This uses 8-bit data rather than 16-bit data, if you have a working example of this with a software table based alternative please email me! the table means I can calculate the CRC in C#
  • Most of code is by billysdomain – I was 80% there, missing 2 lines in my implementation, should have rtfm (more)

Anyhow, hope this helps someone. I might do a quick post on using CRC for synchronising serial streams soon, guess how that’s done :p

Tagged with:
 

Timers on DSPics are easy – and after helping a few people out with timers at uni I thought I would post a really quick how-to on setting up a timer to interrupt at a set frequency. First lets start with a few bits you need to know:

Fosc = 40000000;

That’s it :D Just make sure that this value is the number in Hz on your oscillator (or a multiple of it if using PLL)

Next its important to remember there are different types of timers on a pic, do some research and rtfm before doing any pic / embedded work – will save you hours, I am using timer 1 – as its the first one in the manual :p

Next bits:


T1CONbits.TCKPS = 0x02; // Select the PRESCALER
TMR1 = 0x00; // Make sure the timer is starting from zero
PR1 = 600; // How long the timer should run before an interrupt (in timer ticks)
_T1IF = 0; // Clear the interrupt flag
_T1IE = 1; // Enable the interrupt
T1CONbits.TON = 1; // Turn the interrupt on

void __attribute__((__interrupt__)) __attribute__((no_auto_psv)) _T1Interrupt(void)
{
// Funky interrupt code goes
}

To explain, the prescale waits x many ticks before incrementing the timer counter and is usually a set number like 1,8,32 or 64. To calculate the number for PR1 you then juse a very simple formula:

[math]Fcy = Fosc/2[/math]

[math]PR1 = (Freq^-1)/(Fcy^-1)[/math]

Where Freq is your desired interrupt frequency. Then just round the value of PR1 to the nearest integer if it is not one already. Simple!

As a side note – more information to why the interrupt function has a no_auto_psv attribute can be found at FlyingPic24.com

Hope this has been useful – please comment if you have any corrections/questions/comments.

Tagged with: