#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd.h"
#include "adc.h"
#include "dac.h"
#include "key.h"
void init()
{
delay_init(); //延时函数初始化
uart_init(9600); //串口初始化为9600
LED_Init(); //初始化与LED连接的硬件接口
LCD_Init();
KEY_Init();
Adc_Init();
Dac1_Init();
POINT_COLOR=RED;//设置字体为红色
LCD_ShowString(60,40,200,24,24,"DAC Test ^-^");
LCD_ShowString(60,70,200,16,16,"Bigggg difficulty");
LCD_ShowString(60,90,200,16,16,"2015/1/25");
LCD_ShowString(60,110,200,16,16,"By--Mr yh");
LCD_ShowString(60,130,200,16,16,"WK_UP:+ KEY0:-");
//显示提示信息
POINT_COLOR=BLUE;//设置字体为蓝色
LCD_ShowString(60,150,200,16,16,"DAC VAL:");
LCD_ShowString(60,170,200,16,16,"DAC VOL:0.000V");
LCD_ShowString(60,190,200,16,16,"ADC VOL:0.000V");
Dac1_Set_Vol(330);
}
int main(void)
{
u16 adcnum=0;
u16 dacnum=0;
float tem;
u8 key,t=0;
init();
while(1)
{
t++;
key=KEY_Scan(0);
if(key==WK_UP_PRES)
{
if(dacnum<4000)dacnum+=200;
DAC_SetChannel1Data(DAC_Align_12b_R,dacnum);
}
else if(key==KEY0_PRES)
{
if(dacnum>200)dacnum-=200;
else dacnum=0;
DAC_SetChannel1Data(DAC_Align_12b_R,dacnum);
}
if(t==10||key==WK_UP_PRES||key==KEY0_PRES)
{
adcnum=Dac1_Get_Vol();dacnum=adcnum;
LCD_ShowxNum(124,150,adcnum,4,16,0);//显示DAC寄存器值
tem=(float)adcnum*(3.3/4096); //得到DAC电压值
adcnum=tem;
LCD_ShowxNum(124,170,tem,1,16,0);//显示电压值整数部分
tem-=adcnum;tem*=1000;
LCD_ShowxNum(140,170,tem,3,16,0X80);//显示电压值的小数部分
adcnum=Get_Adc_Average(ADC_Channel_1,10);
tem=(float)adcnum*(3.3/4096);
adcnum=tem;
LCD_ShowxNum(124,190,tem,1,16,0);
tem-=adcnum;tem*=1000;
LCD_ShowxNum(140,190,tem,3,16,0X80);
t=0;
LED0=!LED0;
}
delay_ms(10);
}
}