MSP432学习笔记(不讲原理,只讲解使用方式)

GPIO电平

MAP_GPIO_setAsOutputPin(GPIO_PORT_P1,GPIO_PIN0);                     //配置P1.0为输出模式
MAP_GPIO_setAsInputPin(GPIO_PORT_P1,GPIO_PIN0);                      //配置P1.0为输入模式
MAP_GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);                  //翻转电平
MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1,GPIO_PIN0);                 //配置P1.0输出高电平
MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1,GPIO_PIN0);                  //配置P1.0输出低电平
MAP_GPIO_setAsInputPinWithPullDownResistor(GPIO_PORT_P1,GPIO_PIN0);  //配置P1.0下拉输入
MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1,GPIO_PIN0);    //配置P1.0上拉输入
MAP_GPIO_getInputPinValue(GPIO_PORT_P1,GPIO_PIN0);                   //获取P1.0输入电平(返回uint8)

GPIO中断 

MAP_GPIO_enableInterrupt(GPIO_PORT_P1,GPIO_PIN0);                   //使能P1.0的端口中断
MAP_GPIO_disableInterrupt(GPIO_PORT_P1,GPIO_PIN0);                  //禁用P1.0的端口中断
MAP_GPIO_interruptEdgeSelect(GPIO_PORT_P1,GPIO_PIN1,uint_fast8_t edgeSelect);//设置中断触发方式(GPIO_LOW_TO_HIGH_TRANSITION或GPIO_HIGH_TO_LOW_TRANSITION)
MAP_GPIO_getEnabledInterruptStatus(GPIO_PORT_P1)                     //获取中断端口号(多个返回逻辑或)
MAP_GPIO_getInterruptStatus(GPIO_PORT_P1,GPIO_PIN0);                //获取P1.0中断状态(返回uint16)
MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1,GPIO_PIN0);                //清除P1.0中断标志

配置步骤一般为:
1.配置GPIO输入
2.清除中断标志位
3.配置触发方式
4.开启外部中断
5.开启端口中断
6.开启总中断
7.编写中断服务函数

void interrupt_inint(void)
{
    MAP_GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN1);//1.配置GPIO输入 

    MAP_GPIO_clearInterruptFlag(GPIO_PORT_P1, GPIO_PIN1);//2.清除中断标志位
	
    GPIO_interruptEdgeSelect(GPIO_PORT_P1,GPIO_PIN1,GPIO_HIGH_TO_LOW_TRANSITION);//3.配置触发方式

    GPIO_enableInterrupt(GPIO_PORT_P1,GPIO_PIN1);//4.开启外部中断

    Interrupt_enableInterrupt(INT_PORT1);//5.开启端口中断

    Interrupt_enableMaster();//6.开启总中断
}//中断初始化函数

void  PORT1_IRQHandler(void)
{
	uint16_t status;
	status=GPIO_getEnabledInterruptStatus(GPIO_PORT_P1);//获取中断端口号
	GPIO_clearInterruptFlag(GPIO_PORT_P1,status);//清除中断标志
	
	if(status & GPIO_PIN1)//对应KEY1中断
	{
		delay_ms(15);    //消抖
	    if(GPIO_getInputPinValue(GPIO_PORT_P1,GPIO_PIN1) ==0)	
		{
			MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1,GPIO_PIN0);//LED1亮
		}
	}			
}//中断服务函数

PWM输出

Timer_A_PWMConfig pwmConfig =
{
        TIMER_A_CLOCKSOURCE_SMCLK,
        TIMER_A_CLOCKSOURCE_DIVIDER_20,
        48000,                        //分频系数20,计数周期48000(周期为48000000/48000/20=50HZ)
        TIMER_A_CAPTURECOMPARE_REGISTER_1,//定时器通道设置(通道一)
        TIMER_A_OUTPUTMODE_RESET_SET,
        0                             //占空比
};//PWMAPI初始化


void PWM_Init(void)
{
	MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN4,
            GPIO_PRIMARY_MODULE_FUNCTION);                    //设置输出IO口
	MAP_Timer_A_generatePWM(TIMER_A0_BASE, &pwmConfig);       //开启PWM
    MAP_Timer_A_setCompareValue(TIMER_A1_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_1,24000);//设置A1通道一占空比为50%
}

定时器A中断 

const Timer_A_UpModeConfig upConfig =
{
        TIMER_A_CLOCKSOURCE_SMCLK,              // 选择主时钟频率48MHZ
        TIMER_A_CLOCKSOURCE_DIVIDER_48,         // 分频系数
        25000,                                  // 计数周期25000
        TIMER_A_TAIE_INTERRUPT_DISABLE,         // 禁用中断
        TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE ,    // 使能通道0中断
        TIMER_A_DO_CLEAR                        // 清除计数值
};

