3. ARM9 (S3C2440) serial port UART - program example explanation

Publisher:花海鱼Latest update time:2020-06-11 Source: eefocusKeywords:ARM9 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Serial communication program writing steps


UART communication program can use query, interrupt and DMA mode. We introduce the writing of UART communication program by using more interrupt methods. The simple way is to refer to the example program for writing UART communication program.


Select the channel through the function Uart_Select(); select UART0~UART2;


Select the baud rate and baud rate generator clock. The baud rate is selected through the function Uart_Pclk_En(int ch, int baud) or Uart_Pclk_En(int ch, int baud). The clock is UCLK, rUCON0|=0x400; the clock is PCLK, rUCON0&=0x3ff.


Communication protocol (rULCON0) setting, if normal communication, one stop bit, 8 data bits, no parity check: rULCON0=(0<<6)|(0<<3)|(0<<2)|(3);      


Communication control word (rUCON0) settings, such as clock selection ULK as baud rate generator; Tx interrupt pulse trigger, Rx interrupt pulse trigger; receive timeout interrupt enabled; generate receive error interrupt; send in normal mode:


        rUCON0|=(TX_INTTYPE<<9)|(RX_INTTYPE<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(1);


        I/O port initialization, because UART communication uses the second function of H port, so H port pull-up is disabled: rGPHUP|=0x1ff. H port control register nRTS1, nCTS1 function enable, rGPHCON&=0x3c0000, rGPHCON|=0x2faaa;


Set the interrupt service function entry address, assign the interrupt service function entry address to the function pointer PISR_UARTn, note that the receiving interrupt service function entry address and the sending interrupt service function entry address are the same, in the interrupt service function according to


The status of UTRSTATn[1] and UTRSTATn[0] determines whether it is a transmit interrupt or a receive interrupt.


Open the overall interrupt mask and sub-interrupt mask and wait for the interrupt:


rINTMSK=~(BIT_UART0);


rINTSUBMSK=~(BIT_SUB_TXD0);


        After entering the interrupt, first shield the sending and receiving interrupts to prevent new interrupts from interfering with our normal sending and receiving. After the normal sending and receiving are completed, clear the interrupt pending and interrupt source pending registers: ClearPending(BIT_UART0), rSUBSRCPND=(BIT_SUB_TXD0 (sending), rSUBSRCPND=(BIT_SUB_RXD0|BIT_SUB_ERR0) (receiving);


Cancel interrupt mask and wait for the next interrupt.


The following is a serial communication program using the query method (FL2440 development board)


#include "2440addr.h" // This program is a PC that sends the four numbers 1234 to the development board through the serial port tool to control the on and off of the four LEDs


int TSmain()

{

 char buf,i;

 

 rULCON0 &=0XFFFFFF00;

 rULCON0 |= 0X03; //1 start bit, 8 data bits

 rUCON0 = 0x05; //0X0805; //Serial port clock PCLK, query method East: PCLK is 50M

 rUBRDIV0 =325; //0X1A; //Baud rate 115200****325 is set to 9600

 rGPBCON = 0x1dd7fc; //GPB5,6,8,10 are set to output

 rGPBDAT|=0x560; //4 LEDs are all off

 while(1)

 { 

  if(rUTRSTAT0 & 0X01) //Is receiving completed? =1 End

  {

   

   buf=rURXH0; //Read data

   while(!(rUTRSTAT0 & 0X04));//Is sending allowed? = 1: Allowed


   rUTXH0=buf;

   if(buf=='1')//Judge which number is received

    i=1;

   else if(buf=='2')

    i=2;

   else if(buf=='3')

    i=3;

   else if(buf=='4')

    i=4;

   switch(i){ //Turn the corresponding LED on and off

      

      case 1:

        rGPBDAT^=(1<<5);

        i=0; // Clear i to prevent interference when receiving other data next time

        break;

      case 2:

        rGPBDAT^=(1<<6);

        i=0;

        break;

      case 3:

        rGPBDAT^=(1<<8);

        i=0;

        break;

      case 4:

        rGPBDAT^=(1<<10);

        i=0;

        break;

      default:break;

      }

   

  }

 }

 

 return 0;

}


 The following is a serial communication program using interrupts



#include "2440addr.h"

void __irq UART0RX_isr()

 {

  char buf,i;

  rINTMSK=0xffffffff;

  ClearPending(BIT_UART0);

  

  if(rUTRSTAT0 & 0X01) //Is receiving completed? =1 End

  {

   ClearSubPending(BIT_SUB_RXD0);

   buf=rURXH0; //Read data

   while(!(rUTRSTAT0 & 0X04));//Is sending allowed? = 1: Allowed


   rUTXH0=buf;

   if(buf=='1')

    i=1;

   else if(buf=='2')

    i=2;

   else if(buf=='3')

    i=3;

   else if(buf=='4')

    i=4;

   switch(i){

      

      case 1:

        rGPBDAT^=(1<<5);

        i=0; // Clear i to prevent interference when receiving other data next time

        break;

      case 2:

        rGPBDAT^=(1<<6);

        i=0;

        break;

      case 3:

        rGPBDAT^=(1<<8);

        i=0;

        break;

      case 4:

        rGPBDAT^=(1<<10);

        i=0;

        break;

      default:break;

      }

   

  }

  EnableIrq(BIT_UART0);

  EnableSubIrq(BIT_SUB_RXD0);

  EnableIrq(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);

 }

static void __irq Key_ISR()

{

 char key; // used to identify which key is pressed


 //EnterCritical(&r);

 rINTMSK=0xffffffff;

 if(rINTPND==BIT_EINT0) {

  ClearPending(BIT_EINT0);

  key=1;

 }

  else if(rINTPND==BIT_EINT2) {

   ClearPending(BIT_EINT2);

   key=2;

  }

   else if(rINTPND==BIT_EINT3) {

    ClearPending(BIT_EINT3);

    key=3;

   }

    else if(rINTPND==BIT_EINT4_7){

     rEINTPEND=(1<<4);

     ClearPending(BIT_EINT4_7);

     key=4;

    }

 switch(key){

  case 1:

    rGPBDAT^=(1<<5);

    break;

  case 2:

    rGPBDAT^=(1<<6);

    break;

  case 3:

    rGPBDAT^=(1<<8);

    break;

  case 4:

    rGPBDAT^=(1<<10);

    break;

 }

 

 //ExitCritical(&r);

 EnableIrq(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);

 EnableIrq(BIT_UART0);

 EnableSubIrq(BIT_SUB_RXD0);

    

  

  

}

int TSmain()

{

 

 

 rULCON0 &=0XFFFFFF00;

 rULCON0 |= 0X03; //1 start bit, 8 data bits

 rUCON0 = 0x05; //0X0805; //Serial port clock PCLK, query method East: PCLK is 50M

 rUBRDIV0 =325; //0X1A; //Baud rate 115200****325 is set to 9600

 rGPHUP=0x1ff; //H port pull-up disabled

 rGPHCON&=0x3c0000;

 rGPHCON|=0x2faaa;

 rGPBCON = 0x1dd7fc; //GPB5,6,8,10 are set to output

 rGPBDAT|=0x560; //4 LEDs are all off

 rGPFCON &=~((3<<0)|(3<<4)|(3<<6)|(3<<8)) ;

 rGPFCON |= ((2<<0)|(2<<4)|(2<<6)|(2<<8)) ; //GPF0, GPF2, GPF3, GPF4 work in the second functional state, i.e., interrupt

 rEINTPEND=(1<<4);

 ClearPending(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);

 ClearSubPending(BIT_SUB_RXD0);

 ClearPending(BIT_UART0);

 pISR_EINT0= pISR_EINT2 = pISR_EINT3 = pISR_EINT4_7=(int)Key_ISR;

 EnableIrq(BIT_EINT0|BIT_EINT2|BIT_EINT3|BIT_EINT4_7);

 EnableIrq(BIT_UART0);

 EnableSubIrq(BIT_SUB_RXD0);

 rEINTMASK=~(1<<4);

 pISR_UART0=(unsigned) UART0RX_isr;

 

 while(1)

 { 

  

 }

 

 return 0;

}

Keywords:ARM9 Reference address:3. ARM9 (S3C2440) serial port UART - program example explanation

Previous article:(II) s3c2440——Serial port experiment
Next article:All the problems of S3C2440A ARM learning

Latest Microcontroller Articles
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号