阿里巴巴网站的功能正规html5网站
文章目录
- 13 uart串口基础开发
 - 基本电路图:
 - 实验相关寄存器
 - 波特率设置及计算
 - 计算过程:
 - 设置中断
 - 发送处理代码
 - 串口接收中断处理函数
 - main.c
 
13 uart串口基础开发
基本电路图:

实验相关寄存器
相关寄存器UxCSR、UxCSR、UxGCR、UxBUF、UxBAUD、CLKCONCMD 、CLKCONSTA详见’数据手册的第16章节的16.8。
 
CC2540配置串口的一般步骤:
 1、配置IO,使用外部设备功能。此处配置 PO_2和 PO_3用作串口UART0
 2、配置相应串口的控制和状态寄存器。
 3、配置串口工作的波特率。
 寄存器PxSEL,其中x为端口的标号0~2,用来设置端口的每个引脚为通用IO或者是外部设备IO信号,作为缺省的情况,每当复位之后,所有的数字输入输出引脚都设置为通用输入引脚。
 在任何时候,要改变一个端口引脚的方向,就使用寄存器PxDR来设置每个端口引脚为输入或输出。因此口要设置PxDIR中的指定位为1、其对应的引脚口就被设置X输出了
 当读取端口寄存器PO、P1和P2的值,不管引脚配置如何,输入引脚上的逻辑值都被返回。这在执行读-修改-写指令期间不适用。读-修改-写指令是: ANL,ORL,XRL,JBC,CPL,INC,DEC,DNZ,MOV,CLR和SETB。在一个端口寄存器上操作,以下是正确的:当目标是端口寄存器P0、P1或P2中一个独立的位,寄存器的值,而不是引脚上的值,被读取、修改并写回端口寄存器。
 用作输入时,通用IO端口引脚可以设置为上拉、下拉或三态操作模式。作为缺省的情况。复位之后,所有的端口均设詈为带上拉的输入。要取消输入的上拉或下拉功能,就要将PxINP中的对应位设置为1。IO端口引脚P1.0和P1.1没有上拉/下拉功能。注意配置为外设IO信号的引脚没有上拉/下拉功能,即使外设功能是一个输入。
 在电源模式PM1、PM2和 PM3下 IO引脚保留当进入PMI/PM2/PM3时设置的IO模式和输出值(如果可用的话)。
 7.6.4 USART 0
 The SFR register bit PERCFG.U0 selects whether to use alternative 1 or alternative 2 locations.
 ln Table 7-1, the USART0 signals are shown as follows:
 UART:
 RX:RXDATA
 TX: TXDATA
 RT: RTS
 CT: CTS
 SPI:
 MI: MISO
 MO: MOSI
 C: SCK
 SS: SSN
 P2DIR.PRIP0 selects the order of precedence when assigning several peripherals to Port 0.When set to00, USART 0 has precedence. Note that if UART mode is selected and hardware flow control is disabled,USART 1 or Timer 1 has precedence to use ports P0.4 and P0.5.
 P2SEL.PRI3P1 and P2SEL.PRIOP1 select the order of precedence when assigning several peripheralsto Port 1.USART 0 has precedence when both are set to 0.Note that if UART mode is selected andhardware flow control is disabled, Timer 1 or Timer 3 has precedence to use ports P1.2 and P1.3.
 PERCFG = 0x00; //外设控制寄存器USART 0的IO位置,启用USART 0,故赋值为0
 如果只开启P0.2,P0.3,则:
 P0SEL = 0x0c; //P0_2,P0_3用作串口(外设功能)则为第2,3位,即0000 1100 = 0x0c
 
如果要开启P0.2,P0.3,P0.4,P0.5,则:
 P0SEL = 0x3c; //P0_2,P0_3,P0_4,P0_5用作串口(外设功能)则为第2,3,4,5位,即0011 1100 = 0x3c
 
