/*
* Copyright (c) 2021, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ti_msp_dl_config.h"
#include "mid_button.h"
#include "oled.h"
volatile unsigned int delay_time=0;
volatile unsigned char uart_data = 0;
volatile unsigned int i=0;
void uart0_send_char(char ch);
void uart0_send_string(char* str);
void delay_msfor(unsigned int ms)
{
unsigned int i, j;
// 下面的嵌套循环的次数是根据主控频率和编译器生成的指令周期大致计算出来的,
// 需要通过实际测试调整来达到所需的延时。
for (i = 0; i < ms; i++)
{
for (j = 0; j < 8000; j++)
{
// 仅执行一个足够简单以致于可以预测其执行时间的操作
__asm__("nop"); // "nop" 代表“无操作”,在大多数架构中,这会消耗一个或几个时钟周期
}
}
}
//搭配滴答定时器实现的精确ms延时
void delay_ms(unsigned int ms){
delay_time=ms;
while(delay_time != 0);
}
int main(void)
{
SYSCFG_DL_init();
NVIC_EnableIRQ(KEY_INT_IRQN);//开启按键引脚的GPIOB端口中断
//DL_GPIO_setPins(LED1_PORT,LED1_PIN_22_PIN);
//清除串口中断标志
NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);
//使能串口中断
NVIC_EnableIRQ(UART_0_INST_INT_IRQN);
//清除定时器中断标志
NVIC_ClearPendingIRQ(TIMER_0_INST_INT_IRQN);
//使能定时器中断
NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);
//按键初始化
//user_button_init();
//OLED
while (1) {
__asm__("nop");
/*
DL_GPIO_clearPins(LED1_PORT,LED1_PIN_22_PIN);//输出低电平
delay_ms(1000);//延时大概1S
DL_GPIO_setPins(LED1_PORT,LED1_PIN_22_PIN); //输出高电平
delay_ms(1000);//延时大概1S
*/
/*
if( DL_GPIO_readPins(KEY_PORT, KEY_PIN_21_PIN) == 0){
DL_GPIO_setPins(LED1_PORT, LED1_PIN_22_PIN);//高电平
}else{
DL_GPIO_clearPins(LED1_PORT, LED1_PIN_22_PIN);//低电平
}
*/
// 呼吸灯渐亮过程
for (i = 0; i <= 998; i++)
{
// 设置 LED 亮度
DL_TimerG_setCaptureCompareValue(PWM_0_INST,i,GPIO_PWM_0_C1_IDX);
delay_msfor(1); // 延迟以控制亮度变化速度
}
// 呼吸灯渐暗过程
for (i = 998; i > 0; i--)
{
// 设置 LED 亮度
DL_TimerG_setCaptureCompareValue(PWM_0_INST,i,GPIO_PWM_0_C1_IDX);
delay_msfor(1); // 延迟以控制亮度变化速度
}
//按键扫描
//flex_button_scan();
//delay_ms(20);
}
}
//定时器的中断服务函数 已配置为1秒的周期
void TIMER_0_INST_IRQHandler(void)
{
//如果产生了定时器中断
switch( DL_TimerG_getPendingInterrupt(TIMER_0_INST) )
{
case DL_TIMER_IIDX_ZERO://如果是0溢出中断
//将LED灯的状态翻转
//DL_GPIO_togglePins(LED1_PORT, LED1_PIN_22_PIN);
break;
default://其他的定时器中断
break;
}
}
//滴答定时器中断服务函数
void SysTick_Handler(void)
{
if( delay_time != 0 )
{
delay_time--;
}
}
void GROUP1_IRQHandler(void)//Group1的中断服务函数
{
/*
//读取Group1的中断寄存器并清除中断标志位
switch( DL_Interrupt_getPendingGroup(DL_INTERRUPT_GROUP_1) )
{
//检查是否是KEY的GPIOB端口中断,注意是INT_IIDX,不是PIN_22_IIDX
case KEY_INT_IIDX:
//如果按键按下变为高电平
if( DL_GPIO_readPins(KEY_PORT, KEY_PIN_21_PIN) > 0 )
{
//设置LED引脚状态翻转
//DL_GPIO_togglePins(LED1_PORT, LED1_PIN_22_PIN);
}
break;
}
*/
}
//串口发送单个字符
void uart0_send_char(char ch)
{
//当串口0忙的时候等待,不忙的时候再发送传进来的字符
while( DL_UART_isBusy(UART_0_INST) == true );
//发送单个字符
DL_UART_Main_transmitData(UART_0_INST, ch);
}
//串口发送字符串
void uart0_send_string(char* str)
{
//当前字符串地址不在结尾 并且 字符串首地址不为空
while(*str!=0&&str!=0)
{
//发送字符串首地址中的字符,并且在发送完成之后首地址自增
uart0_send_char(*str++);
}
}
//串口的中断服务函数
void UART_0_INST_IRQHandler(void)
{
//如果产生了串口中断
switch( DL_UART_getPendingInterrupt(UART_0_INST) )
{
case DL_UART_IIDX_RX://如果是接收中断
//接发送过来的数据保存在变量中
uart_data = DL_UART_Main_receiveData(UART_0_INST);
//将保存的数据再发送出去
uart0_send_char(uart_data);
break;
default://其他的串口中断
break;
}
}
【TI MSPM0G3507】 定时器 串口 中断 PWM示例
最新推荐文章于 2025-07-05 10:12:34 发布