串口通信实验


请在Proteus中,完成两个LPC1114芯片通过串口互相发送数据的实验。

【要求】

1、完成硬件电路设计

2、完成软件编程

3、实现从一个芯片发送数据信息到另外一个芯片

4、使用Virtual Terminal显示发送、接收到的数据

【提交】

1、提交电路图仿真截图

2、上传c程序

 

 

 

 

1方发送hello,当收到hi时重新发送 另一方等待hello,当受到hello时,发送hi

 

原理图绘制

 

 

第一块芯片

main.c

/* Main.c file generated by New Project wizard
 *
 * Created:   周四 4月 2 2020
 * Processor: LPC1114FBD48/301
 * Compiler:  GCC for ARM
 */

#include <LPC11xx.h>
#define LSR_RDR  0x01
#define LSR_OE   0x02
#define LSR_PE   0x04
#define LSR_FE   0x08
#define LSR_BI   0x10
#define LSR_THRE 0x20
#define LSR_TEMT 0x40
#define LSR_RXFE 0x80

void UART_Init(uint32_t baudrate)
{
   uint32_t  Fdiv;
   uint32_t  regVal;
   
   NVIC_DisableIRQ(UART_IRQn);
   
   LPC_IOCON->PIO1_6&=~0x07;
   LPC_IOCON->PIO1_6|= 0x01;
   LPC_IOCON->PIO1_7&=~0x07;
   LPC_IOCON->PIO1_7|= 0x01;
   LPC_SYSCON->SYSAHBCLKCTRL|=(1<<12);
    LPC_SYSCON->UARTCLKDIV=0x1;
    
    LPC_UART->LCR=0x83;
    regVal=LPC_SYSCON->UARTCLKDIV;
    Fdiv=((SystemCoreClock/regVal)/16)/baudrate;
    
    LPC_UART->DLM=Fdiv/256;
    LPC_UART->DLL=Fdiv%256;
    LPC_UART->LCR=0x03;
    LPC_UART->FCR=0x07;
    
    regVal=LPC_UART->LSR;
    
    while(LPC_UART->LSR&((LSR_THRE|LSR_TEMT)!=(LSR_THRE|LSR_TEMT)));
    while(LPC_UART->LSR&LSR_RDR)
    {
       regVal=LPC_UART->RBR;
    }
    
}

int UART_GetChar(void)
 {
    while(!(LPC_UART->LSR&0x01));
    return (LPC_UART->RBR);
    
 }

 int UART_PutChar(int ch)
 {
    while(!(LPC_UART->LSR&0x20));
    return (LPC_UART->THR=ch);
    
 }
 


int main (void)
 { 
   // Write your code here
   LPC_SYSCON->SYSAHBCLKCTRL|=(1<<6);
   UART_Init(9600);
   /*char data[1000]="'The beast' was a reference to an evil-looking creature that the author of Revelation saw rising out of the earth in a vision (Revelation 13:11-18). This creature could perform miraculous things, would demand that everyone be 'marked' with its name or number in order to buy and sell anything; and would also kill those who did not worship it. So, who was this? Over the centuries, people have wondered whether this beast referred to someone who has come and gone, was yet to come or to no person in particular.";
   //while(1) UART_PutChar('h');
   int i=0;
   for(i=0;data[i]!='\0';++i)
   {
      UART_PutChar(data[i]);
      if(i%30==0)
      {
    UART_PutChar('\r'); 
      }
   }
   while(1) ;
   */
   while (1) 
    {
       UART_PutChar('h');
       UART_PutChar('e');
       UART_PutChar('l');
       UART_PutChar('l');
       UART_PutChar('o');
       while(!(UART_GetChar() == 'h'));
       while(!(UART_GetChar() == 'i'));
    }
  }   
 
 

第二块芯片

main.c

/* Main.c file generated by New Project wizard
 *
 * Created:   周四 4月 2 2020
 * Processor: LPC1114FBD48/301
 * Compiler:  GCC for ARM
 */

#include <LPC11xx.h>
#define LSR_RDR  0x01
#define LSR_OE   0x02
#define LSR_PE   0x04
#define LSR_FE   0x08
#define LSR_BI   0x10
#define LSR_THRE 0x20
#define LSR_TEMT 0x40
#define LSR_RXFE 0x80

void UART_Init(uint32_t baudrate)
{
   uint32_t  Fdiv;
   uint32_t  regVal;
   
   NVIC_DisableIRQ(UART_IRQn);
   
   LPC_IOCON->PIO1_6&=~0x07;
   LPC_IOCON->PIO1_6|= 0x01;
   LPC_IOCON->PIO1_7&=~0x07;
   LPC_IOCON->PIO1_7|= 0x01;
   LPC_SYSCON->SYSAHBCLKCTRL|=(1<<12);
    LPC_SYSCON->UARTCLKDIV=0x1;
    
    LPC_UART->LCR=0x83;   //查用户手册
    regVal=LPC_SYSCON->UARTCLKDIV;
    Fdiv=((SystemCoreClock/regVal)/16)/baudrate;
    
    LPC_UART->DLM=Fdiv/256;
    LPC_UART->DLL=Fdiv%256;
    LPC_UART->LCR=0x03;
    LPC_UART->FCR=0x07;
    
    regVal=LPC_UART->LSR;
    
    while(LPC_UART->LSR&((LSR_THRE|LSR_TEMT)!=(LSR_THRE|LSR_TEMT)));
    while(LPC_UART->LSR&LSR_RDR)
    {
       regVal=LPC_UART->RBR;
    }
    
}

int UART_GetChar(void)
 {
    while(!(LPC_UART->LSR&0x01));
    return (LPC_UART->RBR);
    
 }

 int UART_PutChar(int ch)
 {
    while(!(LPC_UART->LSR&0x20));
    return (LPC_UART->THR=ch);
    
 }
 


int main (void)
 { 
   // Write your code here
   LPC_SYSCON->SYSAHBCLKCTRL|=(1<<6);
   UART_Init(9600);
   /*int i=0; 
   while(1)
   {
      UART_PutChar(UART_GetChar()+1);
      ++i;
      if(i%30==0)
     UART_PutChar('\r');
   }*/
  while (1) 
    {
       while(!(UART_GetChar() == 'h'));
       while(!(UART_GetChar() == 'e'));   
       while(!(UART_GetChar() == 'l'));
       while(!(UART_GetChar() == 'l'));
       while(!(UART_GetChar() == 'o'));
      
       UART_PutChar('h');
       UART_PutChar('i');
    }  
 }   
 
 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM