PIC Getting Started 3, SPI Communication and Serial Port Debugging Experiment

Publisher:boyatangLatest update time:2016-10-31 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The MSSP module works in SPI master mode, which can be executed directly on the experimental board.

program:

//Suitable for 3EPIC experiment board, configure the MSSP module of PIC microcontroller to work in SPI master mode,
//The received parallel data is sent out as serial data through the SD0 port through a parallel/serial conversion shift register (74HC165),
//PIC stores the serial data in the register and sends it to the D port to drive the LED to light up.
#include
#include
volatile unsigned char data;
/****************************************************************************/
/*Function name: SPIINT() */
/*Function: SPI initialization */
/*****************************************************************************/

void SPIINIT()
{
 PIR1=0;  
 SSPCON=0x30; /*SSPEN=1;CKP=1;FOSC/4 */  
      
 SSPSTAT=0xC0; 
 TRISC=0x10; //SDO pin is input, SCK pin is output 
}
/*************************************************************************/
/*Function name: initial() */
/*Function: Initialize the input and output ports of the system */
/*************************************************************************/

void initial()
{
 TRISE=0x00; //Control PL port
 TRISD=0x00; /*Port D connected to LED is output*/    
 INTCON=0x00; //Disable all interrupts
}
/****************************************************************************/
/*Function name: SPIN() */
/*Function: SPI receiving subroutine */
/*****************************************************************************/

void SPIN()
{
 RE1=0; //PL low, parallel data is placed in
 RE1=1; //PL high, parallel data is read out
 SSPBUF=0; //Start SPI, this operation is only used to clear the BF bit of SSPSTAT, so the actual data in W is not important 
 do
 {
   ;
 }while(SSPIF==0); //Wait for the transmission to be completed 
SSPIF=0; //Clear SSPIF flag
data=SSPBUF;    
}
/*****************************************************************************/
/*Function name: SPIOUT() */
/*Function: Display the data received by SPI on 8 light-emitting diodes through port D */
/*****************************************************************************/

void SPIOUT(int data)
{
 PORTD=~data;
}
/************************************************************************/
/*Function name: main() */
/*Function: Main function */
/*************************************************************************/

main()
{
 initial(); //System initialization
 RE0=1; //LED enable terminal 
 SPIINIT(); //SPI initialization
 while (1)
  {
  SPIN(); //SPI receives external data
  SPIOUT(data); //Send data display
  }  
}

This needs to be connected to 74HC595 separately, but when it is running, it is the fast motion (rapid fire) of 74HC595, so it does not display from 1 to the end, but directly displays the end number, and the slow motion has not been adjusted.

program:

//Suitable for 3ePIC development board, to realize the display of digital tube in SPI mode
//The downstream device is 74HC595 (8-bit serial to parallel register) experimental SPI communication
#include
#include
static volatile int table[]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0X77,0X7C,0X39,0X5E,0X79,0X71};
volatile unsigned char data; 
#define PORTAIT(adr,bit) ((unsigned)(&adr)*8+(bit)) //*Absolute addressing bit operation instruction*/
static bit PORTA_5 @ PORTAIT(PORTA,5); 
/*************************************************************************/
/*Function name: SPIINT() */
/*Function: Initialize SPIi */
/************************************************************************/
void SPIINIT()
{
 PIR1=0;  
 SSPCON=0x30; /*Allow SPI to work in master mode, SSPEN=1; CKP=1, */
       /* FOSC/4 */
 SSPSTAT=0xC0;
 TRISC=0x00; /* SDO pin is output, SCK pin is output*/
}
/*****************************************************************************/
/*Function name: initial() */
/*Function: Initialize the input and output ports of the system */
/*************************************************************************/
void initial()
{
 TRISA=0x00; //Set port A to output
 TRISB=0x00;
 INTCON=0x00; //Disable all interrupts
       // LACK sends low level to prepare for latching
}
/****************************************************************************/
/*Function name: SPILED() */
/*Function: SPI sends data */
/****************************************************************************/
void SPILED(int data)
{
 SSPBUF=data; //Start sending
 do
 {
  ;
 }while(SSPIF==0); //Wait for sending to be completed
SSPIF=0; //Clear SSPIF flag
}

void DELAY()
{
 unsigned int j;
 for(j=0;j<50000;j++)
 continue;
}

/*******************************************************************************/
/*Function name: main() */
/*Function: main program */
/***************************************************************************/
main()
{
 unsigned int i;

 initial(); //System initialization
 RB2=0;
 SPIINIT(); //SPI initialization 
  for(i=0;i<8;i++) //Continuously send 8 data
   { 
  data=table[i]; //Get the segment code to be displayed through array conversion 
  SPILED(data); //Send the display segment code to display */
   }
 PORTA_5=0; 
 DELAY();
 PORTA_5=1; //Finally, give the latch signal to indicate that the display task is completed
}

Serial port debugging experiment:

Experimental Procedure:

//Purpose of the experiment: Get familiar with USART communication
//Use the "Serial Port Debug Assistant" to assist in the work
//Serial Port Debug Assistant sends data to 877, and 877 forwards it back to the Serial Port Assistant after receiving it
//Hardware requirements: All DIP switches S7 are set to ON, and other DIP switches are set to OFF

#include //Includes the predefined
 __CONFIG(0x1832) internal resources of the MCU;        
//Chip configuration word, watchdog off, power-on delay on, power-off detection off, low voltage programming off, encryption, 4M crystal HS oscillation

//---------------------------------------------
//Main program
void main()
 {
  TRISC=0XFF; //Set the direction of port C to all outputs
  SPBRG=0XC; //Set the baud rate to 19200BPS
  TXSTA=0X24; //Enable serial port transmission, select high-speed baud rate
  RCSTA=0X90; //Enable serial port operation, continuous reception
  RCIE=0X1; //Enable receive interrupt
  GIE=0X1; //Open global interrupt
  PEIE=0X1; //Enable external interrupt
  while(1) //Wait for interrupt
   {;}
  }

//--------------------------------------------
//Interrupt function
void interrupt usart(void)
  {
   if(RCIE&&RCIF) //Judge whether it is a serial port receive interrupt
     {
      TXREG=RCREG; //Send the received data back
     }
   }

Reference address:PIC Getting Started 3, SPI Communication and Serial Port Debugging Experiment

Previous article:PIC Instruction Introduction
Next article:PIC Getting Started 8, Buzzer and Music Program Experiment

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号