目录
一、项目描述
二、原理图、PCB、3D图
三、实物
四、程序
TFT屏显示正弦、方波、三角、锯齿,通过按键切换
锯齿波
三角波
方波
正弦波
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "adc.h"
#include "DAC.h"
#include "KEY.h"
u16 s=0,a,b=0;
u16 temp,out_Val=900;
char aa[10];
float temp1;
u16 data1[100]={1844,1762,1655,1539,1402,1245,1094,956,836,738,679,651,655,702,784,890,1022,1157,1305,1470,1596,1724,1808,1820,1840,1844,1762,1655,1539,1402,1245,1094,956,836,738,679,651,655,702,784,890,1022,1157,1305,1470,1596,1724,1808,1820,1840,
1844,1762,1655,1539,1402,1245,1094,956,836,738,679,651,655,702,784,890,1022,1157,1305,1470,1596,1724,1808,1820,1840,1844,1762,1655,1539,1402,1245,1094,956,836,738,679,651,655,702,784,890,1022,1157,1305,1470,1596,1724,1808,1820,1840,};
//正弦波
u16 data2[100]={660,660,660,660,660,660,660,660,660,660,660,660,660,1950,1950,1950,1950,1950,1950,1950,1950,1950,1950,1950,
660,660,660,660,660,660,660,660,660,660,660,660,660,1950,1950,1950,1950,1950,1950,1950,1950,1950,1950,1950,
660,660,660,660,660,660,660,660,660,660,660,660,660,1950,1950,1950,1950,1950,1950,1950,1950,1950,1950,1950,
660,660,660,660,660,660,660,660,660,660,660,660,660,1950,1950,1950,1950,1950,1950,1950,1950,1950,1950,1950,
660,660,660,660,};//方波
u16 data3[100]={1620,1717,1802,1919,1927,1900,1802,1687,1582,1490,1380,1274,1166,1067,964,860,757,659,706,824,926,1030,1119,1236,1329,1427,1549,1624,
1717,1802,1900,1930,1900,1802,1687,1582,1490,1380,1274,1166,1067,964,860,757,659,706,824,926,1030,1119,1236,1329,1427,1549,1624,
1717,1802,1900,1930,1900,1802,1687,1582,1490,1380,1274,1166,1067,964,860,757,659,706,824,926,1030,1119,1236,1329,1427,1549,1624,
1717,1802,1900,1930,1900,1802,1687,1582,1490,1380,1274,1166,1067,964,860,757,659,706,};//三角波
u16 data4[100]={1314,1355,1414,1459,1510,1585,1607,1660,1721,1753,1808,1862,1913,656,706,758,818,862,917,962,1024,1090,1125,1167,1212,1277,
1314,1355,1414,1459,1510,1585,1607,1660,1721,1753,1808,1862,1913,656,706,758,818,862,917,962,1024,1090,1125,1167,1212,1277,
1314,1355,1414,1459,1510,1585,1607,1660,1721,1753,1808,1862,1913,656,706,758,818,862,917,962,1024,1090,1125,1167,1212,1277,
1314,1355,1414,1459,1510,1585,1607,1660,1721,1753,1808,1862,1913,656,706,758,818,862,917,962,1024,1090,};//锯齿波
/********************************************************************************************************************************************************
函 数 名:void LCD_Show1(void)
功 能:显示正弦波
说 明:通过连线函数绘制出正弦波波形
入口参数:无
返 回 值:无
*********************************************************************************************************************************************************/
void LCD_Show1(void)
{
for(s=0;s<99;s++)
{
LCD_DrawLine(s*2+20,300-data1[s]/20,(s+1)*2+20,300-data1[s+1]/20);
}
}
/********************************************************************************************************************************************************
函 数 名:void LCD_Show2(void)
功 能:显示方波函数
说 明:
入口参数:无
返 回 值:无
*********************************************************************************************************************************************************/
void LCD_Show2(void)
{
for(s=0;s<99;s++)
{
LCD_DrawLine(s*2+20,300-data2[s]/20,(s+1)*2+20,300-data2[s+1]/20);
}
}
/********************************************************************************************************************************************************
函 数 名:void LCD_Show3(void)
功 能:显示三角波函数
说 明:
入口参数:无
返 回 值:无
*********************************************************************************************************************************************************/
void LCD_Show3(void)
{
for(s=0;s<99;s++)
{
LCD_DrawLine(s*2+20,300-data3[s]/20,(s+1)*2+20,300-data3[s+1]/20);
}
}
/********************************************************************************************************************************************************
函 数 名:void LCD_Show4(void)
功 能:显示锯齿波函数
说 明:
入口参数:无
返 回 值:无
*********************************************************************************************************************************************************/
void LCD_Show4(void)
{
for(s=0;s<99;s++)
{
LCD_DrawLine(s*2+20,300-data4[s]/20,(s+1)*2+20,300-data4[s+1]/20);
}
}
/********************************************************************************************************************************************************
函 数 名:int main()
功 能:主函数
说 明:
入口参数:无
返 回 值:无
*********************************************************************************************************************************************************/
int main(void)
{
Stm32_Clock_Init(336,8,2,7);//设置时钟,168MHz
delay_init(168); //初始化延时函数
uart_init(84,115200); //串口初始化
LED_init(); //初始化LED时钟
Adc_Init(); //内部AD初始化 引脚PA5
LCD_Init(); //TFT液晶屏初始化
Dac_Init(); //内部DAC初始化 //引脚PA4
KEY_Init(); //按键初始化
while(1)
{
DAC1_Set_val(out_Val); //out_val=0~3300对应输出PA4输出电压0~3.3v
temp=Get_Adc_Average(ADC_CH5,10); //读取内部AD转化的平均值,
temp1=(float)temp*(3.3/4096); //将模拟量转化为数字量
sprintf((char*)aa,"%5.2f",temp1); //将float类型的数据保存到数组里
if(temp1>=0&&temp1<=1) //判断电压在哪个范围
{
b=1;
}
else if(temp1>=1&&temp1<=2)
{
b=2;
}
else if(temp1>=2&&temp1<=3)
{
b=3;
}
else if(temp1>=3&&temp1<=4)
{
b=4;
}
switch(b) //判断电压的范围输出相应的波形
{
case 1: //输出正弦波
LCD_Clear(WHITE); //清屏
LCD_ShowString(10,50,200,16,16,"Acquisition Val: V"); //在屏幕上显示字符串
LCD_ShowString(140,50,200,16,16,aa); //在屏幕固定位置显示电压
LCD_ShowString(10,70,200,16,16,"Output Val: V");
LCD_ShowString(140,70,200,16,16,aa);
LCD_ShowString(10,90,200,16,16,"Current wave:Sinusoid");
LCD_Show1();break;
case 2: //输出方波
LCD_Clear(WHITE);
LCD_ShowString(10,50,200,16,16,"Acquisition Val: V");
LCD_ShowString(140,50,200,16,16,aa);
LCD_ShowString(10,70,200,16,16,"Output Val: V");
LCD_ShowString(140,70,200,16,16,aa);
LCD_ShowString(10,90,200,16,16,"Current wave:Square");
LCD_Show2();break;
case 3: //输出三角波
LCD_Clear(WHITE);
LCD_ShowString(10,50,200,16,16,"Acquisition Val: V");
LCD_ShowString(140,50,200,16,16,aa);
LCD_ShowString(10,70,200,16,16,"Output Val: V");
LCD_ShowString(140,70,200,16,16,aa);
LCD_ShowString(10,90,200,16,16,"Current wave:Triangular");
LCD_Show3();break;
case 4: //输出锯齿波
LCD_Clear(WHITE);
LCD_ShowString(10,50,200,16,16,"Acquisition Val: V");
LCD_ShowString(140,50,200,16,16,aa);
LCD_ShowString(10,70,200,16,16,"Output Val: V");
LCD_ShowString(140,70,200,16,16,aa);
LCD_ShowString(10,90,200,16,16,"Current wave:Sawtooth");
LCD_Show4();break;
default: break;
}
a=KEY_Scan(0); //读取按键值
LED0=0;
//通过按键控制内部DAC输出的电压值,步进值为0.2V
if(a==KEY0_PRES) //判断KEY0是否按下
{
a=0;
LED1=~LED1; //按键按下LED灯状态发生改变
out_Val=out_Val+200;
if(out_Val>=3300)out_Val=3300;
}
else if(a==KEY1_PRES)
{
a=0;
LED0=~LED0;
out_Val=out_Val-200;
if(out_Val<=0)out_Val=0;
}
// printf(":ADC=%d\r\n",a); //串口打印AD采集的值
// delay_ms(500);
}
}