基于通用单片机(久齐) 手机散热器,按摩仪器中冷热头的控制
项目涉及到了硬件以及软件部分,硬件部分涉及到了ME4056电源管理IC,以及NY8B062D IC的应用,USB应用,H桥驱动输出元素 TEC1-7103 半导体制冷设备。
TEC1-7103 半导体制冷
半导体制冷片的工作原理是基于帕尔帖原理,该效应是在1834年由J.A.C帕尔帖首先发现的,即利用当两种不同的导体A和B组成的电路且通有直流电时,在接头处除焦耳热以外还会释放出某种其它的热量,而另一个接头处则吸收热量,且帕尔帖效应所引起的这种现象是可逆的,改变电流方向时,放热和吸热的接头也随之改变,吸收和放出的热量与电流强度I[A]成正比,且与两种导体的性质及热端的温度有关
半导体制冷片工作原理 致冷器件是由半导体所组成的一种冷却装置,随着近代的半导体发展才有实际的应用,也就是致冷器的发明。其工作原理是由直流电源提供电子流所需的能量,通上电源后,电子负极(-)出发,首先经过P型半导体,于此吸热量,到了N型半导体,又将热量放出,每经过一个NP模块,就有热量由一边被送到令外一边造成温差而形成冷热端。冷热端分别由两片陶瓷片所构成,冷端要接热源,也就是欲冷却之。
内容涉及 :
- ANY8B062D IO控制
- ANY8B062D 输入控制
- ANY8B062D 定时器控制
- ANY8B062D 4路PWM器控制
- ANY8B062D AD采集控制
- ANY8B062D IO输入与输出复用
- ME4056电源管理
- PID NTC控制中的 P控制
- ANY8B062D 低功耗控制
一:编程要点
- 初始 单片机的RAM
- 初始化 IC的配置状态;
- 编写驱动层以及软件层;
- 驱动层与软件层分离运行,多线程运行;
SJ02冷热头功能说明
开关机及切换:
长按图案对应热熬开关键2秒,加热功能开启,此时温度约45℃左右,指示灯暗红色;短按一次,温度约50℃左右,指示灯变红色;依此循环,长按3秒热敷键关掉;
长按图案对应冷熬开关键2秒,冷熬功能开启,此时温度约19℃左右,指示灯暗蓝色;短按一次,温度约13℃左右,指示灯变蓝色;依此循环,长按3秒冷敷键关掉;
例如:当前热熬功能,短按冷熬开关键,冷熬功能开启,热熬功能关闭,当冷熬功能状态,短按热熬功能,热熬功能开启,冷熬功能关闭,依此短按开关键可切换功能
二.低电压保护:
当电量低于3.3V时强制关机,低电关机后,重启开机电压3.5V。
三.自动关机:
开机进入使用状态后不论有无功能操作10分钟自动关机。
四. 充电:
适配器5V2A,充电需有独立充电0.5C,充至4.15V变灯充满,4.2V强制停止充电,充电时间2H-2.5H,充电电流到电芯不能大于0.5A,充电不工作,按键无效,超过3小时关闭充电功能。
1).充电模式显示:
充电状态下,呼吸灯变化,通过呼吸灯颜色显示当前电量。
4.15V~以上 (红色灯长亮)
3.3V~4.15V (红色呼吸灯)
五.电池:
1.一节14500锂电池3.7V 800mAh,
2.最大放电电流0.8A,过流保护4A 10ms, 电池过放保护后充电可激活。
六.关机电流静态电流≤10uA
二:NY8B062D 久齐IC介绍
久齐IC 官网
NY8B062D是以EPROM作为存储器的 8 位单片机,专为家电或量测等等的I/O应用设计。采用CMOS制程并同时提供
客户低成本、高性能、及高性价比等显著优势。NY8B062D核心建立在RISC精简指令集架构可以很容易地做编程和控
制,共有 55 条指令。除了少数指令需要两个指令时钟,大多数指令都是一个指令时钟能完成,可以让用户轻松地以
过程控制完成不同的应用。因此非常适合各种中低记忆容量但又复杂的应用。NY8B062D内建高精度十一加一通道 12
位ADC模数转换器,与高精度电压比较器,足以应付各种模拟接口的侦测与量测。 在 I/O 的资源方面,NY8B062D 有 14 根弹性的双向 I/O 脚,每个 I/O 脚都有单独的寄存器控制为输入或输出脚。而且
每一个 I/O 脚位都能通过控制相关的寄存器达成如上拉或下拉电阻或开漏(Open-Drain)输出。此外针对红外线摇控
的产品方面,NY8B062D 内置了可选择频率的红外载波发射口。
NY8B062D 有四组定时器,可用系统时钟当作一般的计时应用或者从外部讯号触发来计数。另外 NY8B062D 提供 3 组 10 位的 PWM 输出,3 组蜂鸣器输出,可用来驱动马达、LED、或蜂鸣器等等。
NY8B062D 采用双时钟机制,高速振荡时钟或者低速振荡时钟都由内部 RC 振荡或外部晶振输入。在双时钟机制下,
NY8B062D 可选择多种工作模式如正常模式(Normal)、慢速模式(Slow mode)、待机模式(Standby mode)与睡
眠模式(Halt mode),可节省电力消耗,延长电池寿命。并且单片机在使用内部 RC 高速振荡时,低速振荡可以同时
使用外部精准的晶振计时。可以维持高速处理同时又能精准计算真实时间。
在省电的模式下,如待机模式(Standby mode)与睡眠模式(Halt mode)中,有多个中断源可以触发来唤醒 NY8B062D
进入正常操作模式(Normal mode)或慢速模式(Slow mode)来处理突发事件。
三:代码分析
软件层
1:main.c
代码如下:
#include <ny8.h>
#include "ny8_constant.h"
#define UPDATE_REG(x) __asm__("MOVR _" #x ",F")
#include "./Hardware/Hardware.h"
#include "./Software/Soft_State.h"
void Sys_sleep(void){
if(machine_state.Modle_state != Modle_IDLE ){
gcount_asleep = 0;
return;
}
if(!b_asleep100ms){ return;}
b_asleep100ms = 0;
if(gcount_asleep++<50){ return;}
gcount_asleep = 0;
b_asleep = 1;
PABIE = 1;
ENI();
SLEEP();
NOP();
NOP();
NOP();
b_asleep = 0;
gcount_asleep = 0;
}
void main(void)
{
clear_memory();
fn_sys_reset();
Hardware_init();
Software_State_init();
Hardware_Start();
while(1)
{
Sys_sleep();
Hardware_run();
Software_Input_Check();
Software_State_Check();
Software_Output_Check();
}
}
static unsigned char cunt1_T0 = 0;
static unsigned char cunt2_T0 = 0;
static unsigned char cunt3_T0 = 0;
void isr(void) __interrupt(0)
{
if(T0IF==1)
{
TMR0 = CST_reload_T0;
T0IF= 0;
if(cunt1_T0++ > 15){
bkey1_10ms = 1;
bkey2_10ms = 1;
cunt1_T0 = 0;
b_pid500ms = 1;
b_led_Robing10ms = 1;
}
if(cunt2_T0++ > 188){
batceck_10ms_flag =1;
badc_10ms_flag = 1;
cunt2_T0 = 0;
bHardware_Start_100ms = 1;
b_asleep100ms = 1;
if(cunt3_T0++ > 99){
cunt3_T0 = 0;
bworktime_100ms_flag = 1;
bchargtime_1s_flag = 1;
}
}
}
T1IF = 0;
T2IF= 0;
T3IF = 0;
if(PABIF == 1)
{
PABIF= 0;
gcount_asleep = 1;
}
if(ADIF == 1)
{
ADIF= 0;
if(ADMDbits.EOC == 1){
if(Count_Adc++>4){
Count_Adc =0;
ad_finish_flag = 1;
}else{
ADMDbits.START = 1;
}
}
ADIF= 0;
}
if(WDTIF==1)
{
WDTIF= 0;
}
else
{
;
}
}
2:BAT_State.h
代码如下:
#ifndef _BAT_State_H_
#define _BAT_State_H_
#include <ny8.h>
#include "ny8_constant.h"
#define _BAT_2V6 1775
#define _BAT_2V7 1843
#define _BAT_3V0 2048
#define _BAT_3V1 2090
#define _BAT_3V3 2250
#define _BAT_3V5 2369
#define _BAT_3V6 2457
#define _BAT_3V7 2525
#define _BAT_4V0 2730
#define _BAT_4V15 2846
#define _BAT_4V2 2867
#define _BAT_CHECKL _BAT_3V3
#define _BAT_CHECKL_OUT _BAT_3V1
#define _BAT_CHECKML _BAT_3V5
#define _BAT_CHECKMH _BAT_4V0
#define _BAT_CHECKH _BAT_4V15
#define _BAT_CHECKH_H _BAT_4V2
void BAT_State_Check(void);
void USB_State_Check(void);
void LED_Hot_Robing(void);
void Low_Flash(void);
#endif
3:BAT_State.c
代码如下:
#include "BAT_State.h"
#include "Soft_State.h"
static unsigned char count_bat_checK = 0;
static unsigned char count_bat_checK1 = 0;
static unsigned char Merry_BAT_state = BAT_IDLE;
static unsigned int Vla_BAT_CHECKL = _BAT_CHECKL;
void BAT_State_Check(void){
if(!batceck_10ms_flag){ return;}
batceck_10ms_flag = 0;
if(machine_state.BAT_state == BAT_IDLE){
if( (PWM1_HardWare_State == PWM_OFF) && (PWM2_HardWare_State == PWM_OFF) ){
Vla_BAT_CHECKL = _BAT_CHECKL;
}
else{
Vla_BAT_CHECKL = _BAT_CHECKL_OUT;
}
if(BAT_Value < Vla_BAT_CHECKL){
count_bat_checK1 = 0;
if(count_bat_checK++<15){return;}
count_bat_checK = 0;
machine_state.BAT_state = BAT_empty;
LowPowerFlash_En = 1;
machine_state.Modle_state = Modle_IDLE;
}
else if((BAT_Value > _BAT_CHECKH) || ((STDBY_Get_Out() == 0) && (GPIO_STDBY == 0))){
count_bat_checK = 0;
if(count_bat_checK1++<15){return;}
count_bat_checK = 0;
machine_state.BAT_state = BAT_fully;
}
else{
count_bat_checK = 0;
count_bat_checK1 = 0;
}
}
else if(machine_state.BAT_state == BAT_empty){
if(BAT_Value > _BAT_CHECKML){
if(count_bat_checK++<15){return;}
count_bat_checK = 0;
machine_state.BAT_state = BAT_IDLE;
count_charg_Time_Check = 0;
bchargoff_fla = 0;
}
else{
count_bat_checK = 0;
}
}
else if(machine_state.BAT_state == BAT_fully){
if(((STDBY_Get_Out() == 0) && (GPIO_STDBY == 1)) && (BAT_Value<_BAT_CHECKMH)){
count_bat_checK = 0;
if(count_bat_checK1++<15){return;}
count_bat_checK = 0;
machine_state.BAT_state = BAT_IDLE;
bchargoff_fla = 0;
count_charg_Time_Check = 0;
}
else{
count_bat_checK1 = 0;
}
}
else{
}
}
void USB_State_Check(void){
if(Hardware_USB_state == USB_IN ){
if(machine_state.USB_state == USB_OFF){
Machine_off();
}
machine_state.USB_state = USB_CH;
machine_state.Modle_state = Modle_ON_USBIN;
}
else{
if(machine_state.USB_state == USB_CH){
Machine_off();
machine_state.Modle_state = Modle_IDLE;
}
machine_state.USB_state = USB_OFF;
}
}
void LED_Hot_Robing(void){
if(machine_state.LED_state != Modle_LED_Bat_Charg ){
b_Exled_Robing = 0 ;
return;
}
if( !b_led_Robing10ms ){ return;}
b_led_Robing10ms = 0;
if( !b_Exled_Robing ){
if(PWM3_Data > 0xFD){
b_Exled_Robing = 1 ;
return;
}
PWM3_Data++;
}
else{
if(PWM3_Data == 0x00 ){
b_Exled_Robing = 0 ;
return;
}
PWM3_Data--;
}
PWM3_HardWare_State = PWM_ON;
fn_PWM3_Check(PWM3_Data);
}
void Low_Flash(void){
unsigned char TI = 0;
unsigned char count_Ti = 0;
if(!LowPowerFlash_En){ return ; }
LowPowerFlash_En = 0;
Machine_off();
for(TI = 0; TI<10 ; TI++){
GPIO_OUT1 = ~ GPIO_OUT1;
for(count_Ti = 0 ;count_Ti<250 ;count_Ti++ ){
fn_MOS_delay(240);
fn_MOS_delay(250);
fn_MOS_delay(240);
fn_MOS_delay(250);
}
}
gcount_asleep = 0;
LED_Hot_Close();
}
4:ColdHotOut_State.h
代码如下:
#ifndef _COLDHOTOUT_STATE_H_
#define _COLDHOTOUT_STATE_H_
#include <ny8.h>
#include "ny8_constant.h"
void LED_State_Check(void);
void LED_OUTCold(unsigned char Dout );
void LED_OUTHot(unsigned char Dout );
void LED_Close_Hot(void );
void LED_Close_Cold(void );
void RTC_State_Check(void);
void RTC_Cold_On(void);
void RTC_Hot_On( void);
void RTC_Hot_Close(void);
void RTC_Cold_Close(void);
void fn_MOS_delay(unsigned char count);
void Hot_PID(unsigned int TargetAD);
void Cold_PID(unsigned int TargetAD);
#endif
5:ColdHotOut_State.c
代码如下:
#include "ColdHotOut_State.h"
#include "Soft_State.h"
#include "PID.h"
void LED_State_Check(void){
switch (machine_state.LED_state){
case Modle_LED_IDE :{
LED_Close_Cold();
LED_Close_Hot();
break;
}
case Modle_LED_Cold_1:{
LED_Close_Hot();
LED_OUTCold(1);
break;
}
case Modle_LED_Cold_2:{
LED_Close_Hot();
LED_OUTCold(2);
break;
}
case Modle_LED_Hot_1:{
LED_Close_Cold();
LED_OUTHot(1);
break;
}
case Modle_LED_Hot_2:{
LED_Close_Cold();
LED_OUTHot(2);
break;
}
case Modle_LED_Bat_Charg:{
LED_Hot_Robing();
break;
}
case Modle_LED_Bat_ChargOff:{
LED_OUTHot(2);
break;
}
default:{
;
}
}
}
void LED_OUTHot(unsigned char Dout ){
switch(Dout){
case 1:{
PWM3_Data = 100;
break;
}
case 2:{
PWM3_Data = 0xFF;
break;
}
default:{
}
}
PWM3_HardWare_State = PWM_ON;
fn_PWM3_Check(PWM3_Data);
}
void LED_OUTCold(unsigned char Dout ){
switch(Dout){
case 1:{
PWM4_Data = 100;
break;
}
case 2:{
PWM4_Data = 0xFF;
break;
}
default:{
}
}
PWM4_HardWare_State = PWM_ON;
fn_PWM4_Check(PWM4_Data);
}
void LED_Close_Hot(void ){
if(PWM3_HardWare_State != PWM_OFF){
PWM3_HardWare_State = PWM_OFF;
PWM3_Data = 0;
fn_PWM3_Check(PWM3_Data);
fn_PWM3_OFF();
}
LED_Hot_Close();
}
void LED_Close_Cold(void ){
if(PWM4_HardWare_State != PWM_OFF){
PWM4_HardWare_State = PWM_OFF;
PWM4_Data = 0;
fn_PWM4_Check(PWM4_Data);
fn_PWM4_OFF();
}
LED_Cold_Close();
}
void RTC_State_Check(void){
if(machine_state.USB_state == USB_IN){ return;}
switch (machine_state.RTC_state){
case Modle_RTC_IDE:{
RTC_Cold_Close();
RTC_Hot_Close();
FA_Close();
break;
}
case Modle_RTC_Cold_1:{
ATarget_PWM_max = TARGET_PWM_MAX1;
RTC_Hot_Close();
Cold_PID(TARGET_PWMAD_Cold1);
RTC_Cold_On();
break;
}
case Modle_RTC_Cold_2:{
ATarget_PWM_max = TARGET_PWM_MAX2;
RTC_Hot_Close();
Cold_PID(TARGET_PWMAD_Cold2);
RTC_Cold_On();
break;
}
case Modle_RTC_Hot_1:{
ATarget_PWM_max = TARGET_PWM_MAX1;
RTC_Cold_Close();
Hot_PID(TARGET_PWMAD_Hot1);
RTC_Hot_On();
break;
}
case Modle_RTC_Hot_2:{
ATarget_PWM_max = TARGET_PWM_MAX2;
RTC_Cold_Close();
Hot_PID(TARGET_PWMAD_Hot2);
RTC_Hot_On();
break;
}
default:{
machine_state.RTC_state = Modle_RTC_IDE;
}
}
}
void RTC_Cold_On( void ){
Cold_EN();
PWM1_HardWare_State = PWM_ON;
fn_PWM1_Check(PWM1_Data);
}
void RTC_Cold_Close(void){
if(PWM1_HardWare_State != PWM_OFF){
PWM1_Data = 0;
fn_PWM1_Check(PWM1_Data);
fn_PWM1_OFF();
PWM1_HardWare_State = PWM_OFF;
Cold_Close();
PWM2_Data = 0;
fn_PWM2_Check(PWM2_Data);
fn_PWM2_OFF();
PWM2_HardWare_State = PWM_OFF;
Hot_Close();
fn_MOS_delay(80);
PWOUT_Cold_OUT();
fn_MOS_delay(80);
PWOUT_Cold_Close();
}
Cold_Close();
}
void RTC_Hot_On(void){
Hot_EN();
PWM2_HardWare_State = PWM_ON;
fn_PWM2_Check(PWM2_Data);
}
void RTC_Hot_Close(void){
if(PWM2_HardWare_State != PWM_OFF){
PWM1_Data = 0;
fn_PWM1_Check(PWM1_Data);
fn_PWM1_OFF();
PWM1_HardWare_State = PWM_OFF;
Cold_Close();
PWM2_Data = 0;
fn_PWM2_Check(PWM2_Data);
fn_PWM2_OFF();
PWM2_HardWare_State = PWM_OFF;
Hot_Close();
fn_MOS_delay(80);
PWOUT_Hot_OUT();
fn_MOS_delay(80);
PWOUT_Hot_Close();
}
Hot_Close();
}
void fn_MOS_delay(unsigned char count){
unsigned char i = 0;
for(i = 0;i <= count;i++){
NOP();
NOP();
}
}
void Cold_PID(unsigned int TargetAD){
unsigned target_pwm = 0;
if(!b_pid500ms){ return ;}
b_pid500ms = 0;
if(NTC_Value > TargetAD){
target_pwm = 0;
GPIO_FAOUT = 1;
}
else{
target_pwm = PIDcompute( TargetAD , NTC_Value );
if(target_pwm > 196){
GPIO_FAOUT = 1;
}else{
GPIO_FAOUT = 0;
}
}
if(PWM1_Data > target_pwm){
PWM1_Data --;
}
else if (PWM1_Data < target_pwm){
PWM1_Data ++;
}
else{
}
}
void Hot_PID(unsigned int TargetAD){
unsigned target_pwm = 0;
if(!b_pid500ms){ return ;}
b_pid500ms = 0;
if(NTC_Value < TargetAD){
target_pwm = 0;
GPIO_FAOUT = 1;
}
else{
GPIO_FAOUT = 0;
target_pwm = PIDcompute( TargetAD , NTC_Value );
}
if(PWM2_Data > target_pwm){
PWM2_Data --;
}
else if (PWM2_Data < target_pwm){
PWM2_Data ++;
}
else{
}
}
6:Key_State.h
代码如下:
#ifndef _Key_State_H_
#define _Key_State_H_
#include <ny8.h>
#include "ny8_constant.h"
void Key_State_Check(void);
#endif
7:Key_State.c
代码如下:
#include "Key_State.h"
#include "Soft_State.h"
void Key_State_Check(void){
if((machine_state.Modle_state == Modle_ON_USBIN)){
Hardware_key_state = Key_IDLE;
return;
}
switch (Hardware_key_state){
case Key1_Effect:{
count_Work_Time_Check = 0;
Hardware_key_state = Key_IDLE;
if((machine_state.Modle_state == Modle_ON_Hot_1) ||(machine_state.Modle_state == Modle_ON_Hot_2)){
machine_state.Modle_state = Modle_ON_Cold_1;
}
else if (machine_state.Modle_state == Modle_ON_Cold_1){
machine_state.Modle_state = Modle_ON_Cold_2;
}
else if (machine_state.Modle_state == Modle_ON_Cold_2){
machine_state.Modle_state = Modle_ON_Cold_1;
}
else{
;
}
break;
}
case Key1_Long_Effect:{
count_Work_Time_Check = 0;
Hardware_key_state = Key_IDLE;
if(machine_state.Modle_state == Modle_IDLE){
machine_state.Modle_state = Modle_ON_Cold_1;
}
else if((machine_state.Modle_state == Modle_ON_Cold_1)||(machine_state.Modle_state == Modle_ON_Cold_2) ){
machine_state.Modle_state = Modle_IDLE;
}
else if((machine_state.Modle_state == Modle_ON_Hot_1)||(machine_state.Modle_state == Modle_ON_Hot_2) ){
machine_state.Modle_state = Modle_ON_Cold_1;
}
else{
;
}
break;
}
case Key2_Effect:{
count_Work_Time_Check = 0;
Hardware_key_state = Key_IDLE;
if((machine_state.Modle_state == Modle_ON_Cold_1)||(machine_state.Modle_state == Modle_ON_Cold_2)){
machine_state.Modle_state = Modle_ON_Hot_1;
}
else if (machine_state.Modle_state == Modle_ON_Hot_1){
machine_state.Modle_state = Modle_ON_Hot_2;
}
else if (machine_state.Modle_state == Modle_ON_Hot_2){
machine_state.Modle_state = Modle_ON_Hot_1;
}
else{
;
}
break;
}
case Key2_Long_Effect:{
count_Work_Time_Check = 0;
Hardware_key_state = Key_IDLE;
if(machine_state.Modle_state == Modle_IDLE){
machine_state.Modle_state = Modle_ON_Hot_1;
}
else if((machine_state.Modle_state == Modle_ON_Hot_1) ||(machine_state.Modle_state == Modle_ON_Hot_2)){
machine_state.Modle_state = Modle_IDLE;
}
else if((machine_state.Modle_state == Modle_ON_Cold_1)||(machine_state.Modle_state == Modle_ON_Cold_2) ){
machine_state.Modle_state = Modle_ON_Hot_1;
}
else{
;
}
break;
}
default:{
return;
}
}
if(machine_state.BAT_state == BAT_empty){
LowPowerFlash_En = 1;
machine_state.Modle_state = Modle_IDLE;
}
}
9:PID.h
代码如下:
#ifndef _PID_H_
#define _PID_H_
#include <ny8.h>
#include "ny8_constant.h"
#define TARGET_PWM_MAX 0X03FF
#define TARGET_PWM_MAX1 0X0398
#define TARGET_PWM_MAX2 0X03FF
#define TARGET_PWM_MIN 0x0000
#define TARGET_PWMAD_Hot2 1145
#define TARGET_PWMAD_Hot1 1300
#define TARGET_PWMAD_Cold1 2399
#define TARGET_PWMAD_Cold2 2658
#define D_TARGET_Real_1 515
#define D_TARGET_Real_2 479
#define D_TARGET_Real_3 343
#define D_TARGET_Real_4 207
#define D_TARGET_Real_5 171
#define D_TARGET_Real_6 100
#define D_TARGET_Real_7 20
#define D_TARGET_Cold 0
#define D_TARGET_Hot 200
extern volatile unsigned int ATarget_PWM_max;
unsigned int PIDcompute(unsigned int Target,unsigned int Real);
#endif
10:PID.c
代码如下:
#include "PID.h"
volatile unsigned int ATarget_PWM_max =0;
unsigned int PIDcompute(unsigned int Target,unsigned int Real)
{
unsigned int Error = 0;
if(Target < Real){
Error = Real - Target;
}else{
Error = Target - Real;
}
if(Error> D_TARGET_Real_5){
return ATarget_PWM_max ;
}
else if(Error> D_TARGET_Real_6){
return 796 ;
}
else if(Error> D_TARGET_Real_7){
return 196 ;
}
else {
return 0 ;
}
}
11:Soft_State.h
代码如下:
#ifndef _Soft_State_H_
#define _Soft_State_H_
#include <ny8.h>
#include "ny8_constant.h"
#include ".././Hardware/Hardware.h"
#include "PID.h"
#include "Key_State.h"
#include "ColdHotOut_State.h"
#include "BAT_State.h"
typedef union {
struct{
unsigned char BIT0:1;unsigned char BIT1:1;unsigned char BIT2:1;unsigned char BIT3:1;
unsigned char BIT4:1;unsigned char BIT5:1;unsigned char BIT6:1;unsigned char BIT7:1;
}DATA_BIT;
unsigned char DATA_BYTE;
}Per_Machine_type;
extern volatile Per_Machine_type machine_flag;
#define bworktime_100ms_flag machine_flag.DATA_BIT.BIT0
#define batceck_10ms_flag machine_flag.DATA_BIT.BIT1
#define b_pid500ms machine_flag.DATA_BIT.BIT2
#define LowPowerFlash_En machine_flag.DATA_BIT.BIT3
#define b_led_Robing10ms machine_flag.DATA_BIT.BIT4
#define b_Exled_Robing machine_flag.DATA_BIT.BIT5
#define bchargtime_1s_flag machine_flag.DATA_BIT.BIT6
#define bchargoff_fla machine_flag.DATA_BIT.BIT7
typedef struct _Machine_State{
unsigned char Modle_state ;
unsigned char USB_state ;
unsigned char BAT_state ;
unsigned char LED_state ;
unsigned char RTC_state ;
}Machine_State ;
extern volatile Machine_State machine_state;
extern volatile unsigned char MerryModle_state;
enum
{
Modle_IDLE = 0,
Modle_ON_Hot_1 ,
Modle_ON_Cold_1 ,
Modle_ON_Hot_2 ,
Modle_ON_Cold_2 ,
Modle_ON_USBIN
};
enum
{
Modle_LED_IDE = 0,
Modle_LED_Hot_1,
Modle_LED_Hot_2,
Modle_LED_Cold_1,
Modle_LED_Cold_2,
Modle_LED_Bat_Charg,
Modle_LED_Bat_ChargOff,
Modle_LED_Error
};
enum
{
Modle_RTC_IDE = 0,
Modle_RTC_Hot_1,
Modle_RTC_Cold_1,
Modle_RTC_Hot_2,
Modle_RTC_Cold_2,
Modle_RTC_Error
};
enum
{
BAT_IDLE = 0,
BAT_fully,
BAT_empty,
BAT_ERROR
};
enum
{
USB_OFF = 0,
USB_CH
};
#define _WORK_TIME_ 59
#define _Charge_TIME_ 1079
extern volatile unsigned char count_Work_Time_Check;
extern volatile unsigned int count_charg_Time_Check;
void Software_State_init(void);
void Software_State_Check(void);
void Software_Input_Check(void);
void Software_Output_Check(void);
void Work_Time_Check(void);
void Work_charg_Check(void);
void Machine_off(void);
#endif
12:Soft_State.c
代码如下:
#include "Soft_State.h"
#include "Key_State.h"
#include "ColdHotOut_State.h"
#include "BAT_State.h"
volatile Machine_State machine_state ;
volatile Per_Machine_type machine_flag;
void Software_State_init(void){
machine_state.Modle_state = Modle_IDLE;
MerryModle_state = machine_state.Modle_state;
machine_state.USB_state = USB_OFF;
machine_state.BAT_state = BAT_IDLE;
machine_state.LED_state = Modle_LED_IDE;
machine_state.RTC_state = Modle_RTC_IDE;
STDBY_TO_IN();
CE_OUT();
PWM1_Data = 0;
PWM2_Data = 0;
PWM3_Data = 0;
PWM4_Data = 0;
}
volatile unsigned char MerryModle_state = 0;
void Software_State_Check(void){
if(MerryModle_state != machine_state.Modle_state){
MerryModle_state = machine_state.Modle_state;
Machine_off();
}
switch( machine_state.Modle_state ){
case Modle_IDLE : {
STDBY_TO_IN();
machine_state.LED_state = Modle_LED_IDE;
machine_state.RTC_state = Modle_RTC_IDE;
break;
}
case Modle_ON_Hot_1 : {
STDBY_TO_OUT();
machine_state.LED_state = Modle_LED_Hot_1;
machine_state.RTC_state = Modle_RTC_Hot_1;
break;
}
case Modle_ON_Cold_1 : {
STDBY_TO_OUT();
machine_state.LED_state = Modle_LED_Cold_1;
machine_state.RTC_state = Modle_RTC_Cold_1;
break;
}
case Modle_ON_Hot_2 : {
STDBY_TO_OUT();
machine_state.LED_state = Modle_LED_Hot_2;
machine_state.RTC_state = Modle_RTC_Hot_2;
break;
}
case Modle_ON_Cold_2 : {
STDBY_TO_OUT();
machine_state.LED_state = Modle_LED_Cold_2;
machine_state.RTC_state = Modle_RTC_Cold_2;
break;
}
case Modle_ON_USBIN : {
STDBY_TO_IN();
if(bchargoff_fla == 1){
machine_state.LED_state = Modle_LED_Bat_ChargOff ;
break;
}
if((machine_state.BAT_state == BAT_fully)){
machine_state.LED_state = Modle_LED_Bat_ChargOff ;
}
else{
machine_state.LED_state = Modle_LED_Bat_Charg ;
}
break;
}
default:{
Software_State_init();
}
}
}
void Software_Input_Check(void){
Key_State_Check();
BAT_State_Check();
USB_State_Check();
Work_Time_Check();
Work_charg_Check();
}
void Software_Output_Check(void){
LED_State_Check();
RTC_State_Check();
Low_Flash();
}
volatile unsigned char count_Work_Time_Check = 0 ;
void Work_Time_Check(void){
if((machine_state.Modle_state != Modle_IDLE) && (machine_state.Modle_state != Modle_ON_USBIN) ){
if(!bworktime_100ms_flag){ return; }
bworktime_100ms_flag = 0;
if(count_Work_Time_Check++ > _WORK_TIME_){
count_Work_Time_Check = 0;
Machine_off();
machine_state.Modle_state = Modle_IDLE ;
}
}
else{
count_Work_Time_Check = 0;
}
}
volatile unsigned int count_charg_Time_Check = 0 ;
void Work_charg_Check(void){
if(machine_state.Modle_state == Modle_ON_USBIN){
if(bchargoff_fla == 1){
CE_Close();
return;
}
CE_OUT();
if(!bchargtime_1s_flag){ return; }
bchargtime_1s_flag = 0;
if(count_charg_Time_Check++ >= _Charge_TIME_){
count_charg_Time_Check = 0;
bchargoff_fla = 1;
}
}
else{
CE_OUT();
count_charg_Time_Check = 0;
bchargoff_fla = 0;
}
}
void Machine_off(void){
LED_Close_Cold();
LED_Close_Hot();
RTC_Cold_Close();
RTC_Hot_Close();
LED_Close_Hot();
FA_Close();
STDBY_TO_IN();
CE_OUT();
}
硬件层
1:Hardware.h
#ifndef _HARDWARE_H_
#define _HARDWARE_H_
#include "Sys_CO.h"
#include "Key_CO.h"
#include "ADC_CO.h"
#include "Pwm_CO.h"
#include "GPIO_OUT.h"
void Hardware_init(void);
void Hardware_run(void);
void Hardware_Start(void);
#endif
2:Hardware.c
#include "Hardware.h"
void Hardware_init(void){
PWM1_HardWare_State = PWM_OFF;
PWM2_HardWare_State = PWM_OFF;
PWM3_HardWare_State = PWM_OFF;
PWM4_HardWare_State = PWM_OFF;
Hardware_key_state = PWM_OFF;
Hardware_USB_state = USB_IDLE;
gcount_asleep = 0;
}
void Hardware_run(void){
fn_key_Check();
fn_PWM1_Check(PWM1_Data);
fn_PWM2_Check(PWM2_Data);
fn_PWM3_Check(PWM3_Data);
fn_PWM4_Check(PWM4_Data);
ADC_Check();
USB_In_Check();
}
void delay_Hardware(unsigned char count){
unsigned char i = 0;
for(i = 0;i <= count;i++){
NOP();
NOP();
NOP();
}
}
void Hardware_Start(void){
unsigned char count_heart = 10;
CE_OUT();
while(count_heart-- > 0){
while(bHardware_Start_100ms == 0){
Hardware_run();
delay_Hardware(100);
}
bHardware_Start_100ms = 0;
}
CE_Close();
}
3:ADC_CO.h
代码如下:
#ifndef _ADC_CO_H_
#define _ADC_CO_H_
#include <ny8.h>
#include "ny8_constant.h"
typedef union {
struct{
unsigned char BIT0:1;unsigned char BIT1:1;unsigned char BIT2:1;unsigned char BIT3:1;
unsigned char BIT4:1;unsigned char BIT5:1;unsigned char BIT6:1;unsigned char BIT7:1;
}DATA_BIT;
unsigned char DATA_BYTE;
}Per_adc_type;
extern volatile Per_adc_type adc_flag;
#define badc_10ms_flag adc_flag.DATA_BIT.BIT0
#define ad_finish_flag adc_flag.DATA_BIT.BIT1
extern volatile unsigned int Count_Adc;
extern volatile unsigned int NTC_Value;
extern volatile unsigned int BAT_Value;
void ADC_Check();
unsigned int ADC_get_value(unsigned char vref_value,unsigned char channal);
void STDBY_TO_IN(void);
void STDBY_TO_OUT(void);
unsigned char STDBY_Get_Out(void);
#endif
4:ADC_CO.c
#include "ADC_CO.h"
#include "Pwm_CO.h"
#include "GPIO_OUT.h"
volatile Per_adc_type adc_flag;
volatile unsigned int NTC_Value;
volatile unsigned int BAT_Value;
void fn_adc_delay(unsigned char count){
unsigned char i = 0;
for(i = 0;i <= count;i++){
NOP();
}
}
volatile unsigned int Count_Adc;
unsigned int ADC_get_value(unsigned char vref_value,unsigned char channal){
volatile unsigned char AD_ValueL = 0;
volatile unsigned int AD_Value = 0;
ADVREFH = vref_value;
ADR |= C_Ckl_Div16;
ADCR |= C_Sample_8clk | C_12BIT;
ADMD |= C_ADC_En | C_ADC_CH_En | channal ;
fn_adc_delay(60);
ENI();
ADIE = 1;
ad_finish_flag = 0;
Count_Adc = 0;
ADMDbits.START = 1;
while( !ad_finish_flag ){
NOP();
}
ADIE = 0;
AD_ValueL = ( 0x0F & ADR);
AD_Value = ADD;
AD_Value <<= 4;
AD_Value += AD_ValueL;
ADMD = 0x00;
return AD_Value ;
}
void ADC_Check(void){
if(!badc_10ms_flag ){return;}
badc_10ms_flag = 0;
if( STDBY_Get_Out() == 1 ){
NTC_Value = ADC_get_value(C_Vrefh_VDD,C_ADC_PA0);
}
BAT_Value = ADC_get_value(C_Vrefh_3V,C_ADC_PB5);
}
void STDBY_TO_IN(void){
IOSTA |= C_PA6_Input;
APHCON &= ~C_PA6_PHB ;
}
void STDBY_TO_OUT(void){
IOSTA &= ~C_PA6_Input;
APHCON |= C_PA6_PHB ;
PA6 = 0;
}
unsigned char STDBY_Get_Out(void) {
if( (IOSTA & (C_PA6_Input)) == C_PA6_Output ){
return 1;
}
return 0;
}
5:GPIO_OUT.h
#ifndef _GPIO_OUT_H_
#define _GPIO_OUT_H_
#include <ny8.h>
#include "ny8_constant.h"
#define GPIO_USBIn PB4
#define GPIO_STDBY PA6
#define GPIO_PWOUT_Cold PB3
#define GPIO_PWOUT_Hot PB2
#define PWOUT_Cold_OUT() GPIO_PWOUT_Cold = 1
#define PWOUT_Cold_Close() GPIO_PWOUT_Cold = 0
#define PWOUT_Hot_OUT() GPIO_PWOUT_Hot = 1
#define PWOUT_Hot_Close() GPIO_PWOUT_Hot = 0
#define GPIO_OUT1 PA2
#define GPIO_OUT2 PA3
#define LED_Cold_OUT() GPIO_OUT2 = 1
#define LED_Hot_OUT() GPIO_OUT1 = 1
#define LED_Cold_Close() GPIO_OUT2 = 0
#define LED_Hot_Close() GPIO_OUT1 = 0
#define GPIO_OUT3 PB1
#define GPIO_OUT4 PB0
#define Cold_EN() GPIO_OUT3 = 1
#define Hot_EN() GPIO_OUT4 = 1
#define Cold_Close() GPIO_OUT3 = 0
#define Hot_Close() GPIO_OUT4 = 0
#define GPIO_CEOUT PA4
void CE_Close();
void CE_OUT();
#define GPIO_FAOUT PA1
#define FA_OUT() GPIO_FAOUT = 1
#define FA_Close() GPIO_FAOUT = 0
extern volatile unsigned char Hardware_USB_state;
enum
{
USB_IDLE = 0,
USB_IN
};
void USB_In_Check(void);
#endif
6:GPIO_OUT.c
#include "GPIO_OUT.h"
#include "Sys_CO.h"
volatile unsigned char Hardware_USB_state;
void USB_In_Check(void){
if(GPIO_USBIn == 1){
gcount_asleep = 0;
Hardware_USB_state = USB_IN ;
}else{
Hardware_USB_state = USB_IDLE ;
}
}
void CE_Close(void){
APHCON |= C_PA4_PHB;
IOSTA &= ~C_PA4_Input;
GPIO_CEOUT = 0;
}
void CE_OUT(void){
IOSTA |= C_PA4_Input;
APHCON &= ~C_PA4_PHB;
}
7:Key_CO.h
#ifndef _KEY_CO_H_
#define _KEY_CO_H_
#include <ny8.h>
#include "ny8_constant.h"
#define KEY1_IN_GPIO_Pin PA7
#define KEY2_IN_GPIO_Pin PA5
typedef union {
struct{
unsigned char BIT0:1;unsigned char BIT1:1;unsigned char BIT2:1;unsigned char BIT3:1;
unsigned char BIT4:1;unsigned char BIT5:1;unsigned char BIT6:1;unsigned char BIT7:1;
}DATA_BIT;
unsigned char DATA_BYTE;
}Per_key_type;
#define _LONG_key 200
extern volatile Per_key_type key1_flag;
#define bkey1_10ms key1_flag.DATA_BIT.BIT0
#define bkey1_judge key1_flag.DATA_BIT.BIT1
#define bkey1_judge_long key1_flag.DATA_BIT.BIT2
#define bkey1_Effect key1_flag.DATA_BIT.BIT3
#define bkey1_LongEffect key1_flag.DATA_BIT.BIT4
#define bkey1_Effect_Lose key1_flag.DATA_BIT.BIT5
#define bkey1_Effect_LLose key1_flag.DATA_BIT.BIT6
#define bkey1_change key1_flag.DATA_BIT.BIT7
extern volatile Per_key_type key2_flag;
#define bkey2_10ms key2_flag.DATA_BIT.BIT0
#define bkey2_judge key2_flag.DATA_BIT.BIT1
#define bkey2_judge_long key2_flag.DATA_BIT.BIT2
#define bkey2_Effect key2_flag.DATA_BIT.BIT3
#define bkey2_LongEffect key2_flag.DATA_BIT.BIT4
#define bkey2_Effect_Lose key2_flag.DATA_BIT.BIT5
#define bkey2_Effect_LLose key2_flag.DATA_BIT.BIT6
#define bkey2_change key2_flag.DATA_BIT.BIT7
extern volatile unsigned char Hardware_key_state;
enum
{
Key_IDLE = 0,
Key1_Effect,
Key1_Long_Effect,
Key2_Effect,
Key2_Long_Effect,
};
void fn_Key_Init(void);
void fn_key_judge(void);
void fn_key_Effect(void);
void fn_key_Check(void);
#endif
8:Key_CO.c
#include "Key_CO.h"
#include "Sys_CO.h"
#include "GPIO_OUT.h"
volatile Per_key_type key1_flag;
volatile Per_key_type key2_flag;
volatile unsigned char Hardware_key_state;
static unsigned char count_key1 ;
void fn_key1_judge(void){
if(!bkey1_10ms){return;}
bkey1_10ms = 0;
if(KEY1_IN_GPIO_Pin==0){
gcount_asleep = 0;
if(count_key1++<3){return;}
if(!bkey1_judge){
bkey1_judge = 1;
bkey1_Effect = 1;
}
else{
if(count_key1>_LONG_key){
count_key1 = 0;
if(!bkey1_judge_long){
bkey1_judge_long = 1;
bkey1_judge_long = 1;
bkey1_LongEffect = 1;
}
}
}
}
else{
count_key1 = 0;
if(bkey1_judge){
if(bkey1_judge_long){
bkey1_judge_long = 0;
bkey1_Effect_LLose = 1;
}else{
bkey1_judge_long = 0;
bkey1_Effect_Lose = 1;
}
}
bkey1_judge = 0;
}
}
static unsigned char count_key2 ;
void fn_key2_judge(void){
if(!bkey2_10ms){return;}
bkey2_10ms = 0;
if(KEY2_IN_GPIO_Pin==0){
gcount_asleep = 0;
if(count_key2++<3){return;}
if(!bkey2_judge){
bkey2_judge = 1;
bkey2_Effect = 1;
}else{
if(count_key2>_LONG_key){
count_key2= 0;
if(!bkey2_judge_long){
bkey2_judge_long = 1;
bkey2_judge_long = 1;
bkey2_LongEffect = 1;
}
}
}
}
else{
count_key2 = 0;
if(bkey2_judge){
if(bkey2_judge_long){
bkey2_judge_long = 0;
bkey2_Effect_LLose = 1;
}else{
bkey2_judge_long = 0;
bkey2_Effect_Lose = 1;
}
}
bkey2_judge = 0;
}
}
void fn_key_Effect(void){
if(bkey1_Effect_Lose){
bkey1_Effect_Lose = 0;
Hardware_key_state = Key1_Effect;
}
else if(bkey1_LongEffect){
bkey1_LongEffect = 0;
Hardware_key_state = Key1_Long_Effect;
}
else if(bkey2_Effect_Lose){
bkey2_Effect_Lose = 0;
Hardware_key_state = Key2_Effect;
}
else if(bkey2_LongEffect){
bkey2_LongEffect = 0;
Hardware_key_state = Key2_Long_Effect;
}
else{
;
}
}
void fn_key_Check(void){
fn_key1_judge();
fn_key2_judge();
fn_key_Effect();
}
9:Pwm_CO.h
#ifndef _PWM_CO_H_
#define _PWM_CO_H_
#include <ny8.h>
#include "ny8_constant.h"
#define P_PWM1_OUT PB3
#define P_PWM2_OUT PB2
#define P_PWM1_close() P_PWM1_OUT = 0;
#define P_PWM2_close() P_PWM2_OUT = 0;
#define P_PWM3_OUT PA2
#define P_PWM4_OUT PA3
#define P_PWM3_close() P_PWM3_OUT = 0;
#define P_PWM4_close() P_PWM3_OUT = 0;
#define PWM1_L_MAX 0xFF
#define PWM1_H_MAX 0x03
#define PWM1_VAL_MAX 0x03FF
#define PWM1_VAL_MIN 0x0000
#define PWM2_L_MAX 0xFF
#define PWM2_H_MAX 0x03
#define PWM2_VAL_MAX 0x03FF
#define PWM2_VAL_MIN 0x0000
#define PWM3_VAL_MAX 0xFF
#define PWM3_VAL_MIN 0x00
#define PWM4_VAL_MAX 0xFF
#define PWM4_VAL_MIN 0x00
typedef union {
struct{
unsigned char BIT0:1;unsigned char BIT1:1;unsigned char BIT2:1;unsigned char BIT3:1;
unsigned char BIT4:1;unsigned char BIT5:1;unsigned char BIT6:1;unsigned char BIT7:1;
}DATA_BIT;
unsigned char DATA_BYTE;
}Per_pwm_type;
extern volatile Per_pwm_type pwm_flag;
#define b_pwm1_start pwm_flag.DATA_BIT.BIT0
#define b_pwm2_start pwm_flag.DATA_BIT.BIT1
#define b_pwm3_start pwm_flag.DATA_BIT.BIT2
#define b_pwm4_start pwm_flag.DATA_BIT.BIT3
#define PWM_OFF 0
#define PWM_ON 1
extern volatile Per_pwm_type pwm_state_flag;
#define PWM1_HardWare_State pwm_state_flag.DATA_BIT.BIT0
#define PWM2_HardWare_State pwm_state_flag.DATA_BIT.BIT1
#define PWM3_HardWare_State pwm_state_flag.DATA_BIT.BIT2
#define PWM4_HardWare_State pwm_state_flag.DATA_BIT.BIT3
extern volatile unsigned int PWM1_Data;
extern volatile unsigned int PWM2_Data;
extern volatile unsigned char PWM3_Data;
extern volatile unsigned char PWM4_Data;
void fn_PWM1_Check(unsigned int PWM_Value);
void fn_PWM2_Check(unsigned int PWM_Value);
void fn_PWM3_Check(unsigned char PWM_Value);
void fn_PWM4_Check(unsigned char PWM_Value);
void fn_PWM1_OFF(void);
void fn_PWM2_OFF(void);
void fn_PWM3_OFF(void);
void fn_PWM4_OFF(void);
#endif
10:Pwm_CO.c
#include "Pwm_CO.h"
volatile Per_pwm_type pwm_flag;
volatile Per_pwm_type pwm_state_flag;
volatile unsigned int PWM1_Data = 0;
volatile unsigned int PWM2_Data = 0;
volatile unsigned char PWM3_Data = 0;
volatile unsigned char PWM4_Data = 0;
void fn_PWM1_run(unsigned int PWM1_VAL) {
volatile unsigned char T1DATAL = 0;
volatile unsigned char T1DATAH = 0;
if(PWM1_VAL == PWM1_VAL_MAX)
{
T1CR1 = 0x00;
b_pwm1_start = 0;
P_PWM1_OUT = 1;
}
else if(PWM1_VAL == PWM1_VAL_MIN)
{
T1CR1 = 0x00;
b_pwm1_start = 0;
P_PWM1_OUT = 0;
}
else{
T1DATAL = ( unsigned char )(PWM1_VAL &(0x00FF));
T1DATAH = ( unsigned char )((PWM1_VAL>>8)&(0x0003));
if(b_pwm1_start == 0){
TMRH = 0x30;
TMR1 = 0xFF;
T1CR2 = C_PS1_Div2;
T1CR1 = C_PWM1_En | C_PWM1_Active_Hi | C_TMR1_Reload | C_TMR1_En;
b_pwm1_start= 1;
}
TMRH = (TMRH & 0xFC )|(T1DATAH & 0x03);
PWM1DUTY = T1DATAL;
}
}
void fn_PWM1_OFF(void){
T1CR1 = 0x00;
b_pwm1_start = 0;
P_PWM1_OUT = 0;
}
void fn_PWM1_Check(unsigned int PWM_Value){
if (PWM1_HardWare_State == PWM_ON){
fn_PWM1_run(PWM_Value);
}
else {
fn_PWM1_OFF();
}
}
void fn_PWM2_run(unsigned int PWM2_VAL) {
volatile unsigned char T2DATAL = 0;
volatile unsigned char T2DATAH = 0;
if(PWM2_VAL == PWM1_VAL_MAX)
{
T2CR1 = 0x00;
b_pwm2_start = 0;
P_PWM2_OUT = 1;
}
else if(PWM2_VAL == PWM2_VAL_MIN)
{
T2CR1 = 0x00;
b_pwm2_start = 0;
P_PWM2_OUT = 0;
}
else{
T2DATAL = ( unsigned char )(PWM2_VAL &(0x00FF));
T2DATAH = ( unsigned char )((PWM2_VAL>>8)&(0x0003));
if(b_pwm2_start == 0){
TMRH = 0xC0;
TMR2 = 0xFF;
T2CR2 = C_PS2_Div2;
T2CR1 = C_PWM2_En | C_PWM2_Active_Hi | C_TMR2_Reload | C_TMR2_En;
b_pwm2_start = 1;
}
TMRH = (TMRH & 0xF3)|((T2DATAH << 2 )& 0x0C);
PWM2DUTY = T2DATAL;
}
}
void fn_PWM2_OFF(void){
T2CR1 = 0x00;
b_pwm2_start = 0;
P_PWM2_OUT = 0;
}
void fn_PWM2_Check(unsigned int PWM_Value){
if (PWM2_HardWare_State == PWM_ON){
fn_PWM2_run(PWM_Value);
}
else {
fn_PWM2_OFF();
}
}
void fn_PWM3_run(unsigned char PWM3_VAL) {
if(PWM3_VAL == PWM3_VAL_MAX)
{
P_PWM3_OUT = 1;
T3CR1 &= (~(C_PWM3_En));
b_pwm3_start = 0;
}
else{
if(b_pwm3_start == 0){
TM3RH = 0x00;
TMR3 = 0xFF;
T3CR2 = C_PS3_Dis | C_TMR3_ClkSrc_Inst;
T3CR1 = C_PWM3_En | C_PWM3_Active_Hi | C_TMR3_Reload | C_TMR3_En;
b_pwm3_start= 1;
}
}
PWM3DUTY = PWM3_VAL;
}
void fn_PWM3_OFF(void){
P_PWM3_OUT = 0;
T3CR1 &= (~(C_PWM3_En));
b_pwm3_start = 0;
}
void fn_PWM3_Check(unsigned char PWM_Value){
if (PWM3_HardWare_State == PWM_ON){
fn_PWM3_run(PWM_Value);
}
else if(PWM3_HardWare_State == PWM_OFF){
fn_PWM3_OFF();
}else{
;
}
}
void fn_PWM4_run(unsigned char PWM4_VAL) {
PWM4DUTY = PWM4_VAL;
if(PWM4_VAL == PWM4_VAL_MAX)
{
P_PWM4_OUT = 1;
P4CR1 &= ~( C_PWM4_En);
b_pwm4_start = 0;
}
else{
if(b_pwm4_start == 0){
P_PWM4_OUT = 1;
TM3RH = 0x00;
TMR3 = 0xFF;
T3CR2 = C_PS3_Dis | C_TMR3_ClkSrc_Inst;
T3CR1 = C_PWM3_Active_Hi | C_TMR3_Reload | C_TMR3_En;
P4CR1 = C_PWM4_En | C_PWM4_Active_Hi;
b_pwm4_start = 1;
}
}
}
void fn_PWM4_OFF(void){
P_PWM4_OUT = 0;
P4CR1 &= ~( C_PWM4_En);
b_pwm4_start = 0;
}
void fn_PWM4_Check(unsigned char PWM_Value){
if (PWM4_HardWare_State == PWM_ON){
fn_PWM4_run(PWM_Value);
}
else{
fn_PWM4_OFF();
}
}
11:Sys_CO.h
#ifndef _Sys_CO_H_
#define _Sys_CO_H_
#include <ny8.h>
#include "ny8_constant.h"
#define CST_reload_T0 0
typedef union {
struct{
unsigned char BIT0:1;unsigned char BIT1:1;unsigned char BIT2:1;unsigned char BIT3:1;
unsigned char BIT4:1;unsigned char BIT5:1;unsigned char BIT6:1;unsigned char BIT7:1;
}DATA_BIT;
unsigned char DATA_BYTE;
}Per_sys_type;
extern volatile Per_sys_type sof_sys_flag;
#define b_asleep100ms sof_sys_flag.DATA_BIT.BIT0
#define b_asleep sof_sys_flag.DATA_BIT.BIT1
#define bHardware_Start_100ms sof_sys_flag.DATA_BIT.BIT2
extern volatile unsigned char gcount_asleep;
void clear_memory(void);
void GPIO_Init(void);
void ADIO_Init(void);
void Time0_init(void);
void fn_sys_reset( void );
#endif
12:Sys_CO.c
#include "Sys_CO.h"
volatile unsigned char gcount_asleep;
volatile Per_sys_type sof_sys_flag;
void clear_memory(void)
{
STATUS=0x00;
for(FSR =0x20; FSR<=0x7F; FSR++)
{
INDF = 0;
}
STATUS=0x40;
for(FSR =0x20; FSR<=0x3F; FSR++)
{
INDF = 0;
}
}
void GPIO_Init(void){
IOSTA = C_PA_Input;
IOSTB = C_PB_Input;
PORTA = 0x00;
PORTB = 0x00;
IOSTA = (( C_PA0_Input )|( C_PA1_Output )|( C_PA2_Output )|( C_PA3_Output )|(C_PA4_Output )|(C_PA5_Input )|( C_PA6_Input )|( C_PA7_Input ));
IOSTB = (( C_PB0_Output )|( C_PB1_Output )|( C_PB2_Output )|( C_PB3_Output )|( C_PB4_Input )|( C_PB5_Input ));
APHCON = 0xFF ^ ((C_PA6_PHB)|(C_PA7_PHB));
ABPLCON = 0xFF;
PHPA5 = 0;
BPHCON = C_PB_PHB ;
AWUCON = (( C_PA5_Wakeup )|( C_PA7_Wakeup ));
BWUCON = ( C_PB4_Wakeup);
BODCON = 0X00;
}
void ADIO_Init(void){
PACON = ( C_PA0_AIN0 )|( C_PA1_AIN1 );
ADCR = ((C_PB5_AIN10)) ;
}
void Time0_init(void){
PCON1 = C_TMR0_Dis;
TMR0 = ( 0XFF - CST_reload_T0 );
T0MD = C_PS0_TMR0 | C_PS0_Div8;
INTE=0x45;
PCON1 |= C_TMR0_En;
T0IE = 1;
T0IF=0;
WDTIF=0;
ENI();
}
void fn_sys_reset( void )
{
PCON = C_LVR_En|C_LVR_En|C_LVD_En&(~C_WDT_En);
DISI();
CLRWDT();
GPIO_Init();
ADIO_Init();
Time0_init();
OSCCR = C_FHOSC_Sel;
}
总结
一.开关机及切换测试 :
1.长按图案对应热敷开关键2秒,加热功能开启短按一次指示灯变红色;依此循环长按2秒热敷键关掉。
2.长按图案对应冷熬开关键2秒,冷熬功能开启短按一次指示灯变蓝色;依此循环长按3秒冷敷键关掉。
3.S1 S2 : 长按开关机,断案切换模式,指示灯会随着变化。
二.温度控制测试 :
热敷与冷敷,目前设计为P控制
所以会在电池电压不一样的时候制冷片功率不同,导致制热制冷效果有一些区别,目前制冷状态我这边测试:
制冷一档18-20°C
制冷二档10-15°C
制热一档44-47°C
制冷二档49-52°C
二.低电压保护测试:
无负载时候,当电量低于3.35V时强制关机,低电关机后,重启开机电压3.56V。
有负载时候,当电量低于3.07V时强制关机,(电池电压为3.34V),低电关机后,重启开机电压3.56V。
测试中途开机3.3V低电压识别保护,3.5V低电压识别,3.6V状态恢复识别。
三.自动关机测试:
开机进入使用状态后 在最后一次能操作后10分钟 偏差10S 自动关机。
四. 充电测试:
适配器5V2A,充电需有独立充电0.574C,充至4.12V变灯充满,4.2V强制停止充电,充电时间2H-2.5H,充电电流到电芯不能大于0.574A,充电不工作,按键无效。
超过3小时关闭充电功能。
1).充电模式显示:
充电状态下,呼吸灯变化,通过呼吸灯颜色显示当前电量。
4.15V~以上 (红色灯长亮)
3.3V~4.15V (红色呼吸灯)
充电过程中关闭所有的功能操作。
测试点: 开机下插入充电口,充电电流为574mA 指示灯闪烁。其他功能关闭风扇关闭。
关机下插入充电口,充电电流为574mA 指示灯闪烁。
4.12V 测试充电充满状态指示灯亮起来停止闪烁为高亮,电压低于3.92V LED充电闪烁状态恢复。
五.功耗:
5.关机电流静态电流≤10uA 旧电路
6.待机功耗为1034UA
|