注册会员
- 积分
- 18
- 威望
- 12
- 贡献
- 6
- 兑换币
- 6
- 注册时间
- 2018-12-8
- 在线时间
- 0 小时
|
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "Platform_Types.h"
#include "CH452.h"
#include "ADC.h"
#include "MCU.h"
UINT8 AD0_value=0;
UINT8 AD1_value=0;
UINT8 AD2_value=0;
uint cnt1=0,cnt2=0,cnt3=0,flag=0;
void delay(void)
{
int i;
for(i=1;i<800;i++){}
}
void CH4_Decimal0(UINT16 data)
{
CH452_WriteBCD(7,(UINT16)((data%10000)/1000));
CH452_WriteBCD(6,(UINT16)((data%1000)/100));
CH452_WriteBCD(5,(UINT16)(data%100)/10);
CH452_WriteBCD(4,(UINT16)(data%10));
}
void CH4_Decimal1(UINT16 data)
{
CH452_WriteBCD(3,(UINT16)((data%10000)/1000));
CH452_WriteBCD(2,(UINT16)((data%1000)/100));
CH452_WriteBCD(1,(UINT16)(data%100)/10);
CH452_WriteBCD(0,(UINT16)(data%10));
}
void PLL_Init(void)
{
CLKSEL=0;
REFDV=0X81; //6<fref<=12,REFDIV=1
SYNR=0X02; //SINDIV=2
POSTDIV=0X00; //fpll=fvco
//fvco=2*fosc*(SINDIV+1)(REFDIV+1)=12*fosc
_asm(nop);
_asm(nop);
while(!(CRGFLG&0x08)); //即RTIF=1时跳出循环
PLLCTL_PLLON=1; //锁相环启动
CLKSEL=0X80; //PLLSEL=1,即fbus=fpll/2
}
void PWM0_Init(void)
{
PWME_PWME0=0;
PWMPOL_PPOL0=1; //ch0先高后低
PWMCLK_PCLK0=1; //时钟源选择ClockSA
PWMPRCLK_PCKA1=1; //PWM预分频4
PWMSCLA=256; //分频,ClockA/(2*PWMSCLA)=ClockSA
PWMCTL=0X00;
PWMCAE_CAE0=0; //左对齐
PWMPER0=0XFF; //再分频PER倍
PWMDTY0=0X7F; //占空比=DTY/PER
PWME_PWME0=1; //使能PWM通道0
}
void PWM1_Init(void)
{
PWME_PWME1=0;
PWMPOL_PPOL1=1; //ch0先高后低
PWMCLK_PCLK1=1; //时钟源选择clockSA
PWMPRCLK_PCKA1=1; //PWM预分频4
PWMSCLA=256; //分频
PWMCTL=0X00;
PWMCAE_CAE1=0; //左对齐
PWMPER1=0XFF;
PWMDTY1=0X7F; //占空比=DTY/PER
PWME_PWME1=1; //使能PWM通道1
}
void PWM2_Init(void)
{
PWME_PWME2=0;
PWMPOL_PPOL2=1; //ch0先高后低
PWMCLK_PCLK2=1; //时钟源选择clockSB
PWMPRCLK_PCKB1=1; //PWM预分频4
PWMSCLA=256; //分频
PWMCTL=0X00;
PWMCAE_CAE2=0; //左对齐
PWMPER2=0XFF;
PWMDTY2=0X7F; //占空比=DTY/PER
PWME_PWME2=1; //使能PWM通道2
}
void main(void)
{
PLL_Init();
PWM0_Init();
PWM1_Init();
PWM2_Init();
DDRJ=0X00;
DDRH=0X00;
DDRA=0XFF;
PORTA=0X01;
MCU_Init();
ADC_Init();
ADC_StartChannelConversion(0, 2); //使能ADC通道0-2
ATD0DIEN = 0x0000; //禁止所有ANx管脚处的数字输入
MODRR_MODRR4=1; //SPI在PM
SPI0_Init();
CH452_Write(CH452_RESET); // CH452复位
CH452_Write(CH452_SYSON1); // 开启显示数据
CH452_Write(CH452_BCD); // 设置BCD译码方式
delay() ;
while(1)
{
if(PTIJ_PTIJ0==1) //系统开启
{
if(flag==1)
{
PORTA|=0x01;
CH452_Write(CH452_SYSON1); // 开启显示数据
CH452_Write(CH452_BCD); // 设置BCD译码方式
flag=0;
}
//设置PWM占空比为ADC数值
AD0_value = ADC_GetChannelResult(0);
PWMDTY0=AD0_value;
AD1_value = ADC_GetChannelResult(1);
PWMDTY1=AD1_value;
AD2_value = ADC_GetChannelResult(2);
PWMDTY2=AD2_value;
//读CH0高电平时间
if(PTIH_PTIH0==1)
{
while(1)
{
if(PTIH_PTIH0==0)
break;
}
}
while(PTIH_PTIH0==0);
while(PTIH_PTIH0==1)
{
cnt1++;
if(cnt1>65530)
break;
}
cnt1=cnt1>>3; //取高4位结果
CH4_Decimal0(cnt1);//显示CH0高电平时间
//LED1-2显示
if(cnt1<2000)
{
PORTA|=0x02;
if(cnt1<1000)
PORTA|=0x04;
else
PORTA&=0xFB;
}
else
PORTA&=0xF9;
cnt1=0;
//读CH1高电平时间
if(PTIH_PTIH1==1)
{
while(1)
{
if(PTIH_PTIH1==0)
break;
}
}
while(PTIH_PTIH1==0);
while(PTIH_PTIH1==1)
{
cnt2++;
if(cnt2>65530)
break;
}
cnt2=cnt2>>3; //结果只显示高4位
CH4_Decimal1(cnt2); //显示CH1高电平时间
//LED3-4显示
if(cnt2<2000)
{
PORTA|=0x08;
if(cnt2<1000)
PORTA|=0x10;
else
PORTA&=0xEF;
}
else
PORTA&=0xE7;
cnt2=0;
//读CH2高电平时间
if(PTIH_PTIH2==1)
{
while(1)
{
if(PTIH_PTIH2==0)
break;
}
}
while(PTIH_PTIH2==0);
while(PTIH_PTIH2==1)
{
cnt3++;
if(cnt3>65530)
break;
}
cnt3=cnt3>>3; //结果只显示高4位
//LED3-4显示
if(cnt3<2000)
{
PORTA|=0x20;
if(cnt3<1000)
PORTA|=0x40;
else
PORTA&=0xBF;
}
else
PORTA&=0x9F;
cnt3=0;
}
else
{
PORTA=0X00;
CH452_Write(CH452_RESET);
flag=1;
}
}
}
|
|