<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > ARM7學(xué)習(xí)---UART0練習(xí)

          ARM7學(xué)習(xí)---UART0練習(xí)

          作者: 時間:2016-11-18 來源:網(wǎng)絡(luò) 收藏
          前段時間在練習(xí)ARM7的時候遇到一個問題,就是串口通信一直沒成功,數(shù)據(jù)總是出錯,好像是波特率沒設(shè)置好。但是我找了很久都沒找出原因。昨天又看數(shù)據(jù)手冊,后來找到了原因,是LPC2103的PLL等時鐘沒配置好,所以導(dǎo)致了波特率和我設(shè)置的不一樣。

          以下是調(diào)試成功的示例程序:

          本文引用地址:http://www.ex-cimer.com/article/201611/316017.htm

          /**************ARM7(LPC2103)練習(xí)程序**************************/
          /*************************************************************/
          /*****File Function : UART test *****/
          /*****Program Author : ZhengWen(ClimberWin) *****/
          /*****MCU : LPC2103F 外部11.0592M晶振 *****/
          /*****Compile Date : 2009/12/3 *****/
          /*****Edition Info : V1.0 *****/
          /*************************************************************/
          //編譯環(huán)境 KEIL for ARM
          //功能描述:串口練習(xí),使用UART0向串口發(fā)送字符和字符串
          //接收串口數(shù)據(jù)并且+1后發(fā)回給串口

          //修改日期 2010年2月8日。
          //以前是系統(tǒng)的各部分時鐘沒配置好,所以導(dǎo)致波特率不對,現(xiàn)在可以正常工作。
          #include
          #include
          #define uint unsigned int
          #define uchar unsigned char

          #define baudrate 9600 //設(shè)置波特率
          #define PE (U0LSR&0x40)//定義串口數(shù)據(jù)發(fā)送忙碌與否,PE=1忙碌;PE=0;不忙綠

          #define Fosc(11059200)//晶振頻率,10MHz~25MHz,應(yīng)當與實際一至
          #define Fcclk(Fosc * 6) //66.3552 系統(tǒng)頻率,必須為Fosc的整數(shù)倍(1~32),且<70MHZ
          #define Fcco(Fcclk * 4) //CCO頻率,必須為Fcclk的2、4、8、16倍,范圍為156MHz~320MHz
          #define Fpclk(Fcclk / 4) * 1 //016.5888,VPB時鐘頻率,只能為(Fcclk / 4)的1 ~ 4倍


          void delayms(unsigned int count); //延時程序
          void UART0_INT(void); //串口初始化
          void UART0_SendByte(unsigned char data); //串口發(fā)送字節(jié)
          void UART0_SendStr(unsigned char const *str);//串口發(fā)送字符串

          void PLL_Init(void)
          {
          /* 設(shè)置系統(tǒng)各部分時鐘 */
          PLLCON = 1;
          #if ((Fcclk / 4) / Fpclk) == 1
          VPBDIV = 0;
          #endif
          #if ((Fcclk / 4) / Fpclk) == 2
          VPBDIV = 2;
          #endif
          #if ((Fcclk / 4) / Fpclk) == 4
          VPBDIV = 1;
          #endif
          #if (Fcco / Fcclk) == 2
          PLLCFG = ((Fcclk / Fosc) - 1) | (0 << 5);
          #endif
          #if (Fcco / Fcclk) == 4
          PLLCFG = ((Fcclk / Fosc) - 1) | (1 << 5);
          #endif
          #if (Fcco / Fcclk) == 8
          PLLCFG = ((Fcclk / Fosc) - 1) | (2 << 5);
          #endif
          #if (Fcco / Fcclk) == 16
          PLLCFG = ((Fcclk / Fosc) - 1) | (3 << 5);
          #endif
          PLLFEED = 0xaa;
          PLLFEED = 0x55;
          while((PLLSTAT & (1 << 10)) == 0);
          PLLCON = 3;
          PLLFEED = 0xaa;
          PLLFEED = 0x55;
          }

          /*************延時程序***************/
          void delayms(unsigned int count)
          {
          unsigned int i,j;
          for(i=0;ifor(j=0;j<5000;j++);
          }
          /***********串口0初始化**********************/
          void UART0_INT(void)
          { unsigned int U0DL;

          U0LCR = 0x83; // DLAB = 1,可設(shè)置波特率;無奇偶校驗 1位停止位 8位數(shù)據(jù)長度。
          U0DL = (Fpclk/16)/baudrate;
          U0DLM= U0DL/256; //高8位
          U0DLL = U0DL%256; //低8位
          U0LCR = 0x03; // DLAB = 0,設(shè)置好波特率;無奇偶校驗 1位停止位 8位數(shù)據(jù)長度。
          }
          /***********串口發(fā)送字節(jié)**********************/
          void UART0_SendByte(unsigned char data)
          {
          U0THR = data; //發(fā)送數(shù)據(jù)
          while( PE==0 ); //等待數(shù)據(jù)發(fā)送完畢 PE=1忙碌;PE=0;不忙綠

          }
          /***********串口發(fā)送字符串**********************/
          void UART0_SendStr(unsigned char const *str)
          { while(1)
          { if( *str == 看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();