Menu

Adskeeper - header widge

728x90

Search This Blog

468x60

Navbar

AdsKeeper - IAB Display Standard Ad Unit

Monday, October 12, 2026

dsPIC30F2010 SPI Programming Example in MikroC

Introduction

The Serial Peripheral Interface (SPI) is high speed serial communication interface. It commonly uses a few wires  to connect to its SPI slave devices. In some case it needs only two wires - serial data and serial clock, for instance the SN74HC595N or SN74HC165 parallel in serial output shift registers. It's popular over the parallel port interface that use an 8-bit data port pluses a few control lines, for example the ADC0808 ADC inputs parallel data output.

 

dsPIC30F2010 SPI Programming Example in MikroC
SPI LED Displays


There are a lot of in-used SPI slave devices especially the SPI Flash memory for a micro-processor program data space. Some LCD controllers offer many communication protocols such as parallel port, SPI and I2C. However finished LCD module use only serial interfaces such as I2C and SPI.

 

dsPIC30F2010 Dedicated SPI module

There is one Serial Peripheral Interface (SPI) module inside this chip - that is SPI1. It is full duplex master and slave mode. It transfer clock frequency is up to a quarter of the CPU frequency. 

 

Unlike the 8-bit PIC micro-controllers, the SPI shift registers (SP1BUF) of this chip is 16-bit. There are only two control and status registers.

 

dsPIC30F2010 SPI Programming Example in MikroC
dsPIC30F SPI Block Diagram

 

 

The programmer just needs to enable the hardware, assign data to the SPI buffer and wait for completion for each data frame.

 

MikroC SPI Programming for dsPIC30F2010

MikroC supports both hardware and software SPI communication protocol. The I/O pin of SPI interface is fixed with variable Slave Select (SS) pins. One master MCU can communicates with many SPI slave devices. The number of SS pins vary depending the connected SPI slave devices. The software SPI optional in the case where the dedicated SPI port is used for other purposes.

 

dsPIC30F2010 SPI1 and SN74HC595N LED Interface

I made a simple Serial LED module that used the SN74HC595N Serial-in-Parallel Out Shift Registers. It's simple DIY single-sided PCB module made hands.

 

dsPIC30F2010 SPI Programming Example in MikroC
Schematic Diagram

 

The SPI communication here is a half-duplex master mode because the SN74HC595N is a single direction input device. 

 

dsPIC30F2010 SPI Programming Example in MikroC
Test Program #1

dsPIC30F2010 SPI Programming Example in MikroC
Test Program #2

dsPIC30F2010 SPI Programming Example in MikroC
Test Program #3

For more information about this PCB click here.





MikroC Source Code:


  1. // Chip Select Pin
  2. sbit Chip_Select at LATE3_bit;
  3. sbit Chip_Select_Direction at TRISE3_bit;

  4. void SN74595_Send(char _data){
  5. SPI_Write(_data); // Send high byte via SPI
  6. Chip_Select = 0; // Select SPI chip
  7. Chip_Select = 1; // Deselect SPI chip
  8. }

  9. void blink(unsigned char counts, unsigned int _times){
  10. char i = 0;
  11. while(i<counts){
  12. SN74595_Send(0);
  13. Vdelay_ms(_times);
  14. SN74595_Send(0xFF);
  15. Vdelay_ms(_times);
  16. i++;
  17. }
  18. }
  19. void main() {
  20. char temp=0;
  21. Chip_Select = 1; // Deselect DAC
  22. Chip_Select_Direction = 0; // Set CS# pin as Output
  23. SPI1_Init(); // Initialize SPI module
  24. blink(10,100);
  25. Delay_ms(500);
  26. SN74595_Send(0x00);
  27. Delay_ms(5000);
  28. SN74595_Send(0x0F);
  29. Delay_ms(5000);
  30. SN74595_Send(0xF0);
  31. Delay_ms(5000);
  32. SN74595_Send(0xFF);
  33. Delay_ms(5000);
  34. SN74595_Send(0xAA);
  35. Delay_ms(5000);
  36. SN74595_Send(0x55);
  37. Delay_ms(5000);
  38. SN74595_Send(0x00);
  39. Delay_ms(5000);
  40. while (1) { // Endless loop
  41. SN74595_Send(1<<temp);
  42. temp++;
  43. Delay_ms(200);
  44. if(temp>7) temp = 0;
  45. }
  46. }


 

dsPIC30F2010 SPI1 and SN74HC595N 7-Segment Display Interface

 

I have one more SPI single digit common anode seven-segment display. I made it many years ago from scrap printer. For more information about this PCB click here.

 

