标题:
晟矽微MC30P6270C单片机LED的呼吸与渐变程序
[打印本页]
作者:
十三丶dream
时间:
2020-12-3 18:42
标题:
晟矽微MC30P6270C单片机LED的呼吸与渐变程序
/******************************************************************************
; * @Create Date : 2020.12.03
; * @Author/Corporation : 有趣的金鱼
; *----------------------Abstract Description---------------------------------
本程序采用晟矽微6270单片机底层,适用于晟矽微6270系列单片机
******************************************************************************/
#include "user.h"
/************************************************
; * @Function Name : CLR_RAM
; * @Description : 初始化RAM
; * @IN_Parameter :
; * @Return parameter :
; ***********************************************/
void CLR_RAM(void)
{
__asm
movai 0x40
movra FSR
movai 48
movra 0x07
decr FSR
clrr INDF
djzr 0x07
goto $-3
clrr 0x07
clrr 0x05
__endasm;
}
/************************************************
; * @Function Name : ConfigSet
; * @Description : 初设置io pwm 定时器等
; * @IN_Parameter :
; * @Return parameter :
; ***********************************************/
void ConfigSet(void)
{
P1 = 0x00;
DDR1 = 0x18; //1:input 0:output
PUCON = 0xf7; //0:Effective 1:invalid
PDCON = 0xff; //0:Effective 1:invalid
ODCON = 0x00; //0:推挽输出 1:开漏输出
/*****************定时器0**************************************/
//T0CR = 0x84; //时钟为CPU时钟 定时器64分频 1ms
//T0CNT = 256 - 125;
//T0IE = 1;
T0CR=0x00; //时钟为CPU时钟 定时器2分频
T0CNT=256-96; //50us 进一次中断
T0IE=1;
/*****************定时器1**************************************/
//T1CR=0x80; //时钟为CPU时钟 定时器2分频
//T1CNT=36; //125=31KHz
//T1LOAD=36; //用于设置 T1 的计数周期
//T1DATA=18; //用于设置 PWM1 的占空比
//PWM1EC=0;
/****************键盘中断**************************************/
P1KBCR=0x08; //使能键盘中断IO口
/****************键盘中断**************************************/
LVDCR1=0x23; //LVD电压检测量值选择
LVDEN=1; //低电压检测 LVD使能位 0:关闭LVD; 1:开启LVD
LVDIE=0; //LVD 中断使能位 0:屏蔽LVD中断; 1:使能LVD中断;
LVDIF=0; //LVD 中断标志位 0:未触发LVD中断; 1:已触发LVD中断,需软件清 0;
/****************变量初始化************************************/
sys_power_flag = 0;
sys_sleep_flag = 0;
sys_work_flag = 0;
sys_led_breathe_direction = 0;
time1s_flag = 0;
time10ms_flag = 0;
timer_count0 = 0;
timer_count1 = 0;
timer_count2 = 0;
function_mode = 0;
led_choice = 0;
led_pwm_red_value = 0;
led_pwm_green_value = 0;
led_pwm_blue_value = 0;
led_breathe_ratio = 0;
led_breathe_period = 0;
led_breathe_frequency = 0;
}
/************************************************
; * @Function Name : Key_Scan();
; * @Description : 按键扫描检测
; * @IN_Parameter : void
; * @Return parameter : 确认的按键信息
; ***********************************************/
char Key_Scan(void)
{
char signal; //用于得到按键信号的值
char KeyValue; //用于返回按键稳定的值
char i;
KeyValue = NO_KEY_MSG;
signal = 0;
if(!PIN_KEY_ON_OFF) //低有效
{
signal |= 0x01;
}
for(i=0;i<KEY_NUMBER;i++) //确认按键的状态
{
if((signal & 0x01) == 1) //有按键按下
{
if(KeySignalCount[i] <= KEY_LONG_TIMES)
KeySignalCount[i]++;
if(KeySignalCount[i] == KEY_LONG_TIMES)
{
KeyValue = (0x01 | KEY_LONG);//长按按下
}
}
else
{
if((KeySignalCount[i] > KEY_SHORT_TIMES)
&&(KeySignalCount[i] < KEY_LONG_TIMES))
{
KeyValue = 0x01;//短按抬起
}
KeySignalCount[i] = 0;
}
signal>>=1;
}
if(KeyValue != NO_KEY_MSG) //有按键
{
return KeyValue;
}
return NO_KEY_MSG;
}
/************************************************
; * @Function Name : Key_Message_Dispose();
; * @Description : 按键消息处理
; * @IN_Parameter : void
; * @Return parameter : void
; ***********************************************/
void Key_Message_Dispose(void)
{
if(KEY_LAMP == key_msg)
{
switch(function_mode)
{
case 0:led_white;sys_work_flag=1;function_mode++;break;
case 1:led_red;function_mode++;break;
case 2:led_green;function_mode++;break;
case 3:led_blue;function_mode++;break;
case 4:led_green;function_mode++;break;
case 5:led_black;sys_work_flag=0;function_mode=0;break;
}
}
}
/************************************************
; * @Function Name : LED_Detection();
; * @Description :
; * @IN_Parameter : void
; * @Return parameter : void
; ***********************************************/
void LED_Detection(void)
{
//第五个模式
if(5 == function_mode)
{
//呼吸灯
led_breathe_period++;
if(LAMP_PERIOD_VALUE <= led_breathe_period)
{
led_breathe_period=0;
led_breathe_frequency++;
if(LAMP_PERIOD_VALUE <= led_breathe_frequency)//周期时间*频率时间*2=总变化时间
{
led_breathe_frequency=0;
if(sys_led_breathe_direction)
{
//亮
if(LAMP_PERIOD_VALUE >= led_breathe_ratio)
led_breathe_ratio++;
else
sys_led_breathe_direction=0;
}
else
{
//灭
if(1 <= led_breathe_ratio)
led_breathe_ratio--;
else
sys_led_breathe_direction=1;
}
}
}
if(led_breathe_ratio <= led_breathe_period)//亮灭
{
PIN_LED_RED=0;
PIN_LED_GREEN=0;
PIN_LED_BLUE=0;
}
else
{
PIN_LED_RED=1;
PIN_LED_GREEN=1;
PIN_LED_BLUE=1;
}
/*
//渐变灯
led_breathe_period++;
if(LAMP_PERIOD_VALUE <= led_breathe_period)
{
led_breathe_period=0;
led_breathe_frequency++;
if(LAMP_PERIOD_VALUE <= led_breathe_frequency)
{
led_breathe_frequency=0;
switch(led_choice)
{
case 0:led_pwm_red_value++;if(LAMP_PERIOD_VALUE <= led_pwm_red_value){led_choice++;}break;
case 1:led_pwm_blue_value--;if(0 >= led_pwm_blue_value){led_choice++;}break;
case 2:led_pwm_green_value++;if(LAMP_PERIOD_VALUE <= led_pwm_green_value){led_choice++;}break;
case 3:led_pwm_red_value--;if(0 >= led_pwm_red_value){led_choice++;}break;
case 4:led_pwm_blue_value++;if(LAMP_PERIOD_VALUE <= led_pwm_blue_value){led_choice++;}break;
case 5:led_pwm_green_value--;if(0 >= led_pwm_green_value){led_choice=0;}break;
default:led_choice=0;break;
}
}
}
if(led_pwm_red_value <= led_breathe_period)
PIN_LED_RED=0;
else
PIN_LED_RED=1;
if(led_pwm_green_value <= led_breathe_period)
PIN_LED_GREEN=0;
else
PIN_LED_GREEN=1;
if(led_pwm_blue_value <= led_breathe_period)
PIN_LED_BLUE=0;
else
PIN_LED_BLUE=1;
*/
}
}
/************************************************
; * @Function Name : Sleep_Mode();
; * @Description : 低功耗模式检测
; * @IN_Parameter : void
; * @Return parameter : void
; ***********************************************/
void Sleep_Mode(void)
{
if(0 == sys_work_flag)//没有在工作
{
sys_sleep_flag=1;
PIN_LED_RED=0;
PIN_LED_GREEN=0;
PIN_LED_BLUE=0;
PIN_LED_WHITE=0;
}
if(1 == sys_sleep_flag)
{
sys_sleep_flag=0;
GIE=0; //总中断使能位
KBIF=0; //键盘中断位清零
KBIE=1; //键盘中断使能位
T0IE=0; //定时器 T0 中断使能位
T1EN=0; //定时器 T1 中断使能位
WDTEN=0; //看门狗使能位
ClrWdt();
Nop();
Nop();
Stop(); //程序运行到低功耗模式,只有发生中断请求或者复位才能退出该模式
Nop();
Nop();
Nop();
ClrWdt();
KBIE=0;
T0IE=1;
T1EN=1;
WDTEN=1;
GIE=1;
}
}
/************************************************
; * @Function Name : Sys_Init
; * @Description : 系统初始化
; * @IN_Parameter :
; * @Return parameter :
; ***********************************************/
void Sys_Init(void)
{
GIE = 0;
CLR_RAM();
ConfigSet();
GIE = 1;
}
/************************************************
; * @Function Name : main
; * @Description :
; * @IN_Parameter :
; * @Return parameter :
; ***********************************************/
void main (void)
{
Sys_Init();
while(1)
{
ClrWdt();
if(time10ms_flag)
{
time10ms_flag=0;
key_msg = Key_Scan();//按键扫描
Key_Message_Dispose();//按键处理
}
if(time1s_flag)
{
time1s_flag=0;
Sleep_Mode(); //低功耗模式
}
}
}
/************************************************
; * @Function Name : Interrupt
; * @Description : The interrupt function
; * @IN_Parameter :
; * @Return parameter :
; ***********************************************/
void int_isr(void)__interrupt
{
__asm
movra _abuf
swapar _STATUS
movra _statusbuf
__endasm;
if((T0IF)&&(T0IE))
{
T0CNT=256-96;//50us
T0IF = 0;
timer_count0++;
timer_count1++;
if(20000 <= timer_count0)//1s
{
timer_count0=0;
time1s_flag=1;
}
if(200 <= timer_count1)//10ms
{
timer_count1=0;
time10ms_flag=1;
}
LED_Detection();
}
__asm
swapar _statusbuf
movra _STATUS
swapr _abuf
swapar _abuf
__endasm;
}
/**************************** end of file *********************************************/
复制代码
全部资料51hei下载地址:
MC30P6270C.zip
(42.33 KB, 下载次数: 54)
2020-12-3 18:51 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
nahaishinima
时间:
2021-3-31 22:13
你好,请问这个芯片能写低电压检测的吗?在3.3V与3V实现灯闪烁,3 V关机,如果可以的话,能说一下大致思路吗?我理解的是只能实现一种电压检测,像这种的,我就不知道了
作者:
stonenth
时间:
2021-4-8 08:42
很不错,看看低功耗的设计
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1