嵌入式第六章作业

1、编写UART_2串口发送程序时,初始化需要设置哪些参数?

波特率(Baud Rate确定数据传输速率。
数据位(Data Bits)指定每个字符的数据位数。
停止位(Stop Bits)指定每个字符之后停止位的位数。
校验位(Parity Bits)用于校验数据传输的正确性。
控制流(Flow Control)包括硬件流控和软件流控,用于控制数据的流动。
时钟使能(Clock Enable)确保UART模块有时钟信号运行。
引脚配置(Pin Configuration)配置UART的发送和接收引脚连接。
中断使能(Interrupt Enable)允许触发接收、发送和错误中断,以实现异步数据收发和错误处理。

2、假设速度为115200,系统时钟为72MHz,波特率寄存器BRR中的值应该是多少?

 当过采样模式为0时,BRR=72000000/115200=625

当过采样模式为1时,BRR=(2*72000000)/115200=1250

3、中断向量表在哪个文件中?表中有多少项?给出部分截图。

4、以下是中断源使能函数,假设中断源为TIM6,将函数实例化(写出各项具体数值)。

查找结果

5、假设将UART_2和TIM6交换其在中断向量表中的位置和IRQ号, UART_2可以正常中断吗?

在中断向量表中交换 UART_2 和 TIM6 的位置及 IRQ 号后,UART_2 的中断号将变为 54(即 TIM6 的原始中断号),而 TIM6 的中断号则变为 UART_2 的原始中断号。这种交换不会影响中断处理函数的逻辑,因为中断处理函数是根据中断号而非位置来确定的。因此,UART_2 可以正常触发中断,但需要在代码中使用新的中断号来引用 UART_2 的中断。

第二部分

实现UART_2串口的接收程序,当收到字符时: ①在电脑的输出窗口显示下一个字符,如收到A显示B; ②亮灯:收到字符G,亮绿灯;收到字符R,亮红灯;收到字符B,亮蓝灯;收到其他字符,不亮灯。 实现方式: 1、用构件调用方式实现; 2、UART部分用直接地址方式实现(即不调用uart.c中的函数,其他部分如GPIO、中断设置可调用函数)。

//用构件调用方式实现

#include "includes.h"
void UART_User_Handler(void)
{
	uint8_t ch;
	uint8_t flag;
	DISABLE_INTERRUPTS; 
	for(;;)
	{
	   ch=uart_re1(UART_User,&flag);  
        if(flag)
       {
            switch (ch) {
            	case 'R':
		           gpio_set(LIGHT_GREEN, 1); 
		           gpio_set(LIGHT_RED, 0);    
		           gpio_set(LIGHT_BLUE, 1);   
		           uart_send1(UART_User, ch + 1);          
		           break;
		       case 'G':
		           gpio_set(LIGHT_GREEN, 0);  
		           gpio_set(LIGHT_RED, 1);    
		           gpio_set(LIGHT_BLUE, 1);   
		           uart_send1(UART_User, ch + 1);
		           break;
		       case 'B':
		           gpio_set(LIGHT_GREEN, 1);  
		           gpio_set(LIGHT_RED, 1);    
		           gpio_set(LIGHT_BLUE, 0);   
		           uart_send1(UART_User, ch + 1);
		           break;
		       default:
		           gpio_set(LIGHT_GREEN, 1);  
		           gpio_set(LIGHT_RED, 1);    
		           gpio_set(LIGHT_BLUE, 1);  
		           uart_send1(UART_User, ch + 1);
		           break;
              }
       }
	}
	ENABLE_INTERRUPTS;   
 }
 

实验报告的图

//Uart部分用直接地址方式实现

#define GLOBLE_VAR

#include "includes.h"      //Include the master header file

int main(void)
{   
    //uart register related addresses
    volatile uint32_t* RCC_AHB2;     //Address of GPIO port A clock enable register
    volatile uint32_t* RCC_APB1;     //Address of UART 2 clock enable register
    volatile uint32_t* gpio_ptr;     //Base address of GPIO port A
    volatile uint32_t* uart_ptr;     //Base address of UART 2 port
    volatile uint32_t* gpio_mode;    //Pin mode register address = port base address
    volatile uint32_t* gpio_afrl;    //GPIO alternate function low register
    volatile uint32_t* uart_brr;     //UART baud rate register address
    volatile uint32_t* uart_isr;     //UART interrupt and status register base address
    volatile uint32_t* uart_cr1;     //UART control register 1 base address
    volatile uint32_t* uart_cr2;     //UART control register 2 base address
    volatile uint32_t* uart_cr3;     //UART control register 3 base address
    volatile uint32_t* uart_tdr;     //UART transmit data register
    uint16_t usartdiv;   //Value to be assigned to BRR register

    //Variable assignment
    RCC_APB1=0x40021058UL;   //UART clock enable register address
    RCC_AHB2=0x4002104CUL;   //Address of GPIO port A clock enable register
    gpio_ptr=0x48000000UL;   //Base address of GPIOA port
    uart_ptr=0x40004400UL;  //Base address of UART2 port
    gpio_mode=0x48000000UL;              //Pin mode register address = port base address
    gpio_afrl=0x48000020UL;           // GPIO alternate function low register
    uart_cr1=0x40004400UL;              //UART control register 1 base address
    uart_brr=0x4000440CUL;          // UART baud rate register address
    uart_isr=0x4000441CUL;         // UART interrupt and status register base address
    uart_tdr=0x40004428UL;         //UART transmit data register
    uart_cr2=0x40004404UL;      // UART control register 2 base address
    uart_cr3=0x40004408UL;      //UART control register 3 base address

    //Disable all interrupts
    DISABLE_INTERRUPTS;

    //User peripheral module initialization
    gpio_init(LIGHT_RED,GPIO_OUTPUT,LIGHT_OFF); //Initialize red LED
    gpio_init(LIGHT_GREEN,GPIO_OUTPUT,LIGHT_OFF); //Initialize green LED
    gpio_init(LIGHT_BLUE,GPIO_OUTPUT,LIGHT_OFF); //Initialize blue LED

    //uart_init(UART_User,115200); (lines 58-102)

    //Enable GPIOA and UART2 clocks
    *RCC_APB1|=(0x1UL<<17U);       //UART2 clock enable
    *RCC_AHB2 |=(0x1UL<<0U);       //GPIOA clock enable

    //Set GPIO port to alternate function
    //First, clear D7, D6, D5, D4
    *gpio_mode &= ~((0x3UL<<4U)|(0x3UL<<6U));
    //Then set D7, D6, D5, D4 to 1010, setting PTA2, PTA3 to alternate function serial function.
    *gpio_mode |=((0x2UL<<4U)|(0x2UL<<6U));

    //Select pin port alternate function
    //First, clear D15~D8
    *gpio_afrl &= ~((0xFUL<<8U)|(0xFUL<<12U));
    //Then set D15~D8 to 01110111, set PTA3, PTA2 pins to USART2_RX, USART2_TX respectively
    *gpio_afrl=(((0x1UL<<8U)|(0x2UL<<8U)|(0x4UL<<8U))|((0x1UL<<12U)|(0x2UL<<12U)|(0x4UL<<12U)));         

    //Temporarily disable UART functionality, the UE-USART enable bit is at position 0 in control register 1.
    //After clearing this bit, the USART prescaler and output will immediately stop and discard all current operations.
    *uart_cr1 &= ~(0x1UL);

    //Temporarily disable serial transmission and reception, control register 1's transmitter enable bit (D3), receiver enable bit (D2)
    *uart_cr1 &= ~((0x1UL<<3U)|(0x1UL<<2U));

    //Configure baud rate
    if(*uart_cr1&(0x1UL<<15) == (0x1UL<<15))             
        usartdiv = (uint16_t)((SystemCoreClock/115200)*2);
    else
        usartdiv = (uint16_t)((SystemCoreClock/115200));
    *uart_brr = usartdiv;

    //Initialize control register and interrupt status register, clear flags
    //Disable interrupts
    *uart_isr = 0x0UL;    
    //Clear the two enable bits in control register 2. D14-LIN mode enable bit, D11-clock enable bit
    *uart_cr2 &= ~((0x1UL<<14U)|(0x1UL<<11U));
    //Clear the three enable bits in control register 3. D5 (SCEN) -smartcard mode enable bit, 
    //D3 (HDSEL) -half-duplex select bit, D1 (IREN) -IrDA mode enable bit
    *uart_cr3 &= ~((0x1UL<<5U) | (0x1UL<<3U) |(0x1UL<<1U));

    //Start serial transmission and reception
    *uart_cr1 |= ((0x1UL<<3U)|(0x1UL<<2U));

    //Enable UART functionality
    *uart_cr1 |= (0x1UL<<0U);

    //Enable module interrupts
    //uart_enable_re_int(UART_User);  Enable UART_User module receive interrupt function
    //uart_enable_re_int() function implementation: ①Enable UART receive interrupt; ②Enable interrupt controller IRQ interrupt
    *uart_cr1 |= (0x1UL<<5); //Set bit RXNEIE at position 5 in USART_CR1 register: generate USART interrupt when ORE=1 or RXNE=1 in USART_ISR register
    NVIC_EnableIRQ(USART2_IRQn); //Enable interrupt controller USART2 interrupt
    //Enable all interrupts
    ENABLE_INTERRUPTS;

    //Output a prompt to open the serial port tool on the serial update page
    printf("Uart部分用直接地址方式实现\r\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值