/*=============================================================================
* File Name : C_cpu.c
* Describe : init mcu
* Author : Tuqiang
* Version : V1.0
* Record : 2014/04/13
=============================================================================*/
/* Includes -------------------------------------------*/
#include "H_cpu_def.h"
#include "H_rom_def.h"
#include "H_macro_def.h"
#include "H_chart_def.h"
#include "H_fun_def.h"
/*=====================================================
* Function : void Init_Mcu(void)
* Describe : Mcu初始化
* Input : none
* Output : none
* Return : none
* Record : 2014/04/14
=====================================================*/
void Init_Mcu(void)
{
//关中断
// Sim();
while((CLK_ICKR&0x02)!=0x02) // 检测HSI是否己经稳定
;
//CLK_ICKR = 0x01; // 时钟选择、唤醒、选择HSI
//CLK_SWR = 0xE1; // 选择内部16M为主频
//CLK_SWCR = 0x00; // 不切换时钟
//CLK_CKDIVR = 0x02; // HSI/2分频,CPU时钟4M
//时钟切换,外部11.0592M
//自动切换
CLK_SWCR |= 0x02;
CLK_SWR = 0xb4;
while((CLK_SWCR&0x01)==0x01) // 检测HSI是否己经稳定
;
//CLK_CKDIVR = 0x02; // HSI/4分频,CPU时钟4M 11.0592/4
//CLK_CKDIVR = 0x00; // CPU时钟不分频
CLK_PCKENR1 |= 0xff; // 开启外设时钟,如定时器TIM2,默认开启
CLK_PCKENR2 |= 0xff;
//TIM1,定时1ms中断,x*(1/11.0592) = 1000us
TIM1_PSCRH = 0x00; //11.0592M不分频,计数周期 T = 1/11.0592 = 0.09us
TIM1_PSCRL = 0x00;
TIM1_ARRH = 0x2B; //2B33 = 11059; 1ms
TIM1_ARRL = 0x33;
TIM1_CR1 = 0x01;
TIM1_IER = 0x01;
//开启或关闭外设时钟
//TIM2
/*
TIM2_PSCR = 0x00; //主时钟/1
TIM2_IER = 0x00;
TIM2_EGR = 0x01;
TIM2_CNTRH = 0x00; //计数器初值
TIM2_CNTRL = 0x00;
TIM2_ARRH = 0xc1;
TIM2_ARRL = 0x7f;
TIM2_CR1 = 0x01;
TIM2_IER = 0x01;
*/
//TIM4
/*
TIM4_CR1 = 0x00;
TIM4_SR = 0;
TIM4_IER = 0x01;
TIM4_CNTR = 0;
TIM4_ARR = 250;
TIM4_PSCR = 7;
TIM4_CR1 = 0x01;
*/
//fclk = 16M,每次计数时间为0.0625us
//初始化IO
/*
RESET VALUE 0x00
Px_DDR: 0 - Input mode 1 - Output mode
Px_CR1: 0 - 无上拉 1 - 带上拉
Px_CR2: 0 - 普通IO 1 - 中断口
*/
//PA3 OUT
PA_DDR = 0x08; //0000 1000
PA_CR1 = 0x08; //0000 1000
PA_CR2 = 0x00; //0000 0000
//PB_DDR = 0x00; //0000 0000
//PB_CR1 = 0x00; //0000 0000
//PB_CR2 = 0x00;
PC_DDR = 0xc0; //1100 0000
PC_CR1 = 0xf8; //1111 1000
PC_CR2 = 0x20; //0010 0000
//PD4 OUT
PD_DDR = 0x18; //0001 1000
PD_CR1 = 0x7c; //0111 1100
PD_CR2 = 0x04; //0000 0100
EXTI_CR1 = 0xA0; //开启PC和PD端口下降沿触发
//ADC配置
//ADC_TDR:作为模拟输入口,用AD时使能设为1
//ADC_CR1 = 0x40; //18分频,持续转换 fadc = fmaster/8
//ADC_TDRL = 0x40; //TDR_4 bit = 1;
//ADC_CSR = 0x04; //选通AIN4通道,PD3
// ADC_CR1 = 0x70; //18分频
//ADC_CR1 |= 0x01; //使能ADC开始转
//ADC_CR1 |= 0x01; //使能ADC开始转换
Init_Uart();
//使能中断
// rim();
}
/*=====================================================
* Function : void Init_Rom(void)
* Describe : rom空间初始化
* Input : none
* Output : none
* Return : none
* Record : 2014/04/14
=====================================================*/
void Init_Rom(void)
{
U8_CURR_WorkMode = D_NORMAL_MODE;
U8_DspMode = 0;
U16_AC_P = 0;
U16_AC_V = 0;
U16_AC_I = 0;
U8_AC_COS = 0;
//校正功率参考值(校正功率1000W)
U16_P_REF_Data = 10000; // 1000.0W
U16_V_REF_Data = 2200; // 220.0V
U16_I_REF_Data = 4545; // 4.545A
//校正功率参考值(校正功率250W)
// U16_P_REF_Data = 2500; // 250.0W
// U16_V_REF_Data = 2200; // 220.0V
// U16_I_REF_Data = 1136; // 1.136A
/*
U32_P_REF_PLUSEWIDTH_TIME = 4807; // 4975us
U32_V_REF_PLUSEWIDTH_TIME = 1659; // 1666us
U32_I_REF_PLUSEWIDTH_TIME = 1736; // 1666us
U16_REF_001_E_Pluse_CNT = 6754; // 1000W 201HZ,36S 0.01度电
*/
U16_P_TotalTimes = 0;
U16_V_TotalTimes = 0;
U16_I_TotalTimes = 0;
U16_P_OneCycleTime = 0;
U16_V_OneCycleTime = 0;
U16_I_OneCycleTime = 0;
U16_P_Last_OneCycleTime = 0;
U16_V_Last_OneCycleTime = 0;
U16_I_Last_OneCycleTime = 0;
U16_P_CNT = 0;
U16_V_CNT = 0;
U16_I_CNT = 0;
U16_P_Last_CNT = 0;
U16_V_Last_CNT = 0;
U16_I_Last_CNT = 0;
//初始化单周期测量模式
B_P_TestOneCycle_Mode = 1;
B_V_TestOneCycle_Mode = 1;
B_I_TestOneCycle_Mode = 1;
B_P_Last_TestOneCycle_Mode = 1;
B_V_Last_TestOneCycle_Mode = 1;
B_I_Last_TestOneCycle_Mode = 1;
//开始测量,置溢出标志位为1
B_P_OVERFLOW = 1;
B_V_OVERFLOW = 1;
B_I_OVERFLOW = 1;
B_P_Last_OVERFLOW = 1;
B_V_Last_OVERFLOW = 1;
B_I_Last_OVERFLOW = 1;
//上电初始化为电压测试模式
B_VI_Test_Mode = 1;
IO_HLW8012_CF1_S = 1;
U16_VI_Test_Times = D_TIME1_2S;
//显示初始化
U8_DspMode = 0;
B_DIS_EN = FALSE;
U16_SendUart_Time = 0;
}
/*=====================================================
* 函数名称: PC中断,IO---PC5
* 函数功能: CF中断 P
* 输入参数: 无
* 输出参数:
* 返 回:
=====================================================*/
#pragma vector = 7
__interrupt void PC5_EXTI_Interrupt(void)
{
//功率测量
U16_P_TotalTimes = 0; //完成一次有效的测量,溢出寄存器清零
U16_P_CNT++;
if (B_P_OVERFLOW == TRUE)
{
//从溢出模式转入,开始测量
B_P_TestOneCycle_Mode = 0; //初始化为计数脉冲测量模式
U16_P_TotalTimes = 0; //清溢出寄存器清零
U16_P_OneCycleTime = 0; //清测量寄存器
U16_P_CNT = 1;
B_P_OVERFLOW = FALSE; //清溢出标志位
}
else
{
if (B_P_TestOneCycle_Mode == 1)
{
if (U16_P_OneCycleTime >= D_TIME1_100MS)
{
//单周期测量模式
U16_P_Last_OneCycleTime = U16_P_OneCycleTime;
B_P_Last_TestOneCycle_Mode = B_P_TestOneCycle_Mode;
B_P_OVERFLOW = FALSE; //溢出标志位清零
B_P_Last_OVERFLOW = B_P_OVERFLOW;
//清状态参数,重新开始测试
B_P_TestOneCycle_Mode = 0; //初始化为计数脉冲测量模式
U16_P_TotalTimes = 0; //完成一次有效的测量,溢出寄存器清零
U16_P_OneCycleTime = 0; //清测量寄存器
U16_P_CNT = 1;
}
}
else
{
if (U16_P_OneCycleTime >= D_TIME1_1S)
{
U16_P_Last_OneCycleTime = U16_P_OneCycleTime;
U16_P_Last_CNT = U16_P_CNT;
B_P_Last_TestOneCycle_Mode = B_P_TestOneCycle_Mode;
B_P_OVERFLOW = FALSE; //溢出标志位清零
B_P_Last_OVERFLOW = B_P_OVERFLOW;
//清状态参数,重新开始测试
B_P_TestOneCycle_Mode = 0; //初始化为计数脉冲测量模式
U16_P_TotalTimes = 0; //完成一次有效的测量,溢出寄存器清零
U16_P_OneCycleTime = 0; //清测量寄存器
U16_P_CNT = 1;
}
}
}
//校正模式
if (U8_CURR_WorkMode == D_CAL_START_MODE)
{
//记录单位时间内的用电量
U16_E_Pluse_CNT++;
}
//电能计量
if (U8_CURR_WorkMode == D_NORMAL_MODE)
{
U16_E_Pluse_CNT++;
if (U16_E_Pluse_CNT == U16_REF_001_E_Pluse_CNT )
{
U16_E_Pluse_CNT = 0;
U32_AC_E++;
}
}
}
/*=====================================================
* 函数名称: PD中断,IO---PD2
* 函数
小贝德罗
- 粉丝: 89
- 资源: 1万+
最新资源
- 《孙权劝学》教学设计与反思.docx
- 《土地的誓言》教学设计与反思.docx
- 《老山界》课程设计及反思.docx
- 《黄河颂》教学方案及反思.docx
- 基于java+springboot+vue+mysql的技术交流和分享平台 源码+数据库+论文(高分毕业设计).zip
- 《诫子书》教学设计.docx
- 《散步》教学设计.docx
- 《小圣施威降大圣》教学设计.docx
- 《狼》教学设计.docx
- 《我的白鸽》教学设计.docx
- 《大雁归来》教学设计及反思.docx
- 《猫》教学设计.docx
- 《秋天的怀念》教学设计.docx
- 《雨的四季》教学设计.docx
- 《春》教学设计.docx
- 《散步》教学设计及课堂流程.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论5