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
__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
}
}
Previous article:PIC Instruction Introduction
Next article:PIC Getting Started 8, Buzzer and Music Program Experiment
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How should I find the development board schematics and PCBs of major manufacturers?
- EEWORLD University ---- Wireless Power 101
- TI mmWave Radar MIMO (2TX4RX) Setup
- [STM32MP157C-EV1] After the first unboxing and evaluation, I will run rtt on it and share other
- MSP430 Program Library---Button
- EEWORLD University ---- Live Replay: How Littelfuse Improves the Safety and Reliability of Electronic Equipment in Smart Buildings in the Internet of Things Era
- [NXP Rapid IoT Review] + Kit Modification-External Lithium Battery
- 【Zero Knowledge ESP8266】HTTP WEB Server Example
- 【K210 Series】4. MicroPython is still not perfect
- The Differences Between Leaded and Lead-free PCB Processes