设置uart0优先,需操作寄存器为:
 P2DIR (OxFF)- Port 2 Direction and Port 0 peripheral priority control
 设置:
 P2DIR &= ~0XC0; //P0优先作为UART0
 0XC0=1100 0000
 ~1100 0000 =0011 1111
 只修改7:6,设置为0,用&操作。
 USART Registers
 The registers for the USART are described in this section.For each USART there are five registersconsisting of the following (x refers to the USART number, i.e., 0 or 1):
 UxCSR,USART x control and status
 UxUCR,USART x UART controlUxGCR,USART x generic control
 UXDBUF,USART x receive/transmit data buffer.UXBAUD.USART x baud rate control
 
 
波特率设置及计算
Baud rate exponent value. BAUD_E along with BAUD_Mdetermines the UART baud rate andthe SPl master sCK clock frequency.
 U0GCR |= 9; U0GCR的[4:0]位设置:BAUD_E[4:0] ,波特率指数。9=0000 1001 [4:0]位为0 1001
 BAUD_E和baud_m一起决定了UART波特率和SPl主sCK时钟频率。
 U0BAUD |= 59; //波特率设为19200,
 
计算过程:
公式为:


 以32MHZ为例,查表结果:
设置中断
U0CSR |= 0x40; //0x40=0100 0000 ,操作第6位,允许接收
 U0CSR (0x86) – USART 0 Control and Status
 UART receiver enable. Note do not enable receive before uart is fully configured.
 0:Receiver disabled
 1: Receiver enabled
 
中断设置IRCON2.UTX0IF
 UTX0IF = 0; //UART0 TX中断标志初始置位0
 
IEN0 |= 0x84; //0x84 = 1000 0100 开总中断允许接收中断
 7 EA 0 R/W Disables all interrupts.
 0: No interrupt is acknowledged.(没有中断被确认)
 1: Each interrupt source is individually enabled or disabled by setting its
 corresponding enable bit.(1:通过设置每个中断源对应的使能位来单独使能或使能。)
 2 URX0IE 0 R/W USART0 RX interrupt enable
 0: Interrupt disabled
 1: Interrupt enabled(中断使能)
发送处理代码
U0DBUF:USART receive and transmit data. When writing this register, the data written is written to the
 internal transmit-data register. When reading this register, the data from the internal read-data
 register is read.(USART接收和传输数据。当写入这个寄存器时,写入的数据被写入内部传输数据寄存器。在读取这个寄存器时,从内部读取数据
 读取寄存器。)
/****************************************************************
串口发送字符串函数
****************************************************************/
void UartTX_Send_String(char *Data,int len) //buff数组通过指针访问
{int j;for(j=0;j<len;j++){U0DBUF = *Data++;  //USART接收和传输数据while(UTX0IF == 0);UTX0IF = 0;}
}
 
串口接收中断处理函数
模板:
 #pragma vector = T3_VECTOR //格式:#pragma vector = 中断向量,紧接着是中断处理程序
 __interrupt void T3_ISR(void)
 {
}
 实际:
 #pragma vector = URX0_VECTOR //格式:#pragma vector = 中断向量,紧接着是中断处理程序
 __interrupt void UART0_ISR(void)
 {
 URX0IF = 0; //UART0 TX中断标志初始置位0
 RxBuf = U0DBUF;
 }
main.c
/****************************************************************
主函数
****************************************************************/
void main(void)
{uchar i;P1DIR = 0x03; 				//P1控制LEDRLED = 1;GLED = 0;				//关LEDinitUARTSEND();UartTX_Send_String(Txdata,25);	        //JIAJIE TECH.INC.for(i=0;i<30;i++)Txdata[i]=' ';strcpy(Txdata,"HELLO! JIAJIE Technology\n");       //将UART0 TX test赋给Txdata;while(1){UartTX_Send_String(Txdata,sizeof("HELLO! JIAJIE Technology\n")); //串口发送数据Delay(50000);                                                       //延时GLED=!GLED;                                           //标志发送状态Delay(50000);Delay(50000);}
}
 
教程中的处理函数:
 
 

 增加打开,关闭灯光的信息代码:
 
 


 作业:增加流水灯模式
 
