1. Serial port communication header/definition file usart.h
#ifndef _SERIAL_H_
#define _SERIAL_H_
#define BAUD 9600
#define DARK 9216000L
#define NINE 0 /* Use 9bit communication? FALSE=8bit */
#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1
#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif
#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif
#if defined(_16F87) || defined(_16F88)
#define RX_PIN TRISB2
#define TX_PIN TRISB5
#else
#define RX_PIN TRISC7
#define TX_PIN TRISC6
#endif
/* Serial initialization */
//'\' is indispensable for defining the attributes within the function
#define init_comms()\
RX_PIN = 1; \
TX_PIN = 1; \
SPBRG = DIVIDER; \
RCSTA = (NINE_BITS|0x90); \
TXSTA = (SPEED|NINE_BITS|0x20)
void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);
#endif
2. Serial communication source/implementation file usart.c
/******************************************/
/*Author:Shen Chucu All Rights Reserved!**
/*Tsinghua University
/*2016-11-15
/********************************************/
#include
#include
#include "usart.h"
__CONFIG(0x3ffa);
void delay(unsigned int x);
static int label=0; //Do not respond to events
void main()
{
INTCON=0x00;
GIE=1;
LIKE=1;
RCIE=1;
init_comms();
BELIEVE=1;
SPEN=1;
while(1)
{//Wait for interrupt and set event response
if(label==1)
{
printf("OK");
label = 0; //Send a feedback signal
delay(50);
}
if(label==2)
{
printf("ERROR");
label = 0; //Send a feedback signal
delay(50);
}
}
}
void interrupt IsReceive()
{
if(RCIE&&RCIF==1) //Receive interrupt enable bit + receive interrupt flag bit
{
unsigned char temp=RCREG; //Save the data sent by the host computer
if(temp=='S')
{
label=1; //Send data label 1
}
else if(temp=='E')
{
label=0; //Send data flag 0
}
else
label=2; //Send data label 2
}
}
void delay(unsigned int x)
{
unsigned int a,b; //delay time 110x
for(a=x;a>1;a--)
for(b=110;b>1;b--)
;
}
Previous article:PIC Microcontroller Mastery_ADC left-aligned and right-aligned data reading issues
Next article:PIC microcontroller mastery_serial communication and serial debugging examples
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Design and Implementation of Digital Video Conversion Interface Based on FPGA
- [AT-START-F403A Evaluation] VI. FreeRTOS system based on IAR security library (sLib) secondary development mode practice
- About the 2021 E-Sports Championship
- [ST NUCLEO-G071RB Review]——First Look at the Kit
- FPGA Advanced Timing Synthesis Tutorial
- How reliable is Dupont wire???
- Classic Books on Signal Integrity Design
- How to avoid copper between the pins of AD18 chip
- [ST waterproof pressure sensor LPS27HHW evaluation 1] Hardware preparation and data collection
- Live broadcast at 10 am today [Design considerations for the precision acquisition signal chain of ADI vibration signals]