串口通信實驗


請在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