dsPIC30F2010 SPI Programming Example in MikroC
Schematic Diagram

 

In this example I make an up/down counter using this display and two on-board push buttons. 

 


 

 

MikroC Source Code:


  1. #define SW4 13
  2. #define SW5 14
  3. #define CN0 0
  4. #define CN1 1

  5. // Chip Select Pin
  6. sbit Chip_Select at LATE3_bit;
  7. sbit Chip_Select_Direction at TRISE3_bit;

  8. void SN74595_Send(char _data){
  9. SPI_Write(_data); // Send high byte via SPI
  10. Chip_Select = 0; // Select SPI chip
  11. Chip_Select = 1; // Deselect SPI chip
  12. }

  13. // Common Cathod Segment Data
  14. const char cc[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
  15. 0x7F,0x6F,0x77,0x7C,0x58,0x5E,0x79,0x71};

  16. void show_number(char number){
  17. SN74595_Send(~cc[number]);
  18. }

  19. void main() {
  20. int temp=0;
  21. Chip_Select = 1; // Deselect DAC
  22. Chip_Select_Direction = 0; // Set CS# pin as Output
  23. SPI1_Init(); // Initialize SPI module
  24. ADPCFG = 0xFFFF; // configure AN pins as digital
  25. TRISD = 0; // set direction to be output
  26. LATD = 0; // turn OFF the PORTD leds
  27. TRISC = (1<<13)|(1<<14); // RC13 and RC14 As Inputs
  28. LATC=0; // Clear Buffer
  29. PORTC=0; // Clear Buffer
  30. // RC13-CN0 and RC14-CN1 : Enable Pull-Up
  31. CNPU1 = (1<<1)|(1<<0);

  32. for(temp=0;temp<16;temp++){
  33. show_number(temp);
  34. Delay_ms(1000);
  35. }
  36. temp = 0;
  37. show_number(temp);
  38. while (1) { // Endless loop
  39. if((PORTC&(1<<SW4))==0){
  40. temp++;
  41. Delay_ms(500);
  42. }
  43. if((PORTC&(1<<SW5))==0){
  44. temp--;
  45. Delay_ms(500);
  46. }
  47. if(temp<0) temp = 0x0F;
  48. if(temp>0x0F) temp = 0;
  49. show_number(temp);
  50. }
  51. }


For more information about dsPIC30F2010 I/O Programming using MikroC click on this link. I test this program on my DIY dsPIC30F1010 Prototype Board.

 

dsPIC30F2010 SPI Programming Example in MikroC
Test Program #1


dsPIC30F2010 SPI Programming Example in MikroC
Test Program #2

 

 

 

 

 

 

 

No comments:

Post a Comment

Adskeep- Standard Ads

336x280

DIY Electronic Projects And Instruments

AdsKeeper - 2091654

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (39) Analog Comparator (1) Arduino (16) ARM (32) AT89C52 (7) ATMega32 (59) ATMega644P (27) AVR (88) AVR PCB (2) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (5) Display (156) Distance Sensor (3) dot matrix (1) ds1307 (10) DS18B20 (6) ds3231 (1) ds3232 (2) dsPIC (13) dsPIC30F1010 (3) dsPIC30F2010 (13) EEPROM (5) Environment Sensor (6) ESP32 (1) esp8266 (1) Graphical LCD (3) HD44780 (10) I2C (38) ILI9341 (1) Input/Output (83) Interrupt (22) IoT PCB (1) Keil (5) Keypad (16) KS0108 (3) LCD (85) LM35 (4) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) MikroC ARM (1) MikroC dsPIC (8) Module PCB (2) Motor (15) MPLABX (73) Nokia 5110 LCD (5) OLED (2) One-Wire (7) Oscillator (8) PCB (13) PCD8544 (3) PCF8574 (10) PIC (117) PIC12F (3) PIC16 (105) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (12) PICKit2 (1) PICMicro PCB (4) Pin Change Interrupt (1) PWM (12) RTC (12) SBN0064G (1) Sensor (15) SH1106 (3) Shift Register (15) Shift Registers (19) SN74HC595N (9) Software SPI (1) Software TWI (2) SPI (44) sprinf (3) ST7735 (1) STM32 (29) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (23) STM32CubeIDE (32) STM32F103C8T6 (9) STM32F103R6 (18) STM32F401CCU6 (1) SysTick (7) temperature sensor (15) TFT (1) TG12864A (1) Thermometer (22) Timer/Counter (32) TM1637 (2) twi (10) UART (11) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (96)