PIC microcontroller mastery_serial communication module C implementation

Publisher:GoldenDreamLatest update time:2018-07-30 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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--)

          ;


Reference address:PIC microcontroller mastery_serial communication module C implementation

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

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号