dsPIC4013 doing SPI and piezo

Status
Not open for further replies.

futz

Active Member
Just for fun, here's the dsPIC4013 doing SPI out to my MAX7219 driven 4-digit LED display and beeping a piezo.



Yup, there's the new PICkit2.



The code:
Code:
#include "p30f4013.h"

_FOSC(FRC)				//osc
_FWDT(WDT_OFF)
_FBORPOR(MCLR_EN & PWRT_OFF)		//MCLR enable & power-up timers off

int spiout(int);
void delay(void);
void beep(int);

#define	CS	LATDbits.LATD8
#define piezo	LATCbits.LATC13

int main(void)
{
	char x,y;
	ADPCFG = 0xffff;		//all digital
	TRISC = TRISD = TRISF = 0;
	SPI1CON = 0x073f;
	SPI1STATbits.SPIEN = 1;

	beep(80);
	spiout(0x0a0f);			//intensity register to full bright
	spiout(0x0b03);			//scan limit register - display digits 0-3
	spiout(0x090f);			//decode mode register - code B
	spiout(0x0c01);			//shutdown register - normal

	while(1)
	{
		for(x=0;x<10;x++)	//display 0-9 all digits
		{
			spiout(0x0100+x);
			spiout(0x0200+x);
			spiout(0x0300+x);
			spiout(0x0400+x);
			delay();
		}
		spiout(0x010c);		//display HELP
		spiout(0x020b);
		spiout(0x030d);
		spiout(0x040e);
		delay();
		for(y=0;y<16;y++)	//cycle thru intensity levels
		{
			spiout(0x0a00+y);
			beep(70);
			delay();
		}
	}
	return 0;
}

int spiout(int send)
{
	CS = 0;				//CS low
	SPI1BUF = send;
	while(!SPI1STATbits.SPIRBF){}
	CS = 1;
	return SPI1BUF;
}	

void beep(int tone)
{
	int x,y;
	for(x=0;x<80;x++)
	{
		piezo = 1;
		for(y=0;y<tone;y++){}
		piezo = 0;
		for(y=0;y<tone;y++){}
	}
}	

void delay(void)
{
	int x,y;
	for(x=0;x<30;x++)
	{
		for(y=0;y<10000;y++){}
	}
}
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…