MAP_Timer_A_configureUpMode(TIMER_A1_BASE, &upConfig);    //配置定时器A1向上计数模式
MAP_Interrupt_enableInterrupt(INT_TA1_0);                 //使能中断
MAP_Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE); //开始计数(向上计数)

MAP_Interrupt_enableMaster();//使能中断控制器



void TA1_0_IRQHandler(void)
{                   
    MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE,
        TIMER_A_CAPTURECOMPARE_REGISTER_0);//清除中断状态
    MAP_Timer_A_clearTimer(TIMER_A1_BASE);//清除定时器计数
}//重写中断回调函数

定时器32中断 

 

void Interrupt_32_Init(void)
{
        //配置timer32
	MAP_Timer32_initModule(TIMER32_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT,
		TIMER32_PERIODIC_MODE);
	//开启中断
	MAP_Interrupt_enableInterrupt(INT_T32_INT1);
	//开启总中断
	MAP_Interrupt_enableMaster();
	//开启定时器
	MAP_Timer32_setCount(TIMER32_BASE,12000);//1ms中断
        MAP_Timer32_enableInterrupt(TIMER32_BASE);
        MAP_Timer32_startTimer(TIMER32_BASE, true);//开启计数
}//定时器32初始化

void T32_INT1_IRQHandler(void)
{
	MAP_Timer32_clearInterruptFlag(TIMER32_BASE);//清除中断标志
	MAP_Timer32_startTimer(TIMER32_BASE, true);//重新开启计数
}//重写中断回调函数

串口通信 

const eUSCI_UART_ConfigV1 uartConfig =
{
        EUSCI_A_UART_CLOCKSOURCE_SMCLK,           // 选择时钟频率SMCLK=12MHZ
        78,                                      // BRDIV = 78
        2,                                       // UCxBRF = 2
        0,                                       // UCxBRS = 0
        EUSCI_A_UART_NO_PARITY,                  // No Parity
        EUSCI_A_UART_LSB_FIRST,                  // LSB First
        EUSCI_A_UART_ONE_STOP_BIT,               // 一位停止位
        EUSCI_A_UART_MODE,                       // UART mode
        EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION,  // Oversampling
        EUSCI_A_UART_8_BIT_LEN                   // 八位数据位
};//串口结构体参数设置


void UART_Init(void)
{
    GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
             GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);//设置P1.2,P1.3为串口引脚(直连PC)

    UART_initModule(EUSCI_A0_BASE, &uartConfig);									//配置串口模式
	
    UART_enableModule(EUSCI_A0_BASE);															//使能串口
	
	UART_enableInterrupt(EUSCI_A0_BASE,EUSCI_A_UART_RECEIVE_INTERRUPT);
    Interrupt_enableInterrupt(INT_EUSCIA0);												//使能中断
	MAP_Interrupt_enableMaster(); 																//使能cpu中断
}//串口初始化

void EUSCIA0_IRQHandler(void)
{
	uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);

  if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)
  {
    MAP_UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE));
  }
}//重写中断回调函数(收啥发啥)

MSP432 低功耗高性能并存10.1 Digital I/O Introduction The digital I/O features include: • Independently programmable individual I/Os • Any combination of input or output • Individually configurable interrupts for ports (available for certain ports only) • Independent input and output data registers • Individually configurable pullup or pulldown resistors • Wake-up capability from ultra-low power modes (available for certain ports only) • Individually configurable high drive I/Os (available for certain I/Os only) Devices within the family may have up to eleven digital I/O ports implemented (P1 to P10 and PJ). Most ports contain eight I/O lines; however, some ports may contain less (see the device-specific data sheet for ports available). Each I/O line is individually configurable for input or output direction, and each can be individually read or written. Each I/O line is individually configurable for pullup or pulldown resistors. Certain ports have interrupt and wake-up capability from ultra-low power modes (see device specific data sheet for ports with interrupt and wake-up capability). Each interrupt can be individually enabled and configured to provide an interrupt on a rising or falling edge of an input signal. All interrupts are fed into an encoded Interrupt Vector register, allowing the application to determine which sub-pin of a port has generated the event. Individual ports can be accessed as byte-wide ports or can be combined into half-word-wide ports. Port pairs P1 and P2, P3 and P4, P5 and P6, P7 and P8, and so on, are associated with the names PA, PB, PC, PD, and so on, respectively. All port registers are handled in this manner with this naming convention. The main exception are the interrupt vector registers, for example, interrupts for ports P1 and P2 must be handled through P1IV and P2IV, PAIV does not exist. When writing to port PA with half-word operations, all 16 bits are written to the port. When writing to the lower byte of port PA using byte operations, the upper byte remains unchanged. Similarly, writing to the upper byte of port PA using byte instructions leaves the lower byte unchanged. When writing to a port that contains less than the maximum number of bits possible, the unused bits are don't care. Ports PB, PC, PD, PE, and PF behave similarly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值